1 /*
2  *
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20 #ifndef __PVRUSB2_HDW_H
21 #define __PVRUSB2_HDW_H
22 
23 #include <linux/usb.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-dev.h>
26 #include "pvrusb2-io.h"
27 #include "pvrusb2-ctrl.h"
28 
29 
30 /* Private internal control ids, look these up with
31    pvr2_hdw_get_ctrl_by_id() - these are NOT visible in V4L */
32 #define PVR2_CID_STDCUR 2
33 #define PVR2_CID_STDAVAIL 3
34 #define PVR2_CID_INPUT 4
35 #define PVR2_CID_AUDIOMODE 5
36 #define PVR2_CID_FREQUENCY 6
37 #define PVR2_CID_HRES 7
38 #define PVR2_CID_VRES 8
39 #define PVR2_CID_CROPL 9
40 #define PVR2_CID_CROPT 10
41 #define PVR2_CID_CROPW 11
42 #define PVR2_CID_CROPH 12
43 #define PVR2_CID_CROPCAPPAN 13
44 #define PVR2_CID_CROPCAPPAD 14
45 #define PVR2_CID_CROPCAPBL 15
46 #define PVR2_CID_CROPCAPBT 16
47 #define PVR2_CID_CROPCAPBW 17
48 #define PVR2_CID_CROPCAPBH 18
49 #define PVR2_CID_STDDETECT 19
50 
51 /* Legal values for the INPUT state variable */
52 #define PVR2_CVAL_INPUT_TV 0
53 #define PVR2_CVAL_INPUT_DTV 1
54 #define PVR2_CVAL_INPUT_COMPOSITE 2
55 #define PVR2_CVAL_INPUT_SVIDEO 3
56 #define PVR2_CVAL_INPUT_RADIO 4
57 
58 enum pvr2_config {
59 	pvr2_config_empty,    /* No configuration */
60 	pvr2_config_mpeg,     /* Encoded / compressed video */
61 	pvr2_config_vbi,      /* Standard vbi info */
62 	pvr2_config_pcm,      /* Audio raw pcm stream */
63 	pvr2_config_rawvideo, /* Video raw frames */
64 };
65 
66 enum pvr2_v4l_type {
67 	pvr2_v4l_type_video,
68 	pvr2_v4l_type_vbi,
69 	pvr2_v4l_type_radio,
70 };
71 
72 /* Major states that we can be in:
73  *
74  *  DEAD - Device is in an unusable state and cannot be recovered.  This
75  *  can happen if we completely lose the ability to communicate with it
76  *  (but it might still on the bus).  In this state there's nothing we can
77  *  do; it must be replugged in order to recover.
78  *
79  *  COLD - Device is in an unusable state, needs microcontroller firmware.
80  *
81  *  WARM - We can communicate with the device and the proper
82  *  microcontroller firmware is running, but other device initialization is
83  *  still needed (e.g. encoder firmware).
84  *
85  *  ERROR - A problem prevents capture operation (e.g. encoder firmware
86  *  missing).
87  *
88  *  READY - Device is operational, but not streaming.
89  *
90  *  RUN - Device is streaming.
91  *
92  */
93 #define PVR2_STATE_NONE 0
94 #define PVR2_STATE_DEAD 1
95 #define PVR2_STATE_COLD 2
96 #define PVR2_STATE_WARM 3
97 #define PVR2_STATE_ERROR 4
98 #define PVR2_STATE_READY 5
99 #define PVR2_STATE_RUN 6
100 
101 /* Translate configuration enum to a string label */
102 const char *pvr2_config_get_name(enum pvr2_config);
103 
104 struct pvr2_hdw;
105 
106 /* Create and return a structure for interacting with the underlying
107    hardware */
108 struct pvr2_hdw *pvr2_hdw_create(struct usb_interface *intf,
109 				 const struct usb_device_id *devid);
110 
111 /* Perform second stage initialization, passing in a notification callback
112    for when the master state changes. */
113 int pvr2_hdw_initialize(struct pvr2_hdw *,
114 			void (*callback_func)(void *),
115 			void *callback_data);
116 
117 /* Destroy hardware interaction structure */
118 void pvr2_hdw_destroy(struct pvr2_hdw *);
119 
120 /* Return true if in the ready (normal) state */
121 int pvr2_hdw_dev_ok(struct pvr2_hdw *);
122 
123 /* Return small integer number [1..N] for logical instance number of this
124    device.  This is useful for indexing array-valued module parameters. */
125 int pvr2_hdw_get_unit_number(struct pvr2_hdw *);
126 
127 /* Get pointer to underlying USB device */
128 struct usb_device *pvr2_hdw_get_dev(struct pvr2_hdw *);
129 
130 /* Retrieve serial number of device */
131 unsigned long pvr2_hdw_get_sn(struct pvr2_hdw *);
132 
133 /* Retrieve bus location info of device */
134 const char *pvr2_hdw_get_bus_info(struct pvr2_hdw *);
135 
136 /* Retrieve per-instance string identifier for this specific device */
137 const char *pvr2_hdw_get_device_identifier(struct pvr2_hdw *);
138 
139 /* Called when hardware has been unplugged */
140 void pvr2_hdw_disconnect(struct pvr2_hdw *);
141 
142 /* Sets v4l2_dev of a video_device struct */
143 void pvr2_hdw_set_v4l2_dev(struct pvr2_hdw *, struct video_device *);
144 
145 /* Get the number of defined controls */
146 unsigned int pvr2_hdw_get_ctrl_count(struct pvr2_hdw *);
147 
148 /* Retrieve a control handle given its index (0..count-1) */
149 struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_index(struct pvr2_hdw *,unsigned int);
150 
151 /* Retrieve a control handle given its internal ID (if any) */
152 struct pvr2_ctrl *pvr2_hdw_get_ctrl_by_id(struct pvr2_hdw *,unsigned int);
153 
154 /* Retrieve a control handle given its V4L ID (if any) */
155 struct pvr2_ctrl *pvr2_hdw_get_ctrl_v4l(struct pvr2_hdw *,unsigned int ctl_id);
156 
157 /* Retrieve a control handle given its immediate predecessor V4L ID (if any) */
158 struct pvr2_ctrl *pvr2_hdw_get_ctrl_nextv4l(struct pvr2_hdw *,
159 					    unsigned int ctl_id);
160 
161 /* Commit all control changes made up to this point */
162 int pvr2_hdw_commit_ctl(struct pvr2_hdw *);
163 
164 /* Return a bit mask of valid input selections for this device.  Mask bits
165  * will be according to PVR_CVAL_INPUT_xxxx definitions. */
166 unsigned int pvr2_hdw_get_input_available(struct pvr2_hdw *);
167 
168 /* Return a bit mask of allowed input selections for this device.  Mask bits
169  * will be according to PVR_CVAL_INPUT_xxxx definitions. */
170 unsigned int pvr2_hdw_get_input_allowed(struct pvr2_hdw *);
171 
172 /* Change the set of allowed input selections for this device.  Both
173    change_mask and change_valu are mask bits according to
174    PVR_CVAL_INPUT_xxxx definitions.  The change_mask parameter indicate
175    which settings are being changed and the change_val parameter indicates
176    whether corresponding settings are being set or cleared. */
177 int pvr2_hdw_set_input_allowed(struct pvr2_hdw *,
178 			       unsigned int change_mask,
179 			       unsigned int change_val);
180 
181 /* Return name for this driver instance */
182 const char *pvr2_hdw_get_driver_name(struct pvr2_hdw *);
183 
184 /* Mark tuner status stale so that it will be re-fetched */
185 void pvr2_hdw_execute_tuner_poll(struct pvr2_hdw *);
186 
187 /* Return information about the tuner */
188 int pvr2_hdw_get_tuner_status(struct pvr2_hdw *,struct v4l2_tuner *);
189 
190 /* Return information about cropping capabilities */
191 int pvr2_hdw_get_cropcap(struct pvr2_hdw *, struct v4l2_cropcap *);
192 
193 /* Query device and see if it thinks it is on a high-speed USB link */
194 int pvr2_hdw_is_hsm(struct pvr2_hdw *);
195 
196 /* Return a string token representative of the hardware type */
197 const char *pvr2_hdw_get_type(struct pvr2_hdw *);
198 
199 /* Return a single line description of the hardware type */
200 const char *pvr2_hdw_get_desc(struct pvr2_hdw *);
201 
202 /* Turn streaming on/off */
203 int pvr2_hdw_set_streaming(struct pvr2_hdw *,int);
204 
205 /* Find out if streaming is on */
206 int pvr2_hdw_get_streaming(struct pvr2_hdw *);
207 
208 /* Retrieve driver overall state */
209 int pvr2_hdw_get_state(struct pvr2_hdw *);
210 
211 /* Configure the type of stream to generate */
212 int pvr2_hdw_set_stream_type(struct pvr2_hdw *, enum pvr2_config);
213 
214 /* Get handle to video output stream */
215 struct pvr2_stream *pvr2_hdw_get_video_stream(struct pvr2_hdw *);
216 
217 /* Enable / disable retrieval of CPU firmware or prom contents.  This must
218    be enabled before pvr2_hdw_cpufw_get() will function.  Note that doing
219    this may prevent the device from running (and leaving this mode may
220    imply a device reset). */
221 void pvr2_hdw_cpufw_set_enabled(struct pvr2_hdw *,
222 				int mode, /* 0=8KB FX2, 1=16KB FX2, 2=PROM */
223 				int enable_flag);
224 
225 /* Return true if we're in a mode for retrieval CPU firmware */
226 int pvr2_hdw_cpufw_get_enabled(struct pvr2_hdw *);
227 
228 /* Retrieve a piece of the CPU's firmware at the given offset.  Return
229    value is the number of bytes retrieved or zero if we're past the end or
230    an error otherwise (e.g. if firmware retrieval is not enabled). */
231 int pvr2_hdw_cpufw_get(struct pvr2_hdw *,unsigned int offs,
232 		       char *buf,unsigned int cnt);
233 
234 /* Retrieve a previously stored v4l minor device number */
235 int pvr2_hdw_v4l_get_minor_number(struct pvr2_hdw *,enum pvr2_v4l_type index);
236 
237 /* Store a v4l minor device number */
238 void pvr2_hdw_v4l_store_minor_number(struct pvr2_hdw *,
239 				     enum pvr2_v4l_type index,int);
240 
241 /* The following entry points are all lower level things you normally don't
242    want to worry about. */
243 
244 /* Issue a command and get a response from the device.  LOTS of higher
245    level stuff is built on this. */
246 int pvr2_send_request(struct pvr2_hdw *,
247 		      void *write_ptr,unsigned int write_len,
248 		      void *read_ptr,unsigned int read_len);
249 
250 /* Slightly higher level device communication functions. */
251 int pvr2_write_register(struct pvr2_hdw *, u16, u32);
252 
253 /* Call if for any reason we can't talk to the hardware anymore - this will
254    cause the driver to stop flailing on the device. */
255 void pvr2_hdw_render_useless(struct pvr2_hdw *);
256 
257 /* Set / clear 8051's reset bit */
258 void pvr2_hdw_cpureset_assert(struct pvr2_hdw *,int);
259 
260 /* Execute a USB-commanded device reset */
261 void pvr2_hdw_device_reset(struct pvr2_hdw *);
262 
263 /* Reset worker's error trapping circuit breaker */
264 int pvr2_hdw_untrip(struct pvr2_hdw *);
265 
266 /* Execute hard reset command (after this point it's likely that the
267    encoder will have to be reconfigured).  This also clears the "useless"
268    state. */
269 int pvr2_hdw_cmd_deep_reset(struct pvr2_hdw *);
270 
271 /* Execute simple reset command */
272 int pvr2_hdw_cmd_powerup(struct pvr2_hdw *);
273 
274 /* Order decoder to reset */
275 int pvr2_hdw_cmd_decoder_reset(struct pvr2_hdw *);
276 
277 /* Direct manipulation of GPIO bits */
278 int pvr2_hdw_gpio_get_dir(struct pvr2_hdw *hdw,u32 *);
279 int pvr2_hdw_gpio_get_out(struct pvr2_hdw *hdw,u32 *);
280 int pvr2_hdw_gpio_get_in(struct pvr2_hdw *hdw,u32 *);
281 int pvr2_hdw_gpio_chg_dir(struct pvr2_hdw *hdw,u32 msk,u32 val);
282 int pvr2_hdw_gpio_chg_out(struct pvr2_hdw *hdw,u32 msk,u32 val);
283 
284 /* This data structure is specifically for the next function... */
285 struct pvr2_hdw_debug_info {
286 	int big_lock_held;
287 	int ctl_lock_held;
288 	int flag_disconnected;
289 	int flag_init_ok;
290 	int flag_ok;
291 	int fw1_state;
292 	int flag_decoder_missed;
293 	int flag_tripped;
294 	int state_encoder_ok;
295 	int state_encoder_run;
296 	int state_decoder_run;
297 	int state_decoder_ready;
298 	int state_usbstream_run;
299 	int state_decoder_quiescent;
300 	int state_pipeline_config;
301 	int state_pipeline_req;
302 	int state_pipeline_pause;
303 	int state_pipeline_idle;
304 	int cmd_debug_state;
305 	int cmd_debug_write_len;
306 	int cmd_debug_read_len;
307 	int cmd_debug_write_pend;
308 	int cmd_debug_read_pend;
309 	int cmd_debug_timeout;
310 	int cmd_debug_rstatus;
311 	int cmd_debug_wstatus;
312 	unsigned char cmd_code;
313 };
314 
315 /* Non-intrusively retrieve internal state info - this is useful for
316    diagnosing lockups.  Note that this operation is completed without any
317    kind of locking and so it is not atomic and may yield inconsistent
318    results.  This is *purely* a debugging aid. */
319 void pvr2_hdw_get_debug_info_unlocked(const struct pvr2_hdw *hdw,
320 				      struct pvr2_hdw_debug_info *);
321 
322 /* Intrusively retrieve internal state info - this is useful for
323    diagnosing overall driver state.  This operation synchronizes against
324    the overall driver mutex - so if there are locking problems this will
325    likely hang!  This is *purely* a debugging aid. */
326 void pvr2_hdw_get_debug_info_locked(struct pvr2_hdw *hdw,
327 				    struct pvr2_hdw_debug_info *);
328 
329 /* Report out several lines of text that describes driver internal state.
330    Results are written into the passed-in buffer. */
331 unsigned int pvr2_hdw_state_report(struct pvr2_hdw *hdw,
332 				   char *buf_ptr,unsigned int buf_size);
333 
334 /* Cause modules to log their state once */
335 void pvr2_hdw_trigger_module_log(struct pvr2_hdw *hdw);
336 
337 /* Cause encoder firmware to be uploaded into the device.  This is normally
338    done autonomously, but the interface is exported here because it is also
339    a debugging aid. */
340 int pvr2_upload_firmware2(struct pvr2_hdw *hdw);
341 
342 #endif /* __PVRUSB2_HDW_H */
343