staging: octeon-usb: cvmx_usb_complete_t -> enum cvmx_usb_complete
[deliverable/linux.git] / drivers / staging / octeon-usb / cvmx-usb.h
1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17
18 * * Neither the name of Cavium Networks nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
21 * permission.
22
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40
41 /**
42 * "cvmx-usb.h" defines a set of low level USB functions to help
43 * developers create Octeon USB drivers for various operating
44 * systems. These functions provide a generic API to the Octeon
45 * USB blocks, hiding the internal hardware specific
46 * operations.
47 *
48 * At a high level the device driver needs to:
49 *
50 * - Call cvmx_usb_get_num_ports() to get the number of
51 * supported ports.
52 * - Call cvmx_usb_initialize() for each Octeon USB port.
53 * - Enable the port using cvmx_usb_enable().
54 * - Either periodically, or in an interrupt handler, call
55 * cvmx_usb_poll() to service USB events.
56 * - Manage pipes using cvmx_usb_open_pipe() and
57 * cvmx_usb_close_pipe().
58 * - Manage transfers using cvmx_usb_submit_*() and
59 * cvmx_usb_cancel*().
60 * - Shutdown USB on unload using cvmx_usb_shutdown().
61 *
62 * To monitor USB status changes, the device driver must use
63 * cvmx_usb_register_callback() to register for events that it
64 * is interested in. Below are a few hints on successfully
65 * implementing a driver on top of this API.
66 *
67 * == Initialization ==
68 *
69 * When a driver is first loaded, it is normally not necessary
70 * to bring up the USB port completely. Most operating systems
71 * expect to initialize and enable the port in two independent
72 * steps. Normally an operating system will probe hardware,
73 * initialize anything found, and then enable the hardware.
74 *
75 * In the probe phase you should:
76 * - Use cvmx_usb_get_num_ports() to determine the number of
77 * USB port to be supported.
78 * - Allocate space for a cvmx_usb_state_t structure for each
79 * port.
80 * - Tell the operating system about each port
81 *
82 * In the initialization phase you should:
83 * - Use cvmx_usb_initialize() on each port.
84 * - Do not call cvmx_usb_enable(). This leaves the USB port in
85 * the disabled state until the operating system is ready.
86 *
87 * Finally, in the enable phase you should:
88 * - Call cvmx_usb_enable() on the appropriate port.
89 * - Note that some operating system use a RESET instead of an
90 * enable call. To implement RESET, you should call
91 * cvmx_usb_disable() followed by cvmx_usb_enable().
92 *
93 * == Locking ==
94 *
95 * All of the functions in the cvmx-usb API assume exclusive
96 * access to the USB hardware and internal data structures. This
97 * means that the driver must provide locking as necessary.
98 *
99 * In the single CPU state it is normally enough to disable
100 * interrupts before every call to cvmx_usb*() and enable them
101 * again after the call is complete. Keep in mind that it is
102 * very common for the callback handlers to make additional
103 * calls into cvmx-usb, so the disable/enable must be protected
104 * against recursion. As an example, the Linux kernel
105 * local_irq_save() and local_irq_restore() are perfect for this
106 * in the non SMP case.
107 *
108 * In the SMP case, locking is more complicated. For SMP you not
109 * only need to disable interrupts on the local core, but also
110 * take a lock to make sure that another core cannot call
111 * cvmx-usb.
112 *
113 * == Port callback ==
114 *
115 * The port callback prototype needs to look as follows:
116 *
117 * void port_callback(cvmx_usb_state_t *usb,
118 * cvmx_usb_callback_t reason,
119 * enum cvmx_usb_complete status,
120 * int pipe_handle,
121 * int submit_handle,
122 * int bytes_transferred,
123 * void *user_data);
124 * - "usb" is the cvmx_usb_state_t for the port.
125 * - "reason" will always be CVMX_USB_CALLBACK_PORT_CHANGED.
126 * - "status" will always be CVMX_USB_COMPLETE_SUCCESS.
127 * - "pipe_handle" will always be -1.
128 * - "submit_handle" will always be -1.
129 * - "bytes_transferred" will always be 0.
130 * - "user_data" is the void pointer originally passed along
131 * with the callback. Use this for any state information you
132 * need.
133 *
134 * The port callback will be called whenever the user plugs /
135 * unplugs a device from the port. It will not be called when a
136 * device is plugged / unplugged from a hub connected to the
137 * root port. Normally all the callback needs to do is tell the
138 * operating system to poll the root hub for status. Under
139 * Linux, this is performed by calling usb_hcd_poll_rh_status().
140 * In the Linux driver we use "user_data". to pass around the
141 * Linux "hcd" structure. Once the port callback completes,
142 * Linux automatically calls octeon_usb_hub_status_data() which
143 * uses cvmx_usb_get_status() to determine the root port status.
144 *
145 * == Complete callback ==
146 *
147 * The completion callback prototype needs to look as follows:
148 *
149 * void complete_callback(cvmx_usb_state_t *usb,
150 * cvmx_usb_callback_t reason,
151 * enum cvmx_usb_complete status,
152 * int pipe_handle,
153 * int submit_handle,
154 * int bytes_transferred,
155 * void *user_data);
156 * - "usb" is the cvmx_usb_state_t for the port.
157 * - "reason" will always be CVMX_USB_CALLBACK_TRANSFER_COMPLETE.
158 * - "status" will be one of the cvmx_usb_complete enumerations.
159 * - "pipe_handle" is the handle to the pipe the transaction
160 * was originally submitted on.
161 * - "submit_handle" is the handle returned by the original
162 * cvmx_usb_submit_* call.
163 * - "bytes_transferred" is the number of bytes successfully
164 * transferred in the transaction. This will be zero on most
165 * error conditions.
166 * - "user_data" is the void pointer originally passed along
167 * with the callback. Use this for any state information you
168 * need. For example, the Linux "urb" is stored in here in the
169 * Linux driver.
170 *
171 * In general your callback handler should use "status" and
172 * "bytes_transferred" to tell the operating system the how the
173 * transaction completed. Normally the pipe is not changed in
174 * this callback.
175 *
176 * == Canceling transactions ==
177 *
178 * When a transaction is cancelled using cvmx_usb_cancel*(), the
179 * actual length of time until the complete callback is called
180 * can vary greatly. It may be called before cvmx_usb_cancel*()
181 * returns, or it may be called a number of usb frames in the
182 * future once the hardware frees the transaction. In either of
183 * these cases, the complete handler will receive
184 * CVMX_USB_COMPLETE_CANCEL.
185 *
186 * == Handling pipes ==
187 *
188 * USB "pipes" is a software construct created by this API to
189 * enable the ordering of usb transactions to a device endpoint.
190 * Octeon's underlying hardware doesn't have any concept
191 * equivalent to "pipes". The hardware instead has eight
192 * channels that can be used simultaneously to have up to eight
193 * transaction in process at the same time. In order to maintain
194 * ordering in a pipe, the transactions for a pipe will only be
195 * active in one hardware channel at a time. From an API user's
196 * perspective, this doesn't matter but it can be helpful to
197 * keep this in mind when you are probing hardware while
198 * debugging.
199 *
200 * Also keep in mind that usb transactions contain state
201 * information about the previous transaction to the same
202 * endpoint. Each transaction has a PID toggle that changes 0/1
203 * between each sub packet. This is maintained in the pipe data
204 * structures. For this reason, you generally cannot create and
205 * destroy a pipe for every transaction. A sequence of
206 * transaction to the same endpoint must use the same pipe.
207 *
208 * == Root Hub ==
209 *
210 * Some operating systems view the usb root port as a normal usb
211 * hub. These systems attempt to control the root hub with
212 * messages similar to the usb 2.0 spec for hub control and
213 * status. For these systems it may be necessary to write
214 * function to decode standard usb control messages into
215 * equivalent cvmx-usb API calls.
216 *
217 * == Interrupts ==
218 *
219 * If you plan on using usb interrupts, cvmx_usb_poll() must be
220 * called on every usb interrupt. It will read the usb state,
221 * call any needed callbacks, and schedule transactions as
222 * needed. Your device driver needs only to hookup an interrupt
223 * handler and call cvmx_usb_poll(). Octeon's usb port 0 causes
224 * CIU bit CIU_INT*_SUM0[USB] to be set (bit 56). For port 1,
225 * CIU bit CIU_INT_SUM1[USB1] is set (bit 17). How these bits
226 * are turned into interrupt numbers is operating system
227 * specific. For Linux, there are the convenient defines
228 * OCTEON_IRQ_USB0 and OCTEON_IRQ_USB1 for the IRQ numbers.
229 *
230 * If you aren't using interrupts, simple call cvmx_usb_poll()
231 * in your main processing loop.
232 */
233
234 #ifndef __CVMX_USB_H__
235 #define __CVMX_USB_H__
236
237 /**
238 * enum cvmx_usb_speed - the possible USB device speeds
239 *
240 * @CVMX_USB_SPEED_HIGH: Device is operation at 480Mbps
241 * @CVMX_USB_SPEED_FULL: Device is operation at 12Mbps
242 * @CVMX_USB_SPEED_LOW: Device is operation at 1.5Mbps
243 */
244 enum cvmx_usb_speed {
245 CVMX_USB_SPEED_HIGH = 0,
246 CVMX_USB_SPEED_FULL = 1,
247 CVMX_USB_SPEED_LOW = 2,
248 };
249
250 /**
251 * enum cvmx_usb_transfer - the possible USB transfer types
252 *
253 * @CVMX_USB_TRANSFER_CONTROL: USB transfer type control for hub and status
254 * transfers
255 * @CVMX_USB_TRANSFER_ISOCHRONOUS: USB transfer type isochronous for low
256 * priority periodic transfers
257 * @CVMX_USB_TRANSFER_BULK: USB transfer type bulk for large low priority
258 * transfers
259 * @CVMX_USB_TRANSFER_INTERRUPT: USB transfer type interrupt for high priority
260 * periodic transfers
261 */
262 enum cvmx_usb_transfer {
263 CVMX_USB_TRANSFER_CONTROL = 0,
264 CVMX_USB_TRANSFER_ISOCHRONOUS = 1,
265 CVMX_USB_TRANSFER_BULK = 2,
266 CVMX_USB_TRANSFER_INTERRUPT = 3,
267 };
268
269 /**
270 * enum cvmx_usb_direction - the transfer directions
271 *
272 * @CVMX_USB_DIRECTION_OUT: Data is transferring from Octeon to the device/host
273 * @CVMX_USB_DIRECTION_IN: Data is transferring from the device/host to Octeon
274 */
275 enum cvmx_usb_direction {
276 CVMX_USB_DIRECTION_OUT,
277 CVMX_USB_DIRECTION_IN,
278 };
279
280 /**
281 * enum cvmx_usb_complete - possible callback function status codes
282 *
283 * @CVMX_USB_COMPLETE_SUCCESS: The transaction / operation finished without
284 * any errors
285 * @CVMX_USB_COMPLETE_SHORT: FIXME: This is currently not implemented
286 * @CVMX_USB_COMPLETE_CANCEL: The transaction was canceled while in flight by
287 * a user call to cvmx_usb_cancel
288 * @CVMX_USB_COMPLETE_ERROR: The transaction aborted with an unexpected
289 * error status
290 * @CVMX_USB_COMPLETE_STALL: The transaction received a USB STALL response
291 * from the device
292 * @CVMX_USB_COMPLETE_XACTERR: The transaction failed with an error from the
293 * device even after a number of retries
294 * @CVMX_USB_COMPLETE_DATATGLERR: The transaction failed with a data toggle
295 * error even after a number of retries
296 * @CVMX_USB_COMPLETE_BABBLEERR: The transaction failed with a babble error
297 * @CVMX_USB_COMPLETE_FRAMEERR: The transaction failed with a frame error
298 * even after a number of retries
299 */
300 enum cvmx_usb_complete {
301 CVMX_USB_COMPLETE_SUCCESS,
302 CVMX_USB_COMPLETE_SHORT,
303 CVMX_USB_COMPLETE_CANCEL,
304 CVMX_USB_COMPLETE_ERROR,
305 CVMX_USB_COMPLETE_STALL,
306 CVMX_USB_COMPLETE_XACTERR,
307 CVMX_USB_COMPLETE_DATATGLERR,
308 CVMX_USB_COMPLETE_BABBLEERR,
309 CVMX_USB_COMPLETE_FRAMEERR,
310 };
311
312 /**
313 * Structure returned containing the USB port status information.
314 */
315 typedef struct
316 {
317 uint32_t reserved : 25;
318 uint32_t port_enabled : 1; /**< 1 = Usb port is enabled, 0 = disabled */
319 uint32_t port_over_current : 1; /**< 1 = Over current detected, 0 = Over current not detected. Octeon doesn't support over current detection */
320 uint32_t port_powered : 1; /**< 1 = Port power is being supplied to the device, 0 = power is off. Octeon doesn't support turning port power off */
321 enum cvmx_usb_speed port_speed : 2; /**< Current port speed */
322 uint32_t connected : 1; /**< 1 = A device is connected to the port, 0 = No device is connected */
323 uint32_t connect_change : 1; /**< 1 = Device connected state changed since the last set status call */
324 } cvmx_usb_port_status_t;
325
326 /**
327 * This is the structure of a Control packet header
328 */
329 typedef union
330 {
331 uint64_t u64;
332 struct
333 {
334 uint64_t request_type : 8; /**< Bit 7 tells the direction: 1=IN, 0=OUT */
335 uint64_t request : 8; /**< The standard usb request to make */
336 uint64_t value : 16; /**< Value parameter for the request in little endian format */
337 uint64_t index : 16; /**< Index for the request in little endian format */
338 uint64_t length : 16; /**< Length of the data associated with this request in little endian format */
339 } s;
340 } cvmx_usb_control_header_t;
341
342 /**
343 * Descriptor for Isochronous packets
344 */
345 typedef struct
346 {
347 int offset; /**< This is the offset in bytes into the main buffer where this data is stored */
348 int length; /**< This is the length in bytes of the data */
349 enum cvmx_usb_complete status; /**< This is the status of this individual packet transfer */
350 } cvmx_usb_iso_packet_t;
351
352 /**
353 * Possible callback reasons for the USB API.
354 */
355 typedef enum
356 {
357 CVMX_USB_CALLBACK_TRANSFER_COMPLETE,
358 /**< A callback of this type is called when a submitted transfer
359 completes. The completion callback will be called even if the
360 transfer fails or is canceled. The status parameter will
361 contain details of why he callback was called. */
362 CVMX_USB_CALLBACK_PORT_CHANGED, /**< The status of the port changed. For example, someone may have
363 plugged a device in. The status parameter contains
364 CVMX_USB_COMPLETE_SUCCESS. Use cvmx_usb_get_status() to get
365 the new port status. */
366 __CVMX_USB_CALLBACK_END /**< Do not use. Used internally for array bounds */
367 } cvmx_usb_callback_t;
368
369 /**
370 * USB state internal data. The contents of this structure
371 * may change in future SDKs. No data in it should be referenced
372 * by user's of this API.
373 */
374 typedef struct
375 {
376 char data[65536];
377 } cvmx_usb_state_t;
378
379 /**
380 * USB callback functions are always of the following type.
381 * The parameters are as follows:
382 * - state = USB device state populated by
383 * cvmx_usb_initialize().
384 * - reason = The cvmx_usb_callback_t used to register
385 * the callback.
386 * - status = The enum cvmx_usb_complete representing the
387 * status code of a transaction.
388 * - pipe_handle = The Pipe that caused this callback, or
389 * -1 if this callback wasn't associated with a pipe.
390 * - submit_handle = Transfer submit handle causing this
391 * callback, or -1 if this callback wasn't associated
392 * with a transfer.
393 * - Actual number of bytes transfer.
394 * - user_data = The user pointer supplied to the
395 * function cvmx_usb_submit() or
396 * cvmx_usb_register_callback() */
397 typedef void (*cvmx_usb_callback_func_t)(cvmx_usb_state_t *state,
398 cvmx_usb_callback_t reason,
399 enum cvmx_usb_complete status,
400 int pipe_handle, int submit_handle,
401 int bytes_transferred, void *user_data);
402
403 /**
404 * Flags to pass the initialization function.
405 */
406 typedef enum
407 {
408 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_XI = 1<<0, /**< The USB port uses a 12MHz crystal as clock source
409 at USB_XO and USB_XI. */
410 CVMX_USB_INITIALIZE_FLAGS_CLOCK_XO_GND = 1<<1, /**< The USB port uses 12/24/48MHz 2.5V board clock
411 source at USB_XO. USB_XI should be tied to GND.*/
412 CVMX_USB_INITIALIZE_FLAGS_CLOCK_AUTO = 0, /**< Automatically determine clock type based on function
413 in cvmx-helper-board.c. */
414 CVMX_USB_INITIALIZE_FLAGS_CLOCK_MHZ_MASK = 3<<3, /**< Mask for clock speed field */
415 CVMX_USB_INITIALIZE_FLAGS_CLOCK_12MHZ = 1<<3, /**< Speed of reference clock or crystal */
416 CVMX_USB_INITIALIZE_FLAGS_CLOCK_24MHZ = 2<<3, /**< Speed of reference clock */
417 CVMX_USB_INITIALIZE_FLAGS_CLOCK_48MHZ = 3<<3, /**< Speed of reference clock */
418 /* Bits 3-4 used to encode the clock frequency */
419 CVMX_USB_INITIALIZE_FLAGS_NO_DMA = 1<<5, /**< Disable DMA and used polled IO for data transfer use for the USB */
420 } cvmx_usb_initialize_flags_t;
421
422 /**
423 * Flags for passing when a pipe is created. Currently no flags
424 * need to be passed.
425 */
426 typedef enum
427 {
428 __CVMX_USB_PIPE_FLAGS_OPEN = 1<<16, /**< Used internally to determine if a pipe is open. Do not use */
429 __CVMX_USB_PIPE_FLAGS_SCHEDULED = 1<<17, /**< Used internally to determine if a pipe is actively using hardware. Do not use */
430 __CVMX_USB_PIPE_FLAGS_NEED_PING = 1<<18, /**< Used internally to determine if a high speed pipe is in the ping state. Do not use */
431 } cvmx_usb_pipe_flags_t;
432
433 extern int cvmx_usb_get_num_ports(void);
434 extern int cvmx_usb_initialize(cvmx_usb_state_t *state, int usb_port_number,
435 cvmx_usb_initialize_flags_t flags);
436 extern int cvmx_usb_shutdown(cvmx_usb_state_t *state);
437 extern int cvmx_usb_enable(cvmx_usb_state_t *state);
438 extern int cvmx_usb_disable(cvmx_usb_state_t *state);
439 extern cvmx_usb_port_status_t cvmx_usb_get_status(cvmx_usb_state_t *state);
440 extern void cvmx_usb_set_status(cvmx_usb_state_t *state, cvmx_usb_port_status_t port_status);
441 extern int cvmx_usb_open_pipe(cvmx_usb_state_t *state,
442 cvmx_usb_pipe_flags_t flags,
443 int device_addr, int endpoint_num,
444 enum cvmx_usb_speed device_speed, int max_packet,
445 enum cvmx_usb_transfer transfer_type,
446 enum cvmx_usb_direction transfer_dir, int interval,
447 int multi_count, int hub_device_addr,
448 int hub_port);
449 extern int cvmx_usb_submit_bulk(cvmx_usb_state_t *state, int pipe_handle,
450 uint64_t buffer, int buffer_length,
451 cvmx_usb_callback_func_t callback,
452 void *user_data);
453 extern int cvmx_usb_submit_interrupt(cvmx_usb_state_t *state, int pipe_handle,
454 uint64_t buffer, int buffer_length,
455 cvmx_usb_callback_func_t callback,
456 void *user_data);
457 extern int cvmx_usb_submit_control(cvmx_usb_state_t *state, int pipe_handle,
458 uint64_t control_header,
459 uint64_t buffer, int buffer_length,
460 cvmx_usb_callback_func_t callback,
461 void *user_data);
462
463 /**
464 * Flags to pass the cvmx_usb_submit_isochronous() function.
465 */
466 typedef enum
467 {
468 CVMX_USB_ISOCHRONOUS_FLAGS_ALLOW_SHORT = 1<<0, /**< Do not return an error if a transfer is less than the maximum packet size of the device */
469 CVMX_USB_ISOCHRONOUS_FLAGS_ASAP = 1<<1, /**< Schedule the transaction as soon as possible */
470 } cvmx_usb_isochronous_flags_t;
471
472 extern int cvmx_usb_submit_isochronous(cvmx_usb_state_t *state, int pipe_handle,
473 int start_frame, int flags,
474 int number_packets,
475 cvmx_usb_iso_packet_t packets[],
476 uint64_t buffer, int buffer_length,
477 cvmx_usb_callback_func_t callback,
478 void *user_data);
479 extern int cvmx_usb_cancel(cvmx_usb_state_t *state, int pipe_handle,
480 int submit_handle);
481 extern int cvmx_usb_cancel_all(cvmx_usb_state_t *state, int pipe_handle);
482 extern int cvmx_usb_close_pipe(cvmx_usb_state_t *state, int pipe_handle);
483 extern int cvmx_usb_register_callback(cvmx_usb_state_t *state,
484 cvmx_usb_callback_t reason,
485 cvmx_usb_callback_func_t callback,
486 void *user_data);
487 extern int cvmx_usb_get_frame_number(cvmx_usb_state_t *state);
488 extern int cvmx_usb_poll(cvmx_usb_state_t *state);
489
490 #endif /* __CVMX_USB_H__ */
This page took 0.043165 seconds and 5 git commands to generate.