1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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/i2c/saa7115.h>
38
39#include "stk1160.h"
40#include "stk1160-reg.h"
41
42static unsigned int input;
43module_param(input, int, 0644);
44MODULE_PARM_DESC(input, "Set default input");
45
46MODULE_LICENSE("GPL");
47MODULE_AUTHOR("Ezequiel Garcia");
48MODULE_DESCRIPTION("STK1160 driver");
49
50
51static struct usb_device_id stk1160_id_table[] = {
52 { USB_DEVICE(0x05e1, 0x0408) },
53 { }
54};
55MODULE_DEVICE_TABLE(usb, stk1160_id_table);
56
57
58static unsigned short saa7113_addrs[] = {
59 0x4a >> 1,
60 I2C_CLIENT_END
61};
62
63
64
65
66int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value)
67{
68 int ret;
69 int pipe = usb_rcvctrlpipe(dev->udev, 0);
70 u8 *buf;
71
72 *value = 0;
73
74 buf = kmalloc(sizeof(u8), GFP_KERNEL);
75 if (!buf)
76 return -ENOMEM;
77 ret = usb_control_msg(dev->udev, pipe, 0x00,
78 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
79 0x00, reg, buf, sizeof(u8), HZ);
80 if (ret < 0) {
81 stk1160_err("read failed on reg 0x%x (%d)\n",
82 reg, ret);
83 kfree(buf);
84 return ret;
85 }
86
87 *value = *buf;
88 kfree(buf);
89 return 0;
90}
91
92int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value)
93{
94 int ret;
95 int pipe = usb_sndctrlpipe(dev->udev, 0);
96
97 ret = usb_control_msg(dev->udev, pipe, 0x01,
98 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99 value, reg, NULL, 0, HZ);
100 if (ret < 0) {
101 stk1160_err("write failed on reg 0x%x (%d)\n",
102 reg, ret);
103 return ret;
104 }
105
106 return 0;
107}
108
109void stk1160_select_input(struct stk1160 *dev)
110{
111 int route;
112 static const u8 gctrl[] = {
113 0x98, 0x90, 0x88, 0x80, 0x98
114 };
115
116 if (dev->ctl_input == STK1160_SVIDEO_INPUT)
117 route = SAA7115_SVIDEO3;
118 else
119 route = SAA7115_COMPOSITE0;
120
121 if (dev->ctl_input < ARRAY_SIZE(gctrl)) {
122 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
123 route, 0, 0);
124 stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]);
125 }
126}
127
128
129static void stk1160_reg_reset(struct stk1160 *dev)
130{
131 int i;
132
133 static const struct regval ctl[] = {
134 {STK1160_GCTRL+2, 0x0078},
135
136 {STK1160_RMCTL+1, 0x0000},
137 {STK1160_RMCTL+3, 0x0002},
138
139 {STK1160_PLLSO, 0x0010},
140 {STK1160_PLLSO+1, 0x0000},
141 {STK1160_PLLSO+2, 0x0014},
142 {STK1160_PLLSO+3, 0x000E},
143
144 {STK1160_PLLFD, 0x0046},
145
146
147 {STK1160_TIGEN, 0x0012},
148 {STK1160_TICTL, 0x002D},
149 {STK1160_TICTL+1, 0x0001},
150 {STK1160_TICTL+2, 0x0000},
151 {STK1160_TICTL+3, 0x0000},
152 {STK1160_TIGEN, 0x0080},
153
154 {0xffff, 0xffff}
155 };
156
157 for (i = 0; ctl[i].reg != 0xffff; i++)
158 stk1160_write_reg(dev, ctl[i].reg, ctl[i].val);
159}
160
161static void stk1160_release(struct v4l2_device *v4l2_dev)
162{
163 struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev);
164
165 stk1160_dbg("releasing all resources\n");
166
167 stk1160_i2c_unregister(dev);
168
169 v4l2_ctrl_handler_free(&dev->ctrl_handler);
170 v4l2_device_unregister(&dev->v4l2_dev);
171 kfree(dev->alt_max_pkt_size);
172 kfree(dev);
173}
174
175
176#define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
177
178
179
180
181
182
183static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev,
184 unsigned int *max_pkt_size)
185{
186 int i, e, sizedescr, size, ifnum;
187 const struct usb_endpoint_descriptor *desc;
188
189 bool has_video = false, has_audio = false;
190 const char *speed;
191
192 ifnum = intf->altsetting[0].desc.bInterfaceNumber;
193
194
195 for (i = 0; i < intf->num_altsetting; i++) {
196
197 for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) {
198
199
200 desc = &intf->altsetting[i].endpoint[e].desc;
201 sizedescr = le16_to_cpu(desc->wMaxPacketSize);
202 size = sizedescr & 0x7ff;
203
204 if (udev->speed == USB_SPEED_HIGH)
205 size = size * hb_mult(sizedescr);
206
207 if (usb_endpoint_xfer_isoc(desc) &&
208 usb_endpoint_dir_in(desc)) {
209 switch (desc->bEndpointAddress) {
210 case STK1160_EP_AUDIO:
211 has_audio = true;
212 break;
213 case STK1160_EP_VIDEO:
214 has_video = true;
215 max_pkt_size[i] = size;
216 break;
217 }
218 }
219 }
220 }
221
222
223 if (!(has_audio || has_video)) {
224 dev_err(&udev->dev, "no audio or video endpoints found\n");
225 return -ENODEV;
226 }
227
228 switch (udev->speed) {
229 case USB_SPEED_LOW:
230 speed = "1.5";
231 break;
232 case USB_SPEED_FULL:
233 speed = "12";
234 break;
235 case USB_SPEED_HIGH:
236 speed = "480";
237 break;
238 default:
239 speed = "unknown";
240 }
241
242 dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n",
243 udev->manufacturer ? udev->manufacturer : "",
244 udev->product ? udev->product : "",
245 speed,
246 le16_to_cpu(udev->descriptor.idVendor),
247 le16_to_cpu(udev->descriptor.idProduct),
248 ifnum,
249 intf->altsetting->desc.bInterfaceNumber);
250
251
252 if (has_audio)
253 dev_warn(&udev->dev, "audio interface %d found.\n\
254 This is not implemented by this driver,\
255 you should use snd-usb-audio instead\n", ifnum);
256
257 if (has_video)
258 dev_info(&udev->dev, "video interface %d found\n",
259 ifnum);
260
261
262
263
264
265
266 if (udev->speed != USB_SPEED_HIGH)
267 dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\
268 You may not be able to stream video smoothly\n");
269
270 return 0;
271}
272
273static int stk1160_probe(struct usb_interface *interface,
274 const struct usb_device_id *id)
275{
276 int rc = 0;
277
278 unsigned int *alt_max_pkt_size;
279 struct usb_device *udev;
280 struct stk1160 *dev;
281
282 udev = interface_to_usbdev(interface);
283
284
285
286
287
288 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
289 return -ENODEV;
290
291
292 alt_max_pkt_size = kmalloc(sizeof(alt_max_pkt_size[0]) *
293 interface->num_altsetting, GFP_KERNEL);
294 if (alt_max_pkt_size == NULL)
295 return -ENOMEM;
296
297
298
299
300
301 rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size);
302 if (rc < 0) {
303 kfree(alt_max_pkt_size);
304 return rc;
305 }
306
307 dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL);
308 if (dev == NULL) {
309 kfree(alt_max_pkt_size);
310 return -ENOMEM;
311 }
312
313 dev->alt_max_pkt_size = alt_max_pkt_size;
314 dev->udev = udev;
315 dev->num_alt = interface->num_altsetting;
316 dev->ctl_input = input;
317
318
319 dev->dev = &interface->dev;
320
321 usb_set_intfdata(interface, dev);
322
323
324 rc = stk1160_vb2_setup(dev);
325 if (rc < 0)
326 goto free_err;
327
328
329
330
331
332 spin_lock_init(&dev->buf_lock);
333 mutex_init(&dev->v4l_lock);
334 mutex_init(&dev->vb_queue_lock);
335
336 rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0);
337 if (rc) {
338 stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc);
339 goto free_err;
340 }
341
342
343
344
345
346
347 dev->v4l2_dev.release = stk1160_release;
348 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
349 rc = v4l2_device_register(dev->dev, &dev->v4l2_dev);
350 if (rc) {
351 stk1160_err("v4l2_device_register failed (%d)\n", rc);
352 goto free_ctrl;
353 }
354
355 rc = stk1160_i2c_register(dev);
356 if (rc < 0)
357 goto unreg_v4l2;
358
359
360
361
362
363 dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
364 "saa7115_auto", 0, saa7113_addrs);
365
366
367 v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0);
368 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
369
370
371 stk1160_reg_reset(dev);
372
373
374 stk1160_select_input(dev);
375
376 stk1160_ac97_register(dev);
377
378 rc = stk1160_video_register(dev);
379 if (rc < 0)
380 goto unreg_i2c;
381
382 return 0;
383
384unreg_i2c:
385 stk1160_i2c_unregister(dev);
386unreg_v4l2:
387 v4l2_device_unregister(&dev->v4l2_dev);
388free_ctrl:
389 v4l2_ctrl_handler_free(&dev->ctrl_handler);
390free_err:
391 kfree(alt_max_pkt_size);
392 kfree(dev);
393
394 return rc;
395}
396
397static void stk1160_disconnect(struct usb_interface *interface)
398{
399 struct stk1160 *dev;
400
401 dev = usb_get_intfdata(interface);
402 usb_set_intfdata(interface, NULL);
403
404
405
406
407
408 mutex_lock(&dev->vb_queue_lock);
409 mutex_lock(&dev->v4l_lock);
410
411
412 stk1160_uninit_isoc(dev);
413
414
415 stk1160_ac97_unregister(dev);
416
417 stk1160_clear_queue(dev);
418
419 video_unregister_device(&dev->vdev);
420 v4l2_device_disconnect(&dev->v4l2_dev);
421
422
423 dev->udev = NULL;
424
425 mutex_unlock(&dev->v4l_lock);
426 mutex_unlock(&dev->vb_queue_lock);
427
428
429
430
431
432 v4l2_device_put(&dev->v4l2_dev);
433}
434
435static struct usb_driver stk1160_usb_driver = {
436 .name = "stk1160",
437 .id_table = stk1160_id_table,
438 .probe = stk1160_probe,
439 .disconnect = stk1160_disconnect,
440};
441
442module_usb_driver(stk1160_usb_driver);
443