xref: /linux/drivers/media/usb/stk1160/stk1160.h (revision 4eee1520ea845a6d6d82e85498d9412419560871)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * STK1160 driver
4  *
5  * Copyright (C) 2012 Ezequiel Garcia
6  * <elezegarcia--a.t--gmail.com>
7  *
8  * Based on Easycap driver by R.M. Thomas
9  *	Copyright (C) 2010 R.M. Thomas
10  *	<rmthomas--a.t--sciolus.org>
11  */
12 
13 #include <linux/i2c.h>
14 #include <sound/core.h>
15 #include <sound/ac97_codec.h>
16 #include <media/videobuf2-v4l2.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-ctrls.h>
19 
20 #define STK1160_VERSION		"0.9.5"
21 #define STK1160_VERSION_NUM	0x000905
22 
23 /* Decide on number of packets for each buffer */
24 #define STK1160_NUM_PACKETS 64
25 
26 /* Number of buffers for isoc transfers */
27 #define STK1160_NUM_BUFS 16
28 #define STK1160_MIN_BUFS 1
29 
30 /* TODO: This endpoint address should be retrieved */
31 #define STK1160_EP_VIDEO 0x82
32 #define STK1160_EP_AUDIO 0x81
33 
34 /* Max and min video buffers */
35 #define STK1160_MIN_VIDEO_BUFFERS 8
36 #define STK1160_MAX_VIDEO_BUFFERS 32
37 
38 #define STK1160_MIN_PKT_SIZE 3072
39 
40 #define STK1160_MAX_INPUT 4
41 #define STK1160_SVIDEO_INPUT 4
42 
43 #define STK1160_AC97_TIMEOUT 50
44 
45 #define STK1160_I2C_TIMEOUT 100
46 
47 /* TODO: Print helpers
48  * I could use dev_xxx, pr_xxx, v4l2_xxx or printk.
49  * However, there isn't a solid consensus on which
50  * new drivers should use.
51  *
52  */
53 #ifdef DEBUG
54 #define stk1160_dbg(fmt, args...) \
55 	printk(KERN_DEBUG "stk1160: " fmt,  ## args)
56 #else
57 #define stk1160_dbg(fmt, args...)
58 #endif
59 
60 #define stk1160_info(fmt, args...) \
61 	pr_info("stk1160: " fmt, ## args)
62 
63 #define stk1160_warn(fmt, args...) \
64 	pr_warn("stk1160: " fmt, ## args)
65 
66 #define stk1160_err(fmt, args...) \
67 	pr_err("stk1160: " fmt, ## args)
68 
69 /* Buffer for one video frame */
70 struct stk1160_buffer {
71 	/* common v4l buffer stuff -- must be first */
72 	struct vb2_v4l2_buffer vb;
73 	struct list_head list;
74 
75 	void *mem;
76 	unsigned int length;		/* buffer length */
77 	unsigned int bytesused;		/* bytes written */
78 	int odd;			/* current oddity */
79 
80 	/*
81 	 * Since we interlace two fields per frame,
82 	 * this is different from bytesused.
83 	 */
84 	unsigned int pos;		/* current pos inside buffer */
85 };
86 
87 struct stk1160_urb {
88 	struct urb *urb;
89 	char *transfer_buffer;
90 	struct sg_table *sgt;
91 	struct stk1160 *dev;
92 	dma_addr_t dma;
93 };
94 
95 struct stk1160_isoc_ctl {
96 	/* max packet size of isoc transaction */
97 	int max_pkt_size;
98 
99 	/* number of allocated urbs */
100 	int num_bufs;
101 
102 	struct stk1160_urb urb_ctl[STK1160_NUM_BUFS];
103 
104 	/* current buffer */
105 	struct stk1160_buffer *buf;
106 };
107 
108 struct stk1160_fmt {
109 	u32   fourcc;          /* v4l2 format id */
110 	int   depth;
111 };
112 
113 struct stk1160 {
114 	struct v4l2_device v4l2_dev;
115 	struct video_device vdev;
116 	struct v4l2_ctrl_handler ctrl_handler;
117 
118 	struct device *dev;
119 	struct usb_device *udev;
120 
121 	/* saa7115 subdev */
122 	struct v4l2_subdev *sd_saa7115;
123 
124 	/* isoc control struct */
125 	struct list_head avail_bufs;
126 
127 	/* video capture */
128 	struct vb2_queue vb_vidq;
129 
130 	/* max packet size of isoc transaction */
131 	int max_pkt_size;
132 	/* array of wMaxPacketSize */
133 	unsigned int *alt_max_pkt_size;
134 	/* alternate */
135 	int alt;
136 	/* Number of alternative settings */
137 	int num_alt;
138 
139 	struct stk1160_isoc_ctl isoc_ctl;
140 
141 	/* frame properties */
142 	int width;		  /* current frame width */
143 	int height;		  /* current frame height */
144 	unsigned int ctl_input;	  /* selected input */
145 	v4l2_std_id norm;	  /* current norm */
146 	struct stk1160_fmt *fmt;  /* selected format */
147 
148 	unsigned int sequence;
149 
150 	/* i2c i/o */
151 	struct i2c_adapter i2c_adap;
152 	struct i2c_client i2c_client;
153 
154 	struct mutex v4l_lock;
155 	struct mutex vb_queue_lock;
156 	spinlock_t buf_lock;
157 
158 	struct file *fh_owner;	/* filehandle ownership */
159 
160 	/* EXPERIMENTAL */
161 	struct snd_card *snd_card;
162 };
163 
164 struct regval {
165 	u16 reg;
166 	u16 val;
167 };
168 
169 /* Provided by stk1160-v4l.c */
170 int stk1160_vb2_setup(struct stk1160 *dev);
171 int stk1160_video_register(struct stk1160 *dev);
172 void stk1160_video_unregister(struct stk1160 *dev);
173 void stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state);
174 
175 /* Provided by stk1160-video.c */
176 int stk1160_alloc_isoc(struct stk1160 *dev);
177 void stk1160_free_isoc(struct stk1160 *dev);
178 void stk1160_cancel_isoc(struct stk1160 *dev);
179 void stk1160_uninit_isoc(struct stk1160 *dev);
180 
181 /* Provided by stk1160-i2c.c */
182 int stk1160_i2c_register(struct stk1160 *dev);
183 int stk1160_i2c_unregister(struct stk1160 *dev);
184 
185 /* Provided by stk1160-core.c */
186 int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value);
187 int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value);
188 int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg,
189 		char *buf, int len);
190 int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg,
191 		char *buf, int len);
192 void stk1160_select_input(struct stk1160 *dev);
193 
194 /* Provided by stk1160-ac97.c */
195 void stk1160_ac97_setup(struct stk1160 *dev);
196