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