blob: 38e73ee5c8fb1b8de696c78bb4feb07ccedc3341 [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
2 * Driver for the Auvitek USB bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
Steven Toth265a6512008-04-18 21:34:00 -03005 *
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
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030022#include "au0828.h"
Shuah Khanf90c5d72016-02-11 21:41:34 -020023#include "au8522.h"
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030024
Steven Toth265a6512008-04-18 21:34:00 -030025#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Steven Toth265a6512008-04-18 21:34:00 -030027#include <linux/videodev2.h>
28#include <media/v4l2-common.h>
29#include <linux/mutex.h>
30
Mauro Carvalho Chehab188d2d52015-08-31 13:23:03 -030031/* Due to enum tuner_pad_index */
32#include <media/tuner.h>
33
Steven Tothbc3c6132008-04-18 21:39:11 -030034/*
35 * 1 = General debug messages
36 * 2 = USB handling
37 * 4 = I2C related
38 * 8 = Bridge related
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -030039 * 16 = IR related
Steven Tothbc3c6132008-04-18 21:39:11 -030040 */
Adrian Bunkb33d24c2008-04-25 19:06:03 -030041int au0828_debug;
42module_param_named(debug, au0828_debug, int, 0644);
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -030043MODULE_PARM_DESC(debug,
44 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
Steven Toth265a6512008-04-18 21:34:00 -030045
Devin Heitmuellerd6a9a432009-05-27 23:46:17 -030046static unsigned int disable_usb_speed_check;
47module_param(disable_usb_speed_check, int, 0444);
48MODULE_PARM_DESC(disable_usb_speed_check,
49 "override min bandwidth requirement of 480M bps");
50
Steven Toth265a6512008-04-18 21:34:00 -030051#define _AU0828_BULKPIPE 0x03
52#define _BULKPIPESIZE 0xffff
53
54static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
Devin Heitmueller8ff63de2012-08-06 22:47:05 -030055 u16 index);
Steven Toth265a6512008-04-18 21:34:00 -030056static 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
63u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
64{
Devin Heitmueller77fc2862012-08-06 22:47:07 -030065 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;
Steven Toth265a6512008-04-18 21:34:00 -030071}
72
73u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
74{
Devin Heitmuellerb80f7702009-03-11 03:00:57 -030075 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
Devin Heitmueller8ff63de2012-08-06 22:47:05 -030076 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
Steven Toth265a6512008-04-18 21:34:00 -030077}
78
Steven Toth265a6512008-04-18 21:34:00 -030079static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
Devin Heitmueller8ff63de2012-08-06 22:47:05 -030080 u16 index)
Steven Toth265a6512008-04-18 21:34:00 -030081{
82 int status = -ENODEV;
Devin Heitmueller8ff63de2012-08-06 22:47:05 -030083
Steven Toth265a6512008-04-18 21:34:00 -030084 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,
Steven Totha8eb9122008-10-16 20:19:41 -030090 USB_DIR_OUT | USB_TYPE_VENDOR |
91 USB_RECIP_DEVICE,
Devin Heitmueller8ff63de2012-08-06 22:47:05 -030092 value, index, NULL, 0, 1000);
Steven Toth265a6512008-04-18 21:34:00 -030093
94 status = min(status, 0);
95
96 if (status < 0) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -030097 pr_err("%s() Failed sending control message, error %d.\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -030098 __func__, status);
Steven Toth265a6512008-04-18 21:34:00 -030099 }
100
101 }
Devin Heitmueller8ff63de2012-08-06 22:47:05 -0300102
Steven Toth265a6512008-04-18 21:34:00 -0300103 return status;
104}
105
106static 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) {
Steven Toth265a6512008-04-18 21:34:00 -0300112 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,
Devin Heitmueller77fc2862012-08-06 22:47:07 -0300117 dev->ctrlmsg, size, 1000);
Steven Toth265a6512008-04-18 21:34:00 -0300118
119 status = min(status, 0);
120
121 if (status < 0) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300122 pr_err("%s() Failed receiving control message, error %d.\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300123 __func__, status);
Devin Heitmueller77fc2862012-08-06 22:47:07 -0300124 }
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);
Steven Toth265a6512008-04-18 21:34:00 -0300129 }
130 mutex_unlock(&dev->mutex);
131 return status;
132}
Steven Totha9c36aa2008-04-17 21:41:28 -0300133
Mauro Carvalho Chehabbc5ccdb2016-03-16 05:04:04 -0700134#ifdef CONFIG_MEDIA_CONTROLLER
135static void au0828_media_graph_notify(struct media_entity *new,
136 void *notify_data);
137#endif
138
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300139static void au0828_unregister_media_device(struct au0828_dev *dev)
140{
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300141#ifdef CONFIG_MEDIA_CONTROLLER
Mauro Carvalho Chehabbc5ccdb2016-03-16 05:04:04 -0700142 struct media_device *mdev = dev->media_dev;
143 struct media_entity_notify *notify, *nextp;
Shuah Khanffa85762016-03-13 00:57:40 -0300144
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300145 if (!mdev || !media_devnode_is_registered(mdev->devnode))
Mauro Carvalho Chehabbc5ccdb2016-03-16 05:04:04 -0700146 return;
147
148 /* Remove au0828 entity_notify callbacks */
149 list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) {
150 if (notify->notify != au0828_media_graph_notify)
151 continue;
152 media_device_unregister_entity_notify(mdev, notify);
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300153 }
Mauro Carvalho Chehabbc5ccdb2016-03-16 05:04:04 -0700154
155 /* clear enable_source, disable_source */
156 dev->media_dev->source_priv = NULL;
157 dev->media_dev->enable_source = NULL;
158 dev->media_dev->disable_source = NULL;
159
160 media_device_unregister(dev->media_dev);
161 media_device_cleanup(dev->media_dev);
162 kfree(dev->media_dev);
163 dev->media_dev = NULL;
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300164#endif
165}
166
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200167void au0828_usb_release(struct au0828_dev *dev)
Hans Verkuil823beb72013-03-11 10:16:45 -0300168{
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300169 au0828_unregister_media_device(dev);
170
Hans Verkuil823beb72013-03-11 10:16:45 -0300171 /* I2C */
172 au0828_i2c_unregister(dev);
173
174 kfree(dev);
175}
176
Steven Toth265a6512008-04-18 21:34:00 -0300177static void au0828_usb_disconnect(struct usb_interface *interface)
178{
179 struct au0828_dev *dev = usb_get_intfdata(interface);
180
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300181 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300182
Shuah Khaneb336ea2014-11-21 21:17:08 -0300183 /* there is a small window after disconnect, before
184 dev->usbdev is NULL, for poll (e.g: IR) try to access
185 the device and fill the dmesg with error messages.
186 Set the status so poll routines can check and avoid
187 access after disconnect.
188 */
Mauro Carvalho Chehabe8e30392016-03-22 09:21:57 -0300189 set_bit(DEV_DISCONNECTED, &dev->dev_state);
Shuah Khaneb336ea2014-11-21 21:17:08 -0300190
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300191 au0828_rc_unregister(dev);
Steven Toth265a6512008-04-18 21:34:00 -0300192 /* Digital TV */
193 au0828_dvb_unregister(dev);
194
Steven Toth265a6512008-04-18 21:34:00 -0300195 usb_set_intfdata(interface, NULL);
Steven Toth265a6512008-04-18 21:34:00 -0300196 mutex_lock(&dev->mutex);
197 dev->usbdev = NULL;
198 mutex_unlock(&dev->mutex);
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200199 if (au0828_analog_unregister(dev)) {
Mauro Carvalho Chehab7e9a8ad2015-12-10 10:58:04 -0200200 /*
201 * No need to call au0828_usb_release() if V4L2 is enabled,
202 * as this is already called via au0828_usb_v4l2_release()
203 */
Hans Verkuil823beb72013-03-11 10:16:45 -0300204 return;
205 }
Hans Verkuil823beb72013-03-11 10:16:45 -0300206 au0828_usb_release(dev);
Steven Toth265a6512008-04-18 21:34:00 -0300207}
208
Mauro Carvalho Chehab9f806792015-12-28 09:55:49 -0200209static int au0828_media_device_init(struct au0828_dev *dev,
210 struct usb_device *udev)
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300211{
212#ifdef CONFIG_MEDIA_CONTROLLER
213 struct media_device *mdev;
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300214
Mauro Carvalho Chehab405ddbf2016-03-31 10:54:05 -0300215 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300216 if (!mdev)
Mauro Carvalho Chehab9f806792015-12-28 09:55:49 -0200217 return -ENOMEM;
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300218
Shuah Khan7b12adf2016-02-11 21:41:31 -0200219 /* check if media device is already initialized */
220 if (!mdev->dev)
221 media_device_usb_init(mdev, udev, udev->product);
Mauro Carvalho Chehab6cf5dad2016-02-22 12:10:49 -0300222
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300223 dev->media_dev = mdev;
224#endif
Mauro Carvalho Chehab9f806792015-12-28 09:55:49 -0200225 return 0;
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300226}
227
Arnd Bergmann0a82edd2016-03-04 21:06:43 -0300228#ifdef CONFIG_MEDIA_CONTROLLER
Shuah Khanf90c5d72016-02-11 21:41:34 -0200229static void au0828_media_graph_notify(struct media_entity *new,
230 void *notify_data)
231{
Shuah Khanf90c5d72016-02-11 21:41:34 -0200232 struct au0828_dev *dev = (struct au0828_dev *) notify_data;
233 int ret;
Shuah Khan9096cae2016-03-03 00:38:19 -0300234 struct media_entity *entity, *mixer = NULL, *decoder = NULL;
Shuah Khanf90c5d72016-02-11 21:41:34 -0200235
Shuah Khan9096cae2016-03-03 00:38:19 -0300236 if (!new) {
237 /*
238 * Called during au0828 probe time to connect
239 * entites that were created prior to registering
240 * the notify handler. Find mixer and decoder.
241 */
242 media_device_for_each_entity(entity, dev->media_dev) {
243 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
244 mixer = entity;
245 else if (entity->function == MEDIA_ENT_F_ATV_DECODER)
246 decoder = entity;
247 }
248 goto create_link;
249 }
Shuah Khanf90c5d72016-02-11 21:41:34 -0200250
251 switch (new->function) {
252 case MEDIA_ENT_F_AUDIO_MIXER:
Shuah Khan9096cae2016-03-03 00:38:19 -0300253 mixer = new;
254 if (dev->decoder)
255 decoder = dev->decoder;
256 break;
257 case MEDIA_ENT_F_ATV_DECODER:
258 /* In case, Mixer is added first, find mixer and create link */
259 media_device_for_each_entity(entity, dev->media_dev) {
260 if (entity->function == MEDIA_ENT_F_AUDIO_MIXER)
261 mixer = entity;
262 }
263 decoder = new;
Shuah Khanf90c5d72016-02-11 21:41:34 -0200264 break;
265 default:
266 break;
267 }
Shuah Khan9096cae2016-03-03 00:38:19 -0300268
269create_link:
270 if (decoder && mixer) {
271 ret = media_create_pad_link(decoder,
272 DEMOD_PAD_AUDIO_OUT,
273 mixer, 0,
274 MEDIA_LNK_FL_ENABLED);
275 if (ret)
276 dev_err(&dev->usbdev->dev,
277 "Mixer Pad Link Create Error: %d\n", ret);
278 }
Shuah Khanf90c5d72016-02-11 21:41:34 -0200279}
280
Shuah Khanc94903f2016-02-11 21:41:35 -0200281static int au0828_enable_source(struct media_entity *entity,
282 struct media_pipeline *pipe)
283{
Shuah Khanc94903f2016-02-11 21:41:35 -0200284 struct media_entity *source, *find_source;
285 struct media_entity *sink;
286 struct media_link *link, *found_link = NULL;
287 int ret = 0;
288 struct media_device *mdev = entity->graph_obj.mdev;
289 struct au0828_dev *dev;
290
291 if (!mdev)
292 return -ENODEV;
293
294 mutex_lock(&mdev->graph_mutex);
295
296 dev = mdev->source_priv;
297
298 /*
299 * For Audio and V4L2 entity, find the link to which decoder
300 * is the sink. Look for an active link between decoder and
301 * source (tuner/s-video/Composite), if one exists, nothing
302 * to do. If not, look for any active links between source
303 * and any other entity. If one exists, source is busy. If
304 * source is free, setup link and start pipeline from source.
305 * For DVB FE entity, the source for the link is the tuner.
306 * Check if tuner is available and setup link and start
307 * pipeline.
308 */
309 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
310 sink = entity;
311 find_source = dev->tuner;
312 } else {
313 /* Analog isn't configured or register failed */
314 if (!dev->decoder) {
315 ret = -ENODEV;
316 goto end;
317 }
318
319 sink = dev->decoder;
320
321 /*
322 * Default input is tuner and default input_type
323 * is AU0828_VMUX_TELEVISION.
324 * FIXME:
325 * There is a problem when s_input is called to
326 * change the default input. s_input will try to
327 * enable_source before attempting to change the
328 * input on the device, and will end up enabling
329 * default source which is tuner.
330 *
331 * Additional logic is necessary in au0828
332 * to detect that the input has changed and
333 * enable the right source.
334 */
335
336 if (dev->input_type == AU0828_VMUX_TELEVISION)
337 find_source = dev->tuner;
338 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
339 dev->input_type == AU0828_VMUX_COMPOSITE)
340 find_source = &dev->input_ent[dev->input_type];
341 else {
342 /* unknown input - let user select input */
343 ret = 0;
344 goto end;
345 }
346 }
347
348 /* Is an active link between sink and source */
349 if (dev->active_link) {
350 /*
351 * If DVB is using the tuner and calling entity is
352 * audio/video, the following check will be false,
353 * since sink is different. Result is Busy.
354 */
355 if (dev->active_link->sink->entity == sink &&
356 dev->active_link->source->entity == find_source) {
357 /*
358 * Either ALSA or Video own tuner. sink is
359 * the same for both. Prevent Video stepping
360 * on ALSA when ALSA owns the source.
361 */
362 if (dev->active_link_owner != entity &&
363 dev->active_link_owner->function ==
364 MEDIA_ENT_F_AUDIO_CAPTURE) {
365 pr_debug("ALSA has the tuner\n");
366 ret = -EBUSY;
367 goto end;
368 }
369 ret = 0;
370 goto end;
371 } else {
372 ret = -EBUSY;
373 goto end;
374 }
375 }
376
377 list_for_each_entry(link, &sink->links, list) {
378 /* Check sink, and source */
379 if (link->sink->entity == sink &&
380 link->source->entity == find_source) {
381 found_link = link;
382 break;
383 }
384 }
385
386 if (!found_link) {
387 ret = -ENODEV;
388 goto end;
389 }
390
391 /* activate link between source and sink and start pipeline */
392 source = found_link->source->entity;
393 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
394 if (ret) {
395 pr_err("Activate tuner link %s->%s. Error %d\n",
396 source->name, sink->name, ret);
397 goto end;
398 }
399
400 ret = __media_entity_pipeline_start(entity, pipe);
401 if (ret) {
402 pr_err("Start Pipeline: %s->%s Error %d\n",
403 source->name, entity->name, ret);
404 ret = __media_entity_setup_link(found_link, 0);
405 pr_err("Deactivate link Error %d\n", ret);
406 goto end;
407 }
408 /*
409 * save active link and active link owner to avoid audio
410 * deactivating video owned link from disable_source and
411 * vice versa
412 */
413 dev->active_link = found_link;
414 dev->active_link_owner = entity;
415 dev->active_source = source;
416 dev->active_sink = sink;
417
418 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
419 dev->active_source->name, dev->active_sink->name,
420 dev->active_link_owner->name, ret);
421end:
422 mutex_unlock(&mdev->graph_mutex);
423 pr_debug("au0828_enable_source() end %s %d %d\n",
424 entity->name, entity->function, ret);
425 return ret;
Shuah Khanc94903f2016-02-11 21:41:35 -0200426}
427
428static void au0828_disable_source(struct media_entity *entity)
429{
Shuah Khanc94903f2016-02-11 21:41:35 -0200430 int ret = 0;
431 struct media_device *mdev = entity->graph_obj.mdev;
432 struct au0828_dev *dev;
433
434 if (!mdev)
435 return;
436
437 mutex_lock(&mdev->graph_mutex);
438 dev = mdev->source_priv;
439
440 if (!dev->active_link) {
441 ret = -ENODEV;
442 goto end;
443 }
444
445 /* link is active - stop pipeline from source (tuner) */
446 if (dev->active_link->sink->entity == dev->active_sink &&
447 dev->active_link->source->entity == dev->active_source) {
448 /*
449 * prevent video from deactivating link when audio
450 * has active pipeline
451 */
452 if (dev->active_link_owner != entity)
453 goto end;
454 __media_entity_pipeline_stop(entity);
455 ret = __media_entity_setup_link(dev->active_link, 0);
456 if (ret)
457 pr_err("Deactivate link Error %d\n", ret);
458
459 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
460 dev->active_source->name, dev->active_sink->name,
461 dev->active_link_owner->name, ret);
462
463 dev->active_link = NULL;
464 dev->active_link_owner = NULL;
465 dev->active_source = NULL;
466 dev->active_sink = NULL;
467 }
468
469end:
470 mutex_unlock(&mdev->graph_mutex);
Shuah Khanc94903f2016-02-11 21:41:35 -0200471}
Arnd Bergmann0a82edd2016-03-04 21:06:43 -0300472#endif
Shuah Khanc94903f2016-02-11 21:41:35 -0200473
Shuah Khan7b12adf2016-02-11 21:41:31 -0200474static int au0828_media_device_register(struct au0828_dev *dev,
475 struct usb_device *udev)
476{
477#ifdef CONFIG_MEDIA_CONTROLLER
478 int ret;
Mauro Carvalho Chehab2e208c62016-03-11 16:02:14 -0300479 struct media_entity *entity, *demod = NULL;
480 struct media_link *link;
Shuah Khan7b12adf2016-02-11 21:41:31 -0200481
Shuah Khanf90c5d72016-02-11 21:41:34 -0200482 if (!dev->media_dev)
483 return 0;
484
Mauro Carvalho Chehaba087ce72016-04-27 19:28:26 -0300485 if (!media_devnode_is_registered(dev->media_dev->devnode)) {
Shuah Khan7b12adf2016-02-11 21:41:31 -0200486
487 /* register media device */
488 ret = media_device_register(dev->media_dev);
489 if (ret) {
490 dev_err(&udev->dev,
491 "Media Device Register Error: %d\n", ret);
492 return ret;
493 }
Shuah Khan9096cae2016-03-03 00:38:19 -0300494 } else {
495 /*
496 * Call au0828_media_graph_notify() to connect
497 * audio graph to our graph. In this case, audio
498 * driver registered the device and there is no
499 * entity_notify to be called when new entities
500 * are added. Invoke it now.
501 */
502 au0828_media_graph_notify(NULL, (void *) dev);
Shuah Khan7b12adf2016-02-11 21:41:31 -0200503 }
Shuah Khan840f5b02016-03-09 19:15:38 -0700504
505 /*
Mauro Carvalho Chehab2e208c62016-03-11 16:02:14 -0300506 * Find tuner, decoder and demod.
507 *
508 * The tuner and decoder should be cached, as they'll be used by
509 * au0828_enable_source.
510 *
511 * It also needs to disable the link between tuner and
512 * decoder/demod, to avoid disable step when tuner is requested
513 * by video or audio. Note that this step can't be done until dvb
514 * graph is created during dvb register.
Shuah Khan840f5b02016-03-09 19:15:38 -0700515 */
516 media_device_for_each_entity(entity, dev->media_dev) {
Mauro Carvalho Chehab2e208c62016-03-11 16:02:14 -0300517 switch (entity->function) {
518 case MEDIA_ENT_F_TUNER:
519 dev->tuner = entity;
520 break;
521 case MEDIA_ENT_F_ATV_DECODER:
522 dev->decoder = entity;
523 break;
524 case MEDIA_ENT_F_DTV_DEMOD:
Shuah Khan840f5b02016-03-09 19:15:38 -0700525 demod = entity;
Mauro Carvalho Chehab2e208c62016-03-11 16:02:14 -0300526 break;
527 }
Shuah Khan840f5b02016-03-09 19:15:38 -0700528 }
Shuah Khan840f5b02016-03-09 19:15:38 -0700529
Mauro Carvalho Chehab2e208c62016-03-11 16:02:14 -0300530 /* Disable link between tuner->demod and/or tuner->decoder */
531 if (dev->tuner) {
532 list_for_each_entry(link, &dev->tuner->links, list) {
533 if (demod && link->sink->entity == demod)
Shuah Khan840f5b02016-03-09 19:15:38 -0700534 media_entity_setup_link(link, 0);
Mauro Carvalho Chehab2e208c62016-03-11 16:02:14 -0300535 if (dev->decoder && link->sink->entity == dev->decoder)
536 media_entity_setup_link(link, 0);
Shuah Khan840f5b02016-03-09 19:15:38 -0700537 }
538 }
539
Shuah Khanf90c5d72016-02-11 21:41:34 -0200540 /* register entity_notify callback */
541 dev->entity_notify.notify_data = (void *) dev;
542 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
543 ret = media_device_register_entity_notify(dev->media_dev,
544 &dev->entity_notify);
545 if (ret) {
546 dev_err(&udev->dev,
547 "Media Device register entity_notify Error: %d\n",
548 ret);
549 return ret;
550 }
Shuah Khanc94903f2016-02-11 21:41:35 -0200551 /* set enable_source */
552 dev->media_dev->source_priv = (void *) dev;
553 dev->media_dev->enable_source = au0828_enable_source;
554 dev->media_dev->disable_source = au0828_disable_source;
Shuah Khan7b12adf2016-02-11 21:41:31 -0200555#endif
556 return 0;
557}
Rafael Lourenço de Lima Chehabbed69192015-06-08 22:20:46 -0300558
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300559static int au0828_usb_probe(struct usb_interface *interface,
Steven Toth265a6512008-04-18 21:34:00 -0300560 const struct usb_device_id *id)
561{
Michael Krufky8a4e7862012-12-04 11:30:00 -0300562 int ifnum;
Tim Mesterf251b3e2014-01-07 01:29:25 -0300563 int retval = 0;
564
Steven Toth265a6512008-04-18 21:34:00 -0300565 struct au0828_dev *dev;
566 struct usb_device *usbdev = interface_to_usbdev(interface);
567
568 ifnum = interface->altsetting->desc.bInterfaceNumber;
569
570 if (ifnum != 0)
571 return -ENODEV;
572
Steven Totha9c36aa2008-04-17 21:41:28 -0300573 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
Steven Toth265a6512008-04-18 21:34:00 -0300574 le16_to_cpu(usbdev->descriptor.idVendor),
575 le16_to_cpu(usbdev->descriptor.idProduct),
576 ifnum);
577
Devin Heitmuelleree3436b2009-05-27 23:25:36 -0300578 /*
579 * Make sure we have 480 Mbps of bandwidth, otherwise things like
580 * video stream wouldn't likely work, since 12 Mbps is generally
581 * not enough even for most Digital TV streams.
582 */
Devin Heitmuellerd6a9a432009-05-27 23:46:17 -0300583 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300584 pr_err("au0828: Device initialization failed.\n");
585 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
Devin Heitmuelleree3436b2009-05-27 23:25:36 -0300586 return -ENODEV;
587 }
588
Steven Toth265a6512008-04-18 21:34:00 -0300589 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
590 if (dev == NULL) {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300591 pr_err("%s() Unable to allocate memory\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300592 return -ENOMEM;
593 }
594
Devin Heitmueller549ee4d2012-08-06 22:46:58 -0300595 mutex_init(&dev->lock);
596 mutex_lock(&dev->lock);
Steven Toth265a6512008-04-18 21:34:00 -0300597 mutex_init(&dev->mutex);
598 mutex_init(&dev->dvb.lock);
599 dev->usbdev = usbdev;
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300600 dev->boardnr = id->driver_info;
Rafael Lourenço de Lima Chehabe42c8c62015-06-08 22:20:45 -0300601 dev->board = au0828_boards[dev->boardnr];
602
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200603 /* Initialize the media controller */
Mauro Carvalho Chehab9f806792015-12-28 09:55:49 -0200604 retval = au0828_media_device_init(dev, usbdev);
605 if (retval) {
606 pr_err("%s() au0828_media_device_init failed\n",
607 __func__);
608 mutex_unlock(&dev->lock);
609 kfree(dev);
610 return retval;
611 }
Steven Toth265a6512008-04-18 21:34:00 -0300612
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200613 retval = au0828_v4l2_device_register(interface, dev);
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300614 if (retval) {
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200615 au0828_usb_v4l2_media_release(dev);
Devin Heitmueller549ee4d2012-08-06 22:46:58 -0300616 mutex_unlock(&dev->lock);
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300617 kfree(dev);
Hans Verkuile8c26f42013-02-15 08:55:41 -0300618 return retval;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300619 }
620
Steven Toth265a6512008-04-18 21:34:00 -0300621 /* Power Up the bridge */
622 au0828_write(dev, REG_600, 1 << 4);
623
624 /* Bring up the GPIO's and supporting devices */
625 au0828_gpio_setup(dev);
626
627 /* I2C */
628 au0828_i2c_register(dev);
629
Steven Toth28930fa2008-03-29 19:53:07 -0300630 /* Setup */
631 au0828_card_setup(dev);
632
Sean Youngf7d3edb02019-05-19 15:28:22 -0400633 /*
634 * Store the pointer to the au0828_dev so it can be accessed in
635 * au0828_usb_disconnect
636 */
637 usb_set_intfdata(interface, dev);
638
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300639 /* Analog TV */
Mauro Carvalho Chehab7b606ff2016-02-09 15:53:39 -0200640 retval = au0828_analog_register(dev, interface);
641 if (retval) {
642 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
643 __func__);
644 goto done;
Mauro Carvalho Chehab82e92f42016-02-09 13:46:05 -0200645 }
Devin Heitmueller8b2f0792009-03-11 03:00:40 -0300646
Steven Toth265a6512008-04-18 21:34:00 -0300647 /* Digital TV */
Tim Mesterf251b3e2014-01-07 01:29:25 -0300648 retval = au0828_dvb_register(dev);
649 if (retval)
650 pr_err("%s() au0282_dev_register failed\n",
651 __func__);
652
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300653 /* Remote controller */
654 au0828_rc_register(dev);
Steven Toth265a6512008-04-18 21:34:00 -0300655
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300656 pr_info("Registered device AU0828 [%s]\n",
Devin Heitmuellerf1add5b2009-03-11 03:00:47 -0300657 dev->board.name == NULL ? "Unset" : dev->board.name);
Steven Toth265a6512008-04-18 21:34:00 -0300658
Devin Heitmueller549ee4d2012-08-06 22:46:58 -0300659 mutex_unlock(&dev->lock);
660
Shuah Khan7b12adf2016-02-11 21:41:31 -0200661 retval = au0828_media_device_register(dev, usbdev);
Javier Martinez Canillas9832e152015-12-11 20:57:08 -0200662
663done:
664 if (retval < 0)
665 au0828_usb_disconnect(interface);
666
Tim Mesterf251b3e2014-01-07 01:29:25 -0300667 return retval;
Steven Toth265a6512008-04-18 21:34:00 -0300668}
669
Mauro Carvalho Chehabaaeac192014-08-09 21:47:11 -0300670static int au0828_suspend(struct usb_interface *interface,
671 pm_message_t message)
672{
673 struct au0828_dev *dev = usb_get_intfdata(interface);
674
675 if (!dev)
676 return 0;
677
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -0300678 pr_info("Suspend\n");
679
Mauro Carvalho Chehabaaeac192014-08-09 21:47:11 -0300680 au0828_rc_suspend(dev);
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -0300681 au0828_v4l2_suspend(dev);
Mauro Carvalho Chehabb799de72014-08-09 21:47:13 -0300682 au0828_dvb_suspend(dev);
Mauro Carvalho Chehabaaeac192014-08-09 21:47:11 -0300683
684 /* FIXME: should suspend also ATV/DTV */
685
686 return 0;
687}
688
689static int au0828_resume(struct usb_interface *interface)
690{
691 struct au0828_dev *dev = usb_get_intfdata(interface);
692 if (!dev)
693 return 0;
694
Mauro Carvalho Chehab81187242014-08-09 21:47:18 -0300695 pr_info("Resume\n");
696
Mauro Carvalho Chehabfa500462014-08-09 21:47:12 -0300697 /* Power Up the bridge */
698 au0828_write(dev, REG_600, 1 << 4);
699
700 /* Bring up the GPIO's and supporting devices */
701 au0828_gpio_setup(dev);
702
Mauro Carvalho Chehabaaeac192014-08-09 21:47:11 -0300703 au0828_rc_resume(dev);
Mauro Carvalho Chehab1a1ba952014-08-09 21:47:15 -0300704 au0828_v4l2_resume(dev);
Mauro Carvalho Chehabb799de72014-08-09 21:47:13 -0300705 au0828_dvb_resume(dev);
Mauro Carvalho Chehabaaeac192014-08-09 21:47:11 -0300706
707 /* FIXME: should resume also ATV/DTV */
708
709 return 0;
710}
711
Steven Toth265a6512008-04-18 21:34:00 -0300712static struct usb_driver au0828_usb_driver = {
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300713 .name = KBUILD_MODNAME,
Steven Toth265a6512008-04-18 21:34:00 -0300714 .probe = au0828_usb_probe,
715 .disconnect = au0828_usb_disconnect,
716 .id_table = au0828_usb_id_table,
Mauro Carvalho Chehabaaeac192014-08-09 21:47:11 -0300717 .suspend = au0828_suspend,
718 .resume = au0828_resume,
719 .reset_resume = au0828_resume,
Steven Toth265a6512008-04-18 21:34:00 -0300720};
721
722static int __init au0828_init(void)
723{
724 int ret;
725
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300726 if (au0828_debug & 1)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300727 pr_info("%s() Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300728
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300729 if (au0828_debug & 2)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300730 pr_info("%s() USB Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300731
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300732 if (au0828_debug & 4)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300733 pr_info("%s() I2C Debugging is enabled\n", __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300734
Adrian Bunkb33d24c2008-04-25 19:06:03 -0300735 if (au0828_debug & 8)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300736 pr_info("%s() Bridge Debugging is enabled\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300737 __func__);
Steven Tothbc3c6132008-04-18 21:39:11 -0300738
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300739 if (au0828_debug & 16)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300740 pr_info("%s() IR Debugging is enabled\n",
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300741 __func__);
742
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300743 pr_info("au0828 driver loaded\n");
Steven Toth265a6512008-04-18 21:34:00 -0300744
745 ret = usb_register(&au0828_usb_driver);
746 if (ret)
Mauro Carvalho Chehab83afb322014-08-09 21:47:17 -0300747 pr_err("usb_register failed, error = %d\n", ret);
Steven Toth265a6512008-04-18 21:34:00 -0300748
749 return ret;
750}
751
752static void __exit au0828_exit(void)
753{
754 usb_deregister(&au0828_usb_driver);
755}
756
757module_init(au0828_init);
758module_exit(au0828_exit);
759
760MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
Steven Toth6d897612008-09-03 17:12:12 -0300761MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Toth265a6512008-04-18 21:34:00 -0300762MODULE_LICENSE("GPL");
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300763MODULE_VERSION("0.0.3");