diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/media/video/Kconfig | 2 | ||||
-rw-r--r-- | drivers/media/video/Makefile | 1 | ||||
-rw-r--r-- | drivers/media/video/stk1160/Kconfig | 20 | ||||
-rw-r--r-- | drivers/media/video/stk1160/Makefile | 11 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160-ac97.c | 153 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160-core.c | 432 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160-i2c.c | 294 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160-reg.h | 93 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160-v4l.c | 738 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160-video.c | 518 | ||||
-rw-r--r-- | drivers/media/video/stk1160/stk1160.h | 208 |
11 files changed, 2470 insertions, 0 deletions
diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig index d5df1fdebf2..966954de599 100644 --- a/drivers/media/video/Kconfig +++ b/drivers/media/video/Kconfig | |||
@@ -748,6 +748,8 @@ source "drivers/media/video/tm6000/Kconfig" | |||
748 | 748 | ||
749 | source "drivers/media/video/usbvision/Kconfig" | 749 | source "drivers/media/video/usbvision/Kconfig" |
750 | 750 | ||
751 | source "drivers/media/video/stk1160/Kconfig" | ||
752 | |||
751 | endif # V4L_USB_DRIVERS | 753 | endif # V4L_USB_DRIVERS |
752 | 754 | ||
753 | # | 755 | # |
diff --git a/drivers/media/video/Makefile b/drivers/media/video/Makefile index b7ada61f08b..12cad120614 100644 --- a/drivers/media/video/Makefile +++ b/drivers/media/video/Makefile | |||
@@ -126,6 +126,7 @@ obj-$(CONFIG_VIDEO_HEXIUM_ORION) += hexium_orion.o | |||
126 | obj-$(CONFIG_VIDEO_HEXIUM_GEMINI) += hexium_gemini.o | 126 | obj-$(CONFIG_VIDEO_HEXIUM_GEMINI) += hexium_gemini.o |
127 | obj-$(CONFIG_STA2X11_VIP) += sta2x11_vip.o | 127 | obj-$(CONFIG_STA2X11_VIP) += sta2x11_vip.o |
128 | obj-$(CONFIG_VIDEO_TIMBERDALE) += timblogiw.o | 128 | obj-$(CONFIG_VIDEO_TIMBERDALE) += timblogiw.o |
129 | obj-$(CONFIG_VIDEO_STK1160) += stk1160/ | ||
129 | 130 | ||
130 | obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o | 131 | obj-$(CONFIG_VIDEOBUF_GEN) += videobuf-core.o |
131 | obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o | 132 | obj-$(CONFIG_VIDEOBUF_DMA_SG) += videobuf-dma-sg.o |
diff --git a/drivers/media/video/stk1160/Kconfig b/drivers/media/video/stk1160/Kconfig new file mode 100644 index 00000000000..1c3a1ec0023 --- /dev/null +++ b/drivers/media/video/stk1160/Kconfig | |||
@@ -0,0 +1,20 @@ | |||
1 | config VIDEO_STK1160 | ||
2 | tristate "STK1160 USB video capture support" | ||
3 | depends on VIDEO_DEV && I2C | ||
4 | select VIDEOBUF2_VMALLOC | ||
5 | select VIDEO_SAA711X | ||
6 | |||
7 | ---help--- | ||
8 | This is a video4linux driver for STK1160 based video capture devices. | ||
9 | |||
10 | To compile this driver as a module, choose M here: the | ||
11 | module will be called stk1160 | ||
12 | |||
13 | config VIDEO_STK1160_AC97 | ||
14 | bool "STK1160 AC97 codec support" | ||
15 | depends on VIDEO_STK1160 && SND | ||
16 | select SND_AC97_CODEC | ||
17 | |||
18 | ---help--- | ||
19 | Enables AC97 codec support for stk1160 driver. | ||
20 | . | ||
diff --git a/drivers/media/video/stk1160/Makefile b/drivers/media/video/stk1160/Makefile new file mode 100644 index 00000000000..8a3c78482e7 --- /dev/null +++ b/drivers/media/video/stk1160/Makefile | |||
@@ -0,0 +1,11 @@ | |||
1 | obj-stk1160-ac97-$(CONFIG_VIDEO_STK1160_AC97) := stk1160-ac97.o | ||
2 | |||
3 | stk1160-y := stk1160-core.o \ | ||
4 | stk1160-v4l.o \ | ||
5 | stk1160-video.o \ | ||
6 | stk1160-i2c.o \ | ||
7 | $(obj-stk1160-ac97-y) | ||
8 | |||
9 | obj-$(CONFIG_VIDEO_STK1160) += stk1160.o | ||
10 | |||
11 | ccflags-y += -Idrivers/media/video | ||
diff --git a/drivers/media/video/stk1160/stk1160-ac97.c b/drivers/media/video/stk1160/stk1160-ac97.c new file mode 100644 index 00000000000..8d325f50c87 --- /dev/null +++ b/drivers/media/video/stk1160/stk1160-ac97.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <linux/module.h> | ||
24 | #include <sound/core.h> | ||
25 | #include <sound/initval.h> | ||
26 | #include <sound/ac97_codec.h> | ||
27 | |||
28 | #include "stk1160.h" | ||
29 | #include "stk1160-reg.h" | ||
30 | |||
31 | static struct snd_ac97 *stk1160_ac97; | ||
32 | |||
33 | static void stk1160_write_ac97(struct snd_ac97 *ac97, u16 reg, u16 value) | ||
34 | { | ||
35 | struct stk1160 *dev = ac97->private_data; | ||
36 | |||
37 | /* Set codec register address */ | ||
38 | stk1160_write_reg(dev, STK1160_AC97_ADDR, reg); | ||
39 | |||
40 | /* Set codec command */ | ||
41 | stk1160_write_reg(dev, STK1160_AC97_CMD, value & 0xff); | ||
42 | stk1160_write_reg(dev, STK1160_AC97_CMD + 1, (value & 0xff00) >> 8); | ||
43 | |||
44 | /* | ||
45 | * Set command write bit to initiate write operation. | ||
46 | * The bit will be cleared when transfer is done. | ||
47 | */ | ||
48 | stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8c); | ||
49 | } | ||
50 | |||
51 | static u16 stk1160_read_ac97(struct snd_ac97 *ac97, u16 reg) | ||
52 | { | ||
53 | struct stk1160 *dev = ac97->private_data; | ||
54 | u8 vall = 0; | ||
55 | u8 valh = 0; | ||
56 | |||
57 | /* Set codec register address */ | ||
58 | stk1160_write_reg(dev, STK1160_AC97_ADDR, reg); | ||
59 | |||
60 | /* | ||
61 | * Set command read bit to initiate read operation. | ||
62 | * The bit will be cleared when transfer is done. | ||
63 | */ | ||
64 | stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x8b); | ||
65 | |||
66 | /* Retrieve register value */ | ||
67 | stk1160_read_reg(dev, STK1160_AC97_CMD, &vall); | ||
68 | stk1160_read_reg(dev, STK1160_AC97_CMD + 1, &valh); | ||
69 | |||
70 | return (valh << 8) | vall; | ||
71 | } | ||
72 | |||
73 | static void stk1160_reset_ac97(struct snd_ac97 *ac97) | ||
74 | { | ||
75 | struct stk1160 *dev = ac97->private_data; | ||
76 | /* Two-step reset AC97 interface and hardware codec */ | ||
77 | stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x94); | ||
78 | stk1160_write_reg(dev, STK1160_AC97CTL_0, 0x88); | ||
79 | |||
80 | /* Set 16-bit audio data and choose L&R channel*/ | ||
81 | stk1160_write_reg(dev, STK1160_AC97CTL_1 + 2, 0x01); | ||
82 | } | ||
83 | |||
84 | static struct snd_ac97_bus_ops stk1160_ac97_ops = { | ||
85 | .read = stk1160_read_ac97, | ||
86 | .write = stk1160_write_ac97, | ||
87 | .reset = stk1160_reset_ac97, | ||
88 | }; | ||
89 | |||
90 | int stk1160_ac97_register(struct stk1160 *dev) | ||
91 | { | ||
92 | struct snd_card *card = NULL; | ||
93 | struct snd_ac97_bus *ac97_bus; | ||
94 | struct snd_ac97_template ac97_template; | ||
95 | int rc; | ||
96 | |||
97 | /* | ||
98 | * Just want a card to access ac96 controls, | ||
99 | * the actual capture interface will be handled by snd-usb-audio | ||
100 | */ | ||
101 | rc = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, | ||
102 | THIS_MODULE, 0, &card); | ||
103 | if (rc < 0) | ||
104 | return rc; | ||
105 | |||
106 | snd_card_set_dev(card, dev->dev); | ||
107 | |||
108 | /* TODO: I'm not sure where should I get these names :-( */ | ||
109 | snprintf(card->shortname, sizeof(card->shortname), | ||
110 | "stk1160-mixer"); | ||
111 | snprintf(card->longname, sizeof(card->longname), | ||
112 | "stk1160 ac97 codec mixer control"); | ||
113 | strncpy(card->driver, dev->dev->driver->name, sizeof(card->driver)); | ||
114 | |||
115 | rc = snd_ac97_bus(card, 0, &stk1160_ac97_ops, NULL, &ac97_bus); | ||
116 | if (rc) | ||
117 | goto err; | ||
118 | |||
119 | /* We must set private_data before calling snd_ac97_mixer */ | ||
120 | memset(&ac97_template, 0, sizeof(ac97_template)); | ||
121 | ac97_template.private_data = dev; | ||
122 | ac97_template.scaps = AC97_SCAP_SKIP_MODEM; | ||
123 | rc = snd_ac97_mixer(ac97_bus, &ac97_template, &stk1160_ac97); | ||
124 | if (rc) | ||
125 | goto err; | ||
126 | |||
127 | dev->snd_card = card; | ||
128 | rc = snd_card_register(card); | ||
129 | if (rc) | ||
130 | goto err; | ||
131 | |||
132 | return 0; | ||
133 | |||
134 | err: | ||
135 | dev->snd_card = NULL; | ||
136 | if (card) | ||
137 | snd_card_free(card); | ||
138 | return rc; | ||
139 | } | ||
140 | |||
141 | int stk1160_ac97_unregister(struct stk1160 *dev) | ||
142 | { | ||
143 | struct snd_card *card = dev->snd_card; | ||
144 | |||
145 | /* | ||
146 | * We need to check usb_device, | ||
147 | * because ac97 release attempts to communicate with codec | ||
148 | */ | ||
149 | if (card && dev->udev) | ||
150 | snd_card_free(card); | ||
151 | |||
152 | return 0; | ||
153 | } | ||
diff --git a/drivers/media/video/stk1160/stk1160-core.c b/drivers/media/video/stk1160/stk1160-core.c new file mode 100644 index 00000000000..74236fd3b7e --- /dev/null +++ b/drivers/media/video/stk1160/stk1160-core.c | |||
@@ -0,0 +1,432 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | * TODO: | ||
22 | * | ||
23 | * 1. (Try to) detect if we must register ac97 mixer | ||
24 | * 2. Support stream at lower speed: lower frame rate or lower frame size. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <linux/module.h> | ||
29 | #include <linux/init.h> | ||
30 | #include <linux/kernel.h> | ||
31 | #include <linux/errno.h> | ||
32 | #include <linux/slab.h> | ||
33 | |||
34 | #include <linux/usb.h> | ||
35 | #include <linux/mm.h> | ||
36 | #include <linux/vmalloc.h> | ||
37 | #include <media/saa7115.h> | ||
38 | |||
39 | #include "stk1160.h" | ||
40 | #include "stk1160-reg.h" | ||
41 | |||
42 | static unsigned int input; | ||
43 | module_param(input, int, 0644); | ||
44 | MODULE_PARM_DESC(input, "Set default input"); | ||
45 | |||
46 | MODULE_LICENSE("GPL"); | ||
47 | MODULE_AUTHOR("Ezequiel Garcia"); | ||
48 | MODULE_DESCRIPTION("STK1160 driver"); | ||
49 | |||
50 | /* Devices supported by this driver */ | ||
51 | static struct usb_device_id stk1160_id_table[] = { | ||
52 | { USB_DEVICE(0x05e1, 0x0408) }, | ||
53 | { } | ||
54 | }; | ||
55 | MODULE_DEVICE_TABLE(usb, stk1160_id_table); | ||
56 | |||
57 | /* saa7113 I2C address */ | ||
58 | static unsigned short saa7113_addrs[] = { | ||
59 | 0x4a >> 1, | ||
60 | I2C_CLIENT_END | ||
61 | }; | ||
62 | |||
63 | /* | ||
64 | * Read/Write stk registers | ||
65 | */ | ||
66 | int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value) | ||
67 | { | ||
68 | int ret; | ||
69 | int pipe = usb_rcvctrlpipe(dev->udev, 0); | ||
70 | |||
71 | *value = 0; | ||
72 | ret = usb_control_msg(dev->udev, pipe, 0x00, | ||
73 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | ||
74 | 0x00, reg, value, sizeof(u8), HZ); | ||
75 | if (ret < 0) { | ||
76 | stk1160_err("read failed on reg 0x%x (%d)\n", | ||
77 | reg, ret); | ||
78 | return ret; | ||
79 | } | ||
80 | |||
81 | return 0; | ||
82 | } | ||
83 | |||
84 | int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value) | ||
85 | { | ||
86 | int ret; | ||
87 | int pipe = usb_sndctrlpipe(dev->udev, 0); | ||
88 | |||
89 | ret = usb_control_msg(dev->udev, pipe, 0x01, | ||
90 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | ||
91 | value, reg, NULL, 0, HZ); | ||
92 | if (ret < 0) { | ||
93 | stk1160_err("write failed on reg 0x%x (%d)\n", | ||
94 | reg, ret); | ||
95 | return ret; | ||
96 | } | ||
97 | |||
98 | return 0; | ||
99 | } | ||
100 | |||
101 | void stk1160_select_input(struct stk1160 *dev) | ||
102 | { | ||
103 | static const u8 gctrl[] = { | ||
104 | 0x98, 0x90, 0x88, 0x80 | ||
105 | }; | ||
106 | |||
107 | if (dev->ctl_input < ARRAY_SIZE(gctrl)) | ||
108 | stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]); | ||
109 | } | ||
110 | |||
111 | /* TODO: We should break this into pieces */ | ||
112 | static void stk1160_reg_reset(struct stk1160 *dev) | ||
113 | { | ||
114 | int i; | ||
115 | |||
116 | static const struct regval ctl[] = { | ||
117 | {STK1160_GCTRL+2, 0x0078}, | ||
118 | |||
119 | {STK1160_RMCTL+1, 0x0000}, | ||
120 | {STK1160_RMCTL+3, 0x0002}, | ||
121 | |||
122 | {STK1160_PLLSO, 0x0010}, | ||
123 | {STK1160_PLLSO+1, 0x0000}, | ||
124 | {STK1160_PLLSO+2, 0x0014}, | ||
125 | {STK1160_PLLSO+3, 0x000E}, | ||
126 | |||
127 | {STK1160_PLLFD, 0x0046}, | ||
128 | |||
129 | /* Timing generator setup */ | ||
130 | {STK1160_TIGEN, 0x0012}, | ||
131 | {STK1160_TICTL, 0x002D}, | ||
132 | {STK1160_TICTL+1, 0x0001}, | ||
133 | {STK1160_TICTL+2, 0x0000}, | ||
134 | {STK1160_TICTL+3, 0x0000}, | ||
135 | {STK1160_TIGEN, 0x0080}, | ||
136 | |||
137 | {0xffff, 0xffff} | ||
138 | }; | ||
139 | |||
140 | for (i = 0; ctl[i].reg != 0xffff; i++) | ||
141 | stk1160_write_reg(dev, ctl[i].reg, ctl[i].val); | ||
142 | } | ||
143 | |||
144 | static void stk1160_release(struct v4l2_device *v4l2_dev) | ||
145 | { | ||
146 | struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev); | ||
147 | |||
148 | stk1160_info("releasing all resources\n"); | ||
149 | |||
150 | stk1160_i2c_unregister(dev); | ||
151 | |||
152 | v4l2_ctrl_handler_free(&dev->ctrl_handler); | ||
153 | v4l2_device_unregister(&dev->v4l2_dev); | ||
154 | kfree(dev->alt_max_pkt_size); | ||
155 | kfree(dev); | ||
156 | } | ||
157 | |||
158 | /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */ | ||
159 | #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03)) | ||
160 | |||
161 | /* | ||
162 | * Scan usb interface and populate max_pkt_size array | ||
163 | * with information on each alternate setting. | ||
164 | * The array should be allocated by the caller. | ||
165 | */ | ||
166 | static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev, | ||
167 | unsigned int *max_pkt_size) | ||
168 | { | ||
169 | int i, e, sizedescr, size, ifnum; | ||
170 | const struct usb_endpoint_descriptor *desc; | ||
171 | |||
172 | bool has_video = false, has_audio = false; | ||
173 | const char *speed; | ||
174 | |||
175 | ifnum = intf->altsetting[0].desc.bInterfaceNumber; | ||
176 | |||
177 | /* Get endpoints */ | ||
178 | for (i = 0; i < intf->num_altsetting; i++) { | ||
179 | |||
180 | for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) { | ||
181 | |||
182 | /* This isn't clear enough, at least to me */ | ||
183 | desc = &intf->altsetting[i].endpoint[e].desc; | ||
184 | sizedescr = le16_to_cpu(desc->wMaxPacketSize); | ||
185 | size = sizedescr & 0x7ff; | ||
186 | |||
187 | if (udev->speed == USB_SPEED_HIGH) | ||
188 | size = size * hb_mult(sizedescr); | ||
189 | |||
190 | if (usb_endpoint_xfer_isoc(desc) && | ||
191 | usb_endpoint_dir_in(desc)) { | ||
192 | switch (desc->bEndpointAddress) { | ||
193 | case STK1160_EP_AUDIO: | ||
194 | has_audio = true; | ||
195 | break; | ||
196 | case STK1160_EP_VIDEO: | ||
197 | has_video = true; | ||
198 | max_pkt_size[i] = size; | ||
199 | break; | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | |||
205 | /* Is this even possible? */ | ||
206 | if (!(has_audio || has_video)) { | ||
207 | dev_err(&udev->dev, "no audio or video endpoints found\n"); | ||
208 | return -ENODEV; | ||
209 | } | ||
210 | |||
211 | switch (udev->speed) { | ||
212 | case USB_SPEED_LOW: | ||
213 | speed = "1.5"; | ||
214 | break; | ||
215 | case USB_SPEED_FULL: | ||
216 | speed = "12"; | ||
217 | break; | ||
218 | case USB_SPEED_HIGH: | ||
219 | speed = "480"; | ||
220 | break; | ||
221 | default: | ||
222 | speed = "unknown"; | ||
223 | } | ||
224 | |||
225 | dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n", | ||
226 | udev->manufacturer ? udev->manufacturer : "", | ||
227 | udev->product ? udev->product : "", | ||
228 | speed, | ||
229 | le16_to_cpu(udev->descriptor.idVendor), | ||
230 | le16_to_cpu(udev->descriptor.idProduct), | ||
231 | ifnum, | ||
232 | intf->altsetting->desc.bInterfaceNumber); | ||
233 | |||
234 | /* This should never happen, since we rejected audio interfaces */ | ||
235 | if (has_audio) | ||
236 | dev_warn(&udev->dev, "audio interface %d found.\n\ | ||
237 | This is not implemented by this driver,\ | ||
238 | you should use snd-usb-audio instead\n", ifnum); | ||
239 | |||
240 | if (has_video) | ||
241 | dev_info(&udev->dev, "video interface %d found\n", | ||
242 | ifnum); | ||
243 | |||
244 | /* | ||
245 | * Make sure we have 480 Mbps of bandwidth, otherwise things like | ||
246 | * video stream wouldn't likely work, since 12 Mbps is generally | ||
247 | * not enough even for most streams. | ||
248 | */ | ||
249 | if (udev->speed != USB_SPEED_HIGH) | ||
250 | dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\ | ||
251 | You may not be able to stream video smoothly\n"); | ||
252 | |||
253 | return 0; | ||
254 | } | ||
255 | |||
256 | static int stk1160_probe(struct usb_interface *interface, | ||
257 | const struct usb_device_id *id) | ||
258 | { | ||
259 | int ifnum; | ||
260 | int rc = 0; | ||
261 | |||
262 | unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */ | ||
263 | struct usb_device *udev; | ||
264 | struct stk1160 *dev; | ||
265 | |||
266 | ifnum = interface->altsetting[0].desc.bInterfaceNumber; | ||
267 | udev = interface_to_usbdev(interface); | ||
268 | |||
269 | /* | ||
270 | * Since usb audio class is supported by snd-usb-audio, | ||
271 | * we reject audio interface. | ||
272 | */ | ||
273 | if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) | ||
274 | return -ENODEV; | ||
275 | |||
276 | /* Alloc an array for all possible max_pkt_size */ | ||
277 | alt_max_pkt_size = kmalloc(sizeof(alt_max_pkt_size[0]) * | ||
278 | interface->num_altsetting, GFP_KERNEL); | ||
279 | if (alt_max_pkt_size == NULL) | ||
280 | return -ENOMEM; | ||
281 | |||
282 | /* | ||
283 | * Scan usb posibilities and populate alt_max_pkt_size array. | ||
284 | * Also, check if device speed is fast enough. | ||
285 | */ | ||
286 | rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size); | ||
287 | if (rc < 0) { | ||
288 | kfree(alt_max_pkt_size); | ||
289 | return rc; | ||
290 | } | ||
291 | |||
292 | dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL); | ||
293 | if (dev == NULL) { | ||
294 | kfree(alt_max_pkt_size); | ||
295 | return -ENOMEM; | ||
296 | } | ||
297 | |||
298 | dev->alt_max_pkt_size = alt_max_pkt_size; | ||
299 | dev->udev = udev; | ||
300 | dev->num_alt = interface->num_altsetting; | ||
301 | dev->ctl_input = input; | ||
302 | |||
303 | /* We save struct device for debug purposes only */ | ||
304 | dev->dev = &interface->dev; | ||
305 | |||
306 | usb_set_intfdata(interface, dev); | ||
307 | |||
308 | /* initialize videobuf2 stuff */ | ||
309 | rc = stk1160_vb2_setup(dev); | ||
310 | if (rc < 0) | ||
311 | goto free_err; | ||
312 | |||
313 | /* | ||
314 | * There is no need to take any locks here in probe | ||
315 | * because we register the device node as the *last* thing. | ||
316 | */ | ||
317 | spin_lock_init(&dev->buf_lock); | ||
318 | mutex_init(&dev->v4l_lock); | ||
319 | mutex_init(&dev->vb_queue_lock); | ||
320 | |||
321 | rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0); | ||
322 | if (rc) { | ||
323 | stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc); | ||
324 | goto free_err; | ||
325 | } | ||
326 | |||
327 | /* | ||
328 | * We obtain a v4l2_dev but defer | ||
329 | * registration of video device node as the last thing. | ||
330 | * There is no need to set the name if we give a device struct | ||
331 | */ | ||
332 | dev->v4l2_dev.release = stk1160_release; | ||
333 | dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler; | ||
334 | rc = v4l2_device_register(dev->dev, &dev->v4l2_dev); | ||
335 | if (rc) { | ||
336 | stk1160_err("v4l2_device_register failed (%d)\n", rc); | ||
337 | goto free_ctrl; | ||
338 | } | ||
339 | |||
340 | rc = stk1160_i2c_register(dev); | ||
341 | if (rc < 0) | ||
342 | goto unreg_v4l2; | ||
343 | |||
344 | /* | ||
345 | * To the best of my knowledge stk1160 boards only have | ||
346 | * saa7113, but it doesn't hurt to support them all. | ||
347 | */ | ||
348 | dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap, | ||
349 | "saa7115_auto", 0, saa7113_addrs); | ||
350 | |||
351 | stk1160_info("driver ver %s successfully loaded\n", | ||
352 | STK1160_VERSION); | ||
353 | |||
354 | /* i2c reset saa711x */ | ||
355 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0); | ||
356 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing, | ||
357 | 0, 0, 0); | ||
358 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); | ||
359 | |||
360 | /* reset stk1160 to default values */ | ||
361 | stk1160_reg_reset(dev); | ||
362 | |||
363 | /* select default input */ | ||
364 | stk1160_select_input(dev); | ||
365 | |||
366 | stk1160_ac97_register(dev); | ||
367 | |||
368 | rc = stk1160_video_register(dev); | ||
369 | if (rc < 0) | ||
370 | goto unreg_i2c; | ||
371 | |||
372 | return 0; | ||
373 | |||
374 | unreg_i2c: | ||
375 | stk1160_i2c_unregister(dev); | ||
376 | unreg_v4l2: | ||
377 | v4l2_device_unregister(&dev->v4l2_dev); | ||
378 | free_ctrl: | ||
379 | v4l2_ctrl_handler_free(&dev->ctrl_handler); | ||
380 | free_err: | ||
381 | kfree(alt_max_pkt_size); | ||
382 | kfree(dev); | ||
383 | |||
384 | return rc; | ||
385 | } | ||
386 | |||
387 | static void stk1160_disconnect(struct usb_interface *interface) | ||
388 | { | ||
389 | struct stk1160 *dev; | ||
390 | |||
391 | dev = usb_get_intfdata(interface); | ||
392 | usb_set_intfdata(interface, NULL); | ||
393 | |||
394 | /* | ||
395 | * Wait until all current v4l2 operation are finished | ||
396 | * then deallocate resources | ||
397 | */ | ||
398 | mutex_lock(&dev->vb_queue_lock); | ||
399 | mutex_lock(&dev->v4l_lock); | ||
400 | |||
401 | /* Here is the only place where isoc get released */ | ||
402 | stk1160_uninit_isoc(dev); | ||
403 | |||
404 | /* ac97 unregister needs to be done before usb_device is cleared */ | ||
405 | stk1160_ac97_unregister(dev); | ||
406 | |||
407 | stk1160_clear_queue(dev); | ||
408 | |||
409 | video_unregister_device(&dev->vdev); | ||
410 | v4l2_device_disconnect(&dev->v4l2_dev); | ||
411 | |||
412 | /* This way current users can detect device is gone */ | ||
413 | dev->udev = NULL; | ||
414 | |||
415 | mutex_unlock(&dev->v4l_lock); | ||
416 | mutex_unlock(&dev->vb_queue_lock); | ||
417 | |||
418 | /* | ||
419 | * This calls stk1160_release if it's the last reference. | ||
420 | * therwise, release is posponed until there are no users left. | ||
421 | */ | ||
422 | v4l2_device_put(&dev->v4l2_dev); | ||
423 | } | ||
424 | |||
425 | static struct usb_driver stk1160_usb_driver = { | ||
426 | .name = "stk1160", | ||
427 | .id_table = stk1160_id_table, | ||
428 | .probe = stk1160_probe, | ||
429 | .disconnect = stk1160_disconnect, | ||
430 | }; | ||
431 | |||
432 | module_usb_driver(stk1160_usb_driver); | ||
diff --git a/drivers/media/video/stk1160/stk1160-i2c.c b/drivers/media/video/stk1160/stk1160-i2c.c new file mode 100644 index 00000000000..176ac937306 --- /dev/null +++ b/drivers/media/video/stk1160/stk1160-i2c.c | |||
@@ -0,0 +1,294 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <linux/module.h> | ||
24 | #include <linux/usb.h> | ||
25 | #include <linux/i2c.h> | ||
26 | |||
27 | #include "stk1160.h" | ||
28 | #include "stk1160-reg.h" | ||
29 | |||
30 | static unsigned int i2c_debug; | ||
31 | module_param(i2c_debug, int, 0644); | ||
32 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); | ||
33 | |||
34 | #define dprintk_i2c(fmt, args...) \ | ||
35 | do { \ | ||
36 | if (i2c_debug) \ | ||
37 | printk(KERN_DEBUG fmt, ##args); \ | ||
38 | } while (0) | ||
39 | |||
40 | static int stk1160_i2c_busy_wait(struct stk1160 *dev, u8 wait_bit_mask) | ||
41 | { | ||
42 | unsigned long end; | ||
43 | u8 flag; | ||
44 | |||
45 | /* Wait until read/write finish bit is set */ | ||
46 | end = jiffies + msecs_to_jiffies(STK1160_I2C_TIMEOUT); | ||
47 | while (time_is_after_jiffies(end)) { | ||
48 | |||
49 | stk1160_read_reg(dev, STK1160_SICTL+1, &flag); | ||
50 | /* read/write done? */ | ||
51 | if (flag & wait_bit_mask) | ||
52 | goto done; | ||
53 | |||
54 | usleep_range(10 * USEC_PER_MSEC, 20 * USEC_PER_MSEC); | ||
55 | } | ||
56 | |||
57 | return -ETIMEDOUT; | ||
58 | |||
59 | done: | ||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | static int stk1160_i2c_write_reg(struct stk1160 *dev, u8 addr, | ||
64 | u8 reg, u8 value) | ||
65 | { | ||
66 | int rc; | ||
67 | |||
68 | /* Set serial device address */ | ||
69 | rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr); | ||
70 | if (rc < 0) | ||
71 | return rc; | ||
72 | |||
73 | /* Set i2c device register sub-address */ | ||
74 | rc = stk1160_write_reg(dev, STK1160_SBUSW_WA, reg); | ||
75 | if (rc < 0) | ||
76 | return rc; | ||
77 | |||
78 | /* Set i2c device register value */ | ||
79 | rc = stk1160_write_reg(dev, STK1160_SBUSW_WD, value); | ||
80 | if (rc < 0) | ||
81 | return rc; | ||
82 | |||
83 | /* Start write now */ | ||
84 | rc = stk1160_write_reg(dev, STK1160_SICTL, 0x01); | ||
85 | if (rc < 0) | ||
86 | return rc; | ||
87 | |||
88 | rc = stk1160_i2c_busy_wait(dev, 0x04); | ||
89 | if (rc < 0) | ||
90 | return rc; | ||
91 | |||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | static int stk1160_i2c_read_reg(struct stk1160 *dev, u8 addr, | ||
96 | u8 reg, u8 *value) | ||
97 | { | ||
98 | int rc; | ||
99 | |||
100 | /* Set serial device address */ | ||
101 | rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr); | ||
102 | if (rc < 0) | ||
103 | return rc; | ||
104 | |||
105 | /* Set i2c device register sub-address */ | ||
106 | rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, reg); | ||
107 | if (rc < 0) | ||
108 | return rc; | ||
109 | |||
110 | /* Start read now */ | ||
111 | rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20); | ||
112 | if (rc < 0) | ||
113 | return rc; | ||
114 | |||
115 | rc = stk1160_i2c_busy_wait(dev, 0x01); | ||
116 | if (rc < 0) | ||
117 | return rc; | ||
118 | |||
119 | stk1160_read_reg(dev, STK1160_SBUSR_RD, value); | ||
120 | if (rc < 0) | ||
121 | return rc; | ||
122 | |||
123 | return 0; | ||
124 | } | ||
125 | |||
126 | /* | ||
127 | * stk1160_i2c_check_for_device() | ||
128 | * check if there is a i2c_device at the supplied address | ||
129 | */ | ||
130 | static int stk1160_i2c_check_for_device(struct stk1160 *dev, | ||
131 | unsigned char addr) | ||
132 | { | ||
133 | int rc; | ||
134 | |||
135 | /* Set serial device address */ | ||
136 | rc = stk1160_write_reg(dev, STK1160_SICTL_SDA, addr); | ||
137 | if (rc < 0) | ||
138 | return rc; | ||
139 | |||
140 | /* Set device sub-address, we'll chip version reg */ | ||
141 | rc = stk1160_write_reg(dev, STK1160_SBUSR_RA, 0x00); | ||
142 | if (rc < 0) | ||
143 | return rc; | ||
144 | |||
145 | /* Start read now */ | ||
146 | rc = stk1160_write_reg(dev, STK1160_SICTL, 0x20); | ||
147 | if (rc < 0) | ||
148 | return rc; | ||
149 | |||
150 | rc = stk1160_i2c_busy_wait(dev, 0x01); | ||
151 | if (rc < 0) | ||
152 | return -ENODEV; | ||
153 | |||
154 | return 0; | ||
155 | } | ||
156 | |||
157 | /* | ||
158 | * stk1160_i2c_xfer() | ||
159 | * the main i2c transfer function | ||
160 | */ | ||
161 | static int stk1160_i2c_xfer(struct i2c_adapter *i2c_adap, | ||
162 | struct i2c_msg msgs[], int num) | ||
163 | { | ||
164 | struct stk1160 *dev = i2c_adap->algo_data; | ||
165 | int addr, rc, i; | ||
166 | |||
167 | for (i = 0; i < num; i++) { | ||
168 | addr = msgs[i].addr << 1; | ||
169 | dprintk_i2c("%s: addr=%x", __func__, addr); | ||
170 | |||
171 | if (!msgs[i].len) { | ||
172 | /* no len: check only for device presence */ | ||
173 | rc = stk1160_i2c_check_for_device(dev, addr); | ||
174 | if (rc < 0) { | ||
175 | dprintk_i2c(" no device\n"); | ||
176 | return rc; | ||
177 | } | ||
178 | |||
179 | } else if (msgs[i].flags & I2C_M_RD) { | ||
180 | /* read request without preceding register selection */ | ||
181 | dprintk_i2c(" subaddr not selected"); | ||
182 | rc = -EOPNOTSUPP; | ||
183 | goto err; | ||
184 | |||
185 | } else if (i + 1 < num && msgs[i].len <= 2 && | ||
186 | (msgs[i + 1].flags & I2C_M_RD) && | ||
187 | msgs[i].addr == msgs[i + 1].addr) { | ||
188 | |||
189 | if (msgs[i].len != 1 || msgs[i + 1].len != 1) { | ||
190 | dprintk_i2c(" len not supported"); | ||
191 | rc = -EOPNOTSUPP; | ||
192 | goto err; | ||
193 | } | ||
194 | |||
195 | dprintk_i2c(" subaddr=%x", msgs[i].buf[0]); | ||
196 | |||
197 | rc = stk1160_i2c_read_reg(dev, addr, msgs[i].buf[0], | ||
198 | msgs[i + 1].buf); | ||
199 | |||
200 | dprintk_i2c(" read=%x", *msgs[i + 1].buf); | ||
201 | |||
202 | /* consumed two msgs, so we skip one of them */ | ||
203 | i++; | ||
204 | |||
205 | } else { | ||
206 | if (msgs[i].len != 2) { | ||
207 | dprintk_i2c(" len not supported"); | ||
208 | rc = -EOPNOTSUPP; | ||
209 | goto err; | ||
210 | } | ||
211 | |||
212 | dprintk_i2c(" subaddr=%x write=%x", | ||
213 | msgs[i].buf[0], msgs[i].buf[1]); | ||
214 | |||
215 | rc = stk1160_i2c_write_reg(dev, addr, msgs[i].buf[0], | ||
216 | msgs[i].buf[1]); | ||
217 | } | ||
218 | |||
219 | if (rc < 0) | ||
220 | goto err; | ||
221 | dprintk_i2c(" OK\n"); | ||
222 | } | ||
223 | |||
224 | return num; | ||
225 | err: | ||
226 | dprintk_i2c(" ERROR: %d\n", rc); | ||
227 | return num; | ||
228 | } | ||
229 | |||
230 | /* | ||
231 | * functionality(), what da heck is this? | ||
232 | */ | ||
233 | static u32 functionality(struct i2c_adapter *adap) | ||
234 | { | ||
235 | return I2C_FUNC_SMBUS_EMUL; | ||
236 | } | ||
237 | |||
238 | static struct i2c_algorithm algo = { | ||
239 | .master_xfer = stk1160_i2c_xfer, | ||
240 | .functionality = functionality, | ||
241 | }; | ||
242 | |||
243 | static struct i2c_adapter adap_template = { | ||
244 | .owner = THIS_MODULE, | ||
245 | .name = "stk1160", | ||
246 | .algo = &algo, | ||
247 | }; | ||
248 | |||
249 | static struct i2c_client client_template = { | ||
250 | .name = "stk1160 internal", | ||
251 | }; | ||
252 | |||
253 | /* | ||
254 | * stk1160_i2c_register() | ||
255 | * register i2c bus | ||
256 | */ | ||
257 | int stk1160_i2c_register(struct stk1160 *dev) | ||
258 | { | ||
259 | int rc; | ||
260 | |||
261 | dev->i2c_adap = adap_template; | ||
262 | dev->i2c_adap.dev.parent = dev->dev; | ||
263 | strcpy(dev->i2c_adap.name, "stk1160"); | ||
264 | dev->i2c_adap.algo_data = dev; | ||
265 | |||
266 | i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev); | ||
267 | |||
268 | rc = i2c_add_adapter(&dev->i2c_adap); | ||
269 | if (rc < 0) { | ||
270 | stk1160_err("cannot add i2c adapter (%d)\n", rc); | ||
271 | return rc; | ||
272 | } | ||
273 | |||
274 | dev->i2c_client = client_template; | ||
275 | dev->i2c_client.adapter = &dev->i2c_adap; | ||
276 | |||
277 | /* Set i2c clock divider device address */ | ||
278 | stk1160_write_reg(dev, STK1160_SICTL_CD, 0x0f); | ||
279 | |||
280 | /* ??? */ | ||
281 | stk1160_write_reg(dev, STK1160_ASIC + 3, 0x00); | ||
282 | |||
283 | return 0; | ||
284 | } | ||
285 | |||
286 | /* | ||
287 | * stk1160_i2c_unregister() | ||
288 | * unregister i2c_bus | ||
289 | */ | ||
290 | int stk1160_i2c_unregister(struct stk1160 *dev) | ||
291 | { | ||
292 | i2c_del_adapter(&dev->i2c_adap); | ||
293 | return 0; | ||
294 | } | ||
diff --git a/drivers/media/video/stk1160/stk1160-reg.h b/drivers/media/video/stk1160/stk1160-reg.h new file mode 100644 index 00000000000..3e49da6e7ed --- /dev/null +++ b/drivers/media/video/stk1160/stk1160-reg.h | |||
@@ -0,0 +1,93 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | /* GPIO Control */ | ||
24 | #define STK1160_GCTRL 0x000 | ||
25 | |||
26 | /* Remote Wakup Control */ | ||
27 | #define STK1160_RMCTL 0x00c | ||
28 | |||
29 | /* | ||
30 | * Decoder Control Register: | ||
31 | * This byte controls capture start/stop | ||
32 | * with bit #7 (0x?? OR 0x80 to activate). | ||
33 | */ | ||
34 | #define STK1160_DCTRL 0x100 | ||
35 | |||
36 | /* Capture Frame Start Position */ | ||
37 | #define STK116_CFSPO 0x110 | ||
38 | #define STK116_CFSPO_STX_L 0x110 | ||
39 | #define STK116_CFSPO_STX_H 0x111 | ||
40 | #define STK116_CFSPO_STY_L 0x112 | ||
41 | #define STK116_CFSPO_STY_H 0x113 | ||
42 | |||
43 | /* Capture Frame End Position */ | ||
44 | #define STK116_CFEPO 0x114 | ||
45 | #define STK116_CFEPO_ENX_L 0x114 | ||
46 | #define STK116_CFEPO_ENX_H 0x115 | ||
47 | #define STK116_CFEPO_ENY_L 0x116 | ||
48 | #define STK116_CFEPO_ENY_H 0x117 | ||
49 | |||
50 | /* Serial Interface Control */ | ||
51 | #define STK1160_SICTL 0x200 | ||
52 | #define STK1160_SICTL_CD 0x202 | ||
53 | #define STK1160_SICTL_SDA 0x203 | ||
54 | |||
55 | /* Serial Bus Write */ | ||
56 | #define STK1160_SBUSW 0x204 | ||
57 | #define STK1160_SBUSW_WA 0x204 | ||
58 | #define STK1160_SBUSW_WD 0x205 | ||
59 | |||
60 | /* Serial Bus Read */ | ||
61 | #define STK1160_SBUSR 0x208 | ||
62 | #define STK1160_SBUSR_RA 0x208 | ||
63 | #define STK1160_SBUSR_RD 0x209 | ||
64 | |||
65 | /* Alternate Serial Inteface Control */ | ||
66 | #define STK1160_ASIC 0x2fc | ||
67 | |||
68 | /* PLL Select Options */ | ||
69 | #define STK1160_PLLSO 0x018 | ||
70 | |||
71 | /* PLL Frequency Divider */ | ||
72 | #define STK1160_PLLFD 0x01c | ||
73 | |||
74 | /* Timing Generator */ | ||
75 | #define STK1160_TIGEN 0x300 | ||
76 | |||
77 | /* Timing Control Parameter */ | ||
78 | #define STK1160_TICTL 0x350 | ||
79 | |||
80 | /* AC97 Audio Control */ | ||
81 | #define STK1160_AC97CTL_0 0x500 | ||
82 | #define STK1160_AC97CTL_1 0x504 | ||
83 | |||
84 | /* Use [0:6] bits of register 0x504 to set codec command address */ | ||
85 | #define STK1160_AC97_ADDR 0x504 | ||
86 | /* Use [16:31] bits of register 0x500 to set codec command data */ | ||
87 | #define STK1160_AC97_CMD 0x502 | ||
88 | |||
89 | /* Audio I2S Interface */ | ||
90 | #define STK1160_I2SCTL 0x50c | ||
91 | |||
92 | /* EEPROM Interface */ | ||
93 | #define STK1160_EEPROM_SZ 0x5f0 | ||
diff --git a/drivers/media/video/stk1160/stk1160-v4l.c b/drivers/media/video/stk1160/stk1160-v4l.c new file mode 100644 index 00000000000..360bdbee427 --- /dev/null +++ b/drivers/media/video/stk1160/stk1160-v4l.c | |||
@@ -0,0 +1,738 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <linux/module.h> | ||
24 | #include <linux/usb.h> | ||
25 | #include <linux/mm.h> | ||
26 | #include <linux/slab.h> | ||
27 | |||
28 | #include <linux/videodev2.h> | ||
29 | #include <media/v4l2-device.h> | ||
30 | #include <media/v4l2-common.h> | ||
31 | #include <media/v4l2-ioctl.h> | ||
32 | #include <media/v4l2-fh.h> | ||
33 | #include <media/v4l2-event.h> | ||
34 | #include <media/v4l2-chip-ident.h> | ||
35 | #include <media/videobuf2-vmalloc.h> | ||
36 | |||
37 | #include <media/saa7115.h> | ||
38 | |||
39 | #include "stk1160.h" | ||
40 | #include "stk1160-reg.h" | ||
41 | |||
42 | static unsigned int vidioc_debug; | ||
43 | module_param(vidioc_debug, int, 0644); | ||
44 | MODULE_PARM_DESC(vidioc_debug, "enable debug messages [vidioc]"); | ||
45 | |||
46 | static bool keep_buffers; | ||
47 | module_param(keep_buffers, bool, 0644); | ||
48 | MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming"); | ||
49 | |||
50 | /* supported video standards */ | ||
51 | static struct stk1160_fmt format[] = { | ||
52 | { | ||
53 | .name = "16 bpp YUY2, 4:2:2, packed", | ||
54 | .fourcc = V4L2_PIX_FMT_UYVY, | ||
55 | .depth = 16, | ||
56 | } | ||
57 | }; | ||
58 | |||
59 | static void stk1160_set_std(struct stk1160 *dev) | ||
60 | { | ||
61 | int i; | ||
62 | |||
63 | static struct regval std525[] = { | ||
64 | |||
65 | /* 720x480 */ | ||
66 | |||
67 | /* Frame start */ | ||
68 | {STK116_CFSPO_STX_L, 0x0000}, | ||
69 | {STK116_CFSPO_STX_H, 0x0000}, | ||
70 | {STK116_CFSPO_STY_L, 0x0003}, | ||
71 | {STK116_CFSPO_STY_H, 0x0000}, | ||
72 | |||
73 | /* Frame end */ | ||
74 | {STK116_CFEPO_ENX_L, 0x05a0}, | ||
75 | {STK116_CFEPO_ENX_H, 0x0005}, | ||
76 | {STK116_CFEPO_ENY_L, 0x00f3}, | ||
77 | {STK116_CFEPO_ENY_H, 0x0000}, | ||
78 | |||
79 | {0xffff, 0xffff} | ||
80 | }; | ||
81 | |||
82 | static struct regval std625[] = { | ||
83 | |||
84 | /* 720x576 */ | ||
85 | |||
86 | /* TODO: Each line of frame has some junk at the end */ | ||
87 | /* Frame start */ | ||
88 | {STK116_CFSPO, 0x0000}, | ||
89 | {STK116_CFSPO+1, 0x0000}, | ||
90 | {STK116_CFSPO+2, 0x0001}, | ||
91 | {STK116_CFSPO+3, 0x0000}, | ||
92 | |||
93 | /* Frame end */ | ||
94 | {STK116_CFEPO, 0x05a0}, | ||
95 | {STK116_CFEPO+1, 0x0005}, | ||
96 | {STK116_CFEPO+2, 0x0121}, | ||
97 | {STK116_CFEPO+3, 0x0001}, | ||
98 | |||
99 | {0xffff, 0xffff} | ||
100 | }; | ||
101 | |||
102 | if (dev->norm & V4L2_STD_525_60) { | ||
103 | stk1160_dbg("registers to NTSC like standard\n"); | ||
104 | for (i = 0; std525[i].reg != 0xffff; i++) | ||
105 | stk1160_write_reg(dev, std525[i].reg, std525[i].val); | ||
106 | } else { | ||
107 | stk1160_dbg("registers to PAL like standard\n"); | ||
108 | for (i = 0; std625[i].reg != 0xffff; i++) | ||
109 | stk1160_write_reg(dev, std625[i].reg, std625[i].val); | ||
110 | } | ||
111 | |||
112 | } | ||
113 | |||
114 | /* | ||
115 | * Set a new alternate setting. | ||
116 | * Returns true is dev->max_pkt_size has changed, false otherwise. | ||
117 | */ | ||
118 | static bool stk1160_set_alternate(struct stk1160 *dev) | ||
119 | { | ||
120 | int i, prev_alt = dev->alt; | ||
121 | unsigned int min_pkt_size; | ||
122 | bool new_pkt_size; | ||
123 | |||
124 | /* | ||
125 | * If we don't set right alternate, | ||
126 | * then we will get a green screen with junk. | ||
127 | */ | ||
128 | min_pkt_size = STK1160_MIN_PKT_SIZE; | ||
129 | |||
130 | for (i = 0; i < dev->num_alt; i++) { | ||
131 | /* stop when the selected alt setting offers enough bandwidth */ | ||
132 | if (dev->alt_max_pkt_size[i] >= min_pkt_size) { | ||
133 | dev->alt = i; | ||
134 | break; | ||
135 | /* | ||
136 | * otherwise make sure that we end up with the maximum bandwidth | ||
137 | * because the min_pkt_size equation might be wrong... | ||
138 | */ | ||
139 | } else if (dev->alt_max_pkt_size[i] > | ||
140 | dev->alt_max_pkt_size[dev->alt]) | ||
141 | dev->alt = i; | ||
142 | } | ||
143 | |||
144 | stk1160_info("setting alternate %d\n", dev->alt); | ||
145 | |||
146 | if (dev->alt != prev_alt) { | ||
147 | stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n", | ||
148 | min_pkt_size, dev->alt); | ||
149 | stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n", | ||
150 | dev->alt, dev->alt_max_pkt_size[dev->alt]); | ||
151 | usb_set_interface(dev->udev, 0, dev->alt); | ||
152 | } | ||
153 | |||
154 | new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt]; | ||
155 | dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt]; | ||
156 | |||
157 | return new_pkt_size; | ||
158 | } | ||
159 | |||
160 | static int stk1160_start_streaming(struct stk1160 *dev) | ||
161 | { | ||
162 | int i, rc; | ||
163 | bool new_pkt_size; | ||
164 | |||
165 | /* Check device presence */ | ||
166 | if (!dev->udev) | ||
167 | return -ENODEV; | ||
168 | |||
169 | if (mutex_lock_interruptible(&dev->v4l_lock)) | ||
170 | return -ERESTARTSYS; | ||
171 | /* | ||
172 | * For some reason it is mandatory to set alternate *first* | ||
173 | * and only *then* initialize isoc urbs. | ||
174 | * Someone please explain me why ;) | ||
175 | */ | ||
176 | new_pkt_size = stk1160_set_alternate(dev); | ||
177 | |||
178 | /* | ||
179 | * We (re)allocate isoc urbs if: | ||
180 | * there is no allocated isoc urbs, OR | ||
181 | * a new dev->max_pkt_size is detected | ||
182 | */ | ||
183 | if (!dev->isoc_ctl.num_bufs || new_pkt_size) { | ||
184 | rc = stk1160_alloc_isoc(dev); | ||
185 | if (rc < 0) | ||
186 | return rc; | ||
187 | } | ||
188 | |||
189 | /* submit urbs and enables IRQ */ | ||
190 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | ||
191 | rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL); | ||
192 | if (rc) { | ||
193 | stk1160_err("cannot submit urb[%d] (%d)\n", i, rc); | ||
194 | stk1160_uninit_isoc(dev); | ||
195 | return rc; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /* Start saa711x */ | ||
200 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1); | ||
201 | |||
202 | /* Start stk1160 */ | ||
203 | stk1160_write_reg(dev, STK1160_DCTRL, 0xb3); | ||
204 | stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); | ||
205 | |||
206 | stk1160_dbg("streaming started\n"); | ||
207 | |||
208 | mutex_unlock(&dev->v4l_lock); | ||
209 | |||
210 | return 0; | ||
211 | } | ||
212 | |||
213 | /* Must be called with v4l_lock hold */ | ||
214 | static void stk1160_stop_hw(struct stk1160 *dev) | ||
215 | { | ||
216 | /* If the device is not physically present, there is nothing to do */ | ||
217 | if (!dev->udev) | ||
218 | return; | ||
219 | |||
220 | /* set alternate 0 */ | ||
221 | dev->alt = 0; | ||
222 | stk1160_info("setting alternate %d\n", dev->alt); | ||
223 | usb_set_interface(dev->udev, 0, 0); | ||
224 | |||
225 | /* Stop stk1160 */ | ||
226 | stk1160_write_reg(dev, STK1160_DCTRL, 0x00); | ||
227 | stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); | ||
228 | |||
229 | /* Stop saa711x */ | ||
230 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); | ||
231 | } | ||
232 | |||
233 | static int stk1160_stop_streaming(struct stk1160 *dev) | ||
234 | { | ||
235 | if (mutex_lock_interruptible(&dev->v4l_lock)) | ||
236 | return -ERESTARTSYS; | ||
237 | |||
238 | stk1160_cancel_isoc(dev); | ||
239 | |||
240 | /* | ||
241 | * It is possible to keep buffers around using a module parameter. | ||
242 | * This is intended to avoid memory fragmentation. | ||
243 | */ | ||
244 | if (!keep_buffers) | ||
245 | stk1160_free_isoc(dev); | ||
246 | |||
247 | stk1160_stop_hw(dev); | ||
248 | |||
249 | stk1160_clear_queue(dev); | ||
250 | |||
251 | stk1160_dbg("streaming stopped\n"); | ||
252 | |||
253 | mutex_unlock(&dev->v4l_lock); | ||
254 | |||
255 | return 0; | ||
256 | } | ||
257 | |||
258 | static struct v4l2_file_operations stk1160_fops = { | ||
259 | .owner = THIS_MODULE, | ||
260 | .open = v4l2_fh_open, | ||
261 | .release = vb2_fop_release, | ||
262 | .read = vb2_fop_read, | ||
263 | .poll = vb2_fop_poll, | ||
264 | .mmap = vb2_fop_mmap, | ||
265 | .unlocked_ioctl = video_ioctl2, | ||
266 | }; | ||
267 | |||
268 | /* | ||
269 | * vidioc ioctls | ||
270 | */ | ||
271 | static int vidioc_querycap(struct file *file, | ||
272 | void *priv, struct v4l2_capability *cap) | ||
273 | { | ||
274 | struct stk1160 *dev = video_drvdata(file); | ||
275 | |||
276 | strcpy(cap->driver, "stk1160"); | ||
277 | strcpy(cap->card, "stk1160"); | ||
278 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); | ||
279 | cap->device_caps = | ||
280 | V4L2_CAP_VIDEO_CAPTURE | | ||
281 | V4L2_CAP_STREAMING | | ||
282 | V4L2_CAP_READWRITE; | ||
283 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | ||
284 | return 0; | ||
285 | } | ||
286 | |||
287 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, | ||
288 | struct v4l2_fmtdesc *f) | ||
289 | { | ||
290 | if (f->index != 0) | ||
291 | return -EINVAL; | ||
292 | |||
293 | strlcpy(f->description, format[f->index].name, sizeof(f->description)); | ||
294 | f->pixelformat = format[f->index].fourcc; | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, | ||
299 | struct v4l2_format *f) | ||
300 | { | ||
301 | struct stk1160 *dev = video_drvdata(file); | ||
302 | |||
303 | f->fmt.pix.width = dev->width; | ||
304 | f->fmt.pix.height = dev->height; | ||
305 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | ||
306 | f->fmt.pix.pixelformat = dev->fmt->fourcc; | ||
307 | f->fmt.pix.bytesperline = dev->width * 2; | ||
308 | f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; | ||
309 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | ||
310 | |||
311 | return 0; | ||
312 | } | ||
313 | |||
314 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, | ||
315 | struct v4l2_format *f) | ||
316 | { | ||
317 | struct stk1160 *dev = video_drvdata(file); | ||
318 | |||
319 | if (f->fmt.pix.pixelformat != format[0].fourcc) { | ||
320 | stk1160_err("fourcc format 0x%08x invalid\n", | ||
321 | f->fmt.pix.pixelformat); | ||
322 | return -EINVAL; | ||
323 | } | ||
324 | |||
325 | /* | ||
326 | * User can't choose size at his own will, | ||
327 | * so we just return him the current size chosen | ||
328 | * at standard selection. | ||
329 | * TODO: Implement frame scaling? | ||
330 | */ | ||
331 | |||
332 | f->fmt.pix.width = dev->width; | ||
333 | f->fmt.pix.height = dev->height; | ||
334 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | ||
335 | f->fmt.pix.bytesperline = dev->width * 2; | ||
336 | f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; | ||
337 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | ||
338 | |||
339 | return 0; | ||
340 | } | ||
341 | |||
342 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, | ||
343 | struct v4l2_format *f) | ||
344 | { | ||
345 | struct stk1160 *dev = video_drvdata(file); | ||
346 | struct vb2_queue *q = &dev->vb_vidq; | ||
347 | int rc; | ||
348 | |||
349 | if (vb2_is_busy(q)) | ||
350 | return -EBUSY; | ||
351 | |||
352 | rc = vidioc_try_fmt_vid_cap(file, priv, f); | ||
353 | if (rc < 0) | ||
354 | return rc; | ||
355 | |||
356 | /* We don't support any format changes */ | ||
357 | |||
358 | return 0; | ||
359 | } | ||
360 | |||
361 | static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm) | ||
362 | { | ||
363 | struct stk1160 *dev = video_drvdata(file); | ||
364 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm); | ||
365 | return 0; | ||
366 | } | ||
367 | |||
368 | static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) | ||
369 | { | ||
370 | struct stk1160 *dev = video_drvdata(file); | ||
371 | |||
372 | *norm = dev->norm; | ||
373 | return 0; | ||
374 | } | ||
375 | |||
376 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) | ||
377 | { | ||
378 | struct stk1160 *dev = video_drvdata(file); | ||
379 | struct vb2_queue *q = &dev->vb_vidq; | ||
380 | |||
381 | if (vb2_is_busy(q)) | ||
382 | return -EBUSY; | ||
383 | |||
384 | /* Check device presence */ | ||
385 | if (!dev->udev) | ||
386 | return -ENODEV; | ||
387 | |||
388 | /* We need to set this now, before we call stk1160_set_std */ | ||
389 | dev->norm = *norm; | ||
390 | |||
391 | /* This is taken from saa7115 video decoder */ | ||
392 | if (dev->norm & V4L2_STD_525_60) { | ||
393 | dev->width = 720; | ||
394 | dev->height = 480; | ||
395 | } else if (dev->norm & V4L2_STD_625_50) { | ||
396 | dev->width = 720; | ||
397 | dev->height = 576; | ||
398 | } else { | ||
399 | stk1160_err("invalid standard\n"); | ||
400 | return -EINVAL; | ||
401 | } | ||
402 | |||
403 | stk1160_set_std(dev); | ||
404 | |||
405 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, | ||
406 | dev->norm); | ||
407 | |||
408 | return 0; | ||
409 | } | ||
410 | |||
411 | |||
412 | static int vidioc_enum_input(struct file *file, void *priv, | ||
413 | struct v4l2_input *i) | ||
414 | { | ||
415 | struct stk1160 *dev = video_drvdata(file); | ||
416 | |||
417 | if (i->index > STK1160_MAX_INPUT) | ||
418 | return -EINVAL; | ||
419 | |||
420 | sprintf(i->name, "Composite%d", i->index); | ||
421 | i->type = V4L2_INPUT_TYPE_CAMERA; | ||
422 | i->std = dev->vdev.tvnorms; | ||
423 | return 0; | ||
424 | } | ||
425 | |||
426 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) | ||
427 | { | ||
428 | struct stk1160 *dev = video_drvdata(file); | ||
429 | *i = dev->ctl_input; | ||
430 | return 0; | ||
431 | } | ||
432 | |||
433 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) | ||
434 | { | ||
435 | struct stk1160 *dev = video_drvdata(file); | ||
436 | |||
437 | if (vb2_is_busy(&dev->vb_vidq)) | ||
438 | return -EBUSY; | ||
439 | |||
440 | if (i > STK1160_MAX_INPUT) | ||
441 | return -EINVAL; | ||
442 | |||
443 | dev->ctl_input = i; | ||
444 | |||
445 | stk1160_select_input(dev); | ||
446 | |||
447 | return 0; | ||
448 | } | ||
449 | |||
450 | static int vidioc_g_chip_ident(struct file *file, void *priv, | ||
451 | struct v4l2_dbg_chip_ident *chip) | ||
452 | { | ||
453 | switch (chip->match.type) { | ||
454 | case V4L2_CHIP_MATCH_HOST: | ||
455 | chip->ident = V4L2_IDENT_NONE; | ||
456 | chip->revision = 0; | ||
457 | return 0; | ||
458 | default: | ||
459 | return -EINVAL; | ||
460 | } | ||
461 | } | ||
462 | |||
463 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
464 | static int vidioc_g_register(struct file *file, void *priv, | ||
465 | struct v4l2_dbg_register *reg) | ||
466 | { | ||
467 | struct stk1160 *dev = video_drvdata(file); | ||
468 | int rc; | ||
469 | u8 val; | ||
470 | |||
471 | switch (reg->match.type) { | ||
472 | case V4L2_CHIP_MATCH_AC97: | ||
473 | /* TODO: Support me please :-( */ | ||
474 | return -EINVAL; | ||
475 | case V4L2_CHIP_MATCH_I2C_DRIVER: | ||
476 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); | ||
477 | return 0; | ||
478 | case V4L2_CHIP_MATCH_I2C_ADDR: | ||
479 | /* TODO: is this correct? */ | ||
480 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); | ||
481 | return 0; | ||
482 | default: | ||
483 | if (!v4l2_chip_match_host(®->match)) | ||
484 | return -EINVAL; | ||
485 | } | ||
486 | |||
487 | /* Match host */ | ||
488 | rc = stk1160_read_reg(dev, reg->reg, &val); | ||
489 | reg->val = val; | ||
490 | reg->size = 1; | ||
491 | |||
492 | return rc; | ||
493 | } | ||
494 | |||
495 | static int vidioc_s_register(struct file *file, void *priv, | ||
496 | struct v4l2_dbg_register *reg) | ||
497 | { | ||
498 | struct stk1160 *dev = video_drvdata(file); | ||
499 | |||
500 | switch (reg->match.type) { | ||
501 | case V4L2_CHIP_MATCH_AC97: | ||
502 | return -EINVAL; | ||
503 | case V4L2_CHIP_MATCH_I2C_DRIVER: | ||
504 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); | ||
505 | return 0; | ||
506 | case V4L2_CHIP_MATCH_I2C_ADDR: | ||
507 | /* TODO: is this correct? */ | ||
508 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); | ||
509 | return 0; | ||
510 | default: | ||
511 | if (!v4l2_chip_match_host(®->match)) | ||
512 | return -EINVAL; | ||
513 | } | ||
514 | |||
515 | /* Match host */ | ||
516 | return stk1160_write_reg(dev, reg->reg, cpu_to_le16(reg->val)); | ||
517 | } | ||
518 | #endif | ||
519 | |||
520 | static const struct v4l2_ioctl_ops stk1160_ioctl_ops = { | ||
521 | .vidioc_querycap = vidioc_querycap, | ||
522 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, | ||
523 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | ||
524 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | ||
525 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | ||
526 | .vidioc_querystd = vidioc_querystd, | ||
527 | .vidioc_g_std = vidioc_g_std, | ||
528 | .vidioc_s_std = vidioc_s_std, | ||
529 | .vidioc_enum_input = vidioc_enum_input, | ||
530 | .vidioc_g_input = vidioc_g_input, | ||
531 | .vidioc_s_input = vidioc_s_input, | ||
532 | |||
533 | /* vb2 takes care of these */ | ||
534 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | ||
535 | .vidioc_querybuf = vb2_ioctl_querybuf, | ||
536 | .vidioc_qbuf = vb2_ioctl_qbuf, | ||
537 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | ||
538 | .vidioc_streamon = vb2_ioctl_streamon, | ||
539 | .vidioc_streamoff = vb2_ioctl_streamoff, | ||
540 | |||
541 | .vidioc_log_status = v4l2_ctrl_log_status, | ||
542 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | ||
543 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | ||
544 | .vidioc_g_chip_ident = vidioc_g_chip_ident, | ||
545 | |||
546 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
547 | .vidioc_g_register = vidioc_g_register, | ||
548 | .vidioc_s_register = vidioc_s_register, | ||
549 | #endif | ||
550 | }; | ||
551 | |||
552 | /********************************************************************/ | ||
553 | |||
554 | /* | ||
555 | * Videobuf2 operations | ||
556 | */ | ||
557 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt, | ||
558 | unsigned int *nbuffers, unsigned int *nplanes, | ||
559 | unsigned int sizes[], void *alloc_ctxs[]) | ||
560 | { | ||
561 | struct stk1160 *dev = vb2_get_drv_priv(vq); | ||
562 | unsigned long size; | ||
563 | |||
564 | size = dev->width * dev->height * 2; | ||
565 | |||
566 | /* | ||
567 | * Here we can change the number of buffers being requested. | ||
568 | * So, we set a minimum and a maximum like this: | ||
569 | */ | ||
570 | *nbuffers = clamp_t(unsigned int, *nbuffers, | ||
571 | STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS); | ||
572 | |||
573 | /* This means a packed colorformat */ | ||
574 | *nplanes = 1; | ||
575 | |||
576 | sizes[0] = size; | ||
577 | |||
578 | stk1160_info("%s: buffer count %d, each %ld bytes\n", | ||
579 | __func__, *nbuffers, size); | ||
580 | |||
581 | return 0; | ||
582 | } | ||
583 | |||
584 | static void buffer_queue(struct vb2_buffer *vb) | ||
585 | { | ||
586 | unsigned long flags; | ||
587 | struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue); | ||
588 | struct stk1160_buffer *buf = | ||
589 | container_of(vb, struct stk1160_buffer, vb); | ||
590 | |||
591 | spin_lock_irqsave(&dev->buf_lock, flags); | ||
592 | if (!dev->udev) { | ||
593 | /* | ||
594 | * If the device is disconnected return the buffer to userspace | ||
595 | * directly. The next QBUF call will fail with -ENODEV. | ||
596 | */ | ||
597 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
598 | } else { | ||
599 | |||
600 | buf->mem = vb2_plane_vaddr(vb, 0); | ||
601 | buf->length = vb2_plane_size(vb, 0); | ||
602 | buf->bytesused = 0; | ||
603 | buf->pos = 0; | ||
604 | |||
605 | /* | ||
606 | * If buffer length is less from expected then we return | ||
607 | * the buffer to userspace directly. | ||
608 | */ | ||
609 | if (buf->length < dev->width * dev->height * 2) | ||
610 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
611 | else | ||
612 | list_add_tail(&buf->list, &dev->avail_bufs); | ||
613 | |||
614 | } | ||
615 | spin_unlock_irqrestore(&dev->buf_lock, flags); | ||
616 | } | ||
617 | |||
618 | static int start_streaming(struct vb2_queue *vq, unsigned int count) | ||
619 | { | ||
620 | struct stk1160 *dev = vb2_get_drv_priv(vq); | ||
621 | return stk1160_start_streaming(dev); | ||
622 | } | ||
623 | |||
624 | /* abort streaming and wait for last buffer */ | ||
625 | static int stop_streaming(struct vb2_queue *vq) | ||
626 | { | ||
627 | struct stk1160 *dev = vb2_get_drv_priv(vq); | ||
628 | return stk1160_stop_streaming(dev); | ||
629 | } | ||
630 | |||
631 | static struct vb2_ops stk1160_video_qops = { | ||
632 | .queue_setup = queue_setup, | ||
633 | .buf_queue = buffer_queue, | ||
634 | .start_streaming = start_streaming, | ||
635 | .stop_streaming = stop_streaming, | ||
636 | .wait_prepare = vb2_ops_wait_prepare, | ||
637 | .wait_finish = vb2_ops_wait_finish, | ||
638 | }; | ||
639 | |||
640 | static struct video_device v4l_template = { | ||
641 | .name = "stk1160", | ||
642 | .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50, | ||
643 | .fops = &stk1160_fops, | ||
644 | .ioctl_ops = &stk1160_ioctl_ops, | ||
645 | .release = video_device_release_empty, | ||
646 | }; | ||
647 | |||
648 | /********************************************************************/ | ||
649 | |||
650 | /* Must be called with both v4l_lock and vb_queue_lock hold */ | ||
651 | void stk1160_clear_queue(struct stk1160 *dev) | ||
652 | { | ||
653 | struct stk1160_buffer *buf; | ||
654 | unsigned long flags; | ||
655 | |||
656 | /* Release all active buffers */ | ||
657 | spin_lock_irqsave(&dev->buf_lock, flags); | ||
658 | while (!list_empty(&dev->avail_bufs)) { | ||
659 | buf = list_first_entry(&dev->avail_bufs, | ||
660 | struct stk1160_buffer, list); | ||
661 | list_del(&buf->list); | ||
662 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
663 | stk1160_info("buffer [%p/%d] aborted\n", | ||
664 | buf, buf->vb.v4l2_buf.index); | ||
665 | } | ||
666 | /* It's important to clear current buffer */ | ||
667 | dev->isoc_ctl.buf = NULL; | ||
668 | spin_unlock_irqrestore(&dev->buf_lock, flags); | ||
669 | } | ||
670 | |||
671 | int stk1160_vb2_setup(struct stk1160 *dev) | ||
672 | { | ||
673 | int rc; | ||
674 | struct vb2_queue *q; | ||
675 | |||
676 | q = &dev->vb_vidq; | ||
677 | memset(q, 0, sizeof(dev->vb_vidq)); | ||
678 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
679 | q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR; | ||
680 | q->drv_priv = dev; | ||
681 | q->buf_struct_size = sizeof(struct stk1160_buffer); | ||
682 | q->ops = &stk1160_video_qops; | ||
683 | q->mem_ops = &vb2_vmalloc_memops; | ||
684 | |||
685 | rc = vb2_queue_init(q); | ||
686 | if (rc < 0) | ||
687 | return rc; | ||
688 | |||
689 | /* initialize video dma queue */ | ||
690 | INIT_LIST_HEAD(&dev->avail_bufs); | ||
691 | |||
692 | return 0; | ||
693 | } | ||
694 | |||
695 | int stk1160_video_register(struct stk1160 *dev) | ||
696 | { | ||
697 | int rc; | ||
698 | |||
699 | /* Initialize video_device with a template structure */ | ||
700 | dev->vdev = v4l_template; | ||
701 | dev->vdev.debug = vidioc_debug; | ||
702 | dev->vdev.queue = &dev->vb_vidq; | ||
703 | |||
704 | /* | ||
705 | * Provide mutexes for v4l2 core and for videobuf2 queue. | ||
706 | * It will be used to protect *only* v4l2 ioctls. | ||
707 | */ | ||
708 | dev->vdev.lock = &dev->v4l_lock; | ||
709 | dev->vdev.queue->lock = &dev->vb_queue_lock; | ||
710 | |||
711 | /* This will be used to set video_device parent */ | ||
712 | dev->vdev.v4l2_dev = &dev->v4l2_dev; | ||
713 | set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags); | ||
714 | |||
715 | /* NTSC is default */ | ||
716 | dev->norm = V4L2_STD_NTSC_M; | ||
717 | dev->width = 720; | ||
718 | dev->height = 480; | ||
719 | |||
720 | /* set default format */ | ||
721 | dev->fmt = &format[0]; | ||
722 | stk1160_set_std(dev); | ||
723 | |||
724 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, | ||
725 | dev->norm); | ||
726 | |||
727 | video_set_drvdata(&dev->vdev, dev); | ||
728 | rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); | ||
729 | if (rc < 0) { | ||
730 | stk1160_err("video_register_device failed (%d)\n", rc); | ||
731 | return rc; | ||
732 | } | ||
733 | |||
734 | v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", | ||
735 | video_device_node_name(&dev->vdev)); | ||
736 | |||
737 | return 0; | ||
738 | } | ||
diff --git a/drivers/media/video/stk1160/stk1160-video.c b/drivers/media/video/stk1160/stk1160-video.c new file mode 100644 index 00000000000..37852698114 --- /dev/null +++ b/drivers/media/video/stk1160/stk1160-video.c | |||
@@ -0,0 +1,518 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <linux/module.h> | ||
24 | #include <linux/usb.h> | ||
25 | #include <linux/slab.h> | ||
26 | #include <linux/ratelimit.h> | ||
27 | |||
28 | #include "stk1160.h" | ||
29 | |||
30 | static unsigned int debug; | ||
31 | module_param(debug, int, 0644); | ||
32 | MODULE_PARM_DESC(debug, "enable debug messages"); | ||
33 | |||
34 | static inline void print_err_status(struct stk1160 *dev, | ||
35 | int packet, int status) | ||
36 | { | ||
37 | char *errmsg = "Unknown"; | ||
38 | |||
39 | switch (status) { | ||
40 | case -ENOENT: | ||
41 | errmsg = "unlinked synchronuously"; | ||
42 | break; | ||
43 | case -ECONNRESET: | ||
44 | errmsg = "unlinked asynchronuously"; | ||
45 | break; | ||
46 | case -ENOSR: | ||
47 | errmsg = "Buffer error (overrun)"; | ||
48 | break; | ||
49 | case -EPIPE: | ||
50 | errmsg = "Stalled (device not responding)"; | ||
51 | break; | ||
52 | case -EOVERFLOW: | ||
53 | errmsg = "Babble (bad cable?)"; | ||
54 | break; | ||
55 | case -EPROTO: | ||
56 | errmsg = "Bit-stuff error (bad cable?)"; | ||
57 | break; | ||
58 | case -EILSEQ: | ||
59 | errmsg = "CRC/Timeout (could be anything)"; | ||
60 | break; | ||
61 | case -ETIME: | ||
62 | errmsg = "Device does not respond"; | ||
63 | break; | ||
64 | } | ||
65 | |||
66 | if (packet < 0) | ||
67 | printk_ratelimited(KERN_WARNING "URB status %d [%s].\n", | ||
68 | status, errmsg); | ||
69 | else | ||
70 | printk_ratelimited(KERN_INFO "URB packet %d, status %d [%s].\n", | ||
71 | packet, status, errmsg); | ||
72 | } | ||
73 | |||
74 | static inline | ||
75 | struct stk1160_buffer *stk1160_next_buffer(struct stk1160 *dev) | ||
76 | { | ||
77 | struct stk1160_buffer *buf = NULL; | ||
78 | unsigned long flags = 0; | ||
79 | |||
80 | /* Current buffer must be NULL when this functions gets called */ | ||
81 | BUG_ON(dev->isoc_ctl.buf); | ||
82 | |||
83 | spin_lock_irqsave(&dev->buf_lock, flags); | ||
84 | if (!list_empty(&dev->avail_bufs)) { | ||
85 | buf = list_first_entry(&dev->avail_bufs, | ||
86 | struct stk1160_buffer, list); | ||
87 | list_del(&buf->list); | ||
88 | } | ||
89 | spin_unlock_irqrestore(&dev->buf_lock, flags); | ||
90 | |||
91 | return buf; | ||
92 | } | ||
93 | |||
94 | static inline | ||
95 | void stk1160_buffer_done(struct stk1160 *dev) | ||
96 | { | ||
97 | struct stk1160_buffer *buf = dev->isoc_ctl.buf; | ||
98 | |||
99 | dev->field_count++; | ||
100 | |||
101 | buf->vb.v4l2_buf.sequence = dev->field_count >> 1; | ||
102 | buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED; | ||
103 | buf->vb.v4l2_buf.bytesused = buf->bytesused; | ||
104 | do_gettimeofday(&buf->vb.v4l2_buf.timestamp); | ||
105 | |||
106 | vb2_set_plane_payload(&buf->vb, 0, buf->bytesused); | ||
107 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE); | ||
108 | |||
109 | dev->isoc_ctl.buf = NULL; | ||
110 | } | ||
111 | |||
112 | static inline | ||
113 | void stk1160_copy_video(struct stk1160 *dev, u8 *src, int len) | ||
114 | { | ||
115 | int linesdone, lineoff, lencopy; | ||
116 | int bytesperline = dev->width * 2; | ||
117 | struct stk1160_buffer *buf = dev->isoc_ctl.buf; | ||
118 | u8 *dst = buf->mem; | ||
119 | int remain; | ||
120 | |||
121 | /* | ||
122 | * TODO: These stk1160_dbg are very spammy! | ||
123 | * We should 1) check why we are getting them | ||
124 | * and 2) add ratelimit. | ||
125 | * | ||
126 | * UPDATE: One of the reasons (the only one?) for getting these | ||
127 | * is incorrect standard (mismatch between expected and configured). | ||
128 | * So perhaps, we could add a counter for errors. When the counter | ||
129 | * reaches some value, we simply stop streaming. | ||
130 | */ | ||
131 | |||
132 | len -= 4; | ||
133 | src += 4; | ||
134 | |||
135 | remain = len; | ||
136 | |||
137 | linesdone = buf->pos / bytesperline; | ||
138 | lineoff = buf->pos % bytesperline; /* offset in current line */ | ||
139 | |||
140 | if (!buf->odd) | ||
141 | dst += bytesperline; | ||
142 | |||
143 | /* Multiply linesdone by two, to take account of the other field */ | ||
144 | dst += linesdone * bytesperline * 2 + lineoff; | ||
145 | |||
146 | /* Copy the remaining of current line */ | ||
147 | if (remain < (bytesperline - lineoff)) | ||
148 | lencopy = remain; | ||
149 | else | ||
150 | lencopy = bytesperline - lineoff; | ||
151 | |||
152 | /* | ||
153 | * Check if we have enough space left in the buffer. | ||
154 | * In that case, we force loop exit after copy. | ||
155 | */ | ||
156 | if (lencopy > buf->bytesused - buf->length) { | ||
157 | lencopy = buf->bytesused - buf->length; | ||
158 | remain = lencopy; | ||
159 | } | ||
160 | |||
161 | /* Check if the copy is done */ | ||
162 | if (lencopy == 0 || remain == 0) | ||
163 | return; | ||
164 | |||
165 | /* Let the bug hunt begin! sanity checks! */ | ||
166 | if (lencopy < 0) { | ||
167 | stk1160_dbg("copy skipped: negative lencopy\n"); | ||
168 | return; | ||
169 | } | ||
170 | |||
171 | if ((unsigned long)dst + lencopy > | ||
172 | (unsigned long)buf->mem + buf->length) { | ||
173 | printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n"); | ||
174 | return; | ||
175 | } | ||
176 | |||
177 | memcpy(dst, src, lencopy); | ||
178 | |||
179 | buf->bytesused += lencopy; | ||
180 | buf->pos += lencopy; | ||
181 | remain -= lencopy; | ||
182 | |||
183 | /* Copy current field line by line, interlacing with the other field */ | ||
184 | while (remain > 0) { | ||
185 | |||
186 | dst += lencopy + bytesperline; | ||
187 | src += lencopy; | ||
188 | |||
189 | /* Copy one line at a time */ | ||
190 | if (remain < bytesperline) | ||
191 | lencopy = remain; | ||
192 | else | ||
193 | lencopy = bytesperline; | ||
194 | |||
195 | /* | ||
196 | * Check if we have enough space left in the buffer. | ||
197 | * In that case, we force loop exit after copy. | ||
198 | */ | ||
199 | if (lencopy > buf->bytesused - buf->length) { | ||
200 | lencopy = buf->bytesused - buf->length; | ||
201 | remain = lencopy; | ||
202 | } | ||
203 | |||
204 | /* Check if the copy is done */ | ||
205 | if (lencopy == 0 || remain == 0) | ||
206 | return; | ||
207 | |||
208 | if (lencopy < 0) { | ||
209 | printk_ratelimited(KERN_WARNING "stk1160: negative lencopy detected\n"); | ||
210 | return; | ||
211 | } | ||
212 | |||
213 | if ((unsigned long)dst + lencopy > | ||
214 | (unsigned long)buf->mem + buf->length) { | ||
215 | printk_ratelimited(KERN_WARNING "stk1160: buffer overflow detected\n"); | ||
216 | return; | ||
217 | } | ||
218 | |||
219 | memcpy(dst, src, lencopy); | ||
220 | remain -= lencopy; | ||
221 | |||
222 | buf->bytesused += lencopy; | ||
223 | buf->pos += lencopy; | ||
224 | } | ||
225 | } | ||
226 | |||
227 | /* | ||
228 | * Controls the isoc copy of each urb packet | ||
229 | */ | ||
230 | static void stk1160_process_isoc(struct stk1160 *dev, struct urb *urb) | ||
231 | { | ||
232 | int i, len, status; | ||
233 | u8 *p; | ||
234 | |||
235 | if (!dev) { | ||
236 | stk1160_warn("%s called with null device\n", __func__); | ||
237 | return; | ||
238 | } | ||
239 | |||
240 | if (urb->status < 0) { | ||
241 | /* Print status and drop current packet (or field?) */ | ||
242 | print_err_status(dev, -1, urb->status); | ||
243 | return; | ||
244 | } | ||
245 | |||
246 | for (i = 0; i < urb->number_of_packets; i++) { | ||
247 | status = urb->iso_frame_desc[i].status; | ||
248 | if (status < 0) { | ||
249 | print_err_status(dev, i, status); | ||
250 | continue; | ||
251 | } | ||
252 | |||
253 | /* Get packet actual length and pointer to data */ | ||
254 | p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | ||
255 | len = urb->iso_frame_desc[i].actual_length; | ||
256 | |||
257 | /* Empty packet */ | ||
258 | if (len <= 4) | ||
259 | continue; | ||
260 | |||
261 | /* | ||
262 | * An 8-byte packet sequence means end of field. | ||
263 | * So if we don't have any packet, we start receiving one now | ||
264 | * and if we do have a packet, then we are done with it. | ||
265 | * | ||
266 | * These end of field packets are always 0xc0 or 0x80, | ||
267 | * but not always 8-byte long so we don't check packet length. | ||
268 | */ | ||
269 | if (p[0] == 0xc0) { | ||
270 | |||
271 | /* | ||
272 | * If first byte is 0xc0 then we received | ||
273 | * second field, and frame has ended. | ||
274 | */ | ||
275 | if (dev->isoc_ctl.buf != NULL) | ||
276 | stk1160_buffer_done(dev); | ||
277 | |||
278 | dev->isoc_ctl.buf = stk1160_next_buffer(dev); | ||
279 | if (dev->isoc_ctl.buf == NULL) | ||
280 | return; | ||
281 | } | ||
282 | |||
283 | /* | ||
284 | * If we don't have a buffer here, then it means we | ||
285 | * haven't found the start mark sequence. | ||
286 | */ | ||
287 | if (dev->isoc_ctl.buf == NULL) | ||
288 | continue; | ||
289 | |||
290 | if (p[0] == 0xc0 || p[0] == 0x80) { | ||
291 | |||
292 | /* We set next packet parity and | ||
293 | * continue to get next one | ||
294 | */ | ||
295 | dev->isoc_ctl.buf->odd = *p & 0x40; | ||
296 | dev->isoc_ctl.buf->pos = 0; | ||
297 | continue; | ||
298 | } | ||
299 | |||
300 | stk1160_copy_video(dev, p, len); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | |||
305 | /* | ||
306 | * IRQ callback, called by URB callback | ||
307 | */ | ||
308 | static void stk1160_isoc_irq(struct urb *urb) | ||
309 | { | ||
310 | int i, rc; | ||
311 | struct stk1160 *dev = urb->context; | ||
312 | |||
313 | switch (urb->status) { | ||
314 | case 0: | ||
315 | break; | ||
316 | case -ECONNRESET: /* kill */ | ||
317 | case -ENOENT: | ||
318 | case -ESHUTDOWN: | ||
319 | /* TODO: check uvc driver: he frees the queue here */ | ||
320 | return; | ||
321 | default: | ||
322 | stk1160_err("urb error! status %d\n", urb->status); | ||
323 | return; | ||
324 | } | ||
325 | |||
326 | stk1160_process_isoc(dev, urb); | ||
327 | |||
328 | /* Reset urb buffers */ | ||
329 | for (i = 0; i < urb->number_of_packets; i++) { | ||
330 | urb->iso_frame_desc[i].status = 0; | ||
331 | urb->iso_frame_desc[i].actual_length = 0; | ||
332 | } | ||
333 | |||
334 | rc = usb_submit_urb(urb, GFP_ATOMIC); | ||
335 | if (rc) | ||
336 | stk1160_err("urb re-submit failed (%d)\n", rc); | ||
337 | } | ||
338 | |||
339 | /* | ||
340 | * Cancel urbs | ||
341 | * This function can't be called in atomic context | ||
342 | */ | ||
343 | void stk1160_cancel_isoc(struct stk1160 *dev) | ||
344 | { | ||
345 | int i; | ||
346 | |||
347 | /* | ||
348 | * This check is not necessary, but we add it | ||
349 | * to avoid a spurious debug message | ||
350 | */ | ||
351 | if (!dev->isoc_ctl.num_bufs) | ||
352 | return; | ||
353 | |||
354 | stk1160_dbg("killing urbs...\n"); | ||
355 | |||
356 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | ||
357 | |||
358 | /* | ||
359 | * To kill urbs we can't be in atomic context. | ||
360 | * We don't care for NULL pointer since | ||
361 | * usb_kill_urb allows it. | ||
362 | */ | ||
363 | usb_kill_urb(dev->isoc_ctl.urb[i]); | ||
364 | } | ||
365 | |||
366 | stk1160_dbg("all urbs killed\n"); | ||
367 | } | ||
368 | |||
369 | /* | ||
370 | * Releases urb and transfer buffers | ||
371 | * Obviusly, associated urb must be killed before releasing it. | ||
372 | */ | ||
373 | void stk1160_free_isoc(struct stk1160 *dev) | ||
374 | { | ||
375 | struct urb *urb; | ||
376 | int i; | ||
377 | |||
378 | stk1160_dbg("freeing urb buffers...\n"); | ||
379 | |||
380 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | ||
381 | |||
382 | urb = dev->isoc_ctl.urb[i]; | ||
383 | if (urb) { | ||
384 | |||
385 | if (dev->isoc_ctl.transfer_buffer[i]) { | ||
386 | #ifndef CONFIG_DMA_NONCOHERENT | ||
387 | usb_free_coherent(dev->udev, | ||
388 | urb->transfer_buffer_length, | ||
389 | dev->isoc_ctl.transfer_buffer[i], | ||
390 | urb->transfer_dma); | ||
391 | #else | ||
392 | kfree(dev->isoc_ctl.transfer_buffer[i]); | ||
393 | #endif | ||
394 | } | ||
395 | usb_free_urb(urb); | ||
396 | dev->isoc_ctl.urb[i] = NULL; | ||
397 | } | ||
398 | dev->isoc_ctl.transfer_buffer[i] = NULL; | ||
399 | } | ||
400 | |||
401 | kfree(dev->isoc_ctl.urb); | ||
402 | kfree(dev->isoc_ctl.transfer_buffer); | ||
403 | |||
404 | dev->isoc_ctl.urb = NULL; | ||
405 | dev->isoc_ctl.transfer_buffer = NULL; | ||
406 | dev->isoc_ctl.num_bufs = 0; | ||
407 | |||
408 | stk1160_dbg("all urb buffers freed\n"); | ||
409 | } | ||
410 | |||
411 | /* | ||
412 | * Helper for cancelling and freeing urbs | ||
413 | * This function can't be called in atomic context | ||
414 | */ | ||
415 | void stk1160_uninit_isoc(struct stk1160 *dev) | ||
416 | { | ||
417 | stk1160_cancel_isoc(dev); | ||
418 | stk1160_free_isoc(dev); | ||
419 | } | ||
420 | |||
421 | /* | ||
422 | * Allocate URBs | ||
423 | */ | ||
424 | int stk1160_alloc_isoc(struct stk1160 *dev) | ||
425 | { | ||
426 | struct urb *urb; | ||
427 | int i, j, k, sb_size, max_packets, num_bufs; | ||
428 | |||
429 | /* | ||
430 | * It may be necessary to release isoc here, | ||
431 | * since isoc are only released on disconnection. | ||
432 | * (see new_pkt_size flag) | ||
433 | */ | ||
434 | if (dev->isoc_ctl.num_bufs) | ||
435 | stk1160_uninit_isoc(dev); | ||
436 | |||
437 | stk1160_dbg("allocating urbs...\n"); | ||
438 | |||
439 | num_bufs = STK1160_NUM_BUFS; | ||
440 | max_packets = STK1160_NUM_PACKETS; | ||
441 | sb_size = max_packets * dev->max_pkt_size; | ||
442 | |||
443 | dev->isoc_ctl.buf = NULL; | ||
444 | dev->isoc_ctl.max_pkt_size = dev->max_pkt_size; | ||
445 | dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL); | ||
446 | if (!dev->isoc_ctl.urb) { | ||
447 | stk1160_err("out of memory for urb array\n"); | ||
448 | return -ENOMEM; | ||
449 | } | ||
450 | |||
451 | dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs, | ||
452 | GFP_KERNEL); | ||
453 | if (!dev->isoc_ctl.transfer_buffer) { | ||
454 | stk1160_err("out of memory for usb transfers\n"); | ||
455 | kfree(dev->isoc_ctl.urb); | ||
456 | return -ENOMEM; | ||
457 | } | ||
458 | |||
459 | /* allocate urbs and transfer buffers */ | ||
460 | for (i = 0; i < num_bufs; i++) { | ||
461 | |||
462 | urb = usb_alloc_urb(max_packets, GFP_KERNEL); | ||
463 | if (!urb) { | ||
464 | stk1160_err("cannot alloc urb[%d]\n", i); | ||
465 | stk1160_uninit_isoc(dev); | ||
466 | return -ENOMEM; | ||
467 | } | ||
468 | dev->isoc_ctl.urb[i] = urb; | ||
469 | |||
470 | #ifndef CONFIG_DMA_NONCOHERENT | ||
471 | dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->udev, | ||
472 | sb_size, GFP_KERNEL, &urb->transfer_dma); | ||
473 | #else | ||
474 | dev->isoc_ctl.transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL); | ||
475 | #endif | ||
476 | if (!dev->isoc_ctl.transfer_buffer[i]) { | ||
477 | stk1160_err("cannot alloc %d bytes for tx buffer\n", | ||
478 | sb_size); | ||
479 | stk1160_uninit_isoc(dev); | ||
480 | return -ENOMEM; | ||
481 | } | ||
482 | memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size); | ||
483 | |||
484 | /* | ||
485 | * FIXME: Where can I get the endpoint? | ||
486 | */ | ||
487 | urb->dev = dev->udev; | ||
488 | urb->pipe = usb_rcvisocpipe(dev->udev, STK1160_EP_VIDEO); | ||
489 | urb->transfer_buffer = dev->isoc_ctl.transfer_buffer[i]; | ||
490 | urb->transfer_buffer_length = sb_size; | ||
491 | urb->complete = stk1160_isoc_irq; | ||
492 | urb->context = dev; | ||
493 | urb->interval = 1; | ||
494 | urb->start_frame = 0; | ||
495 | urb->number_of_packets = max_packets; | ||
496 | #ifndef CONFIG_DMA_NONCOHERENT | ||
497 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | ||
498 | #else | ||
499 | urb->transfer_flags = URB_ISO_ASAP; | ||
500 | #endif | ||
501 | |||
502 | k = 0; | ||
503 | for (j = 0; j < max_packets; j++) { | ||
504 | urb->iso_frame_desc[j].offset = k; | ||
505 | urb->iso_frame_desc[j].length = | ||
506 | dev->isoc_ctl.max_pkt_size; | ||
507 | k += dev->isoc_ctl.max_pkt_size; | ||
508 | } | ||
509 | } | ||
510 | |||
511 | stk1160_dbg("urbs allocated\n"); | ||
512 | |||
513 | /* At last we can say we have some buffers */ | ||
514 | dev->isoc_ctl.num_bufs = num_bufs; | ||
515 | |||
516 | return 0; | ||
517 | } | ||
518 | |||
diff --git a/drivers/media/video/stk1160/stk1160.h b/drivers/media/video/stk1160/stk1160.h new file mode 100644 index 00000000000..3feba0033f9 --- /dev/null +++ b/drivers/media/video/stk1160/stk1160.h | |||
@@ -0,0 +1,208 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <linux/i2c.h> | ||
24 | #include <sound/core.h> | ||
25 | #include <sound/ac97_codec.h> | ||
26 | #include <media/videobuf2-core.h> | ||
27 | #include <media/v4l2-device.h> | ||
28 | #include <media/v4l2-ctrls.h> | ||
29 | |||
30 | #define STK1160_VERSION "0.9.5" | ||
31 | #define STK1160_VERSION_NUM 0x000905 | ||
32 | |||
33 | /* TODO: Decide on number of packets for each buffer */ | ||
34 | #define STK1160_NUM_PACKETS 64 | ||
35 | |||
36 | /* Number of buffers for isoc transfers */ | ||
37 | #define STK1160_NUM_BUFS 16 /* TODO */ | ||
38 | |||
39 | /* TODO: This endpoint address should be retrieved */ | ||
40 | #define STK1160_EP_VIDEO 0x82 | ||
41 | #define STK1160_EP_AUDIO 0x81 | ||
42 | |||
43 | /* Max and min video buffers */ | ||
44 | #define STK1160_MIN_VIDEO_BUFFERS 8 | ||
45 | #define STK1160_MAX_VIDEO_BUFFERS 32 | ||
46 | |||
47 | #define STK1160_MIN_PKT_SIZE 3072 | ||
48 | |||
49 | #define STK1160_MAX_INPUT 3 | ||
50 | |||
51 | #define STK1160_I2C_TIMEOUT 100 | ||
52 | |||
53 | /* TODO: Print helpers | ||
54 | * I could use dev_xxx, pr_xxx, v4l2_xxx or printk. | ||
55 | * However, there isn't a solid consensus on which | ||
56 | * new drivers should use. | ||
57 | * | ||
58 | */ | ||
59 | #define DEBUG | ||
60 | #ifdef DEBUG | ||
61 | #define stk1160_dbg(fmt, args...) \ | ||
62 | printk(KERN_DEBUG "stk1160: " fmt, ## args) | ||
63 | #else | ||
64 | #define stk1160_dbg(fmt, args...) | ||
65 | #endif | ||
66 | |||
67 | #define stk1160_info(fmt, args...) \ | ||
68 | pr_info("stk1160: " fmt, ## args) | ||
69 | |||
70 | #define stk1160_warn(fmt, args...) \ | ||
71 | pr_warn("stk1160: " fmt, ## args) | ||
72 | |||
73 | #define stk1160_err(fmt, args...) \ | ||
74 | pr_err("stk1160: " fmt, ## args) | ||
75 | |||
76 | /* Buffer for one video frame */ | ||
77 | struct stk1160_buffer { | ||
78 | /* common v4l buffer stuff -- must be first */ | ||
79 | struct vb2_buffer vb; | ||
80 | struct list_head list; | ||
81 | |||
82 | void *mem; | ||
83 | unsigned int length; /* buffer length */ | ||
84 | unsigned int bytesused; /* bytes written */ | ||
85 | int odd; /* current oddity */ | ||
86 | |||
87 | /* | ||
88 | * Since we interlace two fields per frame, | ||
89 | * this is different from bytesused. | ||
90 | */ | ||
91 | unsigned int pos; /* current pos inside buffer */ | ||
92 | }; | ||
93 | |||
94 | struct stk1160_isoc_ctl { | ||
95 | /* max packet size of isoc transaction */ | ||
96 | int max_pkt_size; | ||
97 | |||
98 | /* number of allocated urbs */ | ||
99 | int num_bufs; | ||
100 | |||
101 | /* urb for isoc transfers */ | ||
102 | struct urb **urb; | ||
103 | |||
104 | /* transfer buffers for isoc transfer */ | ||
105 | char **transfer_buffer; | ||
106 | |||
107 | /* current buffer */ | ||
108 | struct stk1160_buffer *buf; | ||
109 | }; | ||
110 | |||
111 | struct stk1160_fmt { | ||
112 | char *name; | ||
113 | u32 fourcc; /* v4l2 format id */ | ||
114 | int depth; | ||
115 | }; | ||
116 | |||
117 | struct stk1160 { | ||
118 | struct v4l2_device v4l2_dev; | ||
119 | struct video_device vdev; | ||
120 | struct v4l2_ctrl_handler ctrl_handler; | ||
121 | |||
122 | struct device *dev; | ||
123 | struct usb_device *udev; | ||
124 | |||
125 | /* saa7115 subdev */ | ||
126 | struct v4l2_subdev *sd_saa7115; | ||
127 | |||
128 | /* isoc control struct */ | ||
129 | struct list_head avail_bufs; | ||
130 | |||
131 | /* video capture */ | ||
132 | struct vb2_queue vb_vidq; | ||
133 | |||
134 | /* max packet size of isoc transaction */ | ||
135 | int max_pkt_size; | ||
136 | /* array of wMaxPacketSize */ | ||
137 | unsigned int *alt_max_pkt_size; | ||
138 | /* alternate */ | ||
139 | int alt; | ||
140 | /* Number of alternative settings */ | ||
141 | int num_alt; | ||
142 | |||
143 | struct stk1160_isoc_ctl isoc_ctl; | ||
144 | char urb_buf[255]; /* urb control msg buffer */ | ||
145 | |||
146 | /* frame properties */ | ||
147 | int width; /* current frame width */ | ||
148 | int height; /* current frame height */ | ||
149 | unsigned int ctl_input; /* selected input */ | ||
150 | v4l2_std_id norm; /* current norm */ | ||
151 | struct stk1160_fmt *fmt; /* selected format */ | ||
152 | |||
153 | unsigned int field_count; /* not sure ??? */ | ||
154 | enum v4l2_field field; /* also not sure :/ */ | ||
155 | |||
156 | /* i2c i/o */ | ||
157 | struct i2c_adapter i2c_adap; | ||
158 | struct i2c_client i2c_client; | ||
159 | |||
160 | struct mutex v4l_lock; | ||
161 | struct mutex vb_queue_lock; | ||
162 | spinlock_t buf_lock; | ||
163 | |||
164 | struct file *fh_owner; /* filehandle ownership */ | ||
165 | |||
166 | /* EXPERIMENTAL */ | ||
167 | struct snd_card *snd_card; | ||
168 | }; | ||
169 | |||
170 | struct regval { | ||
171 | u16 reg; | ||
172 | u16 val; | ||
173 | }; | ||
174 | |||
175 | /* Provided by stk1160-v4l.c */ | ||
176 | int stk1160_vb2_setup(struct stk1160 *dev); | ||
177 | int stk1160_video_register(struct stk1160 *dev); | ||
178 | void stk1160_video_unregister(struct stk1160 *dev); | ||
179 | void stk1160_clear_queue(struct stk1160 *dev); | ||
180 | |||
181 | /* Provided by stk1160-video.c */ | ||
182 | int stk1160_alloc_isoc(struct stk1160 *dev); | ||
183 | void stk1160_free_isoc(struct stk1160 *dev); | ||
184 | void stk1160_cancel_isoc(struct stk1160 *dev); | ||
185 | void stk1160_uninit_isoc(struct stk1160 *dev); | ||
186 | |||
187 | /* Provided by stk1160-i2c.c */ | ||
188 | int stk1160_i2c_register(struct stk1160 *dev); | ||
189 | int stk1160_i2c_unregister(struct stk1160 *dev); | ||
190 | |||
191 | /* Provided by stk1160-core.c */ | ||
192 | int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value); | ||
193 | int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value); | ||
194 | int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg, | ||
195 | char *buf, int len); | ||
196 | int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg, | ||
197 | char *buf, int len); | ||
198 | void stk1160_select_input(struct stk1160 *dev); | ||
199 | |||
200 | /* Provided by stk1160-ac97.c */ | ||
201 | #ifdef CONFIG_VIDEO_STK1160_AC97 | ||
202 | int stk1160_ac97_register(struct stk1160 *dev); | ||
203 | int stk1160_ac97_unregister(struct stk1160 *dev); | ||
204 | #else | ||
205 | static inline int stk1160_ac97_register(struct stk1160 *dev) { return 0; } | ||
206 | static inline int stk1160_ac97_unregister(struct stk1160 *dev) { return 0; } | ||
207 | #endif | ||
208 | |||