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 * TODO:
13 *
14 * 1. Support stream at lower speed: lower frame rate or lower frame size.
15 */
16
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22
23 #include <linux/usb.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26 #include <media/i2c/saa7115.h>
27
28 #include "stk1160.h"
29 #include "stk1160-reg.h"
30
31 static unsigned int input;
32 module_param(input, int, 0644);
33 MODULE_PARM_DESC(input, "Set default input");
34
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Ezequiel Garcia");
37 MODULE_DESCRIPTION("STK1160 driver");
38
39 /* Devices supported by this driver */
40 static const struct usb_device_id stk1160_id_table[] = {
41 { USB_DEVICE(0x05e1, 0x0408) },
42 { }
43 };
44 MODULE_DEVICE_TABLE(usb, stk1160_id_table);
45
46 /* saa7113 I2C address */
47 static unsigned short saa7113_addrs[] = {
48 0x4a >> 1,
49 I2C_CLIENT_END
50 };
51
52 /*
53 * Read/Write stk registers
54 */
stk1160_read_reg(struct stk1160 * dev,u16 reg,u8 * value)55 int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value)
56 {
57 int ret;
58 int pipe = usb_rcvctrlpipe(dev->udev, 0);
59 u8 *buf;
60
61 *value = 0;
62
63 buf = kmalloc(sizeof(u8), GFP_KERNEL);
64 if (!buf)
65 return -ENOMEM;
66 ret = usb_control_msg(dev->udev, pipe, 0x00,
67 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
68 0x00, reg, buf, sizeof(u8), 1000);
69 if (ret < 0) {
70 stk1160_err("read failed on reg 0x%x (%d)\n",
71 reg, ret);
72 kfree(buf);
73 return ret;
74 }
75
76 *value = *buf;
77 kfree(buf);
78 return 0;
79 }
80
stk1160_write_reg(struct stk1160 * dev,u16 reg,u16 value)81 int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value)
82 {
83 int ret;
84 int pipe = usb_sndctrlpipe(dev->udev, 0);
85
86 ret = usb_control_msg(dev->udev, pipe, 0x01,
87 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
88 value, reg, NULL, 0, 1000);
89 if (ret < 0) {
90 stk1160_err("write failed on reg 0x%x (%d)\n",
91 reg, ret);
92 return ret;
93 }
94
95 return 0;
96 }
97
stk1160_select_input(struct stk1160 * dev)98 void stk1160_select_input(struct stk1160 *dev)
99 {
100 int route;
101 static const u8 gctrl[] = {
102 0x98, 0x90, 0x88, 0x80, 0x98
103 };
104
105 if (dev->ctl_input == STK1160_SVIDEO_INPUT)
106 route = SAA7115_SVIDEO3;
107 else
108 route = SAA7115_COMPOSITE0;
109
110 if (dev->ctl_input < ARRAY_SIZE(gctrl)) {
111 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
112 route, 0, 0);
113 stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]);
114 }
115 }
116
117 /* TODO: We should break this into pieces */
stk1160_reg_reset(struct stk1160 * dev)118 static void stk1160_reg_reset(struct stk1160 *dev)
119 {
120 int i;
121
122 static const struct regval ctl[] = {
123 {STK1160_GCTRL+2, 0x0078},
124
125 {STK1160_RMCTL+1, 0x0000},
126 {STK1160_RMCTL+3, 0x0002},
127
128 {STK1160_PLLSO, 0x0010},
129 {STK1160_PLLSO+1, 0x0000},
130 {STK1160_PLLSO+2, 0x0014},
131 {STK1160_PLLSO+3, 0x000E},
132
133 {STK1160_PLLFD, 0x0046},
134
135 /* Timing generator setup */
136 {STK1160_TIGEN, 0x0012},
137 {STK1160_TICTL, 0x002D},
138 {STK1160_TICTL+1, 0x0001},
139 {STK1160_TICTL+2, 0x0000},
140 {STK1160_TICTL+3, 0x0000},
141 {STK1160_TIGEN, 0x0080},
142
143 {0xffff, 0xffff}
144 };
145
146 for (i = 0; ctl[i].reg != 0xffff; i++)
147 stk1160_write_reg(dev, ctl[i].reg, ctl[i].val);
148 }
149
stk1160_release(struct v4l2_device * v4l2_dev)150 static void stk1160_release(struct v4l2_device *v4l2_dev)
151 {
152 struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev);
153
154 stk1160_dbg("releasing all resources\n");
155
156 stk1160_i2c_unregister(dev);
157
158 v4l2_ctrl_handler_free(&dev->ctrl_handler);
159 v4l2_device_unregister(&dev->v4l2_dev);
160 mutex_destroy(&dev->v4l_lock);
161 mutex_destroy(&dev->vb_queue_lock);
162 kfree(dev->alt_max_pkt_size);
163 kfree(dev);
164 }
165
166 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
167 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
168
169 /*
170 * Scan usb interface and populate max_pkt_size array
171 * with information on each alternate setting.
172 * The array should be allocated by the caller.
173 */
stk1160_scan_usb(struct usb_interface * intf,struct usb_device * udev,unsigned int * max_pkt_size)174 static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev,
175 unsigned int *max_pkt_size)
176 {
177 int i, e, sizedescr, size, ifnum;
178 const struct usb_endpoint_descriptor *desc;
179
180 bool has_video = false, has_audio = false;
181 const char *speed;
182
183 ifnum = intf->altsetting[0].desc.bInterfaceNumber;
184
185 /* Get endpoints */
186 for (i = 0; i < intf->num_altsetting; i++) {
187
188 for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) {
189
190 /* This isn't clear enough, at least to me */
191 desc = &intf->altsetting[i].endpoint[e].desc;
192 sizedescr = le16_to_cpu(desc->wMaxPacketSize);
193 size = sizedescr & 0x7ff;
194
195 if (udev->speed == USB_SPEED_HIGH)
196 size = size * hb_mult(sizedescr);
197
198 if (usb_endpoint_is_isoc_in(desc)) {
199 switch (desc->bEndpointAddress) {
200 case STK1160_EP_AUDIO:
201 has_audio = true;
202 break;
203 case STK1160_EP_VIDEO:
204 has_video = true;
205 max_pkt_size[i] = size;
206 break;
207 }
208 }
209 }
210 }
211
212 /* Is this even possible? */
213 if (!(has_audio || has_video)) {
214 dev_err(&udev->dev, "no audio or video endpoints found\n");
215 return -ENODEV;
216 }
217
218 switch (udev->speed) {
219 case USB_SPEED_LOW:
220 speed = "1.5";
221 break;
222 case USB_SPEED_FULL:
223 speed = "12";
224 break;
225 case USB_SPEED_HIGH:
226 speed = "480";
227 break;
228 default:
229 speed = "unknown";
230 }
231
232 dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n",
233 udev->manufacturer ? udev->manufacturer : "",
234 udev->product ? udev->product : "",
235 speed,
236 le16_to_cpu(udev->descriptor.idVendor),
237 le16_to_cpu(udev->descriptor.idProduct),
238 ifnum,
239 intf->altsetting->desc.bInterfaceNumber);
240
241 /* This should never happen, since we rejected audio interfaces */
242 if (has_audio)
243 dev_warn(&udev->dev, "audio interface %d found.\n\
244 This is not implemented by this driver,\
245 you should use snd-usb-audio instead\n", ifnum);
246
247 if (has_video)
248 dev_info(&udev->dev, "video interface %d found\n",
249 ifnum);
250
251 /*
252 * Make sure we have 480 Mbps of bandwidth, otherwise things like
253 * video stream wouldn't likely work, since 12 Mbps is generally
254 * not enough even for most streams.
255 */
256 if (udev->speed != USB_SPEED_HIGH)
257 dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\
258 You may not be able to stream video smoothly\n");
259
260 return 0;
261 }
262
stk1160_probe(struct usb_interface * interface,const struct usb_device_id * id)263 static int stk1160_probe(struct usb_interface *interface,
264 const struct usb_device_id *id)
265 {
266 int rc = 0;
267
268 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
269 struct usb_device *udev;
270 struct stk1160 *dev;
271
272 udev = interface_to_usbdev(interface);
273
274 /*
275 * Since usb audio class is supported by snd-usb-audio,
276 * we reject audio interface.
277 */
278 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
279 return -ENODEV;
280
281 /* Alloc an array for all possible max_pkt_size */
282 alt_max_pkt_size = kmalloc_array(interface->num_altsetting,
283 sizeof(alt_max_pkt_size[0]),
284 GFP_KERNEL);
285 if (alt_max_pkt_size == NULL)
286 return -ENOMEM;
287
288 /*
289 * Scan usb possibilities and populate alt_max_pkt_size array.
290 * Also, check if device speed is fast enough.
291 */
292 rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size);
293 if (rc < 0) {
294 kfree(alt_max_pkt_size);
295 return rc;
296 }
297
298 dev = kzalloc_obj(struct stk1160);
299 if (dev == NULL) {
300 kfree(alt_max_pkt_size);
301 return -ENOMEM;
302 }
303
304 dev->alt_max_pkt_size = alt_max_pkt_size;
305 dev->udev = udev;
306 dev->num_alt = interface->num_altsetting;
307 dev->ctl_input = input;
308
309 /* We save struct device for debug purposes only */
310 dev->dev = &interface->dev;
311
312 usb_set_intfdata(interface, dev);
313
314 /* initialize videobuf2 stuff */
315 rc = stk1160_vb2_setup(dev);
316 if (rc < 0)
317 goto free_err;
318
319 /*
320 * There is no need to take any locks here in probe
321 * because we register the device node as the *last* thing.
322 */
323 spin_lock_init(&dev->buf_lock);
324 mutex_init(&dev->v4l_lock);
325 mutex_init(&dev->vb_queue_lock);
326
327 rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0);
328 if (rc) {
329 stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc);
330 goto free_err;
331 }
332
333 /*
334 * We obtain a v4l2_dev but defer
335 * registration of video device node as the last thing.
336 * There is no need to set the name if we give a device struct
337 */
338 dev->v4l2_dev.release = stk1160_release;
339 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
340 rc = v4l2_device_register(dev->dev, &dev->v4l2_dev);
341 if (rc) {
342 stk1160_err("v4l2_device_register failed (%d)\n", rc);
343 goto free_ctrl;
344 }
345
346 rc = stk1160_i2c_register(dev);
347 if (rc < 0)
348 goto unreg_v4l2;
349
350 /*
351 * To the best of my knowledge stk1160 boards only have
352 * saa7113, but it doesn't hurt to support them all.
353 */
354 dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
355 "saa7115_auto", 0, saa7113_addrs);
356
357 /* i2c reset saa711x */
358 v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);
359 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
360
361 /* reset stk1160 to default values */
362 stk1160_reg_reset(dev);
363
364 /* select default input */
365 stk1160_select_input(dev);
366
367 stk1160_ac97_setup(dev);
368
369 rc = stk1160_video_register(dev);
370 if (rc < 0)
371 goto unreg_i2c;
372
373 return 0;
374
375 unreg_i2c:
376 stk1160_i2c_unregister(dev);
377 unreg_v4l2:
378 v4l2_device_unregister(&dev->v4l2_dev);
379 free_ctrl:
380 v4l2_ctrl_handler_free(&dev->ctrl_handler);
381 free_err:
382 kfree(alt_max_pkt_size);
383 kfree(dev);
384
385 return rc;
386 }
387
stk1160_disconnect(struct usb_interface * interface)388 static void stk1160_disconnect(struct usb_interface *interface)
389 {
390 struct stk1160 *dev;
391
392 dev = usb_get_intfdata(interface);
393 usb_set_intfdata(interface, NULL);
394
395 /*
396 * Wait until all current v4l2 operation are finished
397 * then deallocate resources
398 */
399 mutex_lock(&dev->vb_queue_lock);
400 mutex_lock(&dev->v4l_lock);
401
402 /* Here is the only place where isoc get released */
403 stk1160_uninit_isoc(dev);
404
405 stk1160_clear_queue(dev, VB2_BUF_STATE_ERROR);
406
407 video_unregister_device(&dev->vdev);
408 v4l2_device_disconnect(&dev->v4l2_dev);
409
410 /* This way current users can detect device is gone */
411 dev->udev = NULL;
412
413 mutex_unlock(&dev->v4l_lock);
414 mutex_unlock(&dev->vb_queue_lock);
415
416 /*
417 * This calls stk1160_release if it's the last reference.
418 * Otherwise, release is postponed until there are no users left.
419 */
420 v4l2_device_put(&dev->v4l2_dev);
421 }
422
423 static struct usb_driver stk1160_usb_driver = {
424 .name = "stk1160",
425 .id_table = stk1160_id_table,
426 .probe = stk1160_probe,
427 .disconnect = stk1160_disconnect,
428 };
429
430 module_usb_driver(stk1160_usb_driver);
431