Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[sfrench/cifs-2.6.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
18 #include "au0828.h"
19 #include "au8522.h"
20
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-common.h>
25 #include <linux/mutex.h>
26
27 /* Due to enum tuner_pad_index */
28 #include <media/tuner.h>
29
30 /*
31  * 1 = General debug messages
32  * 2 = USB handling
33  * 4 = I2C related
34  * 8 = Bridge related
35  * 16 = IR related
36  */
37 int au0828_debug;
38 module_param_named(debug, au0828_debug, int, 0644);
39 MODULE_PARM_DESC(debug,
40                  "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
41
42 static unsigned int disable_usb_speed_check;
43 module_param(disable_usb_speed_check, int, 0444);
44 MODULE_PARM_DESC(disable_usb_speed_check,
45                  "override min bandwidth requirement of 480M bps");
46
47 #define _AU0828_BULKPIPE 0x03
48 #define _BULKPIPESIZE 0xffff
49
50 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
51                             u16 index);
52 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
53         u16 index, unsigned char *cp, u16 size);
54
55 /* USB Direction */
56 #define CMD_REQUEST_IN          0x00
57 #define CMD_REQUEST_OUT         0x01
58
59 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
60 {
61         u8 result = 0;
62
63         recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
64         dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
65
66         return result;
67 }
68
69 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
70 {
71         dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
72         return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
73 }
74
75 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
76         u16 index)
77 {
78         int status = -ENODEV;
79
80         if (dev->usbdev) {
81
82                 /* cp must be memory that has been allocated by kmalloc */
83                 status = usb_control_msg(dev->usbdev,
84                                 usb_sndctrlpipe(dev->usbdev, 0),
85                                 request,
86                                 USB_DIR_OUT | USB_TYPE_VENDOR |
87                                         USB_RECIP_DEVICE,
88                                 value, index, NULL, 0, 1000);
89
90                 status = min(status, 0);
91
92                 if (status < 0) {
93                         pr_err("%s() Failed sending control message, error %d.\n",
94                                 __func__, status);
95                 }
96
97         }
98
99         return status;
100 }
101
102 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
103         u16 index, unsigned char *cp, u16 size)
104 {
105         int status = -ENODEV;
106         mutex_lock(&dev->mutex);
107         if (dev->usbdev) {
108                 status = usb_control_msg(dev->usbdev,
109                                 usb_rcvctrlpipe(dev->usbdev, 0),
110                                 request,
111                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
112                                 value, index,
113                                 dev->ctrlmsg, size, 1000);
114
115                 status = min(status, 0);
116
117                 if (status < 0) {
118                         pr_err("%s() Failed receiving control message, error %d.\n",
119                                 __func__, status);
120                 }
121
122                 /* the host controller requires heap allocated memory, which
123                    is why we didn't just pass "cp" into usb_control_msg */
124                 memcpy(cp, dev->ctrlmsg, size);
125         }
126         mutex_unlock(&dev->mutex);
127         return status;
128 }
129
130 #ifdef CONFIG_MEDIA_CONTROLLER
131 static void au0828_media_graph_notify(struct media_entity *new,
132                                       void *notify_data);
133 #endif
134
135 static void au0828_unregister_media_device(struct au0828_dev *dev)
136 {
137 #ifdef CONFIG_MEDIA_CONTROLLER
138         struct media_device *mdev = dev->media_dev;
139         struct media_entity_notify *notify, *nextp;
140
141         if (!mdev || !media_devnode_is_registered(mdev->devnode))
142                 return;
143
144         /* Remove au0828 entity_notify callbacks */
145         list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) {
146                 if (notify->notify != au0828_media_graph_notify)
147                         continue;
148                 media_device_unregister_entity_notify(mdev, notify);
149         }
150
151         /* clear enable_source, disable_source */
152         mutex_lock(&mdev->graph_mutex);
153         dev->media_dev->source_priv = NULL;
154         dev->media_dev->enable_source = NULL;
155         dev->media_dev->disable_source = NULL;
156         mutex_unlock(&mdev->graph_mutex);
157
158         media_device_delete(dev->media_dev, KBUILD_MODNAME, THIS_MODULE);
159         dev->media_dev = NULL;
160 #endif
161 }
162
163 void au0828_usb_release(struct au0828_dev *dev)
164 {
165         au0828_unregister_media_device(dev);
166
167         /* I2C */
168         au0828_i2c_unregister(dev);
169
170         kfree(dev);
171 }
172
173 static void au0828_usb_disconnect(struct usb_interface *interface)
174 {
175         struct au0828_dev *dev = usb_get_intfdata(interface);
176
177         dprintk(1, "%s()\n", __func__);
178
179         /* there is a small window after disconnect, before
180            dev->usbdev is NULL, for poll (e.g: IR) try to access
181            the device and fill the dmesg with error messages.
182            Set the status so poll routines can check and avoid
183            access after disconnect.
184         */
185         set_bit(DEV_DISCONNECTED, &dev->dev_state);
186
187         au0828_rc_unregister(dev);
188         /* Digital TV */
189         au0828_dvb_unregister(dev);
190
191         usb_set_intfdata(interface, NULL);
192         mutex_lock(&dev->mutex);
193         dev->usbdev = NULL;
194         mutex_unlock(&dev->mutex);
195         if (au0828_analog_unregister(dev)) {
196                 /*
197                  * No need to call au0828_usb_release() if V4L2 is enabled,
198                  * as this is already called via au0828_usb_v4l2_release()
199                  */
200                 return;
201         }
202         au0828_usb_release(dev);
203 }
204
205 static int au0828_media_device_init(struct au0828_dev *dev,
206                                     struct usb_device *udev)
207 {
208 #ifdef CONFIG_MEDIA_CONTROLLER
209         struct media_device *mdev;
210
211         mdev = media_device_usb_allocate(udev, KBUILD_MODNAME, THIS_MODULE);
212         if (!mdev)
213                 return -ENOMEM;
214
215         dev->media_dev = mdev;
216 #endif
217         return 0;
218 }
219
220 #ifdef CONFIG_MEDIA_CONTROLLER
221 static void au0828_media_graph_notify(struct media_entity *new,
222                                       void *notify_data)
223 {
224         struct au0828_dev *dev = (struct au0828_dev *) notify_data;
225         int ret;
226         struct media_entity *entity, *mixer = NULL, *decoder = NULL;
227
228         if (!new) {
229                 /*
230                  * Called during au0828 probe time to connect
231                  * entities that were created prior to registering
232                  * the notify handler. Find mixer and decoder.
233                 */
234                 media_device_for_each_entity(entity, dev->media_dev) {
235                         if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
236                                 mixer = entity;
237                         else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
238                                 decoder = entity;
239                 }
240                 goto create_link;
241         }
242
243         switch (new->function) {
244         case MEDIA_ENT_F_AUDIO_MIXER:
245                 mixer = new;
246                 if (dev->decoder)
247                         decoder = dev->decoder;
248                 break;
249         case MEDIA_ENT_F_ATV_DECODER:
250                 /* In case, Mixer is added first, find mixer and create link */
251                 media_device_for_each_entity(entity, dev->media_dev) {
252                         if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
253                                 mixer = entity;
254                 }
255                 decoder = new;
256                 break;
257         default:
258                 break;
259         }
260
261 create_link:
262         if (decoder && mixer) {
263                 ret = media_get_pad_index(decoder, false,
264                                           PAD_SIGNAL_AUDIO);
265                 if (ret >= 0)
266                         ret = media_create_pad_link(decoder, ret,
267                                                     mixer, 0,
268                                                     MEDIA_LNK_FL_ENABLED);
269                 if (ret < 0)
270                         dev_err(&dev->usbdev->dev,
271                                 "Mixer Pad Link Create Error: %d\n", ret);
272         }
273 }
274
275 static bool au0828_is_link_shareable(struct media_entity *owner,
276                                      struct media_entity *entity)
277 {
278         bool shareable = false;
279
280         /* Tuner link can be shared by audio, video, and VBI */
281         switch (owner->function) {
282         case MEDIA_ENT_F_IO_V4L:
283         case MEDIA_ENT_F_AUDIO_CAPTURE:
284         case MEDIA_ENT_F_IO_VBI:
285                 if (entity->function == MEDIA_ENT_F_IO_V4L ||
286                     entity->function == MEDIA_ENT_F_AUDIO_CAPTURE ||
287                     entity->function == MEDIA_ENT_F_IO_VBI)
288                         shareable = true;
289                 break;
290         case MEDIA_ENT_F_DTV_DEMOD:
291         default:
292                 break;
293         }
294         return shareable;
295 }
296
297 /* Callers should hold graph_mutex */
298 static int au0828_enable_source(struct media_entity *entity,
299                                 struct media_pipeline *pipe)
300 {
301         struct media_entity  *source, *find_source;
302         struct media_entity *sink;
303         struct media_link *link, *found_link = NULL;
304         int ret = 0;
305         struct media_device *mdev = entity->graph_obj.mdev;
306         struct au0828_dev *dev;
307
308         if (!mdev)
309                 return -ENODEV;
310
311         dev = mdev->source_priv;
312
313         /*
314          * For Audio and V4L2 entity, find the link to which decoder
315          * is the sink. Look for an active link between decoder and
316          * source (tuner/s-video/Composite), if one exists, nothing
317          * to do. If not, look for any  active links between source
318          * and any other entity. If one exists, source is busy. If
319          * source is free, setup link and start pipeline from source.
320          * For DVB FE entity, the source for the link is the tuner.
321          * Check if tuner is available and setup link and start
322          * pipeline.
323         */
324         if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
325                 sink = entity;
326                 find_source = dev->tuner;
327         } else {
328                 /* Analog isn't configured or register failed */
329                 if (!dev->decoder) {
330                         ret = -ENODEV;
331                         goto end;
332                 }
333
334                 sink = dev->decoder;
335
336                 /*
337                  * Default input is tuner and default input_type
338                  * is AU0828_VMUX_TELEVISION.
339                  *
340                  * There is a problem when s_input is called to
341                  * change the default input. s_input will try to
342                  * enable_source before attempting to change the
343                  * input on the device, and will end up enabling
344                  * default source which is tuner.
345                  *
346                  * Additional logic is necessary in au0828 to detect
347                  * that the input has changed and enable the right
348                  * source. au0828 handles this case in its s_input.
349                  * It will disable the old source and enable the new
350                  * source.
351                  *
352                 */
353                 if (dev->input_type == AU0828_VMUX_TELEVISION)
354                         find_source = dev->tuner;
355                 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
356                          dev->input_type == AU0828_VMUX_COMPOSITE)
357                         find_source = &dev->input_ent[dev->input_type];
358                 else {
359                         /* unknown input - let user select input */
360                         ret = 0;
361                         goto end;
362                 }
363         }
364
365         /* Is there an active link between sink and source */
366         if (dev->active_link) {
367                 if (dev->active_link_owner == entity) {
368                         /* This check is necessary to handle multiple
369                          * enable_source calls from v4l_ioctls during
370                          * the course of video/vbi application run-time.
371                         */
372                         pr_debug("%s already owns the tuner\n", entity->name);
373                         ret = 0;
374                         goto end;
375                 } else if (au0828_is_link_shareable(dev->active_link_owner,
376                            entity)) {
377                         /* Either ALSA or Video own tuner. Sink is the same
378                          * for both. Allow sharing the active link between
379                          * their common source (tuner) and sink (decoder).
380                          * Starting pipeline between sharing entity and sink
381                          * will fail with pipe mismatch, while owner has an
382                          * active pipeline. Switch pipeline ownership from
383                          * user to owner when owner disables the source.
384                          */
385                         dev->active_link_shared = true;
386                         /* save the user info to use from disable */
387                         dev->active_link_user = entity;
388                         dev->active_link_user_pipe = pipe;
389                         pr_debug("%s owns the tuner %s can share!\n",
390                                  dev->active_link_owner->name,
391                                  entity->name);
392                         ret = 0;
393                         goto end;
394                 } else {
395                         ret = -EBUSY;
396                         goto end;
397                 }
398         }
399
400         list_for_each_entry(link, &sink->links, list) {
401                 /* Check sink, and source */
402                 if (link->sink->entity == sink &&
403                     link->source->entity == find_source) {
404                         found_link = link;
405                         break;
406                 }
407         }
408
409         if (!found_link) {
410                 ret = -ENODEV;
411                 goto end;
412         }
413
414         /* activate link between source and sink and start pipeline */
415         source = found_link->source->entity;
416         ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
417         if (ret) {
418                 pr_err("Activate link from %s->%s. Error %d\n",
419                         source->name, sink->name, ret);
420                 goto end;
421         }
422
423         ret = __media_pipeline_start(entity, pipe);
424         if (ret) {
425                 pr_err("Start Pipeline: %s->%s Error %d\n",
426                         source->name, entity->name, ret);
427                 ret = __media_entity_setup_link(found_link, 0);
428                 if (ret)
429                         pr_err("Deactivate link Error %d\n", ret);
430                 goto end;
431         }
432
433         /* save link state to allow audio and video share the link
434          * and not disable the link while the other is using it.
435          * active_link_owner is used to deactivate the link.
436         */
437         dev->active_link = found_link;
438         dev->active_link_owner = entity;
439         dev->active_source = source;
440         dev->active_sink = sink;
441
442         pr_info("Enabled Source: %s->%s->%s Ret %d\n",
443                  dev->active_source->name, dev->active_sink->name,
444                  dev->active_link_owner->name, ret);
445 end:
446         pr_debug("%s end: ent:%s fnc:%d ret %d\n",
447                  __func__, entity->name, entity->function, ret);
448         return ret;
449 }
450
451 /* Callers should hold graph_mutex */
452 static void au0828_disable_source(struct media_entity *entity)
453 {
454         int ret = 0;
455         struct media_device *mdev = entity->graph_obj.mdev;
456         struct au0828_dev *dev;
457
458         if (!mdev)
459                 return;
460
461         dev = mdev->source_priv;
462
463         if (!dev->active_link)
464                 return;
465
466         /* link is active - stop pipeline from source
467          * (tuner/s-video/Composite) to the entity
468          * When DVB/s-video/Composite owns tuner, it won't be in
469          * shared state.
470          */
471         if (dev->active_link->sink->entity == dev->active_sink &&
472             dev->active_link->source->entity == dev->active_source) {
473                 /*
474                  * Prevent video from deactivating link when audio
475                  * has active pipeline and vice versa. In addition
476                  * handle the case when more than one video/vbi
477                  * application is sharing the link.
478                 */
479                 bool owner_is_audio = false;
480
481                 if (dev->active_link_owner->function ==
482                     MEDIA_ENT_F_AUDIO_CAPTURE)
483                         owner_is_audio = true;
484
485                 if (dev->active_link_shared) {
486                         pr_debug("Shared link owner %s user %s %d\n",
487                                  dev->active_link_owner->name,
488                                  entity->name, dev->users);
489
490                         /* Handle video device users > 1
491                          * When audio owns the shared link with
492                          * more than one video users, avoid
493                          * disabling the source and/or switching
494                          * the owner until the last disable_source
495                          * call from video _close(). Use dev->users to
496                          * determine when to switch/disable.
497                          */
498                         if (dev->active_link_owner != entity) {
499                                 /* video device has users > 1 */
500                                 if (owner_is_audio && dev->users > 1)
501                                         return;
502
503                                 dev->active_link_user = NULL;
504                                 dev->active_link_user_pipe = NULL;
505                                 dev->active_link_shared = false;
506                                 return;
507                         }
508
509                         /* video owns the link and has users > 1 */
510                         if (!owner_is_audio && dev->users > 1)
511                                 return;
512
513                         /* stop pipeline */
514                         __media_pipeline_stop(dev->active_link_owner);
515                         pr_debug("Pipeline stop for %s\n",
516                                 dev->active_link_owner->name);
517
518                         ret = __media_pipeline_start(
519                                         dev->active_link_user,
520                                         dev->active_link_user_pipe);
521                         if (ret) {
522                                 pr_err("Start Pipeline: %s->%s %d\n",
523                                         dev->active_source->name,
524                                         dev->active_link_user->name,
525                                         ret);
526                                 goto deactivate_link;
527                         }
528                         /* link user is now the owner */
529                         dev->active_link_owner = dev->active_link_user;
530                         dev->active_link_user = NULL;
531                         dev->active_link_user_pipe = NULL;
532                         dev->active_link_shared = false;
533
534                         pr_debug("Pipeline started for %s\n",
535                                 dev->active_link_owner->name);
536                         return;
537                 } else if (!owner_is_audio && dev->users > 1)
538                         /* video/vbi owns the link and has users > 1 */
539                         return;
540
541                 if (dev->active_link_owner != entity)
542                         return;
543
544                 /* stop pipeline */
545                 __media_pipeline_stop(dev->active_link_owner);
546                 pr_debug("Pipeline stop for %s\n",
547                         dev->active_link_owner->name);
548
549 deactivate_link:
550                 ret = __media_entity_setup_link(dev->active_link, 0);
551                 if (ret)
552                         pr_err("Deactivate link Error %d\n", ret);
553
554                 pr_info("Disabled Source: %s->%s->%s Ret %d\n",
555                          dev->active_source->name, dev->active_sink->name,
556                          dev->active_link_owner->name, ret);
557
558                 dev->active_link = NULL;
559                 dev->active_link_owner = NULL;
560                 dev->active_source = NULL;
561                 dev->active_sink = NULL;
562                 dev->active_link_shared = false;
563                 dev->active_link_user = NULL;
564         }
565 }
566 #endif
567
568 static int au0828_media_device_register(struct au0828_dev *dev,
569                                         struct usb_device *udev)
570 {
571 #ifdef CONFIG_MEDIA_CONTROLLER
572         int ret;
573         struct media_entity *entity, *demod = NULL;
574         struct media_link *link;
575
576         if (!dev->media_dev)
577                 return 0;
578
579         if (!media_devnode_is_registered(dev->media_dev->devnode)) {
580
581                 /* register media device */
582                 ret = media_device_register(dev->media_dev);
583                 if (ret) {
584                         media_device_delete(dev->media_dev, KBUILD_MODNAME,
585                                             THIS_MODULE);
586                         dev->media_dev = NULL;
587                         dev_err(&udev->dev,
588                                 "Media Device Register Error: %d\n", ret);
589                         return ret;
590                 }
591         } else {
592                 /*
593                  * Call au0828_media_graph_notify() to connect
594                  * audio graph to our graph. In this case, audio
595                  * driver registered the device and there is no
596                  * entity_notify to be called when new entities
597                  * are added. Invoke it now.
598                 */
599                 au0828_media_graph_notify(NULL, (void *) dev);
600         }
601
602         /*
603          * Find tuner, decoder and demod.
604          *
605          * The tuner and decoder should be cached, as they'll be used by
606          *      au0828_enable_source.
607          *
608          * It also needs to disable the link between tuner and
609          * decoder/demod, to avoid disable step when tuner is requested
610          * by video or audio. Note that this step can't be done until dvb
611          * graph is created during dvb register.
612         */
613         media_device_for_each_entity(entity, dev->media_dev) {
614                 switch (entity->function) {
615                 case MEDIA_ENT_F_TUNER:
616                         dev->tuner = entity;
617                         break;
618                 case MEDIA_ENT_F_ATV_DECODER:
619                         dev->decoder = entity;
620                         break;
621                 case MEDIA_ENT_F_DTV_DEMOD:
622                         demod = entity;
623                         break;
624                 }
625         }
626
627         /* Disable link between tuner->demod and/or tuner->decoder */
628         if (dev->tuner) {
629                 list_for_each_entry(link, &dev->tuner->links, list) {
630                         if (demod && link->sink->entity == demod)
631                                 media_entity_setup_link(link, 0);
632                         if (dev->decoder && link->sink->entity == dev->decoder)
633                                 media_entity_setup_link(link, 0);
634                 }
635         }
636
637         /* register entity_notify callback */
638         dev->entity_notify.notify_data = (void *) dev;
639         dev->entity_notify.notify = (void *) au0828_media_graph_notify;
640         ret = media_device_register_entity_notify(dev->media_dev,
641                                                   &dev->entity_notify);
642         if (ret) {
643                 dev_err(&udev->dev,
644                         "Media Device register entity_notify Error: %d\n",
645                         ret);
646                 return ret;
647         }
648         /* set enable_source */
649         mutex_lock(&dev->media_dev->graph_mutex);
650         dev->media_dev->source_priv = (void *) dev;
651         dev->media_dev->enable_source = au0828_enable_source;
652         dev->media_dev->disable_source = au0828_disable_source;
653         mutex_unlock(&dev->media_dev->graph_mutex);
654 #endif
655         return 0;
656 }
657
658 static int au0828_usb_probe(struct usb_interface *interface,
659         const struct usb_device_id *id)
660 {
661         int ifnum;
662         int retval = 0;
663
664         struct au0828_dev *dev;
665         struct usb_device *usbdev = interface_to_usbdev(interface);
666
667         ifnum = interface->altsetting->desc.bInterfaceNumber;
668
669         if (ifnum != 0)
670                 return -ENODEV;
671
672         dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
673                 le16_to_cpu(usbdev->descriptor.idVendor),
674                 le16_to_cpu(usbdev->descriptor.idProduct),
675                 ifnum);
676
677         /*
678          * Make sure we have 480 Mbps of bandwidth, otherwise things like
679          * video stream wouldn't likely work, since 12 Mbps is generally
680          * not enough even for most Digital TV streams.
681          */
682         if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
683                 pr_err("au0828: Device initialization failed.\n");
684                 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
685                 return -ENODEV;
686         }
687
688         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
689         if (dev == NULL) {
690                 pr_err("%s() Unable to allocate memory\n", __func__);
691                 return -ENOMEM;
692         }
693
694         mutex_init(&dev->lock);
695         mutex_lock(&dev->lock);
696         mutex_init(&dev->mutex);
697         mutex_init(&dev->dvb.lock);
698         dev->usbdev = usbdev;
699         dev->boardnr = id->driver_info;
700         dev->board = au0828_boards[dev->boardnr];
701
702         /* Initialize the media controller */
703         retval = au0828_media_device_init(dev, usbdev);
704         if (retval) {
705                 pr_err("%s() au0828_media_device_init failed\n",
706                        __func__);
707                 mutex_unlock(&dev->lock);
708                 kfree(dev);
709                 return retval;
710         }
711
712         retval = au0828_v4l2_device_register(interface, dev);
713         if (retval) {
714                 au0828_usb_v4l2_media_release(dev);
715                 mutex_unlock(&dev->lock);
716                 kfree(dev);
717                 return retval;
718         }
719
720         /* Power Up the bridge */
721         au0828_write(dev, REG_600, 1 << 4);
722
723         /* Bring up the GPIO's and supporting devices */
724         au0828_gpio_setup(dev);
725
726         /* I2C */
727         au0828_i2c_register(dev);
728
729         /* Setup */
730         au0828_card_setup(dev);
731
732         /* Analog TV */
733         retval = au0828_analog_register(dev, interface);
734         if (retval) {
735                 pr_err("%s() au0828_analog_register failed to register on V4L2\n",
736                         __func__);
737                 mutex_unlock(&dev->lock);
738                 goto done;
739         }
740
741         /* Digital TV */
742         retval = au0828_dvb_register(dev);
743         if (retval)
744                 pr_err("%s() au0828_dvb_register failed\n",
745                        __func__);
746
747         /* Remote controller */
748         au0828_rc_register(dev);
749
750         /*
751          * Store the pointer to the au0828_dev so it can be accessed in
752          * au0828_usb_disconnect
753          */
754         usb_set_intfdata(interface, dev);
755
756         pr_info("Registered device AU0828 [%s]\n",
757                 dev->board.name == NULL ? "Unset" : dev->board.name);
758
759         mutex_unlock(&dev->lock);
760
761         retval = au0828_media_device_register(dev, usbdev);
762
763 done:
764         if (retval < 0)
765                 au0828_usb_disconnect(interface);
766
767         return retval;
768 }
769
770 static int au0828_suspend(struct usb_interface *interface,
771                                 pm_message_t message)
772 {
773         struct au0828_dev *dev = usb_get_intfdata(interface);
774
775         if (!dev)
776                 return 0;
777
778         pr_info("Suspend\n");
779
780         au0828_rc_suspend(dev);
781         au0828_v4l2_suspend(dev);
782         au0828_dvb_suspend(dev);
783
784         /* FIXME: should suspend also ATV/DTV */
785
786         return 0;
787 }
788
789 static int au0828_resume(struct usb_interface *interface)
790 {
791         struct au0828_dev *dev = usb_get_intfdata(interface);
792         if (!dev)
793                 return 0;
794
795         pr_info("Resume\n");
796
797         /* Power Up the bridge */
798         au0828_write(dev, REG_600, 1 << 4);
799
800         /* Bring up the GPIO's and supporting devices */
801         au0828_gpio_setup(dev);
802
803         au0828_rc_resume(dev);
804         au0828_v4l2_resume(dev);
805         au0828_dvb_resume(dev);
806
807         /* FIXME: should resume also ATV/DTV */
808
809         return 0;
810 }
811
812 static struct usb_driver au0828_usb_driver = {
813         .name           = KBUILD_MODNAME,
814         .probe          = au0828_usb_probe,
815         .disconnect     = au0828_usb_disconnect,
816         .id_table       = au0828_usb_id_table,
817         .suspend        = au0828_suspend,
818         .resume         = au0828_resume,
819         .reset_resume   = au0828_resume,
820 };
821
822 static int __init au0828_init(void)
823 {
824         int ret;
825
826         if (au0828_debug & 1)
827                 pr_info("%s() Debugging is enabled\n", __func__);
828
829         if (au0828_debug & 2)
830                 pr_info("%s() USB Debugging is enabled\n", __func__);
831
832         if (au0828_debug & 4)
833                 pr_info("%s() I2C Debugging is enabled\n", __func__);
834
835         if (au0828_debug & 8)
836                 pr_info("%s() Bridge Debugging is enabled\n",
837                        __func__);
838
839         if (au0828_debug & 16)
840                 pr_info("%s() IR Debugging is enabled\n",
841                        __func__);
842
843         pr_info("au0828 driver loaded\n");
844
845         ret = usb_register(&au0828_usb_driver);
846         if (ret)
847                 pr_err("usb_register failed, error = %d\n", ret);
848
849         return ret;
850 }
851
852 static void __exit au0828_exit(void)
853 {
854         usb_deregister(&au0828_usb_driver);
855 }
856
857 module_init(au0828_init);
858 module_exit(au0828_exit);
859
860 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
861 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
862 MODULE_LICENSE("GPL");
863 MODULE_VERSION("0.0.3");