]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/usb/au0828/au0828-core.c
[media] sound/usb: Use Media Controller API to share media resources
[mirror_ubuntu-artful-kernel.git] / drivers / media / usb / au0828 / au0828-core.c
CommitLineData
265a6510
ST
1/*
2 * Driver for the Auvitek USB bridge
3 *
6d897616 4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
265a6510
ST
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
83afb32a 22#include "au0828.h"
f90c5d79 23#include "au8522.h"
83afb32a 24
265a6510 25#include <linux/module.h>
5a0e3ad6 26#include <linux/slab.h>
265a6510
ST
27#include <linux/videodev2.h>
28#include <media/v4l2-common.h>
29#include <linux/mutex.h>
30
188d2d55
MCC
31/* Due to enum tuner_pad_index */
32#include <media/tuner.h>
33
bc3c613c
ST
34/*
35 * 1 = General debug messages
36 * 2 = USB handling
37 * 4 = I2C related
38 * 8 = Bridge related
2fcfd317 39 * 16 = IR related
bc3c613c 40 */
b33d24c4
AB
41int au0828_debug;
42module_param_named(debug, au0828_debug, int, 0644);
2fcfd317
MCC
43MODULE_PARM_DESC(debug,
44 "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
265a6510 45
d6a9a430
DH
46static 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
265a6510
ST
51#define _AU0828_BULKPIPE 0x03
52#define _BULKPIPESIZE 0xffff
53
54static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
8ff63de6 55 u16 index);
265a6510
ST
56static 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{
77fc2863
DH
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;
265a6510
ST
71}
72
73u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
74{
b80f770a 75 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
8ff63de6 76 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
265a6510
ST
77}
78
265a6510 79static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
8ff63de6 80 u16 index)
265a6510
ST
81{
82 int status = -ENODEV;
8ff63de6 83
265a6510
ST
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,
a8eb912c
ST
90 USB_DIR_OUT | USB_TYPE_VENDOR |
91 USB_RECIP_DEVICE,
8ff63de6 92 value, index, NULL, 0, 1000);
265a6510
ST
93
94 status = min(status, 0);
95
96 if (status < 0) {
83afb32a 97 pr_err("%s() Failed sending control message, error %d.\n",
f07e8e4b 98 __func__, status);
265a6510
ST
99 }
100
101 }
8ff63de6 102
265a6510
ST
103 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) {
265a6510
ST
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,
77fc2863 117 dev->ctrlmsg, size, 1000);
265a6510
ST
118
119 status = min(status, 0);
120
121 if (status < 0) {
83afb32a 122 pr_err("%s() Failed receiving control message, error %d.\n",
f07e8e4b 123 __func__, status);
77fc2863
DH
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);
265a6510
ST
129 }
130 mutex_unlock(&dev->mutex);
131 return status;
132}
a9c36aad 133
bed69196
RLLC
134static void au0828_unregister_media_device(struct au0828_dev *dev)
135{
136
137#ifdef CONFIG_MEDIA_CONTROLLER
182dde7c
SK
138 if (dev->media_dev &&
139 media_devnode_is_registered(&dev->media_dev->devnode)) {
bed69196 140 media_device_unregister(dev->media_dev);
9832e155 141 media_device_cleanup(dev->media_dev);
bed69196
RLLC
142 dev->media_dev = NULL;
143 }
144#endif
145}
146
7b606ffd 147void au0828_usb_release(struct au0828_dev *dev)
265a6510 148{
bed69196
RLLC
149 au0828_unregister_media_device(dev);
150
265a6510
ST
151 /* I2C */
152 au0828_i2c_unregister(dev);
153
823beb7e
HV
154 kfree(dev);
155}
156
823beb7e
HV
157static 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
eb336eab
SK
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
2fcfd317 171 au0828_rc_unregister(dev);
823beb7e
HV
172 /* Digital TV */
173 au0828_dvb_unregister(dev);
265a6510 174
823beb7e 175 usb_set_intfdata(interface, NULL);
265a6510
ST
176 mutex_lock(&dev->mutex);
177 dev->usbdev = NULL;
178 mutex_unlock(&dev->mutex);
7b606ffd 179 if (au0828_analog_unregister(dev)) {
7e9a8ad5
MCC
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 */
823beb7e
HV
184 return;
185 }
823beb7e 186 au0828_usb_release(dev);
265a6510
ST
187}
188
9f806795
MCC
189static int au0828_media_device_init(struct au0828_dev *dev,
190 struct usb_device *udev)
bed69196
RLLC
191{
192#ifdef CONFIG_MEDIA_CONTROLLER
193 struct media_device *mdev;
bed69196 194
182dde7c 195 mdev = media_device_get_devres(&udev->dev);
67873d4e
MCC
196 if (!mdev)
197 return -ENOMEM;
bed69196 198
7b12adf6
SK
199 /* check if media device is already initialized */
200 if (!mdev->dev)
201 media_device_usb_init(mdev, udev, udev->product);
6cf5dad1 202
bed69196
RLLC
203 dev->media_dev = mdev;
204#endif
9f806795 205 return 0;
bed69196
RLLC
206}
207
f90c5d79
SK
208static 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
215 if (!dev->decoder)
216 return;
217
218 switch (new->function) {
219 case MEDIA_ENT_F_AUDIO_MIXER:
220 ret = media_create_pad_link(dev->decoder,
bddc4187 221 DEMOD_PAD_AUDIO_OUT,
f90c5d79
SK
222 new, 0,
223 MEDIA_LNK_FL_ENABLED);
224 if (ret)
225 dev_err(&dev->usbdev->dev,
226 "Mixer Pad Link Create Error: %d\n",
227 ret);
228 break;
229 default:
230 break;
231 }
232#endif
233}
234
c94903f1
SK
235static int au0828_enable_source(struct media_entity *entity,
236 struct media_pipeline *pipe)
237{
238#ifdef CONFIG_MEDIA_CONTROLLER
239 struct media_entity *source, *find_source;
240 struct media_entity *sink;
241 struct media_link *link, *found_link = NULL;
242 int ret = 0;
243 struct media_device *mdev = entity->graph_obj.mdev;
244 struct au0828_dev *dev;
245
246 if (!mdev)
247 return -ENODEV;
248
249 mutex_lock(&mdev->graph_mutex);
250
251 dev = mdev->source_priv;
252
253 /*
254 * For Audio and V4L2 entity, find the link to which decoder
255 * is the sink. Look for an active link between decoder and
256 * source (tuner/s-video/Composite), if one exists, nothing
257 * to do. If not, look for any active links between source
258 * and any other entity. If one exists, source is busy. If
259 * source is free, setup link and start pipeline from source.
260 * For DVB FE entity, the source for the link is the tuner.
261 * Check if tuner is available and setup link and start
262 * pipeline.
263 */
264 if (entity->function == MEDIA_ENT_F_DTV_DEMOD) {
265 sink = entity;
266 find_source = dev->tuner;
267 } else {
268 /* Analog isn't configured or register failed */
269 if (!dev->decoder) {
270 ret = -ENODEV;
271 goto end;
272 }
273
274 sink = dev->decoder;
275
276 /*
277 * Default input is tuner and default input_type
278 * is AU0828_VMUX_TELEVISION.
279 * FIXME:
280 * There is a problem when s_input is called to
281 * change the default input. s_input will try to
282 * enable_source before attempting to change the
283 * input on the device, and will end up enabling
284 * default source which is tuner.
285 *
286 * Additional logic is necessary in au0828
287 * to detect that the input has changed and
288 * enable the right source.
289 */
290
291 if (dev->input_type == AU0828_VMUX_TELEVISION)
292 find_source = dev->tuner;
293 else if (dev->input_type == AU0828_VMUX_SVIDEO ||
294 dev->input_type == AU0828_VMUX_COMPOSITE)
295 find_source = &dev->input_ent[dev->input_type];
296 else {
297 /* unknown input - let user select input */
298 ret = 0;
299 goto end;
300 }
301 }
302
303 /* Is an active link between sink and source */
304 if (dev->active_link) {
305 /*
306 * If DVB is using the tuner and calling entity is
307 * audio/video, the following check will be false,
308 * since sink is different. Result is Busy.
309 */
310 if (dev->active_link->sink->entity == sink &&
311 dev->active_link->source->entity == find_source) {
312 /*
313 * Either ALSA or Video own tuner. sink is
314 * the same for both. Prevent Video stepping
315 * on ALSA when ALSA owns the source.
316 */
317 if (dev->active_link_owner != entity &&
318 dev->active_link_owner->function ==
319 MEDIA_ENT_F_AUDIO_CAPTURE) {
320 pr_debug("ALSA has the tuner\n");
321 ret = -EBUSY;
322 goto end;
323 }
324 ret = 0;
325 goto end;
326 } else {
327 ret = -EBUSY;
328 goto end;
329 }
330 }
331
332 list_for_each_entry(link, &sink->links, list) {
333 /* Check sink, and source */
334 if (link->sink->entity == sink &&
335 link->source->entity == find_source) {
336 found_link = link;
337 break;
338 }
339 }
340
341 if (!found_link) {
342 ret = -ENODEV;
343 goto end;
344 }
345
346 /* activate link between source and sink and start pipeline */
347 source = found_link->source->entity;
348 ret = __media_entity_setup_link(found_link, MEDIA_LNK_FL_ENABLED);
349 if (ret) {
350 pr_err("Activate tuner link %s->%s. Error %d\n",
351 source->name, sink->name, ret);
352 goto end;
353 }
354
355 ret = __media_entity_pipeline_start(entity, pipe);
356 if (ret) {
357 pr_err("Start Pipeline: %s->%s Error %d\n",
358 source->name, entity->name, ret);
359 ret = __media_entity_setup_link(found_link, 0);
360 pr_err("Deactivate link Error %d\n", ret);
361 goto end;
362 }
363 /*
364 * save active link and active link owner to avoid audio
365 * deactivating video owned link from disable_source and
366 * vice versa
367 */
368 dev->active_link = found_link;
369 dev->active_link_owner = entity;
370 dev->active_source = source;
371 dev->active_sink = sink;
372
373 pr_debug("Enabled Source: %s->%s->%s Ret %d\n",
374 dev->active_source->name, dev->active_sink->name,
375 dev->active_link_owner->name, ret);
376end:
377 mutex_unlock(&mdev->graph_mutex);
378 pr_debug("au0828_enable_source() end %s %d %d\n",
379 entity->name, entity->function, ret);
380 return ret;
381#endif
382 return 0;
383}
384
385static void au0828_disable_source(struct media_entity *entity)
386{
387#ifdef CONFIG_MEDIA_CONTROLLER
388 int ret = 0;
389 struct media_device *mdev = entity->graph_obj.mdev;
390 struct au0828_dev *dev;
391
392 if (!mdev)
393 return;
394
395 mutex_lock(&mdev->graph_mutex);
396 dev = mdev->source_priv;
397
398 if (!dev->active_link) {
399 ret = -ENODEV;
400 goto end;
401 }
402
403 /* link is active - stop pipeline from source (tuner) */
404 if (dev->active_link->sink->entity == dev->active_sink &&
405 dev->active_link->source->entity == dev->active_source) {
406 /*
407 * prevent video from deactivating link when audio
408 * has active pipeline
409 */
410 if (dev->active_link_owner != entity)
411 goto end;
412 __media_entity_pipeline_stop(entity);
413 ret = __media_entity_setup_link(dev->active_link, 0);
414 if (ret)
415 pr_err("Deactivate link Error %d\n", ret);
416
417 pr_debug("Disabled Source: %s->%s->%s Ret %d\n",
418 dev->active_source->name, dev->active_sink->name,
419 dev->active_link_owner->name, ret);
420
421 dev->active_link = NULL;
422 dev->active_link_owner = NULL;
423 dev->active_source = NULL;
424 dev->active_sink = NULL;
425 }
426
427end:
428 mutex_unlock(&mdev->graph_mutex);
429#endif
430}
431
7b12adf6
SK
432static int au0828_media_device_register(struct au0828_dev *dev,
433 struct usb_device *udev)
434{
435#ifdef CONFIG_MEDIA_CONTROLLER
436 int ret;
437
f90c5d79
SK
438 if (!dev->media_dev)
439 return 0;
440
441 if (!media_devnode_is_registered(&dev->media_dev->devnode)) {
7b12adf6
SK
442
443 /* register media device */
444 ret = media_device_register(dev->media_dev);
445 if (ret) {
446 dev_err(&udev->dev,
447 "Media Device Register Error: %d\n", ret);
448 return ret;
449 }
450 }
f90c5d79
SK
451 /* register entity_notify callback */
452 dev->entity_notify.notify_data = (void *) dev;
453 dev->entity_notify.notify = (void *) au0828_media_graph_notify;
454 ret = media_device_register_entity_notify(dev->media_dev,
455 &dev->entity_notify);
456 if (ret) {
457 dev_err(&udev->dev,
458 "Media Device register entity_notify Error: %d\n",
459 ret);
460 return ret;
461 }
c94903f1
SK
462 /* set enable_source */
463 dev->media_dev->source_priv = (void *) dev;
464 dev->media_dev->enable_source = au0828_enable_source;
465 dev->media_dev->disable_source = au0828_disable_source;
7b12adf6
SK
466#endif
467 return 0;
468}
bed69196 469
18d73c58 470static int au0828_usb_probe(struct usb_interface *interface,
265a6510
ST
471 const struct usb_device_id *id)
472{
8a4e7866 473 int ifnum;
f251b3e7
TM
474 int retval = 0;
475
265a6510
ST
476 struct au0828_dev *dev;
477 struct usb_device *usbdev = interface_to_usbdev(interface);
478
479 ifnum = interface->altsetting->desc.bInterfaceNumber;
480
481 if (ifnum != 0)
482 return -ENODEV;
483
a9c36aad 484 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
265a6510
ST
485 le16_to_cpu(usbdev->descriptor.idVendor),
486 le16_to_cpu(usbdev->descriptor.idProduct),
487 ifnum);
488
ee3436b8
DH
489 /*
490 * Make sure we have 480 Mbps of bandwidth, otherwise things like
491 * video stream wouldn't likely work, since 12 Mbps is generally
492 * not enough even for most Digital TV streams.
493 */
d6a9a430 494 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
83afb32a
MCC
495 pr_err("au0828: Device initialization failed.\n");
496 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
ee3436b8
DH
497 return -ENODEV;
498 }
499
265a6510
ST
500 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
501 if (dev == NULL) {
83afb32a 502 pr_err("%s() Unable to allocate memory\n", __func__);
265a6510
ST
503 return -ENOMEM;
504 }
505
549ee4df
DH
506 mutex_init(&dev->lock);
507 mutex_lock(&dev->lock);
265a6510
ST
508 mutex_init(&dev->mutex);
509 mutex_init(&dev->dvb.lock);
510 dev->usbdev = usbdev;
f1add5b5 511 dev->boardnr = id->driver_info;
e42c8c6e
RLLC
512 dev->board = au0828_boards[dev->boardnr];
513
9832e155 514 /* Initialize the media controller */
9f806795
MCC
515 retval = au0828_media_device_init(dev, usbdev);
516 if (retval) {
517 pr_err("%s() au0828_media_device_init failed\n",
518 __func__);
519 mutex_unlock(&dev->lock);
520 kfree(dev);
521 return retval;
522 }
265a6510 523
7b606ffd 524 retval = au0828_v4l2_device_register(interface, dev);
b14667f3 525 if (retval) {
7b606ffd 526 au0828_usb_v4l2_media_release(dev);
549ee4df 527 mutex_unlock(&dev->lock);
b14667f3 528 kfree(dev);
e8c26f45 529 return retval;
b14667f3
DH
530 }
531
265a6510
ST
532 /* Power Up the bridge */
533 au0828_write(dev, REG_600, 1 << 4);
534
535 /* Bring up the GPIO's and supporting devices */
536 au0828_gpio_setup(dev);
537
538 /* I2C */
539 au0828_i2c_register(dev);
540
28930fa9
ST
541 /* Setup */
542 au0828_card_setup(dev);
543
8b2f0795 544 /* Analog TV */
7b606ffd
MCC
545 retval = au0828_analog_register(dev, interface);
546 if (retval) {
547 pr_err("%s() au0282_dev_register failed to register on V4L2\n",
548 __func__);
549 goto done;
82e92f4c 550 }
8b2f0795 551
265a6510 552 /* Digital TV */
f251b3e7
TM
553 retval = au0828_dvb_register(dev);
554 if (retval)
555 pr_err("%s() au0282_dev_register failed\n",
556 __func__);
557
2fcfd317
MCC
558 /* Remote controller */
559 au0828_rc_register(dev);
265a6510 560
2fcfd317
MCC
561 /*
562 * Store the pointer to the au0828_dev so it can be accessed in
563 * au0828_usb_disconnect
564 */
fe78a49c
DH
565 usb_set_intfdata(interface, dev);
566
83afb32a 567 pr_info("Registered device AU0828 [%s]\n",
f1add5b5 568 dev->board.name == NULL ? "Unset" : dev->board.name);
265a6510 569
549ee4df
DH
570 mutex_unlock(&dev->lock);
571
7b12adf6 572 retval = au0828_media_device_register(dev, usbdev);
9832e155
JMC
573
574done:
575 if (retval < 0)
576 au0828_usb_disconnect(interface);
577
f251b3e7 578 return retval;
265a6510
ST
579}
580
aaeac199
MCC
581static int au0828_suspend(struct usb_interface *interface,
582 pm_message_t message)
583{
584 struct au0828_dev *dev = usb_get_intfdata(interface);
585
586 if (!dev)
587 return 0;
588
81187240
MCC
589 pr_info("Suspend\n");
590
aaeac199 591 au0828_rc_suspend(dev);
1a1ba95e 592 au0828_v4l2_suspend(dev);
b799de75 593 au0828_dvb_suspend(dev);
aaeac199
MCC
594
595 /* FIXME: should suspend also ATV/DTV */
596
597 return 0;
598}
599
600static int au0828_resume(struct usb_interface *interface)
601{
602 struct au0828_dev *dev = usb_get_intfdata(interface);
603 if (!dev)
604 return 0;
605
81187240
MCC
606 pr_info("Resume\n");
607
fa500461
MCC
608 /* Power Up the bridge */
609 au0828_write(dev, REG_600, 1 << 4);
610
611 /* Bring up the GPIO's and supporting devices */
612 au0828_gpio_setup(dev);
613
aaeac199 614 au0828_rc_resume(dev);
1a1ba95e 615 au0828_v4l2_resume(dev);
b799de75 616 au0828_dvb_resume(dev);
aaeac199
MCC
617
618 /* FIXME: should resume also ATV/DTV */
619
620 return 0;
621}
622
265a6510 623static struct usb_driver au0828_usb_driver = {
83afb32a 624 .name = KBUILD_MODNAME,
265a6510
ST
625 .probe = au0828_usb_probe,
626 .disconnect = au0828_usb_disconnect,
627 .id_table = au0828_usb_id_table,
aaeac199
MCC
628 .suspend = au0828_suspend,
629 .resume = au0828_resume,
630 .reset_resume = au0828_resume,
265a6510
ST
631};
632
633static int __init au0828_init(void)
634{
635 int ret;
636
b33d24c4 637 if (au0828_debug & 1)
83afb32a 638 pr_info("%s() Debugging is enabled\n", __func__);
bc3c613c 639
b33d24c4 640 if (au0828_debug & 2)
83afb32a 641 pr_info("%s() USB Debugging is enabled\n", __func__);
bc3c613c 642
b33d24c4 643 if (au0828_debug & 4)
83afb32a 644 pr_info("%s() I2C Debugging is enabled\n", __func__);
bc3c613c 645
b33d24c4 646 if (au0828_debug & 8)
83afb32a 647 pr_info("%s() Bridge Debugging is enabled\n",
f07e8e4b 648 __func__);
bc3c613c 649
2fcfd317 650 if (au0828_debug & 16)
83afb32a 651 pr_info("%s() IR Debugging is enabled\n",
2fcfd317
MCC
652 __func__);
653
83afb32a 654 pr_info("au0828 driver loaded\n");
265a6510
ST
655
656 ret = usb_register(&au0828_usb_driver);
657 if (ret)
83afb32a 658 pr_err("usb_register failed, error = %d\n", ret);
265a6510
ST
659
660 return ret;
661}
662
663static void __exit au0828_exit(void)
664{
665 usb_deregister(&au0828_usb_driver);
666}
667
668module_init(au0828_init);
669module_exit(au0828_exit);
670
671MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
6d897616 672MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
265a6510 673MODULE_LICENSE("GPL");
2fcfd317 674MODULE_VERSION("0.0.3");