1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "au0828.h"
23#include "au8522.h"
24
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/videodev2.h>
28#include <media/v4l2-common.h>
29#include <linux/mutex.h>
30
31
32#include <media/tuner.h>
33
34
35
36
37
38
39
40
41int au0828_debug;
42module_param_named(debug, au0828_debug, int, 0644);
43MODULE_PARM_DESC(debug,
44 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
45
46static unsigned int disable_usb_speed_check;
47module_param(disable_usb_speed_check, int, 0444);
48MODULE_PARM_DESC(disable_usb_speed_check,
49 "override min bandwidth requirement of 480M bps");
50
51#define _AU0828_BULKPIPE 0x03
52#define _BULKPIPESIZE 0xffff
53
54static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
55 u16 index);
56static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
57 u16 index, unsigned char *cp, u16 size);
58
59
60#define CMD_REQUEST_IN 0x00
61#define CMD_REQUEST_OUT 0x01
62
63u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
64{
65 u8 result = 0;
66
67 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
68 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
69
70 return result;
71}
72
73u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
74{
75 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
76 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
77}
78
79static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
80 u16 index)
81{
82 int status = -ENODEV;
83
84 if (dev->usbdev) {
85
86
87 status = usb_control_msg(dev->usbdev,
88 usb_sndctrlpipe(dev->usbdev, 0),
89 request,
90 USB_DIR_OUT | USB_TYPE_VENDOR |
91 USB_RECIP_DEVICE,
92 value, index, NULL, 0, 1000);
93
94 status = min(status, 0);
95
96 if (status < 0) {
97 pr_err("%s() Failed sending control message, error %d.\n",
98 __func__, status);
99 }
100
101 }
102
103 return status;
104}
105
106static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
107 u16 index, unsigned char *cp, u16 size)
108{
109 int status = -ENODEV;
110 mutex_lock(&dev->mutex);
111 if (dev->usbdev) {
112 status = usb_control_msg(dev->usbdev,
113 usb_rcvctrlpipe(dev->usbdev, 0),
114 request,
115 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
116 value, index,
117 dev->ctrlmsg, size, 1000);
118
119 status = min(status, 0);
120
121 if (status < 0) {
122 pr_err("%s() Failed receiving control message, error %d.\n",
123 __func__, status);
124 }
125
126
127
128 memcpy(cp, dev->ctrlmsg, size);
129 }
130 mutex_unlock(&dev->mutex);
131 return status;
132}
133
134#ifdef CONFIG_MEDIA_CONTROLLER
135static void au0828_media_graph_notify(struct media_entity *new,
136 void *notify_data);
137#endif
138
139static void au0828_unregister_media_device(struct au0828_dev *dev)
140{
141#ifdef CONFIG_MEDIA_CONTROLLER
142 struct media_device *mdev = dev->media_dev;
143 struct media_entity_notify *notify, *nextp;
144
145 if (!mdev || !media_devnode_is_registered(mdev->devnode))
146 return;
147
148
149 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) {
150 if (notify->notify != au0828_media_graph_notify)
151 continue;
152 media_device_unregister_entity_notify(mdev, notify);
153 }
154
155
156 dev->media_dev->source_priv = NULL;
157 dev->media_dev->enable_source = NULL;
158 dev->media_dev->disable_source = NULL;
159
160 media_device_unregister(dev->media_dev);
161 media_device_cleanup(dev->media_dev);
162 kfree(dev->media_dev);
163 dev->media_dev = NULL;
164#endif
165}
166
167void au0828_usb_release(struct au0828_dev *dev)
168{
169 au0828_unregister_media_device(dev);
170
171
172 au0828_i2c_unregister(dev);
173
174 kfree(dev);
175}
176
177static void au0828_usb_disconnect(struct usb_interface *interface)
178{
179 struct au0828_dev *dev = usb_get_intfdata(interface);
180
181 dprintk(1, "%s()\n", __func__);
182
183
184
185
186
187
188
189 set_bit(DEV_DISCONNECTED, &dev->dev_state);
190
191 au0828_rc_unregister(dev);
192
193 au0828_dvb_unregister(dev);
194
195 usb_set_intfdata(interface, NULL);
196 mutex_lock(&dev->mutex);
197 dev->usbdev = NULL;
198 mutex_unlock(&dev->mutex);
199 if (au0828_analog_unregister(dev)) {
200
201
202
203
204 return;
205 }
206 au0828_usb_release(dev);
207}
208
209static int au0828_media_device_init(struct au0828_dev *dev,
210 struct usb_device *udev)
211{
212#ifdef CONFIG_MEDIA_CONTROLLER
213 struct media_device *mdev;
214
215 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
216 if (!mdev)
217 return -ENOMEM;
218
219
220 if (!mdev->dev)
221 media_device_usb_init(mdev, udev, udev->product);
222
223 dev->media_dev = mdev;
224#endif
225 return 0;
226}
227
228#ifdef CONFIG_MEDIA_CONTROLLER
229static void au0828_media_graph_notify(struct media_entity *new,
230 void *notify_data)
231{
232 struct au0828_dev *dev = (struct au0828_dev *) notify_data;
233 int ret;
234 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
235
236 if (!new) {
237
238
239
240
241
242 media_device_for_each_entity(entity, dev->media_dev) {
243 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
244 mixer = entity;
245 else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
246 decoder = entity;
247 }
248 goto create_link;
249 }
250
251 switch (new->function) {
252 case MEDIA_ENT_F_AUDIO_MIXER:
253 mixer = new;
254 if (dev->decoder)
255 decoder = dev->decoder;
256 break;
257 case MEDIA_ENT_F_ATV_DECODER:
258
259 media_device_for_each_entity(entity, dev->media_dev) {
260 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
261 mixer = entity;
262 }
263 decoder = new;
264 break;
265 default:
266 break;
267 }
268
269create_link:
270 if (decoder && mixer) {
271 ret = media_create_pad_link(decoder,
272 DEMOD_PAD_AUDIO_OUT,
273 mixer, 0,
274 MEDIA_LNK_FL_ENABLED);
275 if (ret)
276 dev_err(&dev->usbdev->dev,
277 "Mixer Pad Link Create Error: %d\n", ret);
278 }
279}
280
281static int au0828_enable_source(struct media_entity *entity,
282 struct media_pipeline *pipe)
283{
284 struct media_entity *source, *find_source;
285 struct media_entity *sink;
286 struct media_link *link, *found_link = NULL;
287 int ret = 0;
288 struct media_device *mdev = entity->graph_obj.mdev;
289 struct au0828_dev *dev;
290
291 if (!mdev)
292 return -ENODEV;
293
294 mutex_lock(&mdev->graph_mutex);
295
296 dev = mdev->source_priv;
297
298
299
300
301
302
303
304
305
306
307
308
309 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
310 sink = entity;
311 find_source = dev->tuner;
312 } else {
313
314 if (!dev->decoder) {
315 ret = -ENODEV;
316 goto end;
317 }
318
319 sink = dev->decoder;
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336 if (dev->input_type == AU0828_VMUX_TELEVISION)
337 find_source = dev->tuner;
338 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
339 dev->input_type == AU0828_VMUX_COMPOSITE)
340 find_source = &dev->input_ent[dev->input_type];
341 else {
342
343 ret = 0;
344 goto end;
345 }
346 }
347
348
349 if (dev->active_link) {
350
351
352
353
354
355 if (dev->active_link->sink->entity == sink &&
356 dev->active_link->source->entity == find_source) {
357
358
359
360
361
362 if (dev->active_link_owner != entity &&
363 dev->active_link_owner->function ==
364 MEDIA_ENT_F_AUDIO_CAPTURE) {
365 pr_debug("ALSA has the tuner\n");
366 ret = -EBUSY;
367 goto end;
368 }
369 ret = 0;
370 goto end;
371 } else {
372 ret = -EBUSY;
373 goto end;
374 }
375 }
376
377 list_for_each_entry(link, &sink->links, list) {
378
379 if (link->sink->entity == sink &&
380 link->source->entity == find_source) {
381 found_link = link;
382 break;
383 }
384 }
385
386 if (!found_link) {
387 ret = -ENODEV;
388 goto end;
389 }
390
391
392 source = found_link->source->entity;
393 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
394 if (ret) {
395 pr_err("Activate tuner link %s->%s. Error %d\n",
396 source->name, sink->name, ret);
397 goto end;
398 }
399
400 ret = __media_entity_pipeline_start(entity, pipe);
401 if (ret) {
402 pr_err("Start Pipeline: %s->%s Error %d\n",
403 source->name, entity->name, ret);
404 ret = __media_entity_setup_link(found_link, 0);
405 pr_err("Deactivate link Error %d\n", ret);
406 goto end;
407 }
408
409
410
411
412
413 dev->active_link = found_link;
414 dev->active_link_owner = entity;
415 dev->active_source = source;
416 dev->active_sink = sink;
417
418 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
419 dev->active_source->name, dev->active_sink->name,
420 dev->active_link_owner->name, ret);
421end:
422 mutex_unlock(&mdev->graph_mutex);
423 pr_debug("au0828_enable_source() end %s %d %d\n",
424 entity->name, entity->function, ret);
425 return ret;
426}
427
428static void au0828_disable_source(struct media_entity *entity)
429{
430 int ret = 0;
431 struct media_device *mdev = entity->graph_obj.mdev;
432 struct au0828_dev *dev;
433
434 if (!mdev)
435 return;
436
437 mutex_lock(&mdev->graph_mutex);
438 dev = mdev->source_priv;
439
440 if (!dev->active_link) {
441 ret = -ENODEV;
442 goto end;
443 }
444
445
446 if (dev->active_link->sink->entity == dev->active_sink &&
447 dev->active_link->source->entity == dev->active_source) {
448
449
450
451
452 if (dev->active_link_owner != entity)
453 goto end;
454 __media_entity_pipeline_stop(entity);
455 ret = __media_entity_setup_link(dev->active_link, 0);
456 if (ret)
457 pr_err("Deactivate link Error %d\n", ret);
458
459 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
460 dev->active_source->name, dev->active_sink->name,
461 dev->active_link_owner->name, ret);
462
463 dev->active_link = NULL;
464 dev->active_link_owner = NULL;
465 dev->active_source = NULL;
466 dev->active_sink = NULL;
467 }
468
469end:
470 mutex_unlock(&mdev->graph_mutex);
471}
472#endif
473
474static int au0828_media_device_register(struct au0828_dev *dev,
475 struct usb_device *udev)
476{
477#ifdef CONFIG_MEDIA_CONTROLLER
478 int ret;
479 struct media_entity *entity, *demod = NULL;
480 struct media_link *link;
481
482 if (!dev->media_dev)
483 return 0;
484
485 if (!media_devnode_is_registered(dev->media_dev->devnode)) {
486
487
488 ret = media_device_register(dev->media_dev);
489 if (ret) {
490 dev_err(&udev->dev,
491 "Media Device Register Error: %d\n", ret);
492 return ret;
493 }
494 } else {
495
496
497
498
499
500
501
502 au0828_media_graph_notify(NULL, (void *) dev);
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516 media_device_for_each_entity(entity, dev->media_dev) {
517 switch (entity->function) {
518 case MEDIA_ENT_F_TUNER:
519 dev->tuner = entity;
520 break;
521 case MEDIA_ENT_F_ATV_DECODER:
522 dev->decoder = entity;
523 break;
524 case MEDIA_ENT_F_DTV_DEMOD:
525 demod = entity;
526 break;
527 }
528 }
529
530
531 if (dev->tuner) {
532 list_for_each_entry(link, &dev->tuner->links, list) {
533 if (demod && link->sink->entity == demod)
534 media_entity_setup_link(link, 0);
535 if (dev->decoder && link->sink->entity == dev->decoder)
536 media_entity_setup_link(link, 0);
537 }
538 }
539
540
541 dev->entity_notify.notify_data = (void *) dev;
542 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
543 ret = media_device_register_entity_notify(dev->media_dev,
544 &dev->entity_notify);
545 if (ret) {
546 dev_err(&udev->dev,
547 "Media Device register entity_notify Error: %d\n",
548 ret);
549 return ret;
550 }
551
552 dev->media_dev->source_priv = (void *) dev;
553 dev->media_dev->enable_source = au0828_enable_source;
554 dev->media_dev->disable_source = au0828_disable_source;
555#endif
556 return 0;
557}
558
559static int au0828_usb_probe(struct usb_interface *interface,
560 const struct usb_device_id *id)
561{
562 int ifnum;
563 int retval = 0;
564
565 struct au0828_dev *dev;
566 struct usb_device *usbdev = interface_to_usbdev(interface);
567
568 ifnum = interface->altsetting->desc.bInterfaceNumber;
569
570 if (ifnum != 0)
571 return -ENODEV;
572
573 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
574 le16_to_cpu(usbdev->descriptor.idVendor),
575 le16_to_cpu(usbdev->descriptor.idProduct),
576 ifnum);
577
578
579
580
581
582
583 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
584 pr_err("au0828: Device initialization failed.\n");
585 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
586 return -ENODEV;
587 }
588
589 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
590 if (dev == NULL) {
591 pr_err("%s() Unable to allocate memory\n", __func__);
592 return -ENOMEM;
593 }
594
595 mutex_init(&dev->lock);
596 mutex_lock(&dev->lock);
597 mutex_init(&dev->mutex);
598 mutex_init(&dev->dvb.lock);
599 dev->usbdev = usbdev;
600 dev->boardnr = id->driver_info;
601 dev->board = au0828_boards[dev->boardnr];
602
603
604 retval = au0828_media_device_init(dev, usbdev);
605 if (retval) {
606 pr_err("%s() au0828_media_device_init failed\n",
607 __func__);
608 mutex_unlock(&dev->lock);
609 kfree(dev);
610 return retval;
611 }
612
613 retval = au0828_v4l2_device_register(interface, dev);
614 if (retval) {
615 au0828_usb_v4l2_media_release(dev);
616 mutex_unlock(&dev->lock);
617 kfree(dev);
618 return retval;
619 }
620
621
622 au0828_write(dev, REG_600, 1 << 4);
623
624
625 au0828_gpio_setup(dev);
626
627
628 au0828_i2c_register(dev);
629
630
631 au0828_card_setup(dev);
632
633
634 retval = au0828_analog_register(dev, interface);
635 if (retval) {
636 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
637 __func__);
638 goto done;
639 }
640
641
642 retval = au0828_dvb_register(dev);
643 if (retval)
644 pr_err("%s() au0282_dev_register failed\n",
645 __func__);
646
647
648 au0828_rc_register(dev);
649
650
651
652
653
654 usb_set_intfdata(interface, dev);
655
656 pr_info("Registered device AU0828 [%s]\n",
657 dev->board.name == NULL ? "Unset" : dev->board.name);
658
659 mutex_unlock(&dev->lock);
660
661 retval = au0828_media_device_register(dev, usbdev);
662
663done:
664 if (retval < 0)
665 au0828_usb_disconnect(interface);
666
667 return retval;
668}
669
670static int au0828_suspend(struct usb_interface *interface,
671 pm_message_t message)
672{
673 struct au0828_dev *dev = usb_get_intfdata(interface);
674
675 if (!dev)
676 return 0;
677
678 pr_info("Suspend\n");
679
680 au0828_rc_suspend(dev);
681 au0828_v4l2_suspend(dev);
682 au0828_dvb_suspend(dev);
683
684
685
686 return 0;
687}
688
689static int au0828_resume(struct usb_interface *interface)
690{
691 struct au0828_dev *dev = usb_get_intfdata(interface);
692 if (!dev)
693 return 0;
694
695 pr_info("Resume\n");
696
697
698 au0828_write(dev, REG_600, 1 << 4);
699
700
701 au0828_gpio_setup(dev);
702
703 au0828_rc_resume(dev);
704 au0828_v4l2_resume(dev);
705 au0828_dvb_resume(dev);
706
707
708
709 return 0;
710}
711
712static struct usb_driver au0828_usb_driver = {
713 .name = KBUILD_MODNAME,
714 .probe = au0828_usb_probe,
715 .disconnect = au0828_usb_disconnect,
716 .id_table = au0828_usb_id_table,
717 .suspend = au0828_suspend,
718 .resume = au0828_resume,
719 .reset_resume = au0828_resume,
720};
721
722static int __init au0828_init(void)
723{
724 int ret;
725
726 if (au0828_debug & 1)
727 pr_info("%s() Debugging is enabled\n", __func__);
728
729 if (au0828_debug & 2)
730 pr_info("%s() USB Debugging is enabled\n", __func__);
731
732 if (au0828_debug & 4)
733 pr_info("%s() I2C Debugging is enabled\n", __func__);
734
735 if (au0828_debug & 8)
736 pr_info("%s() Bridge Debugging is enabled\n",
737 __func__);
738
739 if (au0828_debug & 16)
740 pr_info("%s() IR Debugging is enabled\n",
741 __func__);
742
743 pr_info("au0828 driver loaded\n");
744
745 ret = usb_register(&au0828_usb_driver);
746 if (ret)
747 pr_err("usb_register failed, error = %d\n", ret);
748
749 return ret;
750}
751
752static void __exit au0828_exit(void)
753{
754 usb_deregister(&au0828_usb_driver);
755}
756
757module_init(au0828_init);
758module_exit(au0828_exit);
759
760MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
761MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
762MODULE_LICENSE("GPL");
763MODULE_VERSION("0.0.3");
764