powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / drivers / media / usb / au0828 / au0828-core.c
blobbf53553d262449db469ea9461fb763cc61ebfa40
1 /*
2 * Driver for the Auvitek USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
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.
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
15 * GNU General Public License for more details.
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.
22 #include "au0828.h"
23 #include "au8522.h"
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>
31 /* Due to enum tuner_pad_index */
32 #include <media/tuner.h>
35 * 1 = General debug messages
36 * 2 = USB handling
37 * 4 = I2C related
38 * 8 = Bridge related
39 * 16 = IR related
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");
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");
51 #define _AU0828_BULKPIPE 0x03
52 #define _BULKPIPESIZE 0xffff
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);
59 /* USB Direction */
60 #define CMD_REQUEST_IN 0x00
61 #define CMD_REQUEST_OUT 0x01
63 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
65 u8 result = 0;
67 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
68 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
70 return result;
73 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
75 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
76 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
79 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
80 u16 index)
82 int status = -ENODEV;
84 if (dev->usbdev) {
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);
94 status = min(status, 0);
96 if (status < 0) {
97 pr_err("%s() Failed sending control message, error %d.\n",
98 __func__, status);
103 return status;
106 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
107 u16 index, unsigned char *cp, u16 size)
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);
119 status = min(status, 0);
121 if (status < 0) {
122 pr_err("%s() Failed receiving control message, error %d.\n",
123 __func__, status);
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);
130 mutex_unlock(&dev->mutex);
131 return status;
134 #ifdef CONFIG_MEDIA_CONTROLLER
135 static void au0828_media_graph_notify(struct media_entity *new,
136 void *notify_data);
137 #endif
139 static void au0828_unregister_media_device(struct au0828_dev *dev)
141 #ifdef CONFIG_MEDIA_CONTROLLER
142 struct media_device *mdev = dev->media_dev;
143 struct media_entity_notify *notify, *nextp;
145 if (!mdev || !media_devnode_is_registered(mdev->devnode))
146 return;
148 /* Remove au0828 entity_notify callbacks */
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);
155 /* clear enable_source, disable_source */
156 dev->media_dev->source_priv = NULL;
157 dev->media_dev->enable_source = NULL;
158 dev->media_dev->disable_source = NULL;
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
167 void au0828_usb_release(struct au0828_dev *dev)
169 au0828_unregister_media_device(dev);
171 /* I2C */
172 au0828_i2c_unregister(dev);
174 kfree(dev);
177 static void au0828_usb_disconnect(struct usb_interface *interface)
179 struct au0828_dev *dev = usb_get_intfdata(interface);
181 dprintk(1, "%s()\n", __func__);
183 /* there is a small window after disconnect, before
184 dev->usbdev is NULL, for poll (e.g: IR) try to access
185 the device and fill the dmesg with error messages.
186 Set the status so poll routines can check and avoid
187 access after disconnect.
189 set_bit(DEV_DISCONNECTED, &dev->dev_state);
191 au0828_rc_unregister(dev);
192 /* Digital TV */
193 au0828_dvb_unregister(dev);
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)) {
201 * No need to call au0828_usb_release() if V4L2 is enabled,
202 * as this is already called via au0828_usb_v4l2_release()
204 return;
206 au0828_usb_release(dev);
209 static int au0828_media_device_init(struct au0828_dev *dev,
210 struct usb_device *udev)
212 #ifdef CONFIG_MEDIA_CONTROLLER
213 struct media_device *mdev;
215 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
216 if (!mdev)
217 return -ENOMEM;
219 /* check if media device is already initialized */
220 if (!mdev->dev)
221 media_device_usb_init(mdev, udev, udev->product);
223 dev->media_dev = mdev;
224 #endif
225 return 0;
228 #ifdef CONFIG_MEDIA_CONTROLLER
229 static void au0828_media_graph_notify(struct media_entity *new,
230 void *notify_data)
232 struct au0828_dev *dev = (struct au0828_dev *) notify_data;
233 int ret;
234 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
236 if (!new) {
238 * Called during au0828 probe time to connect
239 * entites that were created prior to registering
240 * the notify handler. Find mixer and decoder.
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;
248 goto create_link;
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 /* In case, Mixer is added first, find mixer and create link */
259 media_device_for_each_entity(entity, dev->media_dev) {
260 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
261 mixer = entity;
263 decoder = new;
264 break;
265 default:
266 break;
269 create_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);
281 static int au0828_enable_source(struct media_entity *entity,
282 struct media_pipeline *pipe)
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;
291 if (!mdev)
292 return -ENODEV;
294 mutex_lock(&mdev->graph_mutex);
296 dev = mdev->source_priv;
299 * For Audio and V4L2 entity, find the link to which decoder
300 * is the sink. Look for an active link between decoder and
301 * source (tuner/s-video/Composite), if one exists, nothing
302 * to do. If not, look for any active links between source
303 * and any other entity. If one exists, source is busy. If
304 * source is free, setup link and start pipeline from source.
305 * For DVB FE entity, the source for the link is the tuner.
306 * Check if tuner is available and setup link and start
307 * pipeline.
309 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
310 sink = entity;
311 find_source = dev->tuner;
312 } else {
313 /* Analog isn't configured or register failed */
314 if (!dev->decoder) {
315 ret = -ENODEV;
316 goto end;
319 sink = dev->decoder;
322 * Default input is tuner and default input_type
323 * is AU0828_VMUX_TELEVISION.
324 * FIXME:
325 * There is a problem when s_input is called to
326 * change the default input. s_input will try to
327 * enable_source before attempting to change the
328 * input on the device, and will end up enabling
329 * default source which is tuner.
331 * Additional logic is necessary in au0828
332 * to detect that the input has changed and
333 * enable the right source.
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 /* unknown input - let user select input */
343 ret = 0;
344 goto end;
348 /* Is an active link between sink and source */
349 if (dev->active_link) {
351 * If DVB is using the tuner and calling entity is
352 * audio/video, the following check will be false,
353 * since sink is different. Result is Busy.
355 if (dev->active_link->sink->entity == sink &&
356 dev->active_link->source->entity == find_source) {
358 * Either ALSA or Video own tuner. sink is
359 * the same for both. Prevent Video stepping
360 * on ALSA when ALSA owns the source.
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;
369 ret = 0;
370 goto end;
371 } else {
372 ret = -EBUSY;
373 goto end;
377 list_for_each_entry(link, &sink->links, list) {
378 /* Check sink, and source */
379 if (link->sink->entity == sink &&
380 link->source->entity == find_source) {
381 found_link = link;
382 break;
386 if (!found_link) {
387 ret = -ENODEV;
388 goto end;
391 /* activate link between source and sink and start pipeline */
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;
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;
409 * save active link and active link owner to avoid audio
410 * deactivating video owned link from disable_source and
411 * vice versa
413 dev->active_link = found_link;
414 dev->active_link_owner = entity;
415 dev->active_source = source;
416 dev->active_sink = sink;
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);
421 end:
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;
428 static void au0828_disable_source(struct media_entity *entity)
430 int ret = 0;
431 struct media_device *mdev = entity->graph_obj.mdev;
432 struct au0828_dev *dev;
434 if (!mdev)
435 return;
437 mutex_lock(&mdev->graph_mutex);
438 dev = mdev->source_priv;
440 if (!dev->active_link) {
441 ret = -ENODEV;
442 goto end;
445 /* link is active - stop pipeline from source (tuner) */
446 if (dev->active_link->sink->entity == dev->active_sink &&
447 dev->active_link->source->entity == dev->active_source) {
449 * prevent video from deactivating link when audio
450 * has active pipeline
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);
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);
463 dev->active_link = NULL;
464 dev->active_link_owner = NULL;
465 dev->active_source = NULL;
466 dev->active_sink = NULL;
469 end:
470 mutex_unlock(&mdev->graph_mutex);
472 #endif
474 static int au0828_media_device_register(struct au0828_dev *dev,
475 struct usb_device *udev)
477 #ifdef CONFIG_MEDIA_CONTROLLER
478 int ret;
479 struct media_entity *entity, *demod = NULL;
480 struct media_link *link;
482 if (!dev->media_dev)
483 return 0;
485 if (!media_devnode_is_registered(dev->media_dev->devnode)) {
487 /* register media device */
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;
494 } else {
496 * Call au0828_media_graph_notify() to connect
497 * audio graph to our graph. In this case, audio
498 * driver registered the device and there is no
499 * entity_notify to be called when new entities
500 * are added. Invoke it now.
502 au0828_media_graph_notify(NULL, (void *) dev);
506 * Find tuner, decoder and demod.
508 * The tuner and decoder should be cached, as they'll be used by
509 * au0828_enable_source.
511 * It also needs to disable the link between tuner and
512 * decoder/demod, to avoid disable step when tuner is requested
513 * by video or audio. Note that this step can't be done until dvb
514 * graph is created during dvb register.
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;
530 /* Disable link between tuner->demod and/or tuner->decoder */
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);
540 /* register entity_notify callback */
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;
551 /* set enable_source */
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;
559 static int au0828_usb_probe(struct usb_interface *interface,
560 const struct usb_device_id *id)
562 int ifnum;
563 int retval = 0;
565 struct au0828_dev *dev;
566 struct usb_device *usbdev = interface_to_usbdev(interface);
568 ifnum = interface->altsetting->desc.bInterfaceNumber;
570 if (ifnum != 0)
571 return -ENODEV;
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);
579 * Make sure we have 480 Mbps of bandwidth, otherwise things like
580 * video stream wouldn't likely work, since 12 Mbps is generally
581 * not enough even for most Digital TV streams.
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;
589 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
590 if (dev == NULL) {
591 pr_err("%s() Unable to allocate memory\n", __func__);
592 return -ENOMEM;
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];
603 /* Initialize the media controller */
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;
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;
621 /* Power Up the bridge */
622 au0828_write(dev, REG_600, 1 << 4);
624 /* Bring up the GPIO's and supporting devices */
625 au0828_gpio_setup(dev);
627 /* I2C */
628 au0828_i2c_register(dev);
630 /* Setup */
631 au0828_card_setup(dev);
633 /* Analog TV */
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;
641 /* Digital TV */
642 retval = au0828_dvb_register(dev);
643 if (retval)
644 pr_err("%s() au0282_dev_register failed\n",
645 __func__);
647 /* Remote controller */
648 au0828_rc_register(dev);
651 * Store the pointer to the au0828_dev so it can be accessed in
652 * au0828_usb_disconnect
654 usb_set_intfdata(interface, dev);
656 pr_info("Registered device AU0828 [%s]\n",
657 dev->board.name == NULL ? "Unset" : dev->board.name);
659 mutex_unlock(&dev->lock);
661 retval = au0828_media_device_register(dev, usbdev);
663 done:
664 if (retval < 0)
665 au0828_usb_disconnect(interface);
667 return retval;
670 static int au0828_suspend(struct usb_interface *interface,
671 pm_message_t message)
673 struct au0828_dev *dev = usb_get_intfdata(interface);
675 if (!dev)
676 return 0;
678 pr_info("Suspend\n");
680 au0828_rc_suspend(dev);
681 au0828_v4l2_suspend(dev);
682 au0828_dvb_suspend(dev);
684 /* FIXME: should suspend also ATV/DTV */
686 return 0;
689 static int au0828_resume(struct usb_interface *interface)
691 struct au0828_dev *dev = usb_get_intfdata(interface);
692 if (!dev)
693 return 0;
695 pr_info("Resume\n");
697 /* Power Up the bridge */
698 au0828_write(dev, REG_600, 1 << 4);
700 /* Bring up the GPIO's and supporting devices */
701 au0828_gpio_setup(dev);
703 au0828_rc_resume(dev);
704 au0828_v4l2_resume(dev);
705 au0828_dvb_resume(dev);
707 /* FIXME: should resume also ATV/DTV */
709 return 0;
712 static 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,
722 static int __init au0828_init(void)
724 int ret;
726 if (au0828_debug & 1)
727 pr_info("%s() Debugging is enabled\n", __func__);
729 if (au0828_debug & 2)
730 pr_info("%s() USB Debugging is enabled\n", __func__);
732 if (au0828_debug & 4)
733 pr_info("%s() I2C Debugging is enabled\n", __func__);
735 if (au0828_debug & 8)
736 pr_info("%s() Bridge Debugging is enabled\n",
737 __func__);
739 if (au0828_debug & 16)
740 pr_info("%s() IR Debugging is enabled\n",
741 __func__);
743 pr_info("au0828 driver loaded\n");
745 ret = usb_register(&au0828_usb_driver);
746 if (ret)
747 pr_err("usb_register failed, error = %d\n", ret);
749 return ret;
752 static void __exit au0828_exit(void)
754 usb_deregister(&au0828_usb_driver);
757 module_init(au0828_init);
758 module_exit(au0828_exit);
760 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
761 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
762 MODULE_LICENSE("GPL");
763 MODULE_VERSION("0.0.3");