[media] au0828: enforce check for graph creation
[cascardo/linux.git] / drivers / media / usb / au0828 / au0828-core.c
1 /*
2  *  Driver for the Auvitek USB bridge
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "au0828.h"
23
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
27 #include <media/v4l2-common.h>
28 #include <linux/mutex.h>
29
30 /* Due to enum tuner_pad_index */
31 #include <media/tuner.h>
32
33 /*
34  * 1 = General debug messages
35  * 2 = USB handling
36  * 4 = I2C related
37  * 8 = Bridge related
38  * 16 = IR related
39  */
40 int au0828_debug;
41 module_param_named(debug, au0828_debug, int, 0644);
42 MODULE_PARM_DESC(debug,
43                  "set debug bitmask: 1=general, 2=USB, 4=I2C, 8=bridge, 16=IR");
44
45 static unsigned int disable_usb_speed_check;
46 module_param(disable_usb_speed_check, int, 0444);
47 MODULE_PARM_DESC(disable_usb_speed_check,
48                  "override min bandwidth requirement of 480M bps");
49
50 #define _AU0828_BULKPIPE 0x03
51 #define _BULKPIPESIZE 0xffff
52
53 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
54                             u16 index);
55 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
56         u16 index, unsigned char *cp, u16 size);
57
58 /* USB Direction */
59 #define CMD_REQUEST_IN          0x00
60 #define CMD_REQUEST_OUT         0x01
61
62 u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
63 {
64         u8 result = 0;
65
66         recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, &result, 1);
67         dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, result);
68
69         return result;
70 }
71
72 u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
73 {
74         dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
75         return send_control_msg(dev, CMD_REQUEST_OUT, val, reg);
76 }
77
78 static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
79         u16 index)
80 {
81         int status = -ENODEV;
82
83         if (dev->usbdev) {
84
85                 /* cp must be memory that has been allocated by kmalloc */
86                 status = usb_control_msg(dev->usbdev,
87                                 usb_sndctrlpipe(dev->usbdev, 0),
88                                 request,
89                                 USB_DIR_OUT | USB_TYPE_VENDOR |
90                                         USB_RECIP_DEVICE,
91                                 value, index, NULL, 0, 1000);
92
93                 status = min(status, 0);
94
95                 if (status < 0) {
96                         pr_err("%s() Failed sending control message, error %d.\n",
97                                 __func__, status);
98                 }
99
100         }
101
102         return status;
103 }
104
105 static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
106         u16 index, unsigned char *cp, u16 size)
107 {
108         int status = -ENODEV;
109         mutex_lock(&dev->mutex);
110         if (dev->usbdev) {
111                 status = usb_control_msg(dev->usbdev,
112                                 usb_rcvctrlpipe(dev->usbdev, 0),
113                                 request,
114                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
115                                 value, index,
116                                 dev->ctrlmsg, size, 1000);
117
118                 status = min(status, 0);
119
120                 if (status < 0) {
121                         pr_err("%s() Failed receiving control message, error %d.\n",
122                                 __func__, status);
123                 }
124
125                 /* the host controller requires heap allocated memory, which
126                    is why we didn't just pass "cp" into usb_control_msg */
127                 memcpy(cp, dev->ctrlmsg, size);
128         }
129         mutex_unlock(&dev->mutex);
130         return status;
131 }
132
133 static void au0828_unregister_media_device(struct au0828_dev *dev)
134 {
135
136 #ifdef CONFIG_MEDIA_CONTROLLER
137         if (dev->media_dev) {
138                 media_device_unregister(dev->media_dev);
139                 kfree(dev->media_dev);
140                 dev->media_dev = NULL;
141         }
142 #endif
143 }
144
145 static void au0828_usb_release(struct au0828_dev *dev)
146 {
147         au0828_unregister_media_device(dev);
148
149         /* I2C */
150         au0828_i2c_unregister(dev);
151
152         kfree(dev);
153 }
154
155 #ifdef CONFIG_VIDEO_AU0828_V4L2
156
157 static void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
158 {
159 #ifdef CONFIG_MEDIA_CONTROLLER
160         int i;
161
162         for (i = 0; i < AU0828_MAX_INPUT; i++) {
163                 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
164                         return;
165                 media_device_unregister_entity(&dev->input_ent[i]);
166         }
167 #endif
168 }
169
170 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
171 {
172         struct au0828_dev *dev =
173                 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
174
175         v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
176         v4l2_device_unregister(&dev->v4l2_dev);
177         au0828_usb_v4l2_media_release(dev);
178         au0828_usb_release(dev);
179 }
180 #endif
181
182 static void au0828_usb_disconnect(struct usb_interface *interface)
183 {
184         struct au0828_dev *dev = usb_get_intfdata(interface);
185
186         dprintk(1, "%s()\n", __func__);
187
188         /* there is a small window after disconnect, before
189            dev->usbdev is NULL, for poll (e.g: IR) try to access
190            the device and fill the dmesg with error messages.
191            Set the status so poll routines can check and avoid
192            access after disconnect.
193         */
194         dev->dev_state = DEV_DISCONNECTED;
195
196         au0828_rc_unregister(dev);
197         /* Digital TV */
198         au0828_dvb_unregister(dev);
199
200         usb_set_intfdata(interface, NULL);
201         mutex_lock(&dev->mutex);
202         dev->usbdev = NULL;
203         mutex_unlock(&dev->mutex);
204 #ifdef CONFIG_VIDEO_AU0828_V4L2
205         if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) {
206                 au0828_analog_unregister(dev);
207                 v4l2_device_disconnect(&dev->v4l2_dev);
208                 v4l2_device_put(&dev->v4l2_dev);
209                 /*
210                  * No need to call au0828_usb_release() if V4L2 is enabled,
211                  * as this is already called via au0828_usb_v4l2_release()
212                  */
213                 return;
214         }
215 #endif
216         au0828_usb_release(dev);
217 }
218
219 static void au0828_media_device_register(struct au0828_dev *dev,
220                                           struct usb_device *udev)
221 {
222 #ifdef CONFIG_MEDIA_CONTROLLER
223         struct media_device *mdev;
224         int ret;
225
226         mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
227         if (!mdev)
228                 return;
229
230         mdev->dev = &udev->dev;
231
232         if (!dev->board.name)
233                 strlcpy(mdev->model, "unknown au0828", sizeof(mdev->model));
234         else
235                 strlcpy(mdev->model, dev->board.name, sizeof(mdev->model));
236         if (udev->serial)
237                 strlcpy(mdev->serial, udev->serial, sizeof(mdev->serial));
238         strcpy(mdev->bus_info, udev->devpath);
239         mdev->hw_revision = le16_to_cpu(udev->descriptor.bcdDevice);
240         mdev->driver_version = LINUX_VERSION_CODE;
241
242         ret = media_device_register(mdev);
243         if (ret) {
244                 pr_err(
245                         "Couldn't create a media device. Error: %d\n",
246                         ret);
247                 kfree(mdev);
248                 return;
249         }
250
251         dev->media_dev = mdev;
252 #endif
253 }
254
255
256 static int au0828_create_media_graph(struct au0828_dev *dev)
257 {
258 #ifdef CONFIG_MEDIA_CONTROLLER
259         struct media_device *mdev = dev->media_dev;
260         struct media_entity *entity;
261         struct media_entity *tuner = NULL, *decoder = NULL;
262         int i, ret;
263
264         if (!mdev)
265                 return 0;
266
267         media_device_for_each_entity(entity, mdev) {
268                 switch (entity->type) {
269                 case MEDIA_ENT_T_V4L2_SUBDEV_TUNER:
270                         tuner = entity;
271                         break;
272                 case MEDIA_ENT_T_V4L2_SUBDEV_DECODER:
273                         decoder = entity;
274                         break;
275                 }
276         }
277
278         /* Analog setup, using tuner as a link */
279
280         /* Something bad happened! */
281         if (!decoder)
282                 return -EINVAL;
283
284         if (tuner) {
285                 ret = media_create_pad_link(tuner, TUNER_PAD_IF_OUTPUT,
286                                             decoder, 0,
287                                             MEDIA_LNK_FL_ENABLED);
288                 if (ret)
289                         return ret;
290         }
291         ret = media_create_pad_link(decoder, 1, &dev->vdev.entity, 0,
292                                     MEDIA_LNK_FL_ENABLED);
293         if (ret)
294                 return ret;
295         ret = media_create_pad_link(decoder, 2, &dev->vbi_dev.entity, 0,
296                                     MEDIA_LNK_FL_ENABLED);
297         if (ret)
298                 return ret;
299
300         for (i = 0; i < AU0828_MAX_INPUT; i++) {
301                 struct media_entity *ent = &dev->input_ent[i];
302
303                 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
304                         break;
305
306                 switch (AUVI_INPUT(i).type) {
307                 case AU0828_VMUX_CABLE:
308                 case AU0828_VMUX_TELEVISION:
309                 case AU0828_VMUX_DVB:
310                         if (!tuner)
311                                 break;
312
313                         ret = media_create_pad_link(ent, 0, tuner,
314                                                     TUNER_PAD_RF_INPUT,
315                                                     MEDIA_LNK_FL_ENABLED);
316                         if (ret)
317                                 return ret;
318                         break;
319                 case AU0828_VMUX_COMPOSITE:
320                 case AU0828_VMUX_SVIDEO:
321                 default: /* AU0828_VMUX_DEBUG */
322                         /* FIXME: fix the decoder PAD */
323                         ret = media_create_pad_link(ent, 0, decoder, 0, 0);
324                         if (ret)
325                                 return ret;
326                         break;
327                 }
328         }
329 #endif
330         return 0;
331 }
332
333 static int au0828_usb_probe(struct usb_interface *interface,
334         const struct usb_device_id *id)
335 {
336         int ifnum;
337         int retval = 0;
338
339         struct au0828_dev *dev;
340         struct usb_device *usbdev = interface_to_usbdev(interface);
341
342         ifnum = interface->altsetting->desc.bInterfaceNumber;
343
344         if (ifnum != 0)
345                 return -ENODEV;
346
347         dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
348                 le16_to_cpu(usbdev->descriptor.idVendor),
349                 le16_to_cpu(usbdev->descriptor.idProduct),
350                 ifnum);
351
352         /*
353          * Make sure we have 480 Mbps of bandwidth, otherwise things like
354          * video stream wouldn't likely work, since 12 Mbps is generally
355          * not enough even for most Digital TV streams.
356          */
357         if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
358                 pr_err("au0828: Device initialization failed.\n");
359                 pr_err("au0828: Device must be connected to a high-speed USB 2.0 port.\n");
360                 return -ENODEV;
361         }
362
363         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
364         if (dev == NULL) {
365                 pr_err("%s() Unable to allocate memory\n", __func__);
366                 return -ENOMEM;
367         }
368
369         mutex_init(&dev->lock);
370         mutex_lock(&dev->lock);
371         mutex_init(&dev->mutex);
372         mutex_init(&dev->dvb.lock);
373         dev->usbdev = usbdev;
374         dev->boardnr = id->driver_info;
375         dev->board = au0828_boards[dev->boardnr];
376
377         /* Register the media controller */
378         au0828_media_device_register(dev, usbdev);
379
380 #ifdef CONFIG_VIDEO_AU0828_V4L2
381         dev->v4l2_dev.release = au0828_usb_v4l2_release;
382
383         /* Create the v4l2_device */
384 #ifdef CONFIG_MEDIA_CONTROLLER
385         dev->v4l2_dev.mdev = dev->media_dev;
386 #endif
387         retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
388         if (retval) {
389                 pr_err("%s() v4l2_device_register failed\n",
390                        __func__);
391                 mutex_unlock(&dev->lock);
392                 kfree(dev);
393                 return retval;
394         }
395         /* This control handler will inherit the controls from au8522 */
396         retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
397         if (retval) {
398                 pr_err("%s() v4l2_ctrl_handler_init failed\n",
399                        __func__);
400                 mutex_unlock(&dev->lock);
401                 kfree(dev);
402                 return retval;
403         }
404         dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
405 #endif
406
407         /* Power Up the bridge */
408         au0828_write(dev, REG_600, 1 << 4);
409
410         /* Bring up the GPIO's and supporting devices */
411         au0828_gpio_setup(dev);
412
413         /* I2C */
414         au0828_i2c_register(dev);
415
416         /* Setup */
417         au0828_card_setup(dev);
418
419 #ifdef CONFIG_VIDEO_AU0828_V4L2
420         /* Analog TV */
421         if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
422                 au0828_analog_register(dev, interface);
423 #endif
424
425         /* Digital TV */
426         retval = au0828_dvb_register(dev);
427         if (retval)
428                 pr_err("%s() au0282_dev_register failed\n",
429                        __func__);
430
431         /* Remote controller */
432         au0828_rc_register(dev);
433
434         /*
435          * Store the pointer to the au0828_dev so it can be accessed in
436          * au0828_usb_disconnect
437          */
438         usb_set_intfdata(interface, dev);
439
440         pr_info("Registered device AU0828 [%s]\n",
441                 dev->board.name == NULL ? "Unset" : dev->board.name);
442
443         mutex_unlock(&dev->lock);
444
445         retval = au0828_create_media_graph(dev);
446         if (retval) {
447                 pr_err("%s() au0282_dev_register failed to create graph\n",
448                        __func__);
449                 au0828_usb_disconnect(interface);
450         }
451
452         return retval;
453 }
454
455 static int au0828_suspend(struct usb_interface *interface,
456                                 pm_message_t message)
457 {
458         struct au0828_dev *dev = usb_get_intfdata(interface);
459
460         if (!dev)
461                 return 0;
462
463         pr_info("Suspend\n");
464
465         au0828_rc_suspend(dev);
466         au0828_v4l2_suspend(dev);
467         au0828_dvb_suspend(dev);
468
469         /* FIXME: should suspend also ATV/DTV */
470
471         return 0;
472 }
473
474 static int au0828_resume(struct usb_interface *interface)
475 {
476         struct au0828_dev *dev = usb_get_intfdata(interface);
477         if (!dev)
478                 return 0;
479
480         pr_info("Resume\n");
481
482         /* Power Up the bridge */
483         au0828_write(dev, REG_600, 1 << 4);
484
485         /* Bring up the GPIO's and supporting devices */
486         au0828_gpio_setup(dev);
487
488         au0828_rc_resume(dev);
489         au0828_v4l2_resume(dev);
490         au0828_dvb_resume(dev);
491
492         /* FIXME: should resume also ATV/DTV */
493
494         return 0;
495 }
496
497 static struct usb_driver au0828_usb_driver = {
498         .name           = KBUILD_MODNAME,
499         .probe          = au0828_usb_probe,
500         .disconnect     = au0828_usb_disconnect,
501         .id_table       = au0828_usb_id_table,
502         .suspend        = au0828_suspend,
503         .resume         = au0828_resume,
504         .reset_resume   = au0828_resume,
505 };
506
507 static int __init au0828_init(void)
508 {
509         int ret;
510
511         if (au0828_debug & 1)
512                 pr_info("%s() Debugging is enabled\n", __func__);
513
514         if (au0828_debug & 2)
515                 pr_info("%s() USB Debugging is enabled\n", __func__);
516
517         if (au0828_debug & 4)
518                 pr_info("%s() I2C Debugging is enabled\n", __func__);
519
520         if (au0828_debug & 8)
521                 pr_info("%s() Bridge Debugging is enabled\n",
522                        __func__);
523
524         if (au0828_debug & 16)
525                 pr_info("%s() IR Debugging is enabled\n",
526                        __func__);
527
528         pr_info("au0828 driver loaded\n");
529
530         ret = usb_register(&au0828_usb_driver);
531         if (ret)
532                 pr_err("usb_register failed, error = %d\n", ret);
533
534         return ret;
535 }
536
537 static void __exit au0828_exit(void)
538 {
539         usb_deregister(&au0828_usb_driver);
540 }
541
542 module_init(au0828_init);
543 module_exit(au0828_exit);
544
545 MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
546 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
547 MODULE_LICENSE("GPL");
548 MODULE_VERSION("0.0.3");