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
80 #ifndef __DRV_USB_H
81 #define __DRV_USB_H
82
83 #define USB_MAJOR 180
84
85 #include "drvDevice.h"
86 #include "drvList.h"
87
88 #include "drvConfig.h"
89 #include "drvCompiler.h"
90 #include "drvPorts.h"
91 #include "drvPCIMEM.h"
92
93 #include "drvUSB_CH9.h"
94 #include "drvTimer.h"
95 #include "drvCompletion.h"
96 #include <MsCommon.h>
97 #include <drvUsbcommon.h> // public include
98
99 struct usb_device;
100
101 struct usb_host_endpoint {
102 struct usb_endpoint_descriptor desc;
103 };
104
105 struct usb_host_interface {
106 struct usb_interface_descriptor desc;
107 struct usb_host_endpoint *endpoint;
108 };
109
110 struct usb_interface {
111 struct usb_host_interface *altsetting;
112
113 MS_S32 act_altsetting;
114 MS_S32 num_altsetting;
115 MS_S32 max_altsetting;
116
117 struct usb_driver *driver;
118 struct device_s dev;
119 };
120
ms_usb_get_intfdata(struct usb_interface * intf)121 static __inline__ void *ms_usb_get_intfdata (struct usb_interface *intf)
122 {
123 return ms_dev_get_drvdata (&intf->dev);
124 }
125
ms_usb_set_intfdata(struct usb_interface * intf,void * data)126 static __inline__ void ms_usb_set_intfdata (struct usb_interface *intf, void *data)
127 {
128 ms_dev_set_drvdata(&intf->dev, data);
129 }
130
131 #define USB_MAX_INTERFACES 32
132
133 struct usb_host_config {
134 struct usb_config_descriptor desc;
135
136 struct usb_interface *interface[USB_MAX_INTERFACES];
137
138 };
139
140 /* -------------------------------------------------------------------------- */
141
142 //struct ms_usb_bus_ops;
143
144 struct ms_usb_devmap {
145 MS_U32 usb_devicemap[128 / (8*sizeof(MS_U32))];
146 };
147
148 struct usb_bus {
149 struct device_s *controller;
150 MS_S32 busnum;
151 char *bus_name;
152
153 MS_S32 devnum_next;
154
155 struct ms_usb_devmap devmap;
156 //struct ms_usb_bus_ops *bus_ops; // Obsolete
157 struct usb_device *root_hub;
158 struct list_head bus_list;
159 void *hcpriv;
160 void (*release)(struct usb_bus *bus);
161 };
162
163 #define USB_MAXCHILDREN (16)
164
165 struct usb_tt;
166
167 struct usb_device {
168 MS_S32 u32DevNum;
169 char devpath [16];
170 enum usb_device_state eState;
171 enum usb_device_speed eSpeed;
172
173 struct usb_tt *tt;
174 MS_S32 u32TTPort;
175
176 MS_U32 toggle[2];
177 //MS_U32 halted[2]; // Obsolete
178
179 MS_S32 pEpMaxPacketIn[16];
180 MS_S32 pEpMaxPacketOut[16];
181
182 struct usb_device *parent;
183 struct usb_bus *bus;
184
185 struct device_s dev;
186
187 struct usb_device_descriptor descriptor;
188 struct usb_host_config *config;
189 struct usb_host_config *actconfig;
190
191 char **pRawDescriptors;
192
193 MS_U8 level; // to restric hub topo level
194
195 MS_S32 u32HaveLangId;
196 MS_S32 u32StringLangId;
197
198 void *hcpriv;
199 char *pProductString;
200 char *pManufacturerString;
201 char *pSerialNumberString;
202
203 MS_S32 u32MaxChild;
204 struct usb_device *children[USB_MAXCHILDREN];
205 };
206
ms_to_usb_device(struct device_s * d)207 static __inline__ struct usb_device *ms_to_usb_device(struct device_s *d)
208 {
209 const struct usb_interface *__mptr = (struct usb_interface *)(d);
210 return (struct usb_device *)( (char *)__mptr - (char *)offsetof(struct usb_device,dev));
211
212 }
get_usb_interface(struct device_s * d)213 static __inline__ struct usb_interface *get_usb_interface(struct device_s *d)
214 {
215 const struct usb_interface *__mptr = (struct usb_interface *)(d);
216 return (struct usb_interface *)((char *)__mptr - (char *)offsetof(struct usb_interface,dev));
217 }
218
interface_to_usbdev(struct usb_interface * ptr)219 static __inline__ struct usb_device *interface_to_usbdev(struct usb_interface *ptr)
220 {
221 const struct usb_device *__mptr = (struct usb_device *)(ptr->dev.parent);
222 return (struct usb_device *)((char *)__mptr - (char *)offsetof( struct usb_device, dev));
223 }
224
225 extern struct usb_device *ms_usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus);
226 extern struct usb_device *ms_usb_get_dev(struct usb_device *dev);
227 extern void ms_usb_put_dev(struct usb_device *dev);
228
229 extern int ms_usb_reset_device(struct usb_device *dev);
230
231 extern struct usb_interface *ms_usb_ifnum_to_if(struct usb_device *dev, int ifnum);
232
233 /*-------------------------------------------------------------------------*/
234 #define USB_DEVICE_ID_MATCH_INT_INFO \
235 (USBDEV_MATCH_ID_INT_CLASS | USBDEV_MATCH_ID_INT_SUBCLASS | USBDEV_MATCH_ID_INT_PROTOCOL)
236
237 struct usb_driver {
238 char name[16];
239
240 int (*probe_func) (struct usb_interface *intf,
241 const struct usb_device_id *id);
242
243 void (*discon_func) (struct usb_interface *intf);
244
245 const struct usb_device_id *match_id_table;
246
247 struct device_driver driver;
248
249 };
250
get_usb_driver(struct device_driver * dev_ptr)251 static __inline__ struct usb_driver *get_usb_driver(struct device_driver *dev_ptr)
252 {
253 const struct device_driver *__mptr = (dev_ptr);
254 return (struct usb_driver *)( (char *)__mptr - (char *)offsetof(struct usb_driver,driver) );
255 }
256
257 extern struct ms_bus_type usb_bus_type;
258
259 extern int ms_usb_register(struct usb_driver *);
260 extern void ms_usb_deregister(struct usb_driver *);
261
262 #define MS_FLAG_URB_SHORT_NOT_OK 0x0001 // short reads have errors
263 #define MS_FLAG_URB_NO_TRANSFER_DMA_MAP 0x0004 // set the bit if urb->transfer_dma valid
264 #define MS_FLAG_URB_NO_SETUP_DMA_MAP 0x0008 // set the bit if urb->setup_dma valid
265 #define MS_FLAG_URB_ASYNC_UNLINK 0x0010 // ms_hcd_unlink_urb() returns asap
266 #define MS_FLAG_URB_ZERO_PACKET 0x0040 // complete bulk OUTs transfer with short packet
267 #define MS_FLAG_URB_NO_INTERRUPT 0x0080 // no non-error interrupt needed
268
269 #define MS_FLAG_URB_MAP_LOCAL 0x00080000 // HCD-local-memory mapping
270
271
272 struct urb;
273 struct stPtRegs;
274
275 typedef void (*usb_complete_t)(struct urb *, struct stPtRegs *);
276
277 struct urb
278 {
279 spinlock_t lock;
280 atomic_t count;
281 void *hcpriv;
282 struct list_head urb_list;
283
284 struct usb_device *dev;
285 MS_U32 u32Pipe;
286 MS_S32 s32Status;
287 MS_U32 u32TransferFlags;
288 void *pTransferBuffer;
289 dma_addr_t tTransferDma;
290 MS_S32 u32TransferBufferLength;
291 MS_S32 u32ActualLength;
292 unsigned char *pSetupPacket;
293 dma_addr_t tSetupDma;
294 MS_S32 u32Interval;
295 void *pContext;
296 usb_complete_t complete_func;
297 //__Test
298 void *SetDMABuf; //Note, for DMA
299 MS_S32 SetDMALen;
300 void *TxDMABuf;
301 MS_S32 TxDMALen;
302 //__Test
303
304 MS_S32 WaitFlag_storage; //added by jonas
305
306 };
307
ms_usb_stuff_intr_urb(struct urb * urb,struct usb_device * pDev,MS_U32 u32Pipe,void * pTransferBuffer,MS_S32 u32Length,usb_complete_t tCompleteFunc,void * pContext,MS_S32 u32Interval)308 static __inline__ void ms_usb_stuff_intr_urb (struct urb *urb,
309 struct usb_device *pDev,
310 MS_U32 u32Pipe,
311 void *pTransferBuffer,
312 MS_S32 u32Length,
313 usb_complete_t tCompleteFunc,
314 void *pContext,
315 MS_S32 u32Interval)
316 {
317 osapi_spin_lock_init(&urb->lock);
318 urb->dev = pDev;
319 urb->u32Pipe = u32Pipe;
320 urb->pTransferBuffer = pTransferBuffer;
321 urb->u32TransferBufferLength = u32Length;
322 urb->complete_func = tCompleteFunc;
323 urb->pContext = pContext;
324 urb->u32Interval = (pDev->eSpeed == USB_HIGH_SPEED) ? (1 << (u32Interval - 1)) : u32Interval;
325 urb->SetDMABuf = NULL;
326 urb->SetDMALen = 0;
327 urb->TxDMABuf = NULL;
328 urb->TxDMALen = 0;
329 }
330
ms_usb_stuff_bulk_urb(struct urb * urb,struct usb_device * pDev,MS_U32 u32Pipe,void * pTransferBuffer,MS_S32 u32Length,usb_complete_t tCompleteFunc,void * pContext)331 static __inline__ void ms_usb_stuff_bulk_urb (struct urb *urb,
332 struct usb_device *pDev,
333 MS_U32 u32Pipe,
334 void *pTransferBuffer,
335 MS_S32 u32Length,
336 usb_complete_t tCompleteFunc,
337 void *pContext)
338 {
339 osapi_spin_lock_init(&urb->lock);
340 urb->dev = pDev;
341 urb->u32Pipe = u32Pipe;
342 urb->pTransferBuffer = pTransferBuffer;
343 urb->u32TransferBufferLength = u32Length;
344 urb->complete_func = tCompleteFunc;
345 urb->pContext = pContext;
346 urb->SetDMABuf = NULL;
347 urb->SetDMALen = 0;
348 urb->TxDMABuf = NULL;
349 urb->TxDMALen = 0;
350 }
351
ms_usb_stuff_control_urb(struct urb * urb,struct usb_device * pDev,MS_U32 u32Pipe,unsigned char * pSetupPacket,void * pTransferBuffer,MS_S32 u32Length,usb_complete_t tCompleteFunc,void * pContext)352 static __inline__ void ms_usb_stuff_control_urb (struct urb *urb,
353 struct usb_device *pDev,
354 MS_U32 u32Pipe,
355 unsigned char *pSetupPacket,
356 void *pTransferBuffer,
357 MS_S32 u32Length,
358 usb_complete_t tCompleteFunc,
359 void *pContext)
360 {
361 osapi_spin_lock_init(&urb->lock);
362 urb->dev = pDev;
363 urb->u32Pipe = u32Pipe;
364 urb->pSetupPacket = pSetupPacket;
365 urb->pTransferBuffer = pTransferBuffer;
366 urb->u32TransferBufferLength = u32Length;
367 urb->complete_func = tCompleteFunc;
368 urb->pContext = pContext;
369 urb->SetDMABuf = NULL;
370 urb->SetDMALen = 0;
371 urb->TxDMABuf = NULL;
372 urb->TxDMALen = 0;
373 }
374
375 extern struct urb *ms_usb_alloc_urb(int mem_flags);
376 extern void ms_usb_free_urb(struct urb *urb);
377
378 #define usb_put_urb ms_usb_free_urb
379 extern struct urb *ms_usb_get_urb(struct urb *urb);
380 extern int ms_usb_submit_urb(struct urb *urb, int mem_flags);
381 extern int ms_usb_unlink_urb(struct urb *urb);
382
383 void *ms_usb_buffer_alloc (struct usb_device *dev, size_t size, int mem_flags, dma_addr_t *dma);
384 void ms_usb_buffer_free (struct usb_device *dev, size_t size, void *addr, dma_addr_t dma);
385
386 extern int ms_usb_control_cmd(struct usb_device *dev, unsigned int pipe,
387 unsigned char request, unsigned char requesttype, unsigned short value, unsigned short index,
388 void *data, unsigned short size, int timeout);
389 extern int ms_usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
390 void *data, int len, int *actual_length,
391 int timeout);
392
393
394 #define USB_CTRL_GET_TIMEOUT 5
395 #define USB_CTRL_SET_TIMEOUT 5
396
397 #define EP_CONTROL 0
398 #define EP_BULK 1
399 #define EP_INTERRUPT 2
400
401 #define usb_maxpacket(dev, pipe, out) (out \
402 ? (dev)->pEpMaxPacketOut[usb_pipeendpoint(pipe)] \
403 : (dev)->pEpMaxPacketIn [usb_pipeendpoint(pipe)] )
404
405 #define usb_pipein(ms_pipe) ((ms_pipe) & USB_DIR_IN)
406 #define usb_pipeout(ms_pipe) (!usb_pipein(ms_pipe))
407 #define usb_pipedevice(ms_pipe) (((ms_pipe) >> 8) & 0x7f)
408 #define usb_pipeendpoint(ms_pipe) (((ms_pipe) >> 15) & 0xf)
409 #define usb_pipetype(ms_pipe) (((ms_pipe) >> 30) & 3)
410 #define usb_pipeint(ms_pipe) (usb_pipetype((ms_pipe)) == EP_INTERRUPT)
411 #define usb_pipecontrol(ms_pipe) (usb_pipetype((ms_pipe)) == EP_CONTROL)
412 #define usb_pipebulk(ms_pipe) (usb_pipetype((ms_pipe)) == EP_BULK)
413
414 //#define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))
415 //#define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))
416
__pack_pipe(struct usb_device * dev,MS_U32 endpoint)417 static __inline__ MS_U32 __pack_pipe(struct usb_device *dev, MS_U32 endpoint)
418 {
419 return (dev->u32DevNum << 8) | (endpoint << 15);
420 }
421
422 #define usb_gettoggle(dev, ms_ep, out) (((dev)->toggle[out] >> (ms_ep)) & 1)
423 #define usb_dotoggle(dev, ms_ep, out) ((dev)->toggle[out] ^= (1 << (ms_ep)))
424 #define usb_settoggle(dev, ms_ep, out, bit) ((dev)->toggle[out] = ((dev)->toggle[out] & ~(1 << (ms_ep))) | ((bit) << (ms_ep)))
425
426 #define usb_sndctrlpipe(dev,ms_ep) (((MS_U32)EP_CONTROL << 30) | __pack_pipe(dev,ms_ep))
427 #define usb_rcvctrlpipe(dev,ms_ep) (((MS_U32)EP_CONTROL << 30) | __pack_pipe(dev,ms_ep) | USB_DIR_IN)
428 #define usb_sndbulkpipe(dev,ms_ep) (((MS_U32)EP_BULK << 30) | __pack_pipe(dev,ms_ep))
429 #define usb_rcvbulkpipe(dev,ms_ep) (((MS_U32)EP_BULK << 30) | __pack_pipe(dev,ms_ep) | USB_DIR_IN)
430 #define usb_rcvintpipe(dev,ms_ep) (((MS_U32)EP_INTERRUPT << 30) | __pack_pipe(dev,ms_ep) | USB_DIR_IN)
431
432 extern int ms_usb_get_descriptor(struct usb_device *ms_dev, unsigned char desctype,
433 unsigned char descindex, void *buf, int size);
434 extern int ms_usb_get_dev_descriptor(struct usb_device *ms_dev);
435 extern int ms_usb_string(struct usb_device *ms_dev, int index,
436 char *buf, size_t size);
437
438 extern int ms_usb_clear_halt(struct usb_device *ms_dev, int pipe);
439 extern int ms_usb_set_config(struct usb_device *ms_dev, int configuration);
440 extern int ms_usb_set_interface(struct usb_device *ms_dev, int ifnum, int alternate);
441 extern void ms_usb_set_device_state(struct usb_device *udev, enum usb_device_state new_state);
442
443 //extern int USB_core_lock; // NUSED
444 //extern int Timer_task_lock; // NUSED
445
446 #define MSC_STRING_LEN 32
447
448 #endif
449
450