1 /*
2  * SPDX-License-Identifier: GPL-2.0
3  * tm6000.h - driver for TM5600/TM6000/TM6010 USB video capture devices
4  *
5  * Copyright (c) 2006-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
6  *
7  * Copyright (c) 2007 Michel Ludwig <michel.ludwig@gmail.com>
8  *	- DVB-T support
9  */
10 
11 #include <linux/videodev2.h>
12 #include <media/v4l2-common.h>
13 #include <media/videobuf-vmalloc.h>
14 #include "tm6000-usb-isoc.h"
15 #include <linux/i2c.h>
16 #include <linux/mutex.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-fh.h>
20 
21 #include <linux/dvb/frontend.h>
22 #include <media/dvb_demux.h>
23 #include <media/dvb_frontend.h>
24 #include <media/dmxdev.h>
25 
26 /* Inputs */
27 enum tm6000_itype {
28 	TM6000_INPUT_TV	= 1,
29 	TM6000_INPUT_COMPOSITE1,
30 	TM6000_INPUT_COMPOSITE2,
31 	TM6000_INPUT_SVIDEO,
32 	TM6000_INPUT_DVB,
33 	TM6000_INPUT_RADIO,
34 };
35 
36 enum tm6000_mux {
37 	TM6000_VMUX_VIDEO_A = 1,
38 	TM6000_VMUX_VIDEO_B,
39 	TM6000_VMUX_VIDEO_AB,
40 	TM6000_AMUX_ADC1,
41 	TM6000_AMUX_ADC2,
42 	TM6000_AMUX_SIF1,
43 	TM6000_AMUX_SIF2,
44 	TM6000_AMUX_I2S,
45 };
46 
47 enum tm6000_devtype {
48 	TM6000 = 0,
49 	TM5600,
50 	TM6010,
51 };
52 
53 struct tm6000_input {
54 	enum tm6000_itype	type;
55 	enum tm6000_mux		vmux;
56 	enum tm6000_mux		amux;
57 	unsigned int		v_gpio;
58 	unsigned int		a_gpio;
59 };
60 
61 /* ------------------------------------------------------------------
62  *	Basic structures
63  * ------------------------------------------------------------------
64  */
65 
66 struct tm6000_fmt {
67 	char  *name;
68 	u32   fourcc;          /* v4l2 format id */
69 	int   depth;
70 };
71 
72 /* buffer for one video frame */
73 struct tm6000_buffer {
74 	/* common v4l buffer stuff -- must be first */
75 	struct videobuf_buffer vb;
76 
77 	struct tm6000_fmt      *fmt;
78 };
79 
80 struct tm6000_dmaqueue {
81 	struct list_head       active;
82 	struct list_head       queued;
83 
84 	/* thread for generating video stream*/
85 	struct task_struct         *kthread;
86 	wait_queue_head_t          wq;
87 	/* Counters to control fps rate */
88 	int                        frame;
89 	int                        ini_jiffies;
90 };
91 
92 /* device states */
93 enum tm6000_core_state {
94 	DEV_INITIALIZED   = 0x01,
95 	DEV_DISCONNECTED  = 0x02,
96 	DEV_MISCONFIGURED = 0x04,
97 };
98 
99 /* io methods */
100 enum tm6000_io_method {
101 	IO_NONE,
102 	IO_READ,
103 	IO_MMAP,
104 };
105 
106 enum tm6000_mode {
107 	TM6000_MODE_UNKNOWN = 0,
108 	TM6000_MODE_ANALOG,
109 	TM6000_MODE_DIGITAL,
110 };
111 
112 struct tm6000_gpio {
113 	int		tuner_reset;
114 	int		tuner_on;
115 	int		demod_reset;
116 	int		demod_on;
117 	int		power_led;
118 	int		dvb_led;
119 	int		ir;
120 };
121 
122 struct tm6000_capabilities {
123 	unsigned int    has_tuner:1;
124 	unsigned int    has_tda9874:1;
125 	unsigned int    has_dvb:1;
126 	unsigned int    has_zl10353:1;
127 	unsigned int    has_eeprom:1;
128 	unsigned int    has_remote:1;
129 	unsigned int    has_radio:1;
130 };
131 
132 struct tm6000_dvb {
133 	struct dvb_adapter	adapter;
134 	struct dvb_demux	demux;
135 	struct dvb_frontend	*frontend;
136 	struct dmxdev		dmxdev;
137 	unsigned int		streams;
138 	struct urb		*bulk_urb;
139 	struct mutex		mutex;
140 };
141 
142 struct snd_tm6000_card {
143 	struct snd_card			*card;
144 	spinlock_t			reg_lock;
145 	struct tm6000_core		*core;
146 	struct snd_pcm_substream	*substream;
147 
148 	/* temporary data for buffer fill processing */
149 	unsigned			buf_pos;
150 	unsigned			period_pos;
151 };
152 
153 struct tm6000_endpoint {
154 	struct usb_host_endpoint	*endp;
155 	__u8				bInterfaceNumber;
156 	__u8				bAlternateSetting;
157 	unsigned			maxsize;
158 };
159 
160 #define TM6000_QUIRK_NO_USB_DELAY (1 << 0)
161 
162 struct tm6000_core {
163 	/* generic device properties */
164 	char				name[30];	/* name (including minor) of the device */
165 	int				model;		/* index in the device_data struct */
166 	int				devno;		/* marks the number of this device */
167 	enum tm6000_devtype		dev_type;	/* type of device */
168 	unsigned char			eedata[256];	/* Eeprom data */
169 	unsigned			eedata_size;	/* Size of the eeprom info */
170 
171 	v4l2_std_id                     norm;           /* Current norm */
172 	int				width, height;	/* Selected resolution */
173 
174 	enum tm6000_core_state		state;
175 
176 	/* Device Capabilities*/
177 	struct tm6000_capabilities	caps;
178 
179 	/* Used to load alsa/dvb */
180 	struct work_struct		request_module_wk;
181 
182 	/* Tuner configuration */
183 	int				tuner_type;		/* type of the tuner */
184 	int				tuner_addr;		/* tuner address */
185 
186 	struct tm6000_gpio		gpio;
187 
188 	char				*ir_codes;
189 
190 	__u8				radio;
191 
192 	/* Demodulator configuration */
193 	int				demod_addr;	/* demodulator address */
194 
195 	int				audio_bitrate;
196 	/* i2c i/o */
197 	struct i2c_adapter		i2c_adap;
198 	struct i2c_client		i2c_client;
199 
200 
201 	/* extension */
202 	struct list_head		devlist;
203 
204 	/* video for linux */
205 	int				users;
206 
207 	/* various device info */
208 	struct tm6000_fh		*resources;	/* Points to fh that is streaming */
209 	bool				is_res_read;
210 
211 	struct video_device		vfd;
212 	struct video_device		radio_dev;
213 	struct tm6000_dmaqueue		vidq;
214 	struct v4l2_device		v4l2_dev;
215 	struct v4l2_ctrl_handler	ctrl_handler;
216 	struct v4l2_ctrl_handler	radio_ctrl_handler;
217 
218 	int				input;
219 	struct tm6000_input		vinput[3];	/* video input */
220 	struct tm6000_input		rinput;		/* radio input */
221 
222 	int				freq;
223 	unsigned int			fourcc;
224 
225 	enum tm6000_mode		mode;
226 
227 	int				ctl_mute;             /* audio */
228 	int				ctl_volume;
229 	int				amode;
230 
231 	/* DVB-T support */
232 	struct tm6000_dvb		*dvb;
233 
234 	/* audio support */
235 	struct snd_tm6000_card		*adev;
236 	struct work_struct		wq_trigger;   /* Trigger to start/stop audio for alsa module */
237 	atomic_t			stream_started;  /* stream should be running if true */
238 
239 	struct tm6000_IR		*ir;
240 
241 	/* locks */
242 	struct mutex			lock;
243 	struct mutex			usb_lock;
244 
245 	/* usb transfer */
246 	struct usb_device		*udev;		/* the usb device */
247 
248 	struct tm6000_endpoint		bulk_in, bulk_out, isoc_in, isoc_out;
249 	struct tm6000_endpoint		int_in, int_out;
250 
251 	/* scaler!=0 if scaler is active*/
252 	int				scaler;
253 
254 		/* Isoc control struct */
255 	struct usb_isoc_ctl          isoc_ctl;
256 
257 	spinlock_t                   slock;
258 
259 	/* urb dma buffers */
260 	char				**urb_buffer;
261 	dma_addr_t			*urb_dma;
262 	unsigned int			urb_size;
263 
264 	unsigned long quirks;
265 };
266 
267 enum tm6000_ops_type {
268 	TM6000_AUDIO = 0x10,
269 	TM6000_DVB = 0x20,
270 };
271 
272 struct tm6000_ops {
273 	struct list_head	next;
274 	char			*name;
275 	enum tm6000_ops_type	type;
276 	int (*init)(struct tm6000_core *);
277 	int (*fini)(struct tm6000_core *);
278 	int (*fillbuf)(struct tm6000_core *, char *buf, int size);
279 };
280 
281 struct tm6000_fh {
282 	struct v4l2_fh		     fh;
283 	struct tm6000_core           *dev;
284 	unsigned int                 radio;
285 
286 	/* video capture */
287 	struct tm6000_fmt            *fmt;
288 	unsigned int                 width, height;
289 	struct videobuf_queue        vb_vidq;
290 
291 	enum v4l2_buf_type           type;
292 };
293 
294 #define TM6000_STD	(V4L2_STD_PAL|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|    \
295 			V4L2_STD_PAL_M|V4L2_STD_PAL_60|V4L2_STD_NTSC_M| \
296 			V4L2_STD_NTSC_M_JP|V4L2_STD_SECAM)
297 
298 /* In tm6000-cards.c */
299 
300 int tm6000_tuner_callback(void *ptr, int component, int command, int arg);
301 int tm6000_xc5000_callback(void *ptr, int component, int command, int arg);
302 int tm6000_cards_setup(struct tm6000_core *dev);
303 void tm6000_flash_led(struct tm6000_core *dev, u8 state);
304 
305 /* In tm6000-core.c */
306 
307 int tm6000_read_write_usb(struct tm6000_core *dev, u8 reqtype, u8 req,
308 			   u16 value, u16 index, u8 *buf, u16 len);
309 int tm6000_get_reg(struct tm6000_core *dev, u8 req, u16 value, u16 index);
310 int tm6000_get_reg16(struct tm6000_core *dev, u8 req, u16 value, u16 index);
311 int tm6000_get_reg32(struct tm6000_core *dev, u8 req, u16 value, u16 index);
312 int tm6000_set_reg(struct tm6000_core *dev, u8 req, u16 value, u16 index);
313 int tm6000_set_reg_mask(struct tm6000_core *dev, u8 req, u16 value,
314 						u16 index, u16 mask);
315 int tm6000_i2c_reset(struct tm6000_core *dev, u16 tsleep);
316 int tm6000_init(struct tm6000_core *dev);
317 int tm6000_reset(struct tm6000_core *dev);
318 
319 int tm6000_init_analog_mode(struct tm6000_core *dev);
320 int tm6000_init_digital_mode(struct tm6000_core *dev);
321 int tm6000_set_audio_bitrate(struct tm6000_core *dev, int bitrate);
322 int tm6000_set_audio_rinput(struct tm6000_core *dev);
323 int tm6000_tvaudio_set_mute(struct tm6000_core *dev, u8 mute);
324 void tm6000_set_volume(struct tm6000_core *dev, int vol);
325 
326 int tm6000_v4l2_register(struct tm6000_core *dev);
327 int tm6000_v4l2_unregister(struct tm6000_core *dev);
328 int tm6000_v4l2_exit(void);
329 void tm6000_set_fourcc_format(struct tm6000_core *dev);
330 
331 void tm6000_remove_from_devlist(struct tm6000_core *dev);
332 void tm6000_add_into_devlist(struct tm6000_core *dev);
333 int tm6000_register_extension(struct tm6000_ops *ops);
334 void tm6000_unregister_extension(struct tm6000_ops *ops);
335 void tm6000_init_extension(struct tm6000_core *dev);
336 void tm6000_close_extension(struct tm6000_core *dev);
337 int tm6000_call_fillbuf(struct tm6000_core *dev, enum tm6000_ops_type type,
338 			char *buf, int size);
339 
340 
341 /* In tm6000-stds.c */
342 void tm6000_get_std_res(struct tm6000_core *dev);
343 int tm6000_set_standard(struct tm6000_core *dev);
344 
345 /* In tm6000-i2c.c */
346 int tm6000_i2c_register(struct tm6000_core *dev);
347 int tm6000_i2c_unregister(struct tm6000_core *dev);
348 
349 /* In tm6000-queue.c */
350 
351 int tm6000_v4l2_mmap(struct file *filp, struct vm_area_struct *vma);
352 
353 int tm6000_vidioc_streamon(struct file *file, void *priv,
354 			   enum v4l2_buf_type i);
355 int tm6000_vidioc_streamoff(struct file *file, void *priv,
356 			    enum v4l2_buf_type i);
357 int tm6000_vidioc_reqbufs(struct file *file, void *priv,
358 			  struct v4l2_requestbuffers *rb);
359 int tm6000_vidioc_querybuf(struct file *file, void *priv,
360 			   struct v4l2_buffer *b);
361 int tm6000_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b);
362 int tm6000_vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b);
363 ssize_t tm6000_v4l2_read(struct file *filp, char __user * buf, size_t count,
364 			 loff_t *f_pos);
365 unsigned int tm6000_v4l2_poll(struct file *file,
366 			      struct poll_table_struct *wait);
367 int tm6000_queue_init(struct tm6000_core *dev);
368 
369 /* In tm6000-alsa.c */
370 /*int tm6000_audio_init(struct tm6000_core *dev, int idx);*/
371 
372 /* In tm6000-input.c */
373 int tm6000_ir_init(struct tm6000_core *dev);
374 int tm6000_ir_fini(struct tm6000_core *dev);
375 void tm6000_ir_wait(struct tm6000_core *dev, u8 state);
376 int tm6000_ir_int_start(struct tm6000_core *dev);
377 void tm6000_ir_int_stop(struct tm6000_core *dev);
378 
379 /* Debug stuff */
380 
381 extern int tm6000_debug;
382 
383 #define dprintk(dev, level, fmt, arg...) do {\
384 	if (tm6000_debug & level) \
385 		printk(KERN_INFO "(%lu) %s %s :"fmt, jiffies, \
386 			 dev->name, __func__ , ##arg); } while (0)
387 
388 #define V4L2_DEBUG_REG		0x0004
389 #define V4L2_DEBUG_I2C		0x0008
390 #define V4L2_DEBUG_QUEUE	0x0010
391 #define V4L2_DEBUG_ISOC		0x0020
392 #define V4L2_DEBUG_RES_LOCK	0x0040	/* Resource locking */
393 #define V4L2_DEBUG_OPEN		0x0080	/* video open/close debug */
394 
395 #define tm6000_err(fmt, arg...) do {\
396 	printk(KERN_ERR "tm6000 %s :"fmt, \
397 		__func__ , ##arg); } while (0)
398