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