Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / media / usb / au0828 / au0828-core.c
blob5dc82e8c8670f8ea299cf01151bf7293455410b7
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 static void au0828_unregister_media_device(struct au0828_dev *dev)
137 #ifdef CONFIG_MEDIA_CONTROLLER
138 if (dev->media_dev &&
139 media_devnode_is_registered(&dev->media_dev->devnode)) {
140 media_device_unregister(dev->media_dev);
141 media_device_cleanup(dev->media_dev);
142 dev->media_dev = NULL;
144 #endif
147 void au0828_usb_release(struct au0828_dev *dev)
149 au0828_unregister_media_device(dev);
151 /* I2C */
152 au0828_i2c_unregister(dev);
154 kfree(dev);
157 static void au0828_usb_disconnect(struct usb_interface *interface)
159 struct au0828_dev *dev = usb_get_intfdata(interface);
161 dprintk(1, "%s()\n", __func__);
163 /* there is a small window after disconnect, before
164 dev->usbdev is NULL, for poll (e.g: IR) try to access
165 the device and fill the dmesg with error messages.
166 Set the status so poll routines can check and avoid
167 access after disconnect.
169 dev->dev_state = DEV_DISCONNECTED;
171 au0828_rc_unregister(dev);
172 /* Digital TV */
173 au0828_dvb_unregister(dev);
175 usb_set_intfdata(interface, NULL);
176 mutex_lock(&dev->mutex);
177 dev->usbdev = NULL;
178 mutex_unlock(&dev->mutex);
179 if (au0828_analog_unregister(dev)) {
181 * No need to call au0828_usb_release() if V4L2 is enabled,
182 * as this is already called via au0828_usb_v4l2_release()
184 return;
186 au0828_usb_release(dev);
189 static int au0828_media_device_init(struct au0828_dev *dev,
190 struct usb_device *udev)
192 #ifdef CONFIG_MEDIA_CONTROLLER
193 struct media_device *mdev;
195 mdev = media_device_get_devres(&udev->dev);
196 if (!mdev)
197 return -ENOMEM;
199 /* check if media device is already initialized */
200 if (!mdev->dev)
201 media_device_usb_init(mdev, udev, udev->product);
203 dev->media_dev = mdev;
204 #endif
205 return 0;
208 #ifdef CONFIG_MEDIA_CONTROLLER
209 static void au0828_media_graph_notify(struct media_entity *new,
210 void *notify_data)
212 struct au0828_dev *dev = (struct au0828_dev *) notify_data;
213 int ret;
214 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
216 if (!new) {
218 * Called during au0828 probe time to connect
219 * entites that were created prior to registering
220 * the notify handler. Find mixer and decoder.
222 media_device_for_each_entity(entity, dev->media_dev) {
223 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
224 mixer = entity;
225 else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
226 decoder = entity;
228 goto create_link;
231 switch (new->function) {
232 case MEDIA_ENT_F_AUDIO_MIXER:
233 mixer = new;
234 if (dev->decoder)
235 decoder = dev->decoder;
236 break;
237 case MEDIA_ENT_F_ATV_DECODER:
238 /* In case, Mixer is added first, find mixer and create link */
239 media_device_for_each_entity(entity, dev->media_dev) {
240 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
241 mixer = entity;
243 decoder = new;
244 break;
245 default:
246 break;
249 create_link:
250 if (decoder && mixer) {
251 ret = media_create_pad_link(decoder,
252 DEMOD_PAD_AUDIO_OUT,
253 mixer, 0,
254 MEDIA_LNK_FL_ENABLED);
255 if (ret)
256 dev_err(&dev->usbdev->dev,
257 "Mixer Pad Link Create Error: %d\n", ret);
261 static int au0828_enable_source(struct media_entity *entity,
262 struct media_pipeline *pipe)
264 struct media_entity *source, *find_source;
265 struct media_entity *sink;
266 struct media_link *link, *found_link = NULL;
267 int ret = 0;
268 struct media_device *mdev = entity->graph_obj.mdev;
269 struct au0828_dev *dev;
271 if (!mdev)
272 return -ENODEV;
274 mutex_lock(&mdev->graph_mutex);
276 dev = mdev->source_priv;
279 * For Audio and V4L2 entity, find the link to which decoder
280 * is the sink. Look for an active link between decoder and
281 * source (tuner/s-video/Composite), if one exists, nothing
282 * to do. If not, look for any active links between source
283 * and any other entity. If one exists, source is busy. If
284 * source is free, setup link and start pipeline from source.
285 * For DVB FE entity, the source for the link is the tuner.
286 * Check if tuner is available and setup link and start
287 * pipeline.
289 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
290 sink = entity;
291 find_source = dev->tuner;
292 } else {
293 /* Analog isn't configured or register failed */
294 if (!dev->decoder) {
295 ret = -ENODEV;
296 goto end;
299 sink = dev->decoder;
302 * Default input is tuner and default input_type
303 * is AU0828_VMUX_TELEVISION.
304 * FIXME:
305 * There is a problem when s_input is called to
306 * change the default input. s_input will try to
307 * enable_source before attempting to change the
308 * input on the device, and will end up enabling
309 * default source which is tuner.
311 * Additional logic is necessary in au0828
312 * to detect that the input has changed and
313 * enable the right source.
316 if (dev->input_type == AU0828_VMUX_TELEVISION)
317 find_source = dev->tuner;
318 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
319 dev->input_type == AU0828_VMUX_COMPOSITE)
320 find_source = &dev->input_ent[dev->input_type];
321 else {
322 /* unknown input - let user select input */
323 ret = 0;
324 goto end;
328 /* Is an active link between sink and source */
329 if (dev->active_link) {
331 * If DVB is using the tuner and calling entity is
332 * audio/video, the following check will be false,
333 * since sink is different. Result is Busy.
335 if (dev->active_link->sink->entity == sink &&
336 dev->active_link->source->entity == find_source) {
338 * Either ALSA or Video own tuner. sink is
339 * the same for both. Prevent Video stepping
340 * on ALSA when ALSA owns the source.
342 if (dev->active_link_owner != entity &&
343 dev->active_link_owner->function ==
344 MEDIA_ENT_F_AUDIO_CAPTURE) {
345 pr_debug("ALSA has the tuner\n");
346 ret = -EBUSY;
347 goto end;
349 ret = 0;
350 goto end;
351 } else {
352 ret = -EBUSY;
353 goto end;
357 list_for_each_entry(link, &sink->links, list) {
358 /* Check sink, and source */
359 if (link->sink->entity == sink &&
360 link->source->entity == find_source) {
361 found_link = link;
362 break;
366 if (!found_link) {
367 ret = -ENODEV;
368 goto end;
371 /* activate link between source and sink and start pipeline */
372 source = found_link->source->entity;
373 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
374 if (ret) {
375 pr_err("Activate tuner link %s->%s. Error %d\n",
376 source->name, sink->name, ret);
377 goto end;
380 ret = __media_entity_pipeline_start(entity, pipe);
381 if (ret) {
382 pr_err("Start Pipeline: %s->%s Error %d\n",
383 source->name, entity->name, ret);
384 ret = __media_entity_setup_link(found_link, 0);
385 pr_err("Deactivate link Error %d\n", ret);
386 goto end;
389 * save active link and active link owner to avoid audio
390 * deactivating video owned link from disable_source and
391 * vice versa
393 dev->active_link = found_link;
394 dev->active_link_owner = entity;
395 dev->active_source = source;
396 dev->active_sink = sink;
398 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
399 dev->active_source->name, dev->active_sink->name,
400 dev->active_link_owner->name, ret);
401 end:
402 mutex_unlock(&mdev->graph_mutex);
403 pr_debug("au0828_enable_source() end %s %d %d\n",
404 entity->name, entity->function, ret);
405 return ret;
408 static void au0828_disable_source(struct media_entity *entity)
410 int ret = 0;
411 struct media_device *mdev = entity->graph_obj.mdev;
412 struct au0828_dev *dev;
414 if (!mdev)
415 return;
417 mutex_lock(&mdev->graph_mutex);
418 dev = mdev->source_priv;
420 if (!dev->active_link) {
421 ret = -ENODEV;
422 goto end;
425 /* link is active - stop pipeline from source (tuner) */
426 if (dev->active_link->sink->entity == dev->active_sink &&
427 dev->active_link->source->entity == dev->active_source) {
429 * prevent video from deactivating link when audio
430 * has active pipeline
432 if (dev->active_link_owner != entity)
433 goto end;
434 __media_entity_pipeline_stop(entity);
435 ret = __media_entity_setup_link(dev->active_link, 0);
436 if (ret)
437 pr_err("Deactivate link Error %d\n", ret);
439 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
440 dev->active_source->name, dev->active_sink->name,
441 dev->active_link_owner->name, ret);
443 dev->active_link = NULL;
444 dev->active_link_owner = NULL;
445 dev->active_source = NULL;
446 dev->active_sink = NULL;
449 end:
450 mutex_unlock(&mdev->graph_mutex);
452 #endif
454 static int au0828_media_device_register(struct au0828_dev *dev,
455 struct usb_device *udev)
457 #ifdef CONFIG_MEDIA_CONTROLLER
458 int ret;
459 struct media_entity *entity, *demod = NULL, *tuner = NULL;
461 if (!dev->media_dev)
462 return 0;
464 if (!media_devnode_is_registered(&dev->media_dev->devnode)) {
466 /* register media device */
467 ret = media_device_register(dev->media_dev);
468 if (ret) {
469 dev_err(&udev->dev,
470 "Media Device Register Error: %d\n", ret);
471 return ret;
473 } else {
475 * Call au0828_media_graph_notify() to connect
476 * audio graph to our graph. In this case, audio
477 * driver registered the device and there is no
478 * entity_notify to be called when new entities
479 * are added. Invoke it now.
481 au0828_media_graph_notify(NULL, (void *) dev);
485 * Find tuner and demod to disable the link between
486 * the two to avoid disable step when tuner is requested
487 * by video or audio. Note that this step can't be done
488 * until dvb graph is created during dvb register.
490 media_device_for_each_entity(entity, dev->media_dev) {
491 if (entity->function == MEDIA_ENT_F_DTV_DEMOD)
492 demod = entity;
493 else if (entity->function == MEDIA_ENT_F_TUNER)
494 tuner = entity;
496 /* Disable link between tuner and demod */
497 if (tuner && demod) {
498 struct media_link *link;
500 list_for_each_entry(link, &demod->links, list) {
501 if (link->sink->entity == demod &&
502 link->source->entity == tuner) {
503 media_entity_setup_link(link, 0);
508 /* register entity_notify callback */
509 dev->entity_notify.notify_data = (void *) dev;
510 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
511 ret = media_device_register_entity_notify(dev->media_dev,
512 &dev->entity_notify);
513 if (ret) {
514 dev_err(&udev->dev,
515 "Media Device register entity_notify Error: %d\n",
516 ret);
517 return ret;
519 /* set enable_source */
520 dev->media_dev->source_priv = (void *) dev;
521 dev->media_dev->enable_source = au0828_enable_source;
522 dev->media_dev->disable_source = au0828_disable_source;
523 #endif
524 return 0;
527 static int au0828_usb_probe(struct usb_interface *interface,
528 const struct usb_device_id *id)
530 int ifnum;
531 int retval = 0;
533 struct au0828_dev *dev;
534 struct usb_device *usbdev = interface_to_usbdev(interface);
536 ifnum = interface->altsetting->desc.bInterfaceNumber;
538 if (ifnum != 0)
539 return -ENODEV;
541 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
542 le16_to_cpu(usbdev->descriptor.idVendor),
543 le16_to_cpu(usbdev->descriptor.idProduct),
544 ifnum);
547 * Make sure we have 480 Mbps of bandwidth, otherwise things like
548 * video stream wouldn't likely work, since 12 Mbps is generally
549 * not enough even for most Digital TV streams.
551 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
552 pr_err("au0828: Device initialization failed.\n");
553 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
554 return -ENODEV;
557 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
558 if (dev == NULL) {
559 pr_err("%s() Unable to allocate memory\n", __func__);
560 return -ENOMEM;
563 mutex_init(&dev->lock);
564 mutex_lock(&dev->lock);
565 mutex_init(&dev->mutex);
566 mutex_init(&dev->dvb.lock);
567 dev->usbdev = usbdev;
568 dev->boardnr = id->driver_info;
569 dev->board = au0828_boards[dev->boardnr];
571 /* Initialize the media controller */
572 retval = au0828_media_device_init(dev, usbdev);
573 if (retval) {
574 pr_err("%s() au0828_media_device_init failed\n",
575 __func__);
576 mutex_unlock(&dev->lock);
577 kfree(dev);
578 return retval;
581 retval = au0828_v4l2_device_register(interface, dev);
582 if (retval) {
583 au0828_usb_v4l2_media_release(dev);
584 mutex_unlock(&dev->lock);
585 kfree(dev);
586 return retval;
589 /* Power Up the bridge */
590 au0828_write(dev, REG_600, 1 << 4);
592 /* Bring up the GPIO's and supporting devices */
593 au0828_gpio_setup(dev);
595 /* I2C */
596 au0828_i2c_register(dev);
598 /* Setup */
599 au0828_card_setup(dev);
601 /* Analog TV */
602 retval = au0828_analog_register(dev, interface);
603 if (retval) {
604 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
605 __func__);
606 goto done;
609 /* Digital TV */
610 retval = au0828_dvb_register(dev);
611 if (retval)
612 pr_err("%s() au0282_dev_register failed\n",
613 __func__);
615 /* Remote controller */
616 au0828_rc_register(dev);
619 * Store the pointer to the au0828_dev so it can be accessed in
620 * au0828_usb_disconnect
622 usb_set_intfdata(interface, dev);
624 pr_info("Registered device AU0828 [%s]\n",
625 dev->board.name == NULL ? "Unset" : dev->board.name);
627 mutex_unlock(&dev->lock);
629 retval = au0828_media_device_register(dev, usbdev);
631 done:
632 if (retval < 0)
633 au0828_usb_disconnect(interface);
635 return retval;
638 static int au0828_suspend(struct usb_interface *interface,
639 pm_message_t message)
641 struct au0828_dev *dev = usb_get_intfdata(interface);
643 if (!dev)
644 return 0;
646 pr_info("Suspend\n");
648 au0828_rc_suspend(dev);
649 au0828_v4l2_suspend(dev);
650 au0828_dvb_suspend(dev);
652 /* FIXME: should suspend also ATV/DTV */
654 return 0;
657 static int au0828_resume(struct usb_interface *interface)
659 struct au0828_dev *dev = usb_get_intfdata(interface);
660 if (!dev)
661 return 0;
663 pr_info("Resume\n");
665 /* Power Up the bridge */
666 au0828_write(dev, REG_600, 1 << 4);
668 /* Bring up the GPIO's and supporting devices */
669 au0828_gpio_setup(dev);
671 au0828_rc_resume(dev);
672 au0828_v4l2_resume(dev);
673 au0828_dvb_resume(dev);
675 /* FIXME: should resume also ATV/DTV */
677 return 0;
680 static struct usb_driver au0828_usb_driver = {
681 .name = KBUILD_MODNAME,
682 .probe = au0828_usb_probe,
683 .disconnect = au0828_usb_disconnect,
684 .id_table = au0828_usb_id_table,
685 .suspend = au0828_suspend,
686 .resume = au0828_resume,
687 .reset_resume = au0828_resume,
690 static int __init au0828_init(void)
692 int ret;
694 if (au0828_debug & 1)
695 pr_info("%s() Debugging is enabled\n", __func__);
697 if (au0828_debug & 2)
698 pr_info("%s() USB Debugging is enabled\n", __func__);
700 if (au0828_debug & 4)
701 pr_info("%s() I2C Debugging is enabled\n", __func__);
703 if (au0828_debug & 8)
704 pr_info("%s() Bridge Debugging is enabled\n",
705 __func__);
707 if (au0828_debug & 16)
708 pr_info("%s() IR Debugging is enabled\n",
709 __func__);
711 pr_info("au0828 driver loaded\n");
713 ret = usb_register(&au0828_usb_driver);
714 if (ret)
715 pr_err("usb_register failed, error = %d\n", ret);
717 return ret;
720 static void __exit au0828_exit(void)
722 usb_deregister(&au0828_usb_driver);
725 module_init(au0828_init);
726 module_exit(au0828_exit);
728 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
729 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
730 MODULE_LICENSE("GPL");
731 MODULE_VERSION("0.0.3");