1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab * STK1160 driver
40c0d06caSMauro Carvalho Chehab *
50c0d06caSMauro Carvalho Chehab * Copyright (C) 2012 Ezequiel Garcia
60c0d06caSMauro Carvalho Chehab * <elezegarcia--a.t--gmail.com>
70c0d06caSMauro Carvalho Chehab *
80c0d06caSMauro Carvalho Chehab * Based on Easycap driver by R.M. Thomas
90c0d06caSMauro Carvalho Chehab * Copyright (C) 2010 R.M. Thomas
100c0d06caSMauro Carvalho Chehab * <rmthomas--a.t--sciolus.org>
110c0d06caSMauro Carvalho Chehab *
120c0d06caSMauro Carvalho Chehab * TODO:
130c0d06caSMauro Carvalho Chehab *
141dc7df4dSMarcel Hasler * 1. Support stream at lower speed: lower frame rate or lower frame size.
150c0d06caSMauro Carvalho Chehab */
160c0d06caSMauro Carvalho Chehab
170c0d06caSMauro Carvalho Chehab #include <linux/module.h>
180c0d06caSMauro Carvalho Chehab #include <linux/init.h>
190c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
200c0d06caSMauro Carvalho Chehab #include <linux/errno.h>
210c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
220c0d06caSMauro Carvalho Chehab
230c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
240c0d06caSMauro Carvalho Chehab #include <linux/mm.h>
250c0d06caSMauro Carvalho Chehab #include <linux/vmalloc.h>
26b5dcee22SMauro Carvalho Chehab #include <media/i2c/saa7115.h>
270c0d06caSMauro Carvalho Chehab
280c0d06caSMauro Carvalho Chehab #include "stk1160.h"
290c0d06caSMauro Carvalho Chehab #include "stk1160-reg.h"
300c0d06caSMauro Carvalho Chehab
310c0d06caSMauro Carvalho Chehab static unsigned int input;
320c0d06caSMauro Carvalho Chehab module_param(input, int, 0644);
330c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(input, "Set default input");
340c0d06caSMauro Carvalho Chehab
350c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
360c0d06caSMauro Carvalho Chehab MODULE_AUTHOR("Ezequiel Garcia");
370c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION("STK1160 driver");
380c0d06caSMauro Carvalho Chehab
390c0d06caSMauro Carvalho Chehab /* Devices supported by this driver */
407fb2e072SArvind Yadav static const struct usb_device_id stk1160_id_table[] = {
410c0d06caSMauro Carvalho Chehab { USB_DEVICE(0x05e1, 0x0408) },
420c0d06caSMauro Carvalho Chehab { }
430c0d06caSMauro Carvalho Chehab };
440c0d06caSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, stk1160_id_table);
450c0d06caSMauro Carvalho Chehab
460c0d06caSMauro Carvalho Chehab /* saa7113 I2C address */
470c0d06caSMauro Carvalho Chehab static unsigned short saa7113_addrs[] = {
480c0d06caSMauro Carvalho Chehab 0x4a >> 1,
490c0d06caSMauro Carvalho Chehab I2C_CLIENT_END
500c0d06caSMauro Carvalho Chehab };
510c0d06caSMauro Carvalho Chehab
520c0d06caSMauro Carvalho Chehab /*
530c0d06caSMauro Carvalho Chehab * Read/Write stk registers
540c0d06caSMauro Carvalho Chehab */
stk1160_read_reg(struct stk1160 * dev,u16 reg,u8 * value)550c0d06caSMauro Carvalho Chehab int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value)
560c0d06caSMauro Carvalho Chehab {
570c0d06caSMauro Carvalho Chehab int ret;
580c0d06caSMauro Carvalho Chehab int pipe = usb_rcvctrlpipe(dev->udev, 0);
5985ac1a17SEzequiel Garcia u8 *buf;
600c0d06caSMauro Carvalho Chehab
610c0d06caSMauro Carvalho Chehab *value = 0;
6285ac1a17SEzequiel Garcia
6385ac1a17SEzequiel Garcia buf = kmalloc(sizeof(u8), GFP_KERNEL);
6485ac1a17SEzequiel Garcia if (!buf)
6585ac1a17SEzequiel Garcia return -ENOMEM;
660c0d06caSMauro Carvalho Chehab ret = usb_control_msg(dev->udev, pipe, 0x00,
670c0d06caSMauro Carvalho Chehab USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
686aa6e70cSJohan Hovold 0x00, reg, buf, sizeof(u8), 1000);
690c0d06caSMauro Carvalho Chehab if (ret < 0) {
700c0d06caSMauro Carvalho Chehab stk1160_err("read failed on reg 0x%x (%d)\n",
710c0d06caSMauro Carvalho Chehab reg, ret);
7285ac1a17SEzequiel Garcia kfree(buf);
730c0d06caSMauro Carvalho Chehab return ret;
740c0d06caSMauro Carvalho Chehab }
750c0d06caSMauro Carvalho Chehab
7685ac1a17SEzequiel Garcia *value = *buf;
7785ac1a17SEzequiel Garcia kfree(buf);
780c0d06caSMauro Carvalho Chehab return 0;
790c0d06caSMauro Carvalho Chehab }
800c0d06caSMauro Carvalho Chehab
stk1160_write_reg(struct stk1160 * dev,u16 reg,u16 value)810c0d06caSMauro Carvalho Chehab int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value)
820c0d06caSMauro Carvalho Chehab {
830c0d06caSMauro Carvalho Chehab int ret;
840c0d06caSMauro Carvalho Chehab int pipe = usb_sndctrlpipe(dev->udev, 0);
850c0d06caSMauro Carvalho Chehab
860c0d06caSMauro Carvalho Chehab ret = usb_control_msg(dev->udev, pipe, 0x01,
870c0d06caSMauro Carvalho Chehab USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
886aa6e70cSJohan Hovold value, reg, NULL, 0, 1000);
890c0d06caSMauro Carvalho Chehab if (ret < 0) {
900c0d06caSMauro Carvalho Chehab stk1160_err("write failed on reg 0x%x (%d)\n",
910c0d06caSMauro Carvalho Chehab reg, ret);
920c0d06caSMauro Carvalho Chehab return ret;
930c0d06caSMauro Carvalho Chehab }
940c0d06caSMauro Carvalho Chehab
950c0d06caSMauro Carvalho Chehab return 0;
960c0d06caSMauro Carvalho Chehab }
970c0d06caSMauro Carvalho Chehab
stk1160_select_input(struct stk1160 * dev)980c0d06caSMauro Carvalho Chehab void stk1160_select_input(struct stk1160 *dev)
990c0d06caSMauro Carvalho Chehab {
10056a960bfSEzequiel Garcia int route;
1010c0d06caSMauro Carvalho Chehab static const u8 gctrl[] = {
10256a960bfSEzequiel Garcia 0x98, 0x90, 0x88, 0x80, 0x98
1030c0d06caSMauro Carvalho Chehab };
1040c0d06caSMauro Carvalho Chehab
10556a960bfSEzequiel Garcia if (dev->ctl_input == STK1160_SVIDEO_INPUT)
10656a960bfSEzequiel Garcia route = SAA7115_SVIDEO3;
10756a960bfSEzequiel Garcia else
10856a960bfSEzequiel Garcia route = SAA7115_COMPOSITE0;
10956a960bfSEzequiel Garcia
11056a960bfSEzequiel Garcia if (dev->ctl_input < ARRAY_SIZE(gctrl)) {
11156a960bfSEzequiel Garcia v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
11256a960bfSEzequiel Garcia route, 0, 0);
1130c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]);
1140c0d06caSMauro Carvalho Chehab }
11556a960bfSEzequiel Garcia }
1160c0d06caSMauro Carvalho Chehab
1170c0d06caSMauro Carvalho Chehab /* TODO: We should break this into pieces */
stk1160_reg_reset(struct stk1160 * dev)1180c0d06caSMauro Carvalho Chehab static void stk1160_reg_reset(struct stk1160 *dev)
1190c0d06caSMauro Carvalho Chehab {
1200c0d06caSMauro Carvalho Chehab int i;
1210c0d06caSMauro Carvalho Chehab
1220c0d06caSMauro Carvalho Chehab static const struct regval ctl[] = {
1230c0d06caSMauro Carvalho Chehab {STK1160_GCTRL+2, 0x0078},
1240c0d06caSMauro Carvalho Chehab
1250c0d06caSMauro Carvalho Chehab {STK1160_RMCTL+1, 0x0000},
1260c0d06caSMauro Carvalho Chehab {STK1160_RMCTL+3, 0x0002},
1270c0d06caSMauro Carvalho Chehab
1280c0d06caSMauro Carvalho Chehab {STK1160_PLLSO, 0x0010},
1290c0d06caSMauro Carvalho Chehab {STK1160_PLLSO+1, 0x0000},
1300c0d06caSMauro Carvalho Chehab {STK1160_PLLSO+2, 0x0014},
1310c0d06caSMauro Carvalho Chehab {STK1160_PLLSO+3, 0x000E},
1320c0d06caSMauro Carvalho Chehab
1330c0d06caSMauro Carvalho Chehab {STK1160_PLLFD, 0x0046},
1340c0d06caSMauro Carvalho Chehab
1350c0d06caSMauro Carvalho Chehab /* Timing generator setup */
1360c0d06caSMauro Carvalho Chehab {STK1160_TIGEN, 0x0012},
1370c0d06caSMauro Carvalho Chehab {STK1160_TICTL, 0x002D},
1380c0d06caSMauro Carvalho Chehab {STK1160_TICTL+1, 0x0001},
1390c0d06caSMauro Carvalho Chehab {STK1160_TICTL+2, 0x0000},
1400c0d06caSMauro Carvalho Chehab {STK1160_TICTL+3, 0x0000},
1410c0d06caSMauro Carvalho Chehab {STK1160_TIGEN, 0x0080},
1420c0d06caSMauro Carvalho Chehab
1430c0d06caSMauro Carvalho Chehab {0xffff, 0xffff}
1440c0d06caSMauro Carvalho Chehab };
1450c0d06caSMauro Carvalho Chehab
1460c0d06caSMauro Carvalho Chehab for (i = 0; ctl[i].reg != 0xffff; i++)
1470c0d06caSMauro Carvalho Chehab stk1160_write_reg(dev, ctl[i].reg, ctl[i].val);
1480c0d06caSMauro Carvalho Chehab }
1490c0d06caSMauro Carvalho Chehab
stk1160_release(struct v4l2_device * v4l2_dev)1500c0d06caSMauro Carvalho Chehab static void stk1160_release(struct v4l2_device *v4l2_dev)
1510c0d06caSMauro Carvalho Chehab {
1520c0d06caSMauro Carvalho Chehab struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev);
1530c0d06caSMauro Carvalho Chehab
154890024adSEzequiel Garcia stk1160_dbg("releasing all resources\n");
1550c0d06caSMauro Carvalho Chehab
1560c0d06caSMauro Carvalho Chehab stk1160_i2c_unregister(dev);
1570c0d06caSMauro Carvalho Chehab
1580c0d06caSMauro Carvalho Chehab v4l2_ctrl_handler_free(&dev->ctrl_handler);
1590c0d06caSMauro Carvalho Chehab v4l2_device_unregister(&dev->v4l2_dev);
16031fa0d12SEzequiel Garcia mutex_destroy(&dev->v4l_lock);
16131fa0d12SEzequiel Garcia mutex_destroy(&dev->vb_queue_lock);
1620c0d06caSMauro Carvalho Chehab kfree(dev->alt_max_pkt_size);
1630c0d06caSMauro Carvalho Chehab kfree(dev);
1640c0d06caSMauro Carvalho Chehab }
1650c0d06caSMauro Carvalho Chehab
1660c0d06caSMauro Carvalho Chehab /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
1670c0d06caSMauro Carvalho Chehab #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
1680c0d06caSMauro Carvalho Chehab
1690c0d06caSMauro Carvalho Chehab /*
1700c0d06caSMauro Carvalho Chehab * Scan usb interface and populate max_pkt_size array
1710c0d06caSMauro Carvalho Chehab * with information on each alternate setting.
1720c0d06caSMauro Carvalho Chehab * The array should be allocated by the caller.
1730c0d06caSMauro Carvalho Chehab */
stk1160_scan_usb(struct usb_interface * intf,struct usb_device * udev,unsigned int * max_pkt_size)1740c0d06caSMauro Carvalho Chehab static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev,
1750c0d06caSMauro Carvalho Chehab unsigned int *max_pkt_size)
1760c0d06caSMauro Carvalho Chehab {
1770c0d06caSMauro Carvalho Chehab int i, e, sizedescr, size, ifnum;
1780c0d06caSMauro Carvalho Chehab const struct usb_endpoint_descriptor *desc;
1790c0d06caSMauro Carvalho Chehab
1800c0d06caSMauro Carvalho Chehab bool has_video = false, has_audio = false;
1810c0d06caSMauro Carvalho Chehab const char *speed;
1820c0d06caSMauro Carvalho Chehab
1830c0d06caSMauro Carvalho Chehab ifnum = intf->altsetting[0].desc.bInterfaceNumber;
1840c0d06caSMauro Carvalho Chehab
1850c0d06caSMauro Carvalho Chehab /* Get endpoints */
1860c0d06caSMauro Carvalho Chehab for (i = 0; i < intf->num_altsetting; i++) {
1870c0d06caSMauro Carvalho Chehab
1880c0d06caSMauro Carvalho Chehab for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) {
1890c0d06caSMauro Carvalho Chehab
1900c0d06caSMauro Carvalho Chehab /* This isn't clear enough, at least to me */
1910c0d06caSMauro Carvalho Chehab desc = &intf->altsetting[i].endpoint[e].desc;
1920c0d06caSMauro Carvalho Chehab sizedescr = le16_to_cpu(desc->wMaxPacketSize);
1930c0d06caSMauro Carvalho Chehab size = sizedescr & 0x7ff;
1940c0d06caSMauro Carvalho Chehab
1950c0d06caSMauro Carvalho Chehab if (udev->speed == USB_SPEED_HIGH)
1960c0d06caSMauro Carvalho Chehab size = size * hb_mult(sizedescr);
1970c0d06caSMauro Carvalho Chehab
1980c0d06caSMauro Carvalho Chehab if (usb_endpoint_xfer_isoc(desc) &&
1990c0d06caSMauro Carvalho Chehab usb_endpoint_dir_in(desc)) {
2000c0d06caSMauro Carvalho Chehab switch (desc->bEndpointAddress) {
2010c0d06caSMauro Carvalho Chehab case STK1160_EP_AUDIO:
2020c0d06caSMauro Carvalho Chehab has_audio = true;
2030c0d06caSMauro Carvalho Chehab break;
2040c0d06caSMauro Carvalho Chehab case STK1160_EP_VIDEO:
2050c0d06caSMauro Carvalho Chehab has_video = true;
2060c0d06caSMauro Carvalho Chehab max_pkt_size[i] = size;
2070c0d06caSMauro Carvalho Chehab break;
2080c0d06caSMauro Carvalho Chehab }
2090c0d06caSMauro Carvalho Chehab }
2100c0d06caSMauro Carvalho Chehab }
2110c0d06caSMauro Carvalho Chehab }
2120c0d06caSMauro Carvalho Chehab
2130c0d06caSMauro Carvalho Chehab /* Is this even possible? */
2140c0d06caSMauro Carvalho Chehab if (!(has_audio || has_video)) {
2150c0d06caSMauro Carvalho Chehab dev_err(&udev->dev, "no audio or video endpoints found\n");
2160c0d06caSMauro Carvalho Chehab return -ENODEV;
2170c0d06caSMauro Carvalho Chehab }
2180c0d06caSMauro Carvalho Chehab
2190c0d06caSMauro Carvalho Chehab switch (udev->speed) {
2200c0d06caSMauro Carvalho Chehab case USB_SPEED_LOW:
2210c0d06caSMauro Carvalho Chehab speed = "1.5";
2220c0d06caSMauro Carvalho Chehab break;
2230c0d06caSMauro Carvalho Chehab case USB_SPEED_FULL:
2240c0d06caSMauro Carvalho Chehab speed = "12";
2250c0d06caSMauro Carvalho Chehab break;
2260c0d06caSMauro Carvalho Chehab case USB_SPEED_HIGH:
2270c0d06caSMauro Carvalho Chehab speed = "480";
2280c0d06caSMauro Carvalho Chehab break;
2290c0d06caSMauro Carvalho Chehab default:
2300c0d06caSMauro Carvalho Chehab speed = "unknown";
2310c0d06caSMauro Carvalho Chehab }
2320c0d06caSMauro Carvalho Chehab
2330c0d06caSMauro Carvalho Chehab dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n",
2340c0d06caSMauro Carvalho Chehab udev->manufacturer ? udev->manufacturer : "",
2350c0d06caSMauro Carvalho Chehab udev->product ? udev->product : "",
2360c0d06caSMauro Carvalho Chehab speed,
2370c0d06caSMauro Carvalho Chehab le16_to_cpu(udev->descriptor.idVendor),
2380c0d06caSMauro Carvalho Chehab le16_to_cpu(udev->descriptor.idProduct),
2390c0d06caSMauro Carvalho Chehab ifnum,
2400c0d06caSMauro Carvalho Chehab intf->altsetting->desc.bInterfaceNumber);
2410c0d06caSMauro Carvalho Chehab
2420c0d06caSMauro Carvalho Chehab /* This should never happen, since we rejected audio interfaces */
2430c0d06caSMauro Carvalho Chehab if (has_audio)
2440c0d06caSMauro Carvalho Chehab dev_warn(&udev->dev, "audio interface %d found.\n\
2450c0d06caSMauro Carvalho Chehab This is not implemented by this driver,\
2460c0d06caSMauro Carvalho Chehab you should use snd-usb-audio instead\n", ifnum);
2470c0d06caSMauro Carvalho Chehab
2480c0d06caSMauro Carvalho Chehab if (has_video)
2490c0d06caSMauro Carvalho Chehab dev_info(&udev->dev, "video interface %d found\n",
2500c0d06caSMauro Carvalho Chehab ifnum);
2510c0d06caSMauro Carvalho Chehab
2520c0d06caSMauro Carvalho Chehab /*
2530c0d06caSMauro Carvalho Chehab * Make sure we have 480 Mbps of bandwidth, otherwise things like
2540c0d06caSMauro Carvalho Chehab * video stream wouldn't likely work, since 12 Mbps is generally
2550c0d06caSMauro Carvalho Chehab * not enough even for most streams.
2560c0d06caSMauro Carvalho Chehab */
2570c0d06caSMauro Carvalho Chehab if (udev->speed != USB_SPEED_HIGH)
2580c0d06caSMauro Carvalho Chehab dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\
2590c0d06caSMauro Carvalho Chehab You may not be able to stream video smoothly\n");
2600c0d06caSMauro Carvalho Chehab
2610c0d06caSMauro Carvalho Chehab return 0;
2620c0d06caSMauro Carvalho Chehab }
2630c0d06caSMauro Carvalho Chehab
stk1160_probe(struct usb_interface * interface,const struct usb_device_id * id)2640c0d06caSMauro Carvalho Chehab static int stk1160_probe(struct usb_interface *interface,
2650c0d06caSMauro Carvalho Chehab const struct usb_device_id *id)
2660c0d06caSMauro Carvalho Chehab {
2670c0d06caSMauro Carvalho Chehab int rc = 0;
2680c0d06caSMauro Carvalho Chehab
2690c0d06caSMauro Carvalho Chehab unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
2700c0d06caSMauro Carvalho Chehab struct usb_device *udev;
2710c0d06caSMauro Carvalho Chehab struct stk1160 *dev;
2720c0d06caSMauro Carvalho Chehab
2730c0d06caSMauro Carvalho Chehab udev = interface_to_usbdev(interface);
2740c0d06caSMauro Carvalho Chehab
2750c0d06caSMauro Carvalho Chehab /*
2760c0d06caSMauro Carvalho Chehab * Since usb audio class is supported by snd-usb-audio,
2770c0d06caSMauro Carvalho Chehab * we reject audio interface.
2780c0d06caSMauro Carvalho Chehab */
2790c0d06caSMauro Carvalho Chehab if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
2800c0d06caSMauro Carvalho Chehab return -ENODEV;
2810c0d06caSMauro Carvalho Chehab
2820c0d06caSMauro Carvalho Chehab /* Alloc an array for all possible max_pkt_size */
2836da2ec56SKees Cook alt_max_pkt_size = kmalloc_array(interface->num_altsetting,
2846da2ec56SKees Cook sizeof(alt_max_pkt_size[0]),
2856da2ec56SKees Cook GFP_KERNEL);
2860c0d06caSMauro Carvalho Chehab if (alt_max_pkt_size == NULL)
2870c0d06caSMauro Carvalho Chehab return -ENOMEM;
2880c0d06caSMauro Carvalho Chehab
2890c0d06caSMauro Carvalho Chehab /*
2903e4d8f48SMauro Carvalho Chehab * Scan usb possibilities and populate alt_max_pkt_size array.
2910c0d06caSMauro Carvalho Chehab * Also, check if device speed is fast enough.
2920c0d06caSMauro Carvalho Chehab */
2930c0d06caSMauro Carvalho Chehab rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size);
2940c0d06caSMauro Carvalho Chehab if (rc < 0) {
2950c0d06caSMauro Carvalho Chehab kfree(alt_max_pkt_size);
2960c0d06caSMauro Carvalho Chehab return rc;
2970c0d06caSMauro Carvalho Chehab }
2980c0d06caSMauro Carvalho Chehab
2990c0d06caSMauro Carvalho Chehab dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL);
3000c0d06caSMauro Carvalho Chehab if (dev == NULL) {
3010c0d06caSMauro Carvalho Chehab kfree(alt_max_pkt_size);
3020c0d06caSMauro Carvalho Chehab return -ENOMEM;
3030c0d06caSMauro Carvalho Chehab }
3040c0d06caSMauro Carvalho Chehab
3050c0d06caSMauro Carvalho Chehab dev->alt_max_pkt_size = alt_max_pkt_size;
3060c0d06caSMauro Carvalho Chehab dev->udev = udev;
3070c0d06caSMauro Carvalho Chehab dev->num_alt = interface->num_altsetting;
3080c0d06caSMauro Carvalho Chehab dev->ctl_input = input;
3090c0d06caSMauro Carvalho Chehab
3100c0d06caSMauro Carvalho Chehab /* We save struct device for debug purposes only */
3110c0d06caSMauro Carvalho Chehab dev->dev = &interface->dev;
3120c0d06caSMauro Carvalho Chehab
3130c0d06caSMauro Carvalho Chehab usb_set_intfdata(interface, dev);
3140c0d06caSMauro Carvalho Chehab
3150c0d06caSMauro Carvalho Chehab /* initialize videobuf2 stuff */
3160c0d06caSMauro Carvalho Chehab rc = stk1160_vb2_setup(dev);
3170c0d06caSMauro Carvalho Chehab if (rc < 0)
3180c0d06caSMauro Carvalho Chehab goto free_err;
3190c0d06caSMauro Carvalho Chehab
3200c0d06caSMauro Carvalho Chehab /*
3210c0d06caSMauro Carvalho Chehab * There is no need to take any locks here in probe
3220c0d06caSMauro Carvalho Chehab * because we register the device node as the *last* thing.
3230c0d06caSMauro Carvalho Chehab */
3240c0d06caSMauro Carvalho Chehab spin_lock_init(&dev->buf_lock);
3250c0d06caSMauro Carvalho Chehab mutex_init(&dev->v4l_lock);
3260c0d06caSMauro Carvalho Chehab mutex_init(&dev->vb_queue_lock);
3270c0d06caSMauro Carvalho Chehab
3280c0d06caSMauro Carvalho Chehab rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0);
3290c0d06caSMauro Carvalho Chehab if (rc) {
3300c0d06caSMauro Carvalho Chehab stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc);
3310c0d06caSMauro Carvalho Chehab goto free_err;
3320c0d06caSMauro Carvalho Chehab }
3330c0d06caSMauro Carvalho Chehab
3340c0d06caSMauro Carvalho Chehab /*
3350c0d06caSMauro Carvalho Chehab * We obtain a v4l2_dev but defer
3360c0d06caSMauro Carvalho Chehab * registration of video device node as the last thing.
3370c0d06caSMauro Carvalho Chehab * There is no need to set the name if we give a device struct
3380c0d06caSMauro Carvalho Chehab */
3390c0d06caSMauro Carvalho Chehab dev->v4l2_dev.release = stk1160_release;
3400c0d06caSMauro Carvalho Chehab dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
3410c0d06caSMauro Carvalho Chehab rc = v4l2_device_register(dev->dev, &dev->v4l2_dev);
3420c0d06caSMauro Carvalho Chehab if (rc) {
3430c0d06caSMauro Carvalho Chehab stk1160_err("v4l2_device_register failed (%d)\n", rc);
3440c0d06caSMauro Carvalho Chehab goto free_ctrl;
3450c0d06caSMauro Carvalho Chehab }
3460c0d06caSMauro Carvalho Chehab
3470c0d06caSMauro Carvalho Chehab rc = stk1160_i2c_register(dev);
3480c0d06caSMauro Carvalho Chehab if (rc < 0)
3490c0d06caSMauro Carvalho Chehab goto unreg_v4l2;
3500c0d06caSMauro Carvalho Chehab
3510c0d06caSMauro Carvalho Chehab /*
3520c0d06caSMauro Carvalho Chehab * To the best of my knowledge stk1160 boards only have
3530c0d06caSMauro Carvalho Chehab * saa7113, but it doesn't hurt to support them all.
3540c0d06caSMauro Carvalho Chehab */
3550c0d06caSMauro Carvalho Chehab dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
3560c0d06caSMauro Carvalho Chehab "saa7115_auto", 0, saa7113_addrs);
3570c0d06caSMauro Carvalho Chehab
3580c0d06caSMauro Carvalho Chehab /* i2c reset saa711x */
3590c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);
3600c0d06caSMauro Carvalho Chehab v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
3610c0d06caSMauro Carvalho Chehab
3620c0d06caSMauro Carvalho Chehab /* reset stk1160 to default values */
3630c0d06caSMauro Carvalho Chehab stk1160_reg_reset(dev);
3640c0d06caSMauro Carvalho Chehab
3650c0d06caSMauro Carvalho Chehab /* select default input */
3660c0d06caSMauro Carvalho Chehab stk1160_select_input(dev);
3670c0d06caSMauro Carvalho Chehab
368e36e6b5fSMarcel Hasler stk1160_ac97_setup(dev);
3690c0d06caSMauro Carvalho Chehab
3700c0d06caSMauro Carvalho Chehab rc = stk1160_video_register(dev);
3710c0d06caSMauro Carvalho Chehab if (rc < 0)
3720c0d06caSMauro Carvalho Chehab goto unreg_i2c;
3730c0d06caSMauro Carvalho Chehab
3740c0d06caSMauro Carvalho Chehab return 0;
3750c0d06caSMauro Carvalho Chehab
3760c0d06caSMauro Carvalho Chehab unreg_i2c:
3770c0d06caSMauro Carvalho Chehab stk1160_i2c_unregister(dev);
3780c0d06caSMauro Carvalho Chehab unreg_v4l2:
3790c0d06caSMauro Carvalho Chehab v4l2_device_unregister(&dev->v4l2_dev);
3800c0d06caSMauro Carvalho Chehab free_ctrl:
3810c0d06caSMauro Carvalho Chehab v4l2_ctrl_handler_free(&dev->ctrl_handler);
3820c0d06caSMauro Carvalho Chehab free_err:
3830c0d06caSMauro Carvalho Chehab kfree(alt_max_pkt_size);
3840c0d06caSMauro Carvalho Chehab kfree(dev);
3850c0d06caSMauro Carvalho Chehab
3860c0d06caSMauro Carvalho Chehab return rc;
3870c0d06caSMauro Carvalho Chehab }
3880c0d06caSMauro Carvalho Chehab
stk1160_disconnect(struct usb_interface * interface)3890c0d06caSMauro Carvalho Chehab static void stk1160_disconnect(struct usb_interface *interface)
3900c0d06caSMauro Carvalho Chehab {
3910c0d06caSMauro Carvalho Chehab struct stk1160 *dev;
3920c0d06caSMauro Carvalho Chehab
3930c0d06caSMauro Carvalho Chehab dev = usb_get_intfdata(interface);
3940c0d06caSMauro Carvalho Chehab usb_set_intfdata(interface, NULL);
3950c0d06caSMauro Carvalho Chehab
3960c0d06caSMauro Carvalho Chehab /*
3970c0d06caSMauro Carvalho Chehab * Wait until all current v4l2 operation are finished
3980c0d06caSMauro Carvalho Chehab * then deallocate resources
3990c0d06caSMauro Carvalho Chehab */
4000c0d06caSMauro Carvalho Chehab mutex_lock(&dev->vb_queue_lock);
4010c0d06caSMauro Carvalho Chehab mutex_lock(&dev->v4l_lock);
4020c0d06caSMauro Carvalho Chehab
4030c0d06caSMauro Carvalho Chehab /* Here is the only place where isoc get released */
4040c0d06caSMauro Carvalho Chehab stk1160_uninit_isoc(dev);
4050c0d06caSMauro Carvalho Chehab
406*fbe04b49SDafna Hirschfeld stk1160_clear_queue(dev, VB2_BUF_STATE_ERROR);
4070c0d06caSMauro Carvalho Chehab
4080c0d06caSMauro Carvalho Chehab video_unregister_device(&dev->vdev);
4090c0d06caSMauro Carvalho Chehab v4l2_device_disconnect(&dev->v4l2_dev);
4100c0d06caSMauro Carvalho Chehab
4110c0d06caSMauro Carvalho Chehab /* This way current users can detect device is gone */
4120c0d06caSMauro Carvalho Chehab dev->udev = NULL;
4130c0d06caSMauro Carvalho Chehab
4140c0d06caSMauro Carvalho Chehab mutex_unlock(&dev->v4l_lock);
4150c0d06caSMauro Carvalho Chehab mutex_unlock(&dev->vb_queue_lock);
4160c0d06caSMauro Carvalho Chehab
4170c0d06caSMauro Carvalho Chehab /*
4180c0d06caSMauro Carvalho Chehab * This calls stk1160_release if it's the last reference.
4193e4d8f48SMauro Carvalho Chehab * Otherwise, release is postponed until there are no users left.
4200c0d06caSMauro Carvalho Chehab */
4210c0d06caSMauro Carvalho Chehab v4l2_device_put(&dev->v4l2_dev);
4220c0d06caSMauro Carvalho Chehab }
4230c0d06caSMauro Carvalho Chehab
4240c0d06caSMauro Carvalho Chehab static struct usb_driver stk1160_usb_driver = {
4250c0d06caSMauro Carvalho Chehab .name = "stk1160",
4260c0d06caSMauro Carvalho Chehab .id_table = stk1160_id_table,
4270c0d06caSMauro Carvalho Chehab .probe = stk1160_probe,
4280c0d06caSMauro Carvalho Chehab .disconnect = stk1160_disconnect,
4290c0d06caSMauro Carvalho Chehab };
4300c0d06caSMauro Carvalho Chehab
4310c0d06caSMauro Carvalho Chehab module_usb_driver(stk1160_usb_driver);
432