4ffaa3b2e90573676e21753b80056bcb7668ef44
[cascardo/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                 media_device_unregister(dev->media_dev);
141                 media_device_cleanup(dev->media_dev);
142                 dev->media_dev = NULL;
143         }
144 #endif
145 }
146
147 void au0828_usb_release(struct au0828_dev *dev)
148 {
149         au0828_unregister_media_device(dev);
150
151         /* I2C */
152         au0828_i2c_unregister(dev);
153
154         kfree(dev);
155 }
156
157 static void au0828_usb_disconnect(struct usb_interface *interface)
158 {
159         struct au0828_dev *dev = usb_get_intfdata(interface);
160
161         dprintk(1, "%s()\n", __func__);
162
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.
168         */
169         dev->dev_state = DEV_DISCONNECTED;
170
171         au0828_rc_unregister(dev);
172         /* Digital TV */
173         au0828_dvb_unregister(dev);
174
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)) {
180                 /*
181                  * No need to call au0828_usb_release() if V4L2 is enabled,
182                  * as this is already called via au0828_usb_v4l2_release()
183                  */
184                 return;
185         }
186         au0828_usb_release(dev);
187 }
188
189 static int au0828_media_device_init(struct au0828_dev *dev,
190                                     struct usb_device *udev)
191 {
192 #ifdef CONFIG_MEDIA_CONTROLLER
193         struct media_device *mdev;
194
195         mdev = media_device_get_devres(&udev->dev);
196         if (!mdev)
197                 return -ENOMEM;
198
199         /* check if media device is already initialized */
200         if (!mdev->dev)
201                 media_device_usb_init(mdev, udev, udev->product);
202
203         dev->media_dev = mdev;
204 #endif
205         return 0;
206 }
207
208 static void au0828_media_graph_notify(struct media_entity *new,
209                                       void *notify_data)
210 {
211 #ifdef CONFIG_MEDIA_CONTROLLER
212         struct au0828_dev *dev = (struct au0828_dev *) notify_data;
213         int ret;
214         struct media_entity *entity, *mixer = NULL, *decoder = NULL;
215
216         if (!new) {
217                 /*
218                  * Called during au0828 probe time to connect
219                  * entites that were created prior to registering
220                  * the notify handler. Find mixer and decoder.
221                 */
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;
227                 }
228                 goto create_link;
229         }
230
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;
242                 }
243                 decoder = new;
244                 break;
245         default:
246                 break;
247         }
248
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);
258         }
259 #endif
260 }
261
262 static int au0828_enable_source(struct media_entity *entity,
263                                 struct media_pipeline *pipe)
264 {
265 #ifdef CONFIG_MEDIA_CONTROLLER
266         struct media_entity  *source, *find_source;
267         struct media_entity *sink;
268         struct media_link *link, *found_link = NULL;
269         int ret = 0;
270         struct media_device *mdev = entity->graph_obj.mdev;
271         struct au0828_dev *dev;
272
273         if (!mdev)
274                 return -ENODEV;
275
276         mutex_lock(&mdev->graph_mutex);
277
278         dev = mdev->source_priv;
279
280         /*
281          * For Audio and V4L2 entity, find the link to which decoder
282          * is the sink. Look for an active link between decoder and
283          * source (tuner/s-video/Composite), if one exists, nothing
284          * to do. If not, look for any  active links between source
285          * and any other entity. If one exists, source is busy. If
286          * source is free, setup link and start pipeline from source.
287          * For DVB FE entity, the source for the link is the tuner.
288          * Check if tuner is available and setup link and start
289          * pipeline.
290         */
291         if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
292                 sink = entity;
293                 find_source = dev->tuner;
294         } else {
295                 /* Analog isn't configured or register failed */
296                 if (!dev->decoder) {
297                         ret = -ENODEV;
298                         goto end;
299                 }
300
301                 sink = dev->decoder;
302
303                 /*
304                  * Default input is tuner and default input_type
305                  * is AU0828_VMUX_TELEVISION.
306                  * FIXME:
307                  * There is a problem when s_input is called to
308                  * change the default input. s_input will try to
309                  * enable_source before attempting to change the
310                  * input on the device, and will end up enabling
311                  * default source which is tuner.
312                  *
313                  * Additional logic is necessary in au0828
314                  * to detect that the input has changed and
315                  * enable the right source.
316                 */
317
318                 if (dev->input_type == AU0828_VMUX_TELEVISION)
319                         find_source = dev->tuner;
320                 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
321                          dev->input_type == AU0828_VMUX_COMPOSITE)
322                         find_source = &dev->input_ent[dev->input_type];
323                 else {
324                         /* unknown input - let user select input */
325                         ret = 0;
326                         goto end;
327                 }
328         }
329
330         /* Is an active link between sink and source */
331         if (dev->active_link) {
332                 /*
333                  * If DVB is using the tuner and calling entity is
334                  * audio/video, the following check will be false,
335                  * since sink is different. Result is Busy.
336                  */
337                 if (dev->active_link->sink->entity == sink &&
338                     dev->active_link->source->entity == find_source) {
339                         /*
340                          * Either ALSA or Video own tuner. sink is
341                          * the same for both. Prevent Video stepping
342                          * on ALSA when ALSA owns the source.
343                         */
344                         if (dev->active_link_owner != entity &&
345                             dev->active_link_owner->function ==
346                                                 MEDIA_ENT_F_AUDIO_CAPTURE) {
347                                 pr_debug("ALSA has the tuner\n");
348                                 ret = -EBUSY;
349                                 goto end;
350                         }
351                         ret = 0;
352                         goto end;
353                 } else {
354                         ret = -EBUSY;
355                         goto end;
356                 }
357         }
358
359         list_for_each_entry(link, &sink->links, list) {
360                 /* Check sink, and source */
361                 if (link->sink->entity == sink &&
362                     link->source->entity == find_source) {
363                         found_link = link;
364                         break;
365                 }
366         }
367
368         if (!found_link) {
369                 ret = -ENODEV;
370                 goto end;
371         }
372
373         /* activate link between source and sink and start pipeline */
374         source = found_link->source->entity;
375         ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
376         if (ret) {
377                 pr_err("Activate tuner link %s->%s. Error %d\n",
378                         source->name, sink->name, ret);
379                 goto end;
380         }
381
382         ret = __media_entity_pipeline_start(entity, pipe);
383         if (ret) {
384                 pr_err("Start Pipeline: %s->%s Error %d\n",
385                         source->name, entity->name, ret);
386                 ret = __media_entity_setup_link(found_link, 0);
387                 pr_err("Deactivate link Error %d\n", ret);
388                 goto end;
389         }
390         /*
391          * save active link and active link owner to avoid audio
392          * deactivating video owned link from disable_source and
393          * vice versa
394         */
395         dev->active_link = found_link;
396         dev->active_link_owner = entity;
397         dev->active_source = source;
398         dev->active_sink = sink;
399
400         pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
401                  dev->active_source->name, dev->active_sink->name,
402                  dev->active_link_owner->name, ret);
403 end:
404         mutex_unlock(&mdev->graph_mutex);
405         pr_debug("au0828_enable_source() end %s %d %d\n",
406                  entity->name, entity->function, ret);
407         return ret;
408 #endif
409         return 0;
410 }
411
412 static void au0828_disable_source(struct media_entity *entity)
413 {
414 #ifdef CONFIG_MEDIA_CONTROLLER
415         int ret = 0;
416         struct media_device *mdev = entity->graph_obj.mdev;
417         struct au0828_dev *dev;
418
419         if (!mdev)
420                 return;
421
422         mutex_lock(&mdev->graph_mutex);
423         dev = mdev->source_priv;
424
425         if (!dev->active_link) {
426                 ret = -ENODEV;
427                 goto end;
428         }
429
430         /* link is active - stop pipeline from source (tuner) */
431         if (dev->active_link->sink->entity == dev->active_sink &&
432             dev->active_link->source->entity == dev->active_source) {
433                 /*
434                  * prevent video from deactivating link when audio
435                  * has active pipeline
436                 */
437                 if (dev->active_link_owner != entity)
438                         goto end;
439                 __media_entity_pipeline_stop(entity);
440                 ret = __media_entity_setup_link(dev->active_link, 0);
441                 if (ret)
442                         pr_err("Deactivate link Error %d\n", ret);
443
444                 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
445                          dev->active_source->name, dev->active_sink->name,
446                          dev->active_link_owner->name, ret);
447
448                 dev->active_link = NULL;
449                 dev->active_link_owner = NULL;
450                 dev->active_source = NULL;
451                 dev->active_sink = NULL;
452         }
453
454 end:
455         mutex_unlock(&mdev->graph_mutex);
456 #endif
457 }
458
459 static int au0828_media_device_register(struct au0828_dev *dev,
460                                         struct usb_device *udev)
461 {
462 #ifdef CONFIG_MEDIA_CONTROLLER
463         int ret;
464
465         if (!dev->media_dev)
466                 return 0;
467
468         if (!media_devnode_is_registered(&dev->media_dev->devnode)) {
469
470                 /* register media device */
471                 ret = media_device_register(dev->media_dev);
472                 if (ret) {
473                         dev_err(&udev->dev,
474                                 "Media Device Register Error: %d\n", ret);
475                         return ret;
476                 }
477         } else {
478                 /*
479                  * Call au0828_media_graph_notify() to connect
480                  * audio graph to our graph. In this case, audio
481                  * driver registered the device and there is no
482                  * entity_notify to be called when new entities
483                  * are added. Invoke it now.
484                 */
485                 au0828_media_graph_notify(NULL, (void *) dev);
486         }
487         /* register entity_notify callback */
488         dev->entity_notify.notify_data = (void *) dev;
489         dev->entity_notify.notify = (void *) au0828_media_graph_notify;
490         ret = media_device_register_entity_notify(dev->media_dev,
491                                                   &dev->entity_notify);
492         if (ret) {
493                 dev_err(&udev->dev,
494                         "Media Device register entity_notify Error: %d\n",
495                         ret);
496                 return ret;
497         }
498         /* set enable_source */
499         dev->media_dev->source_priv = (void *) dev;
500         dev->media_dev->enable_source = au0828_enable_source;
501         dev->media_dev->disable_source = au0828_disable_source;
502 #endif
503         return 0;
504 }
505
506 static int au0828_usb_probe(struct usb_interface *interface,
507         const struct usb_device_id *id)
508 {
509         int ifnum;
510         int retval = 0;
511
512         struct au0828_dev *dev;
513         struct usb_device *usbdev = interface_to_usbdev(interface);
514
515         ifnum = interface->altsetting->desc.bInterfaceNumber;
516
517         if (ifnum != 0)
518                 return -ENODEV;
519
520         dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
521                 le16_to_cpu(usbdev->descriptor.idVendor),
522                 le16_to_cpu(usbdev->descriptor.idProduct),
523                 ifnum);
524
525         /*
526          * Make sure we have 480 Mbps of bandwidth, otherwise things like
527          * video stream wouldn't likely work, since 12 Mbps is generally
528          * not enough even for most Digital TV streams.
529          */
530         if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
531                 pr_err("au0828: Device initialization failed.\n");
532                 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
533                 return -ENODEV;
534         }
535
536         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
537         if (dev == NULL) {
538                 pr_err("%s() Unable to allocate memory\n", __func__);
539                 return -ENOMEM;
540         }
541
542         mutex_init(&dev->lock);
543         mutex_lock(&dev->lock);
544         mutex_init(&dev->mutex);
545         mutex_init(&dev->dvb.lock);
546         dev->usbdev = usbdev;
547         dev->boardnr = id->driver_info;
548         dev->board = au0828_boards[dev->boardnr];
549
550         /* Initialize the media controller */
551         retval = au0828_media_device_init(dev, usbdev);
552         if (retval) {
553                 pr_err("%s() au0828_media_device_init failed\n",
554                        __func__);
555                 mutex_unlock(&dev->lock);
556                 kfree(dev);
557                 return retval;
558         }
559
560         retval = au0828_v4l2_device_register(interface, dev);
561         if (retval) {
562                 au0828_usb_v4l2_media_release(dev);
563                 mutex_unlock(&dev->lock);
564                 kfree(dev);
565                 return retval;
566         }
567
568         /* Power Up the bridge */
569         au0828_write(dev, REG_600, 1 << 4);
570
571         /* Bring up the GPIO's and supporting devices */
572         au0828_gpio_setup(dev);
573
574         /* I2C */
575         au0828_i2c_register(dev);
576
577         /* Setup */
578         au0828_card_setup(dev);
579
580         /* Analog TV */
581         retval = au0828_analog_register(dev, interface);
582         if (retval) {
583                 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
584                         __func__);
585                 goto done;
586         }
587
588         /* Digital TV */
589         retval = au0828_dvb_register(dev);
590         if (retval)
591                 pr_err("%s() au0282_dev_register failed\n",
592                        __func__);
593
594         /* Remote controller */
595         au0828_rc_register(dev);
596
597         /*
598          * Store the pointer to the au0828_dev so it can be accessed in
599          * au0828_usb_disconnect
600          */
601         usb_set_intfdata(interface, dev);
602
603         pr_info("Registered device AU0828 [%s]\n",
604                 dev->board.name == NULL ? "Unset" : dev->board.name);
605
606         mutex_unlock(&dev->lock);
607
608         retval = au0828_media_device_register(dev, usbdev);
609
610 done:
611         if (retval < 0)
612                 au0828_usb_disconnect(interface);
613
614         return retval;
615 }
616
617 static int au0828_suspend(struct usb_interface *interface,
618                                 pm_message_t message)
619 {
620         struct au0828_dev *dev = usb_get_intfdata(interface);
621
622         if (!dev)
623                 return 0;
624
625         pr_info("Suspend\n");
626
627         au0828_rc_suspend(dev);
628         au0828_v4l2_suspend(dev);
629         au0828_dvb_suspend(dev);
630
631         /* FIXME: should suspend also ATV/DTV */
632
633         return 0;
634 }
635
636 static int au0828_resume(struct usb_interface *interface)
637 {
638         struct au0828_dev *dev = usb_get_intfdata(interface);
639         if (!dev)
640                 return 0;
641
642         pr_info("Resume\n");
643
644         /* Power Up the bridge */
645         au0828_write(dev, REG_600, 1 << 4);
646
647         /* Bring up the GPIO's and supporting devices */
648         au0828_gpio_setup(dev);
649
650         au0828_rc_resume(dev);
651         au0828_v4l2_resume(dev);
652         au0828_dvb_resume(dev);
653
654         /* FIXME: should resume also ATV/DTV */
655
656         return 0;
657 }
658
659 static struct usb_driver au0828_usb_driver = {
660         .name           = KBUILD_MODNAME,
661         .probe          = au0828_usb_probe,
662         .disconnect     = au0828_usb_disconnect,
663         .id_table       = au0828_usb_id_table,
664         .suspend        = au0828_suspend,
665         .resume         = au0828_resume,
666         .reset_resume   = au0828_resume,
667 };
668
669 static int __init au0828_init(void)
670 {
671         int ret;
672
673         if (au0828_debug & 1)
674                 pr_info("%s() Debugging is enabled\n", __func__);
675
676         if (au0828_debug & 2)
677                 pr_info("%s() USB Debugging is enabled\n", __func__);
678
679         if (au0828_debug & 4)
680                 pr_info("%s() I2C Debugging is enabled\n", __func__);
681
682         if (au0828_debug & 8)
683                 pr_info("%s() Bridge Debugging is enabled\n",
684                        __func__);
685
686         if (au0828_debug & 16)
687                 pr_info("%s() IR Debugging is enabled\n",
688                        __func__);
689
690         pr_info("au0828 driver loaded\n");
691
692         ret = usb_register(&au0828_usb_driver);
693         if (ret)
694                 pr_err("usb_register failed, error = %d\n", ret);
695
696         return ret;
697 }
698
699 static void __exit au0828_exit(void)
700 {
701         usb_deregister(&au0828_usb_driver);
702 }
703
704 module_init(au0828_init);
705 module_exit(au0828_exit);
706
707 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
708 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
709 MODULE_LICENSE("GPL");
710 MODULE_VERSION("0.0.3");