Merge branch 'linux-4.6' of git://github.com/skeggsb/linux into drm-fixes
[deliverable/linux.git] / drivers / media / usb / au0828 / au0828-core.c
1 /*
2 * Driver for the Auvitek USB bridge
3 *
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 /* Due to enum tuner_pad_index */
32 #include <media/tuner.h>
33
34 /*
35 * 1 = General debug messages
36 * 2 = USB handling
37 * 4 = I2C related
38 * 8 = Bridge related
39 * 16 = IR related
40 */
41 int au0828_debug;
42 module_param_named(debug, au0828_debug, int, 0644);
43 MODULE_PARM_DESC(debug,
44 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
45
46 static unsigned int disable_usb_speed_check;
47 module_param(disable_usb_speed_check, int, 0444);
48 MODULE_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
54 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
55 u16 index);
56 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
57 u16 index, unsigned char *cp, u16 size);
58
59 /* USB Direction */
60 #define CMD_REQUEST_IN 0x00
61 #define CMD_REQUEST_OUT 0x01
62
63 u32 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
73 u32 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
79 static 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 /* cp must be memory that has been allocated by kmalloc */
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
106 static 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 /* the host controller requires heap allocated memory, which
127 is why we didn't just pass "cp" into usb_control_msg */
128 memcpy(cp, dev->ctrlmsg, size);
129 }
130 mutex_unlock(&dev->mutex);
131 return status;
132 }
133
134 static void au0828_unregister_media_device(struct au0828_dev *dev)
135 {
136
137 #ifdef CONFIG_MEDIA_CONTROLLER
138 if (dev->media_dev &&
139 media_devnode_is_registered(&dev->media_dev->devnode)) {
140 /* clear enable_source, disable_source */
141 dev->media_dev->source_priv = NULL;
142 dev->media_dev->enable_source = NULL;
143 dev->media_dev->disable_source = NULL;
144
145 media_device_unregister(dev->media_dev);
146 media_device_cleanup(dev->media_dev);
147 kfree(dev->media_dev);
148 dev->media_dev = NULL;
149 }
150 #endif
151 }
152
153 void au0828_usb_release(struct au0828_dev *dev)
154 {
155 au0828_unregister_media_device(dev);
156
157 /* I2C */
158 au0828_i2c_unregister(dev);
159
160 kfree(dev);
161 }
162
163 static void au0828_usb_disconnect(struct usb_interface *interface)
164 {
165 struct au0828_dev *dev = usb_get_intfdata(interface);
166
167 dprintk(1, "%s()\n", __func__);
168
169 /* there is a small window after disconnect, before
170 dev->usbdev is NULL, for poll (e.g: IR) try to access
171 the device and fill the dmesg with error messages.
172 Set the status so poll routines can check and avoid
173 access after disconnect.
174 */
175 set_bit(DEV_DISCONNECTED, &dev->dev_state);
176
177 au0828_rc_unregister(dev);
178 /* Digital TV */
179 au0828_dvb_unregister(dev);
180
181 usb_set_intfdata(interface, NULL);
182 mutex_lock(&dev->mutex);
183 dev->usbdev = NULL;
184 mutex_unlock(&dev->mutex);
185 if (au0828_analog_unregister(dev)) {
186 /*
187 * No need to call au0828_usb_release() if V4L2 is enabled,
188 * as this is already called via au0828_usb_v4l2_release()
189 */
190 return;
191 }
192 au0828_usb_release(dev);
193 }
194
195 static int au0828_media_device_init(struct au0828_dev *dev,
196 struct usb_device *udev)
197 {
198 #ifdef CONFIG_MEDIA_CONTROLLER
199 struct media_device *mdev;
200
201 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
202 if (!mdev)
203 return -ENOMEM;
204
205 /* check if media device is already initialized */
206 if (!mdev->dev)
207 media_device_usb_init(mdev, udev, udev->product);
208
209 dev->media_dev = mdev;
210 #endif
211 return 0;
212 }
213
214 #ifdef CONFIG_MEDIA_CONTROLLER
215 static void au0828_media_graph_notify(struct media_entity *new,
216 void *notify_data)
217 {
218 struct au0828_dev *dev = (struct au0828_dev *) notify_data;
219 int ret;
220 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
221
222 if (!new) {
223 /*
224 * Called during au0828 probe time to connect
225 * entites that were created prior to registering
226 * the notify handler. Find mixer and decoder.
227 */
228 media_device_for_each_entity(entity, dev->media_dev) {
229 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
230 mixer = entity;
231 else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
232 decoder = entity;
233 }
234 goto create_link;
235 }
236
237 switch (new->function) {
238 case MEDIA_ENT_F_AUDIO_MIXER:
239 mixer = new;
240 if (dev->decoder)
241 decoder = dev->decoder;
242 break;
243 case MEDIA_ENT_F_ATV_DECODER:
244 /* In case, Mixer is added first, find mixer and create link */
245 media_device_for_each_entity(entity, dev->media_dev) {
246 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
247 mixer = entity;
248 }
249 decoder = new;
250 break;
251 default:
252 break;
253 }
254
255 create_link:
256 if (decoder && mixer) {
257 ret = media_create_pad_link(decoder,
258 DEMOD_PAD_AUDIO_OUT,
259 mixer, 0,
260 MEDIA_LNK_FL_ENABLED);
261 if (ret)
262 dev_err(&dev->usbdev->dev,
263 "Mixer Pad Link Create Error: %d\n", ret);
264 }
265 }
266
267 static int au0828_enable_source(struct media_entity *entity,
268 struct media_pipeline *pipe)
269 {
270 struct media_entity *source, *find_source;
271 struct media_entity *sink;
272 struct media_link *link, *found_link = NULL;
273 int ret = 0;
274 struct media_device *mdev = entity->graph_obj.mdev;
275 struct au0828_dev *dev;
276
277 if (!mdev)
278 return -ENODEV;
279
280 mutex_lock(&mdev->graph_mutex);
281
282 dev = mdev->source_priv;
283
284 /*
285 * For Audio and V4L2 entity, find the link to which decoder
286 * is the sink. Look for an active link between decoder and
287 * source (tuner/s-video/Composite), if one exists, nothing
288 * to do. If not, look for any active links between source
289 * and any other entity. If one exists, source is busy. If
290 * source is free, setup link and start pipeline from source.
291 * For DVB FE entity, the source for the link is the tuner.
292 * Check if tuner is available and setup link and start
293 * pipeline.
294 */
295 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
296 sink = entity;
297 find_source = dev->tuner;
298 } else {
299 /* Analog isn't configured or register failed */
300 if (!dev->decoder) {
301 ret = -ENODEV;
302 goto end;
303 }
304
305 sink = dev->decoder;
306
307 /*
308 * Default input is tuner and default input_type
309 * is AU0828_VMUX_TELEVISION.
310 * FIXME:
311 * There is a problem when s_input is called to
312 * change the default input. s_input will try to
313 * enable_source before attempting to change the
314 * input on the device, and will end up enabling
315 * default source which is tuner.
316 *
317 * Additional logic is necessary in au0828
318 * to detect that the input has changed and
319 * enable the right source.
320 */
321
322 if (dev->input_type == AU0828_VMUX_TELEVISION)
323 find_source = dev->tuner;
324 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
325 dev->input_type == AU0828_VMUX_COMPOSITE)
326 find_source = &dev->input_ent[dev->input_type];
327 else {
328 /* unknown input - let user select input */
329 ret = 0;
330 goto end;
331 }
332 }
333
334 /* Is an active link between sink and source */
335 if (dev->active_link) {
336 /*
337 * If DVB is using the tuner and calling entity is
338 * audio/video, the following check will be false,
339 * since sink is different. Result is Busy.
340 */
341 if (dev->active_link->sink->entity == sink &&
342 dev->active_link->source->entity == find_source) {
343 /*
344 * Either ALSA or Video own tuner. sink is
345 * the same for both. Prevent Video stepping
346 * on ALSA when ALSA owns the source.
347 */
348 if (dev->active_link_owner != entity &&
349 dev->active_link_owner->function ==
350 MEDIA_ENT_F_AUDIO_CAPTURE) {
351 pr_debug("ALSA has the tuner\n");
352 ret = -EBUSY;
353 goto end;
354 }
355 ret = 0;
356 goto end;
357 } else {
358 ret = -EBUSY;
359 goto end;
360 }
361 }
362
363 list_for_each_entry(link, &sink->links, list) {
364 /* Check sink, and source */
365 if (link->sink->entity == sink &&
366 link->source->entity == find_source) {
367 found_link = link;
368 break;
369 }
370 }
371
372 if (!found_link) {
373 ret = -ENODEV;
374 goto end;
375 }
376
377 /* activate link between source and sink and start pipeline */
378 source = found_link->source->entity;
379 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
380 if (ret) {
381 pr_err("Activate tuner link %s->%s. Error %d\n",
382 source->name, sink->name, ret);
383 goto end;
384 }
385
386 ret = __media_entity_pipeline_start(entity, pipe);
387 if (ret) {
388 pr_err("Start Pipeline: %s->%s Error %d\n",
389 source->name, entity->name, ret);
390 ret = __media_entity_setup_link(found_link, 0);
391 pr_err("Deactivate link Error %d\n", ret);
392 goto end;
393 }
394 /*
395 * save active link and active link owner to avoid audio
396 * deactivating video owned link from disable_source and
397 * vice versa
398 */
399 dev->active_link = found_link;
400 dev->active_link_owner = entity;
401 dev->active_source = source;
402 dev->active_sink = sink;
403
404 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
405 dev->active_source->name, dev->active_sink->name,
406 dev->active_link_owner->name, ret);
407 end:
408 mutex_unlock(&mdev->graph_mutex);
409 pr_debug("au0828_enable_source() end %s %d %d\n",
410 entity->name, entity->function, ret);
411 return ret;
412 }
413
414 static void au0828_disable_source(struct media_entity *entity)
415 {
416 int ret = 0;
417 struct media_device *mdev = entity->graph_obj.mdev;
418 struct au0828_dev *dev;
419
420 if (!mdev)
421 return;
422
423 mutex_lock(&mdev->graph_mutex);
424 dev = mdev->source_priv;
425
426 if (!dev->active_link) {
427 ret = -ENODEV;
428 goto end;
429 }
430
431 /* link is active - stop pipeline from source (tuner) */
432 if (dev->active_link->sink->entity == dev->active_sink &&
433 dev->active_link->source->entity == dev->active_source) {
434 /*
435 * prevent video from deactivating link when audio
436 * has active pipeline
437 */
438 if (dev->active_link_owner != entity)
439 goto end;
440 __media_entity_pipeline_stop(entity);
441 ret = __media_entity_setup_link(dev->active_link, 0);
442 if (ret)
443 pr_err("Deactivate link Error %d\n", ret);
444
445 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
446 dev->active_source->name, dev->active_sink->name,
447 dev->active_link_owner->name, ret);
448
449 dev->active_link = NULL;
450 dev->active_link_owner = NULL;
451 dev->active_source = NULL;
452 dev->active_sink = NULL;
453 }
454
455 end:
456 mutex_unlock(&mdev->graph_mutex);
457 }
458 #endif
459
460 static int au0828_media_device_register(struct au0828_dev *dev,
461 struct usb_device *udev)
462 {
463 #ifdef CONFIG_MEDIA_CONTROLLER
464 int ret;
465 struct media_entity *entity, *demod = NULL;
466 struct media_link *link;
467
468 if (!dev->media_dev)
469 return 0;
470
471 if (!media_devnode_is_registered(&dev->media_dev->devnode)) {
472
473 /* register media device */
474 ret = media_device_register(dev->media_dev);
475 if (ret) {
476 dev_err(&udev->dev,
477 "Media Device Register Error: %d\n", ret);
478 return ret;
479 }
480 } else {
481 /*
482 * Call au0828_media_graph_notify() to connect
483 * audio graph to our graph. In this case, audio
484 * driver registered the device and there is no
485 * entity_notify to be called when new entities
486 * are added. Invoke it now.
487 */
488 au0828_media_graph_notify(NULL, (void *) dev);
489 }
490
491 /*
492 * Find tuner, decoder and demod.
493 *
494 * The tuner and decoder should be cached, as they'll be used by
495 * au0828_enable_source.
496 *
497 * It also needs to disable the link between tuner and
498 * decoder/demod, to avoid disable step when tuner is requested
499 * by video or audio. Note that this step can't be done until dvb
500 * graph is created during dvb register.
501 */
502 media_device_for_each_entity(entity, dev->media_dev) {
503 switch (entity->function) {
504 case MEDIA_ENT_F_TUNER:
505 dev->tuner = entity;
506 break;
507 case MEDIA_ENT_F_ATV_DECODER:
508 dev->decoder = entity;
509 break;
510 case MEDIA_ENT_F_DTV_DEMOD:
511 demod = entity;
512 break;
513 }
514 }
515
516 /* Disable link between tuner->demod and/or tuner->decoder */
517 if (dev->tuner) {
518 list_for_each_entry(link, &dev->tuner->links, list) {
519 if (demod && link->sink->entity == demod)
520 media_entity_setup_link(link, 0);
521 if (dev->decoder && link->sink->entity == dev->decoder)
522 media_entity_setup_link(link, 0);
523 }
524 }
525
526 /* register entity_notify callback */
527 dev->entity_notify.notify_data = (void *) dev;
528 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
529 ret = media_device_register_entity_notify(dev->media_dev,
530 &dev->entity_notify);
531 if (ret) {
532 dev_err(&udev->dev,
533 "Media Device register entity_notify Error: %d\n",
534 ret);
535 return ret;
536 }
537 /* set enable_source */
538 dev->media_dev->source_priv = (void *) dev;
539 dev->media_dev->enable_source = au0828_enable_source;
540 dev->media_dev->disable_source = au0828_disable_source;
541 #endif
542 return 0;
543 }
544
545 static int au0828_usb_probe(struct usb_interface *interface,
546 const struct usb_device_id *id)
547 {
548 int ifnum;
549 int retval = 0;
550
551 struct au0828_dev *dev;
552 struct usb_device *usbdev = interface_to_usbdev(interface);
553
554 ifnum = interface->altsetting->desc.bInterfaceNumber;
555
556 if (ifnum != 0)
557 return -ENODEV;
558
559 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
560 le16_to_cpu(usbdev->descriptor.idVendor),
561 le16_to_cpu(usbdev->descriptor.idProduct),
562 ifnum);
563
564 /*
565 * Make sure we have 480 Mbps of bandwidth, otherwise things like
566 * video stream wouldn't likely work, since 12 Mbps is generally
567 * not enough even for most Digital TV streams.
568 */
569 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
570 pr_err("au0828: Device initialization failed.\n");
571 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
572 return -ENODEV;
573 }
574
575 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
576 if (dev == NULL) {
577 pr_err("%s() Unable to allocate memory\n", __func__);
578 return -ENOMEM;
579 }
580
581 mutex_init(&dev->lock);
582 mutex_lock(&dev->lock);
583 mutex_init(&dev->mutex);
584 mutex_init(&dev->dvb.lock);
585 dev->usbdev = usbdev;
586 dev->boardnr = id->driver_info;
587 dev->board = au0828_boards[dev->boardnr];
588
589 /* Initialize the media controller */
590 retval = au0828_media_device_init(dev, usbdev);
591 if (retval) {
592 pr_err("%s() au0828_media_device_init failed\n",
593 __func__);
594 mutex_unlock(&dev->lock);
595 kfree(dev);
596 return retval;
597 }
598
599 retval = au0828_v4l2_device_register(interface, dev);
600 if (retval) {
601 au0828_usb_v4l2_media_release(dev);
602 mutex_unlock(&dev->lock);
603 kfree(dev);
604 return retval;
605 }
606
607 /* Power Up the bridge */
608 au0828_write(dev, REG_600, 1 << 4);
609
610 /* Bring up the GPIO's and supporting devices */
611 au0828_gpio_setup(dev);
612
613 /* I2C */
614 au0828_i2c_register(dev);
615
616 /* Setup */
617 au0828_card_setup(dev);
618
619 /* Analog TV */
620 retval = au0828_analog_register(dev, interface);
621 if (retval) {
622 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
623 __func__);
624 goto done;
625 }
626
627 /* Digital TV */
628 retval = au0828_dvb_register(dev);
629 if (retval)
630 pr_err("%s() au0282_dev_register failed\n",
631 __func__);
632
633 /* Remote controller */
634 au0828_rc_register(dev);
635
636 /*
637 * Store the pointer to the au0828_dev so it can be accessed in
638 * au0828_usb_disconnect
639 */
640 usb_set_intfdata(interface, dev);
641
642 pr_info("Registered device AU0828 [%s]\n",
643 dev->board.name == NULL ? "Unset" : dev->board.name);
644
645 mutex_unlock(&dev->lock);
646
647 retval = au0828_media_device_register(dev, usbdev);
648
649 done:
650 if (retval < 0)
651 au0828_usb_disconnect(interface);
652
653 return retval;
654 }
655
656 static int au0828_suspend(struct usb_interface *interface,
657 pm_message_t message)
658 {
659 struct au0828_dev *dev = usb_get_intfdata(interface);
660
661 if (!dev)
662 return 0;
663
664 pr_info("Suspend\n");
665
666 au0828_rc_suspend(dev);
667 au0828_v4l2_suspend(dev);
668 au0828_dvb_suspend(dev);
669
670 /* FIXME: should suspend also ATV/DTV */
671
672 return 0;
673 }
674
675 static int au0828_resume(struct usb_interface *interface)
676 {
677 struct au0828_dev *dev = usb_get_intfdata(interface);
678 if (!dev)
679 return 0;
680
681 pr_info("Resume\n");
682
683 /* Power Up the bridge */
684 au0828_write(dev, REG_600, 1 << 4);
685
686 /* Bring up the GPIO's and supporting devices */
687 au0828_gpio_setup(dev);
688
689 au0828_rc_resume(dev);
690 au0828_v4l2_resume(dev);
691 au0828_dvb_resume(dev);
692
693 /* FIXME: should resume also ATV/DTV */
694
695 return 0;
696 }
697
698 static struct usb_driver au0828_usb_driver = {
699 .name = KBUILD_MODNAME,
700 .probe = au0828_usb_probe,
701 .disconnect = au0828_usb_disconnect,
702 .id_table = au0828_usb_id_table,
703 .suspend = au0828_suspend,
704 .resume = au0828_resume,
705 .reset_resume = au0828_resume,
706 };
707
708 static int __init au0828_init(void)
709 {
710 int ret;
711
712 if (au0828_debug & 1)
713 pr_info("%s() Debugging is enabled\n", __func__);
714
715 if (au0828_debug & 2)
716 pr_info("%s() USB Debugging is enabled\n", __func__);
717
718 if (au0828_debug & 4)
719 pr_info("%s() I2C Debugging is enabled\n", __func__);
720
721 if (au0828_debug & 8)
722 pr_info("%s() Bridge Debugging is enabled\n",
723 __func__);
724
725 if (au0828_debug & 16)
726 pr_info("%s() IR Debugging is enabled\n",
727 __func__);
728
729 pr_info("au0828 driver loaded\n");
730
731 ret = usb_register(&au0828_usb_driver);
732 if (ret)
733 pr_err("usb_register failed, error = %d\n", ret);
734
735 return ret;
736 }
737
738 static void __exit au0828_exit(void)
739 {
740 usb_deregister(&au0828_usb_driver);
741 }
742
743 module_init(au0828_init);
744 module_exit(au0828_exit);
745
746 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
747 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
748 MODULE_LICENSE("GPL");
749 MODULE_VERSION("0.0.3");
This page took 0.080845 seconds and 6 git commands to generate.