aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/usb/em28xx
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/usb/em28xx')
-rw-r--r--drivers/media/usb/em28xx/Kconfig8
-rw-r--r--drivers/media/usb/em28xx/em28xx-cards.c270
-rw-r--r--drivers/media/usb/em28xx/em28xx-core.c296
-rw-r--r--drivers/media/usb/em28xx/em28xx-dvb.c96
-rw-r--r--drivers/media/usb/em28xx/em28xx-i2c.c293
-rw-r--r--drivers/media/usb/em28xx/em28xx-input.c359
-rw-r--r--drivers/media/usb/em28xx/em28xx-reg.h5
-rw-r--r--drivers/media/usb/em28xx/em28xx-vbi.c123
-rw-r--r--drivers/media/usb/em28xx/em28xx-video.c1689
-rw-r--r--drivers/media/usb/em28xx/em28xx.h149
10 files changed, 1490 insertions, 1798 deletions
diff --git a/drivers/media/usb/em28xx/Kconfig b/drivers/media/usb/em28xx/Kconfig
index 7a5bd61bd3bb..c754a80a8d8b 100644
--- a/drivers/media/usb/em28xx/Kconfig
+++ b/drivers/media/usb/em28xx/Kconfig
@@ -3,7 +3,7 @@ config VIDEO_EM28XX
3 depends on VIDEO_DEV && I2C 3 depends on VIDEO_DEV && I2C
4 select VIDEO_TUNER 4 select VIDEO_TUNER
5 select VIDEO_TVEEPROM 5 select VIDEO_TVEEPROM
6 select VIDEOBUF_VMALLOC 6 select VIDEOBUF2_VMALLOC
7 select VIDEO_SAA711X if MEDIA_SUBDRV_AUTOSELECT 7 select VIDEO_SAA711X if MEDIA_SUBDRV_AUTOSELECT
8 select VIDEO_TVP5150 if MEDIA_SUBDRV_AUTOSELECT 8 select VIDEO_TVP5150 if MEDIA_SUBDRV_AUTOSELECT
9 select VIDEO_MSP3400 if MEDIA_SUBDRV_AUTOSELECT 9 select VIDEO_MSP3400 if MEDIA_SUBDRV_AUTOSELECT
@@ -34,6 +34,7 @@ config VIDEO_EM28XX_DVB
34 tristate "DVB/ATSC Support for em28xx based TV cards" 34 tristate "DVB/ATSC Support for em28xx based TV cards"
35 depends on VIDEO_EM28XX && DVB_CORE 35 depends on VIDEO_EM28XX && DVB_CORE
36 select DVB_LGDT330X if MEDIA_SUBDRV_AUTOSELECT 36 select DVB_LGDT330X if MEDIA_SUBDRV_AUTOSELECT
37 select DVB_LGDT3305 if MEDIA_SUBDRV_AUTOSELECT
37 select DVB_ZL10353 if MEDIA_SUBDRV_AUTOSELECT 38 select DVB_ZL10353 if MEDIA_SUBDRV_AUTOSELECT
38 select DVB_TDA10023 if MEDIA_SUBDRV_AUTOSELECT 39 select DVB_TDA10023 if MEDIA_SUBDRV_AUTOSELECT
39 select DVB_S921 if MEDIA_SUBDRV_AUTOSELECT 40 select DVB_S921 if MEDIA_SUBDRV_AUTOSELECT
@@ -43,7 +44,10 @@ config VIDEO_EM28XX_DVB
43 select DVB_TDA18271C2DD if MEDIA_SUBDRV_AUTOSELECT 44 select DVB_TDA18271C2DD if MEDIA_SUBDRV_AUTOSELECT
44 select DVB_TDA10071 if MEDIA_SUBDRV_AUTOSELECT 45 select DVB_TDA10071 if MEDIA_SUBDRV_AUTOSELECT
45 select DVB_A8293 if MEDIA_SUBDRV_AUTOSELECT 46 select DVB_A8293 if MEDIA_SUBDRV_AUTOSELECT
46 select VIDEOBUF_DVB 47 select DVB_MT352 if MEDIA_SUBDRV_AUTOSELECT
48 select DVB_S5H1409 if MEDIA_SUBDRV_AUTOSELECT
49 select MEDIA_TUNER_QT1010 if MEDIA_SUBDRV_AUTOSELECT
50 select MEDIA_TUNER_TDA18271 if MEDIA_SUBDRV_AUTOSELECT
47 ---help--- 51 ---help---
48 This adds support for DVB cards based on the 52 This adds support for DVB cards based on the
49 Empiatech em28xx chips. 53 Empiatech em28xx chips.
diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c
index 619bffbab3bc..54a03b20de6e 100644
--- a/drivers/media/usb/em28xx/em28xx-cards.c
+++ b/drivers/media/usb/em28xx/em28xx-cards.c
@@ -6,6 +6,7 @@
6 Markus Rechberger <mrechberger@gmail.com> 6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org> 7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de> 8 Sascha Sommer <saschasommer@freenet.de>
9 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
9 10
10 This program is free software; you can redistribute it and/or modify 11 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by 12 it under the terms of the GNU General Public License as published by
@@ -56,10 +57,16 @@ module_param(disable_usb_speed_check, int, 0444);
56MODULE_PARM_DESC(disable_usb_speed_check, 57MODULE_PARM_DESC(disable_usb_speed_check,
57 "override min bandwidth requirement of 480M bps"); 58 "override min bandwidth requirement of 480M bps");
58 59
59static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 60static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
60module_param_array(card, int, NULL, 0444); 61module_param_array(card, int, NULL, 0444);
61MODULE_PARM_DESC(card, "card type"); 62MODULE_PARM_DESC(card, "card type");
62 63
64static int usb_xfer_mode = -1;
65module_param(usb_xfer_mode, int, 0444);
66MODULE_PARM_DESC(usb_xfer_mode,
67 "USB transfer mode for frame data (-1 = auto, 0 = prefer isoc, 1 = prefer bulk)");
68
69
63/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS - 1 */ 70/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS - 1 */
64static unsigned long em28xx_devused; 71static unsigned long em28xx_devused;
65 72
@@ -486,7 +493,7 @@ struct em28xx_board em28xx_boards[] = {
486 .input = { { 493 .input = { {
487 .type = EM28XX_VMUX_TELEVISION, 494 .type = EM28XX_VMUX_TELEVISION,
488 .vmux = SAA7115_COMPOSITE2, 495 .vmux = SAA7115_COMPOSITE2,
489 .amux = EM28XX_AMUX_LINE_IN, 496 .amux = EM28XX_AMUX_VIDEO,
490 }, { 497 }, {
491 .type = EM28XX_VMUX_COMPOSITE1, 498 .type = EM28XX_VMUX_COMPOSITE1,
492 .vmux = SAA7115_COMPOSITE0, 499 .vmux = SAA7115_COMPOSITE0,
@@ -2073,6 +2080,8 @@ struct usb_device_id em28xx_id_table[] = {
2073 .driver_info = EM2884_BOARD_TERRATEC_H5 }, 2080 .driver_info = EM2884_BOARD_TERRATEC_H5 },
2074 { USB_DEVICE(0x0ccd, 0x10ad), /* H5 Rev. 2 */ 2081 { USB_DEVICE(0x0ccd, 0x10ad), /* H5 Rev. 2 */
2075 .driver_info = EM2884_BOARD_TERRATEC_H5 }, 2082 .driver_info = EM2884_BOARD_TERRATEC_H5 },
2083 { USB_DEVICE(0x0ccd, 0x10b6), /* H5 Rev. 3 */
2084 .driver_info = EM2884_BOARD_TERRATEC_H5 },
2076 { USB_DEVICE(0x0ccd, 0x0084), 2085 { USB_DEVICE(0x0ccd, 0x0084),
2077 .driver_info = EM2860_BOARD_TERRATEC_AV350 }, 2086 .driver_info = EM2860_BOARD_TERRATEC_AV350 },
2078 { USB_DEVICE(0x0ccd, 0x0096), 2087 { USB_DEVICE(0x0ccd, 0x0096),
@@ -2905,7 +2914,7 @@ static void request_module_async(struct work_struct *work)
2905 2914
2906 if (dev->board.has_dvb) 2915 if (dev->board.has_dvb)
2907 request_module("em28xx-dvb"); 2916 request_module("em28xx-dvb");
2908 if (dev->board.ir_codes && !disable_ir) 2917 if ((dev->board.ir_codes || dev->board.has_ir_i2c) && !disable_ir)
2909 request_module("em28xx-rc"); 2918 request_module("em28xx-rc");
2910#endif /* CONFIG_MODULES */ 2919#endif /* CONFIG_MODULES */
2911} 2920}
@@ -2934,6 +2943,8 @@ void em28xx_release_resources(struct em28xx *dev)
2934 2943
2935 em28xx_i2c_unregister(dev); 2944 em28xx_i2c_unregister(dev);
2936 2945
2946 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2947
2937 v4l2_device_unregister(&dev->v4l2_dev); 2948 v4l2_device_unregister(&dev->v4l2_dev);
2938 2949
2939 usb_put_dev(dev->udev); 2950 usb_put_dev(dev->udev);
@@ -2950,9 +2961,14 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
2950 struct usb_interface *interface, 2961 struct usb_interface *interface,
2951 int minor) 2962 int minor)
2952{ 2963{
2964 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2953 int retval; 2965 int retval;
2966 static const char *default_chip_name = "em28xx";
2967 const char *chip_name = default_chip_name;
2954 2968
2955 dev->udev = udev; 2969 dev->udev = udev;
2970 mutex_init(&dev->vb_queue_lock);
2971 mutex_init(&dev->vb_vbi_queue_lock);
2956 mutex_init(&dev->ctrl_urb_lock); 2972 mutex_init(&dev->ctrl_urb_lock);
2957 spin_lock_init(&dev->slock); 2973 spin_lock_init(&dev->slock);
2958 2974
@@ -2978,51 +2994,62 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
2978 2994
2979 switch (dev->chip_id) { 2995 switch (dev->chip_id) {
2980 case CHIP_ID_EM2800: 2996 case CHIP_ID_EM2800:
2981 em28xx_info("chip ID is em2800\n"); 2997 chip_name = "em2800";
2982 break; 2998 break;
2983 case CHIP_ID_EM2710: 2999 case CHIP_ID_EM2710:
2984 em28xx_info("chip ID is em2710\n"); 3000 chip_name = "em2710";
2985 break; 3001 break;
2986 case CHIP_ID_EM2750: 3002 case CHIP_ID_EM2750:
2987 em28xx_info("chip ID is em2750\n"); 3003 chip_name = "em2750";
2988 break; 3004 break;
2989 case CHIP_ID_EM2820: 3005 case CHIP_ID_EM2820:
2990 em28xx_info("chip ID is em2820 (or em2710)\n"); 3006 chip_name = "em2710/2820";
2991 break; 3007 break;
2992 case CHIP_ID_EM2840: 3008 case CHIP_ID_EM2840:
2993 em28xx_info("chip ID is em2840\n"); 3009 chip_name = "em2840";
2994 break; 3010 break;
2995 case CHIP_ID_EM2860: 3011 case CHIP_ID_EM2860:
2996 em28xx_info("chip ID is em2860\n"); 3012 chip_name = "em2860";
2997 break; 3013 break;
2998 case CHIP_ID_EM2870: 3014 case CHIP_ID_EM2870:
2999 em28xx_info("chip ID is em2870\n"); 3015 chip_name = "em2870";
3000 dev->wait_after_write = 0; 3016 dev->wait_after_write = 0;
3001 break; 3017 break;
3002 case CHIP_ID_EM2874: 3018 case CHIP_ID_EM2874:
3003 em28xx_info("chip ID is em2874\n"); 3019 chip_name = "em2874";
3004 dev->reg_gpio_num = EM2874_R80_GPIO; 3020 dev->reg_gpio_num = EM2874_R80_GPIO;
3005 dev->wait_after_write = 0; 3021 dev->wait_after_write = 0;
3006 break; 3022 break;
3007 case CHIP_ID_EM28174: 3023 case CHIP_ID_EM28174:
3008 em28xx_info("chip ID is em28174\n"); 3024 chip_name = "em28174";
3009 dev->reg_gpio_num = EM2874_R80_GPIO; 3025 dev->reg_gpio_num = EM2874_R80_GPIO;
3010 dev->wait_after_write = 0; 3026 dev->wait_after_write = 0;
3011 break; 3027 break;
3012 case CHIP_ID_EM2883: 3028 case CHIP_ID_EM2883:
3013 em28xx_info("chip ID is em2882/em2883\n"); 3029 chip_name = "em2882/3";
3014 dev->wait_after_write = 0; 3030 dev->wait_after_write = 0;
3015 break; 3031 break;
3016 case CHIP_ID_EM2884: 3032 case CHIP_ID_EM2884:
3017 em28xx_info("chip ID is em2884\n"); 3033 chip_name = "em2884";
3018 dev->reg_gpio_num = EM2874_R80_GPIO; 3034 dev->reg_gpio_num = EM2874_R80_GPIO;
3019 dev->wait_after_write = 0; 3035 dev->wait_after_write = 0;
3020 break; 3036 break;
3021 default: 3037 default:
3022 em28xx_info("em28xx chip ID = %d\n", dev->chip_id); 3038 printk(KERN_INFO DRIVER_NAME
3039 ": unknown em28xx chip ID (%d)\n", dev->chip_id);
3023 } 3040 }
3024 } 3041 }
3025 3042
3043 if (chip_name != default_chip_name)
3044 printk(KERN_INFO DRIVER_NAME
3045 ": chip ID is %s\n", chip_name);
3046
3047 /*
3048 * For em2820/em2710, the name may change latter, after checking
3049 * if the device has a sensor (so, it is em2710) or not.
3050 */
3051 snprintf(dev->name, sizeof(dev->name), "%s #%d", chip_name, dev->devno);
3052
3026 if (dev->is_audio_only) { 3053 if (dev->is_audio_only) {
3027 retval = em28xx_audio_setup(dev); 3054 retval = em28xx_audio_setup(dev);
3028 if (retval) 3055 if (retval)
@@ -3039,6 +3066,14 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
3039 3066
3040 em28xx_pre_card_setup(dev); 3067 em28xx_pre_card_setup(dev);
3041 3068
3069 if (dev->chip_id == CHIP_ID_EM2820) {
3070 if (dev->board.is_webcam)
3071 chip_name = "em2710";
3072 else
3073 chip_name = "em2820";
3074 snprintf(dev->name, sizeof(dev->name), "%s #%d", chip_name, dev->devno);
3075 }
3076
3042 if (!dev->board.is_em2800) { 3077 if (!dev->board.is_em2800) {
3043 /* Resets I2C speed */ 3078 /* Resets I2C speed */
3044 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, dev->board.i2c_speed); 3079 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, dev->board.i2c_speed);
@@ -3056,6 +3091,9 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
3056 return retval; 3091 return retval;
3057 } 3092 }
3058 3093
3094 v4l2_ctrl_handler_init(hdl, 4);
3095 dev->v4l2_dev.ctrl_handler = hdl;
3096
3059 /* register i2c bus */ 3097 /* register i2c bus */
3060 retval = em28xx_i2c_register(dev); 3098 retval = em28xx_i2c_register(dev);
3061 if (retval < 0) { 3099 if (retval < 0) {
@@ -3081,6 +3119,18 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
3081 __func__, retval); 3119 __func__, retval);
3082 goto fail; 3120 goto fail;
3083 } 3121 }
3122 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
3123 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
3124 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
3125 v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
3126 V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f);
3127 } else {
3128 /* install the em28xx notify callback */
3129 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE),
3130 em28xx_ctrl_notify, dev);
3131 v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME),
3132 em28xx_ctrl_notify, dev);
3133 }
3084 3134
3085 /* wake i2c devices */ 3135 /* wake i2c devices */
3086 em28xx_wake_i2c(dev); 3136 em28xx_wake_i2c(dev);
@@ -3110,6 +3160,11 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
3110 msleep(3); 3160 msleep(3);
3111 } 3161 }
3112 3162
3163 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
3164 retval = dev->ctrl_handler.error;
3165 if (retval)
3166 goto fail;
3167
3113 retval = em28xx_register_analog_devices(dev); 3168 retval = em28xx_register_analog_devices(dev);
3114 if (retval < 0) { 3169 if (retval < 0) {
3115 goto fail; 3170 goto fail;
@@ -3122,6 +3177,7 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
3122 3177
3123fail: 3178fail:
3124 em28xx_i2c_unregister(dev); 3179 em28xx_i2c_unregister(dev);
3180 v4l2_ctrl_handler_free(&dev->ctrl_handler);
3125 3181
3126unregister_dev: 3182unregister_dev:
3127 v4l2_device_unregister(&dev->v4l2_dev); 3183 v4l2_device_unregister(&dev->v4l2_dev);
@@ -3143,7 +3199,7 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3143 struct em28xx *dev = NULL; 3199 struct em28xx *dev = NULL;
3144 int retval; 3200 int retval;
3145 bool has_audio = false, has_video = false, has_dvb = false; 3201 bool has_audio = false, has_video = false, has_dvb = false;
3146 int i, nr; 3202 int i, nr, try_bulk;
3147 const int ifnum = interface->altsetting[0].desc.bInterfaceNumber; 3203 const int ifnum = interface->altsetting[0].desc.bInterfaceNumber;
3148 char *speed; 3204 char *speed;
3149 3205
@@ -3183,9 +3239,10 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3183 } 3239 }
3184 3240
3185 /* compute alternate max packet sizes */ 3241 /* compute alternate max packet sizes */
3186 dev->alt_max_pkt_size = kmalloc(sizeof(dev->alt_max_pkt_size[0]) * 3242 dev->alt_max_pkt_size_isoc =
3243 kmalloc(sizeof(dev->alt_max_pkt_size_isoc[0]) *
3187 interface->num_altsetting, GFP_KERNEL); 3244 interface->num_altsetting, GFP_KERNEL);
3188 if (dev->alt_max_pkt_size == NULL) { 3245 if (dev->alt_max_pkt_size_isoc == NULL) {
3189 em28xx_errdev("out of memory!\n"); 3246 em28xx_errdev("out of memory!\n");
3190 kfree(dev); 3247 kfree(dev);
3191 retval = -ENOMEM; 3248 retval = -ENOMEM;
@@ -3208,25 +3265,67 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3208 if (udev->speed == USB_SPEED_HIGH) 3265 if (udev->speed == USB_SPEED_HIGH)
3209 size = size * hb_mult(sizedescr); 3266 size = size * hb_mult(sizedescr);
3210 3267
3211 if (usb_endpoint_xfer_isoc(e) && 3268 if (usb_endpoint_dir_in(e)) {
3212 usb_endpoint_dir_in(e)) {
3213 switch (e->bEndpointAddress) { 3269 switch (e->bEndpointAddress) {
3214 case EM28XX_EP_AUDIO: 3270 case 0x82:
3215 has_audio = true;
3216 break;
3217 case EM28XX_EP_ANALOG:
3218 has_video = true; 3271 has_video = true;
3219 dev->alt_max_pkt_size[i] = size; 3272 if (usb_endpoint_xfer_isoc(e)) {
3273 dev->analog_ep_isoc =
3274 e->bEndpointAddress;
3275 dev->alt_max_pkt_size_isoc[i] = size;
3276 } else if (usb_endpoint_xfer_bulk(e)) {
3277 dev->analog_ep_bulk =
3278 e->bEndpointAddress;
3279 }
3220 break; 3280 break;
3221 case EM28XX_EP_DIGITAL: 3281 case 0x83:
3222 has_dvb = true; 3282 if (usb_endpoint_xfer_isoc(e)) {
3223 if (size > dev->dvb_max_pkt_size) { 3283 has_audio = true;
3224 dev->dvb_max_pkt_size = size; 3284 } else {
3225 dev->dvb_alt = i; 3285 printk(KERN_INFO DRIVER_NAME
3286 ": error: skipping audio endpoint 0x83, because it uses bulk transfers !\n");
3287 }
3288 break;
3289 case 0x84:
3290 if (has_video &&
3291 (usb_endpoint_xfer_bulk(e))) {
3292 dev->analog_ep_bulk =
3293 e->bEndpointAddress;
3294 } else {
3295 has_dvb = true;
3296 if (usb_endpoint_xfer_isoc(e)) {
3297 dev->dvb_ep_isoc = e->bEndpointAddress;
3298 if (size > dev->dvb_max_pkt_size_isoc) {
3299 dev->dvb_max_pkt_size_isoc = size;
3300 dev->dvb_alt_isoc = i;
3301 }
3302 } else {
3303 dev->dvb_ep_bulk = e->bEndpointAddress;
3304 }
3226 } 3305 }
3227 break; 3306 break;
3228 } 3307 }
3229 } 3308 }
3309 /* NOTE:
3310 * Old logic with support for isoc transfers only was:
3311 * 0x82 isoc => analog
3312 * 0x83 isoc => audio
3313 * 0x84 isoc => digital
3314 *
3315 * New logic with support for bulk transfers
3316 * 0x82 isoc => analog
3317 * 0x82 bulk => analog
3318 * 0x83 isoc* => audio
3319 * 0x84 isoc => digital
3320 * 0x84 bulk => analog or digital**
3321 * (*: audio should always be isoc)
3322 * (**: analog, if ep 0x82 is isoc, otherwise digital)
3323 *
3324 * The new logic preserves backwards compatibility and
3325 * reflects the endpoint configurations we have seen
3326 * so far. But there might be devices for which this
3327 * logic is not sufficient...
3328 */
3230 } 3329 }
3231 } 3330 }
3232 3331
@@ -3261,19 +3360,6 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3261 ifnum, 3360 ifnum,
3262 interface->altsetting->desc.bInterfaceNumber); 3361 interface->altsetting->desc.bInterfaceNumber);
3263 3362
3264 if (has_audio)
3265 printk(KERN_INFO DRIVER_NAME
3266 ": Audio Vendor Class interface %i found\n",
3267 ifnum);
3268 if (has_video)
3269 printk(KERN_INFO DRIVER_NAME
3270 ": Video interface %i found\n",
3271 ifnum);
3272 if (has_dvb)
3273 printk(KERN_INFO DRIVER_NAME
3274 ": DVB interface %i found\n",
3275 ifnum);
3276
3277 /* 3363 /*
3278 * Make sure we have 480 Mbps of bandwidth, otherwise things like 3364 * Make sure we have 480 Mbps of bandwidth, otherwise things like
3279 * video stream wouldn't likely work, since 12 Mbps is generally 3365 * video stream wouldn't likely work, since 12 Mbps is generally
@@ -3287,7 +3373,6 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3287 goto err_free; 3373 goto err_free;
3288 } 3374 }
3289 3375
3290 snprintf(dev->name, sizeof(dev->name), "em28xx #%d", nr);
3291 dev->devno = nr; 3376 dev->devno = nr;
3292 dev->model = id->driver_info; 3377 dev->model = id->driver_info;
3293 dev->alt = -1; 3378 dev->alt = -1;
@@ -3304,6 +3389,24 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3304 } 3389 }
3305 } 3390 }
3306 3391
3392 if (has_audio)
3393 printk(KERN_INFO DRIVER_NAME
3394 ": Audio interface %i found %s\n",
3395 ifnum,
3396 dev->has_audio_class ? "(USB Audio Class)" : "(Vendor Class)");
3397 if (has_video)
3398 printk(KERN_INFO DRIVER_NAME
3399 ": Video interface %i found:%s%s\n",
3400 ifnum,
3401 dev->analog_ep_bulk ? " bulk" : "",
3402 dev->analog_ep_isoc ? " isoc" : "");
3403 if (has_dvb)
3404 printk(KERN_INFO DRIVER_NAME
3405 ": DVB interface %i found:%s%s\n",
3406 ifnum,
3407 dev->dvb_ep_bulk ? " bulk" : "",
3408 dev->dvb_ep_isoc ? " isoc" : "");
3409
3307 dev->num_alt = interface->num_altsetting; 3410 dev->num_alt = interface->num_altsetting;
3308 3411
3309 if ((unsigned)card[nr] < em28xx_bcount) 3412 if ((unsigned)card[nr] < em28xx_bcount)
@@ -3312,6 +3415,9 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3312 /* save our data pointer in this interface device */ 3415 /* save our data pointer in this interface device */
3313 usb_set_intfdata(interface, dev); 3416 usb_set_intfdata(interface, dev);
3314 3417
3418 /* initialize videobuf2 stuff */
3419 em28xx_vb2_setup(dev);
3420
3315 /* allocate device struct */ 3421 /* allocate device struct */
3316 mutex_init(&dev->lock); 3422 mutex_init(&dev->lock);
3317 mutex_lock(&dev->lock); 3423 mutex_lock(&dev->lock);
@@ -3320,13 +3426,46 @@ static int em28xx_usb_probe(struct usb_interface *interface,
3320 goto unlock_and_free; 3426 goto unlock_and_free;
3321 } 3427 }
3322 3428
3429 if (usb_xfer_mode < 0) {
3430 if (dev->board.is_webcam)
3431 try_bulk = 1;
3432 else
3433 try_bulk = 0;
3434 } else {
3435 try_bulk = usb_xfer_mode > 0;
3436 }
3437
3438 /* Select USB transfer types to use */
3439 if (has_video) {
3440 if (!dev->analog_ep_isoc || (try_bulk && dev->analog_ep_bulk))
3441 dev->analog_xfer_bulk = 1;
3442 em28xx_info("analog set to %s mode.\n",
3443 dev->analog_xfer_bulk ? "bulk" : "isoc");
3444 }
3323 if (has_dvb) { 3445 if (has_dvb) {
3324 /* pre-allocate DVB isoc transfer buffers */ 3446 if (!dev->dvb_ep_isoc || (try_bulk && dev->dvb_ep_bulk))
3325 retval = em28xx_alloc_isoc(dev, EM28XX_DIGITAL_MODE, 3447 dev->dvb_xfer_bulk = 1;
3326 EM28XX_DVB_MAX_PACKETS, 3448
3327 EM28XX_DVB_NUM_BUFS, 3449 em28xx_info("dvb set to %s mode.\n",
3328 dev->dvb_max_pkt_size); 3450 dev->dvb_xfer_bulk ? "bulk" : "isoc");
3451
3452 /* pre-allocate DVB usb transfer buffers */
3453 if (dev->dvb_xfer_bulk) {
3454 retval = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
3455 dev->dvb_xfer_bulk,
3456 EM28XX_DVB_NUM_BUFS,
3457 512,
3458 EM28XX_DVB_BULK_PACKET_MULTIPLIER);
3459 } else {
3460 retval = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
3461 dev->dvb_xfer_bulk,
3462 EM28XX_DVB_NUM_BUFS,
3463 dev->dvb_max_pkt_size_isoc,
3464 EM28XX_DVB_NUM_ISOC_PACKETS);
3465 }
3329 if (retval) { 3466 if (retval) {
3467 printk(DRIVER_NAME
3468 ": Failed to pre-allocate USB transfer buffers for DVB.\n");
3330 goto unlock_and_free; 3469 goto unlock_and_free;
3331 } 3470 }
3332 } 3471 }
@@ -3344,7 +3483,7 @@ unlock_and_free:
3344 mutex_unlock(&dev->lock); 3483 mutex_unlock(&dev->lock);
3345 3484
3346err_free: 3485err_free:
3347 kfree(dev->alt_max_pkt_size); 3486 kfree(dev->alt_max_pkt_size_isoc);
3348 kfree(dev); 3487 kfree(dev);
3349 3488
3350err: 3489err:
@@ -3370,6 +3509,8 @@ static void em28xx_usb_disconnect(struct usb_interface *interface)
3370 if (!dev) 3509 if (!dev)
3371 return; 3510 return;
3372 3511
3512 dev->disconnected = 1;
3513
3373 if (dev->is_audio_only) { 3514 if (dev->is_audio_only) {
3374 mutex_lock(&dev->lock); 3515 mutex_lock(&dev->lock);
3375 em28xx_close_extension(dev); 3516 em28xx_close_extension(dev);
@@ -3381,35 +3522,28 @@ static void em28xx_usb_disconnect(struct usb_interface *interface)
3381 3522
3382 flush_request_modules(dev); 3523 flush_request_modules(dev);
3383 3524
3384 /* wait until all current v4l2 io is finished then deallocate
3385 resources */
3386 mutex_lock(&dev->lock); 3525 mutex_lock(&dev->lock);
3387 3526
3388 v4l2_device_disconnect(&dev->v4l2_dev); 3527 v4l2_device_disconnect(&dev->v4l2_dev);
3389 3528
3390 if (dev->users) { 3529 if (dev->users) {
3391 em28xx_warn 3530 em28xx_warn("device %s is open! Deregistration and memory deallocation are deferred on close.\n",
3392 ("device %s is open! Deregistration and memory " 3531 video_device_node_name(dev->vdev));
3393 "deallocation are deferred on close.\n", 3532
3394 video_device_node_name(dev->vdev)); 3533 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
3395 3534 em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
3396 dev->state |= DEV_MISCONFIGURED;
3397 em28xx_uninit_isoc(dev, dev->mode);
3398 dev->state |= DEV_DISCONNECTED;
3399 } else {
3400 dev->state |= DEV_DISCONNECTED;
3401 em28xx_release_resources(dev);
3402 } 3535 }
3403 3536
3404 /* free DVB isoc buffers */ 3537 em28xx_close_extension(dev);
3405 em28xx_uninit_isoc(dev, EM28XX_DIGITAL_MODE); 3538 /* NOTE: must be called BEFORE the resources are released */
3406 3539
3407 mutex_unlock(&dev->lock); 3540 if (!dev->users)
3541 em28xx_release_resources(dev);
3408 3542
3409 em28xx_close_extension(dev); 3543 mutex_unlock(&dev->lock);
3410 3544
3411 if (!dev->users) { 3545 if (!dev->users) {
3412 kfree(dev->alt_max_pkt_size); 3546 kfree(dev->alt_max_pkt_size_isoc);
3413 kfree(dev); 3547 kfree(dev);
3414 } 3548 }
3415} 3549}
diff --git a/drivers/media/usb/em28xx/em28xx-core.c b/drivers/media/usb/em28xx/em28xx-core.c
index bed07a6c33f8..aaedd11791f2 100644
--- a/drivers/media/usb/em28xx/em28xx-core.c
+++ b/drivers/media/usb/em28xx/em28xx-core.c
@@ -5,6 +5,7 @@
5 Markus Rechberger <mrechberger@gmail.com> 5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org> 6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de> 7 Sascha Sommer <saschasommer@freenet.de>
8 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
8 9
9 This program is free software; you can redistribute it and/or modify 10 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by 11 it under the terms of the GNU General Public License as published by
@@ -76,7 +77,7 @@ int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
76 int ret; 77 int ret;
77 int pipe = usb_rcvctrlpipe(dev->udev, 0); 78 int pipe = usb_rcvctrlpipe(dev->udev, 0);
78 79
79 if (dev->state & DEV_DISCONNECTED) 80 if (dev->disconnected)
80 return -ENODEV; 81 return -ENODEV;
81 82
82 if (len > URB_MAX_CTRL_SIZE) 83 if (len > URB_MAX_CTRL_SIZE)
@@ -100,7 +101,7 @@ int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
100 if (reg_debug) 101 if (reg_debug)
101 printk(" failed!\n"); 102 printk(" failed!\n");
102 mutex_unlock(&dev->ctrl_urb_lock); 103 mutex_unlock(&dev->ctrl_urb_lock);
103 return ret; 104 return usb_translate_errors(ret);
104 } 105 }
105 106
106 if (len) 107 if (len)
@@ -152,7 +153,7 @@ int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
152 int ret; 153 int ret;
153 int pipe = usb_sndctrlpipe(dev->udev, 0); 154 int pipe = usb_sndctrlpipe(dev->udev, 0);
154 155
155 if (dev->state & DEV_DISCONNECTED) 156 if (dev->disconnected)
156 return -ENODEV; 157 return -ENODEV;
157 158
158 if ((len < 1) || (len > URB_MAX_CTRL_SIZE)) 159 if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
@@ -181,6 +182,9 @@ int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
181 0x0000, reg, dev->urb_buf, len, HZ); 182 0x0000, reg, dev->urb_buf, len, HZ);
182 mutex_unlock(&dev->ctrl_urb_lock); 183 mutex_unlock(&dev->ctrl_urb_lock);
183 184
185 if (ret < 0)
186 return usb_translate_errors(ret);
187
184 if (dev->wait_after_write) 188 if (dev->wait_after_write)
185 msleep(dev->wait_after_write); 189 msleep(dev->wait_after_write);
186 190
@@ -729,22 +733,24 @@ static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
729 return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1); 733 return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1);
730} 734}
731 735
732static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart, 736static void em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
733 u16 width, u16 height) 737 u16 width, u16 height)
734{ 738{
735 u8 cwidth = width; 739 u8 cwidth = width >> 2;
736 u8 cheight = height; 740 u8 cheight = height >> 2;
737 u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01); 741 u8 overflow = (height >> 9 & 0x02) | (width >> 10 & 0x01);
742 /* NOTE: size limit: 2047x1023 = 2MPix */
738 743
739 em28xx_coredbg("em28xx Area Set: (%d,%d)\n", 744 em28xx_coredbg("capture area set to (%d,%d): %dx%d\n",
740 (width | (overflow & 2) << 7), 745 hstart, vstart,
741 (height | (overflow & 1) << 8)); 746 ((overflow & 2) << 9 | cwidth << 2),
747 ((overflow & 1) << 10 | cheight << 2));
742 748
743 em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1); 749 em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1);
744 em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1); 750 em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1);
745 em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1); 751 em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1);
746 em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1); 752 em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1);
747 return em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1); 753 em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1);
748} 754}
749 755
750static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v) 756static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
@@ -797,28 +803,30 @@ int em28xx_resolution_set(struct em28xx *dev)
797 it out, we end up with the same format as the rest of the VBI 803 it out, we end up with the same format as the rest of the VBI
798 region */ 804 region */
799 if (em28xx_vbi_supported(dev) == 1) 805 if (em28xx_vbi_supported(dev) == 1)
800 em28xx_capture_area_set(dev, 0, 2, width >> 2, height >> 2); 806 em28xx_capture_area_set(dev, 0, 2, width, height);
801 else 807 else
802 em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2); 808 em28xx_capture_area_set(dev, 0, 0, width, height);
803 809
804 return em28xx_scaler_set(dev, dev->hscale, dev->vscale); 810 return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
805} 811}
806 812
813/* Set USB alternate setting for analog video */
807int em28xx_set_alternate(struct em28xx *dev) 814int em28xx_set_alternate(struct em28xx *dev)
808{ 815{
809 int errCode, prev_alt = dev->alt; 816 int errCode;
810 int i; 817 int i;
811 unsigned int min_pkt_size = dev->width * 2 + 4; 818 unsigned int min_pkt_size = dev->width * 2 + 4;
812 819
813 /* 820 /* NOTE: for isoc transfers, only alt settings > 0 are allowed
814 * alt = 0 is used only for control messages, so, only values 821 bulk transfers seem to work only with alt=0 ! */
815 * greater than 0 can be used for streaming. 822 dev->alt = 0;
816 */ 823 if ((alt > 0) && (alt < dev->num_alt)) {
817 if (alt && alt < dev->num_alt) {
818 em28xx_coredbg("alternate forced to %d\n", dev->alt); 824 em28xx_coredbg("alternate forced to %d\n", dev->alt);
819 dev->alt = alt; 825 dev->alt = alt;
820 goto set_alt; 826 goto set_alt;
821 } 827 }
828 if (dev->analog_xfer_bulk)
829 goto set_alt;
822 830
823 /* When image size is bigger than a certain value, 831 /* When image size is bigger than a certain value,
824 the frame size should be increased, otherwise, only 832 the frame size should be increased, otherwise, only
@@ -829,30 +837,38 @@ int em28xx_set_alternate(struct em28xx *dev)
829 837
830 for (i = 0; i < dev->num_alt; i++) { 838 for (i = 0; i < dev->num_alt; i++) {
831 /* stop when the selected alt setting offers enough bandwidth */ 839 /* stop when the selected alt setting offers enough bandwidth */
832 if (dev->alt_max_pkt_size[i] >= min_pkt_size) { 840 if (dev->alt_max_pkt_size_isoc[i] >= min_pkt_size) {
833 dev->alt = i; 841 dev->alt = i;
834 break; 842 break;
835 /* otherwise make sure that we end up with the maximum bandwidth 843 /* otherwise make sure that we end up with the maximum bandwidth
836 because the min_pkt_size equation might be wrong... 844 because the min_pkt_size equation might be wrong...
837 */ 845 */
838 } else if (dev->alt_max_pkt_size[i] > 846 } else if (dev->alt_max_pkt_size_isoc[i] >
839 dev->alt_max_pkt_size[dev->alt]) 847 dev->alt_max_pkt_size_isoc[dev->alt])
840 dev->alt = i; 848 dev->alt = i;
841 } 849 }
842 850
843set_alt: 851set_alt:
844 if (dev->alt != prev_alt) { 852 /* NOTE: for bulk transfers, we need to call usb_set_interface()
853 * even if the previous settings were the same. Otherwise streaming
854 * fails with all urbs having status = -EOVERFLOW ! */
855 if (dev->analog_xfer_bulk) {
856 dev->max_pkt_size = 512; /* USB 2.0 spec */
857 dev->packet_multiplier = EM28XX_BULK_PACKET_MULTIPLIER;
858 } else { /* isoc */
845 em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n", 859 em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
846 min_pkt_size, dev->alt); 860 min_pkt_size, dev->alt);
847 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt]; 861 dev->max_pkt_size =
848 em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", 862 dev->alt_max_pkt_size_isoc[dev->alt];
849 dev->alt, dev->max_pkt_size); 863 dev->packet_multiplier = EM28XX_NUM_ISOC_PACKETS;
850 errCode = usb_set_interface(dev->udev, 0, dev->alt); 864 }
851 if (errCode < 0) { 865 em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
852 em28xx_errdev("cannot change alternate number to %d (error=%i)\n", 866 dev->alt, dev->max_pkt_size);
853 dev->alt, errCode); 867 errCode = usb_set_interface(dev->udev, 0, dev->alt);
854 return errCode; 868 if (errCode < 0) {
855 } 869 em28xx_errdev("cannot change alternate number to %d (error=%i)\n",
870 dev->alt, errCode);
871 return errCode;
856 } 872 }
857 return 0; 873 return 0;
858} 874}
@@ -919,7 +935,7 @@ EXPORT_SYMBOL_GPL(em28xx_set_mode);
919 ------------------------------------------------------------------*/ 935 ------------------------------------------------------------------*/
920 936
921/* 937/*
922 * IRQ callback, called by URB callback 938 * URB completion handler for isoc/bulk transfers
923 */ 939 */
924static void em28xx_irq_callback(struct urb *urb) 940static void em28xx_irq_callback(struct urb *urb)
925{ 941{
@@ -941,11 +957,12 @@ static void em28xx_irq_callback(struct urb *urb)
941 957
942 /* Copy data from URB */ 958 /* Copy data from URB */
943 spin_lock(&dev->slock); 959 spin_lock(&dev->slock);
944 dev->isoc_ctl.isoc_copy(dev, urb); 960 dev->usb_ctl.urb_data_copy(dev, urb);
945 spin_unlock(&dev->slock); 961 spin_unlock(&dev->slock);
946 962
947 /* Reset urb buffers */ 963 /* Reset urb buffers */
948 for (i = 0; i < urb->number_of_packets; i++) { 964 for (i = 0; i < urb->number_of_packets; i++) {
965 /* isoc only (bulk: number_of_packets = 0) */
949 urb->iso_frame_desc[i].status = 0; 966 urb->iso_frame_desc[i].status = 0;
950 urb->iso_frame_desc[i].actual_length = 0; 967 urb->iso_frame_desc[i].actual_length = 0;
951 } 968 }
@@ -961,49 +978,50 @@ static void em28xx_irq_callback(struct urb *urb)
961/* 978/*
962 * Stop and Deallocate URBs 979 * Stop and Deallocate URBs
963 */ 980 */
964void em28xx_uninit_isoc(struct em28xx *dev, enum em28xx_mode mode) 981void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
965{ 982{
966 struct urb *urb; 983 struct urb *urb;
967 struct em28xx_usb_isoc_bufs *isoc_bufs; 984 struct em28xx_usb_bufs *usb_bufs;
968 int i; 985 int i;
969 986
970 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc in mode %d\n", mode); 987 em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
988 mode);
971 989
972 if (mode == EM28XX_DIGITAL_MODE) 990 if (mode == EM28XX_DIGITAL_MODE)
973 isoc_bufs = &dev->isoc_ctl.digital_bufs; 991 usb_bufs = &dev->usb_ctl.digital_bufs;
974 else 992 else
975 isoc_bufs = &dev->isoc_ctl.analog_bufs; 993 usb_bufs = &dev->usb_ctl.analog_bufs;
976 994
977 for (i = 0; i < isoc_bufs->num_bufs; i++) { 995 for (i = 0; i < usb_bufs->num_bufs; i++) {
978 urb = isoc_bufs->urb[i]; 996 urb = usb_bufs->urb[i];
979 if (urb) { 997 if (urb) {
980 if (!irqs_disabled()) 998 if (!irqs_disabled())
981 usb_kill_urb(urb); 999 usb_kill_urb(urb);
982 else 1000 else
983 usb_unlink_urb(urb); 1001 usb_unlink_urb(urb);
984 1002
985 if (isoc_bufs->transfer_buffer[i]) { 1003 if (usb_bufs->transfer_buffer[i]) {
986 usb_free_coherent(dev->udev, 1004 usb_free_coherent(dev->udev,
987 urb->transfer_buffer_length, 1005 urb->transfer_buffer_length,
988 isoc_bufs->transfer_buffer[i], 1006 usb_bufs->transfer_buffer[i],
989 urb->transfer_dma); 1007 urb->transfer_dma);
990 } 1008 }
991 usb_free_urb(urb); 1009 usb_free_urb(urb);
992 isoc_bufs->urb[i] = NULL; 1010 usb_bufs->urb[i] = NULL;
993 } 1011 }
994 isoc_bufs->transfer_buffer[i] = NULL; 1012 usb_bufs->transfer_buffer[i] = NULL;
995 } 1013 }
996 1014
997 kfree(isoc_bufs->urb); 1015 kfree(usb_bufs->urb);
998 kfree(isoc_bufs->transfer_buffer); 1016 kfree(usb_bufs->transfer_buffer);
999 1017
1000 isoc_bufs->urb = NULL; 1018 usb_bufs->urb = NULL;
1001 isoc_bufs->transfer_buffer = NULL; 1019 usb_bufs->transfer_buffer = NULL;
1002 isoc_bufs->num_bufs = 0; 1020 usb_bufs->num_bufs = 0;
1003 1021
1004 em28xx_capture_start(dev, 0); 1022 em28xx_capture_start(dev, 0);
1005} 1023}
1006EXPORT_SYMBOL_GPL(em28xx_uninit_isoc); 1024EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer);
1007 1025
1008/* 1026/*
1009 * Stop URBs 1027 * Stop URBs
@@ -1012,7 +1030,7 @@ void em28xx_stop_urbs(struct em28xx *dev)
1012{ 1030{
1013 int i; 1031 int i;
1014 struct urb *urb; 1032 struct urb *urb;
1015 struct em28xx_usb_isoc_bufs *isoc_bufs = &dev->isoc_ctl.digital_bufs; 1033 struct em28xx_usb_bufs *isoc_bufs = &dev->usb_ctl.digital_bufs;
1016 1034
1017 em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n"); 1035 em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n");
1018 1036
@@ -1033,10 +1051,10 @@ EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
1033/* 1051/*
1034 * Allocate URBs 1052 * Allocate URBs
1035 */ 1053 */
1036int em28xx_alloc_isoc(struct em28xx *dev, enum em28xx_mode mode, 1054int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
1037 int max_packets, int num_bufs, int max_pkt_size) 1055 int num_bufs, int max_pkt_size, int packet_multiplier)
1038{ 1056{
1039 struct em28xx_usb_isoc_bufs *isoc_bufs; 1057 struct em28xx_usb_bufs *usb_bufs;
1040 int i; 1058 int i;
1041 int sb_size, pipe; 1059 int sb_size, pipe;
1042 struct urb *urb; 1060 struct urb *urb;
@@ -1044,140 +1062,180 @@ int em28xx_alloc_isoc(struct em28xx *dev, enum em28xx_mode mode,
1044 1062
1045 em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode); 1063 em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode);
1046 1064
1047 if (mode == EM28XX_DIGITAL_MODE) 1065 /* Check mode and if we have an endpoint for the selected
1048 isoc_bufs = &dev->isoc_ctl.digital_bufs; 1066 transfer type, select buffer */
1049 else 1067 if (mode == EM28XX_DIGITAL_MODE) {
1050 isoc_bufs = &dev->isoc_ctl.analog_bufs; 1068 if ((xfer_bulk && !dev->dvb_ep_bulk) ||
1069 (!xfer_bulk && !dev->dvb_ep_isoc)) {
1070 em28xx_errdev("no endpoint for DVB mode and transfer type %d\n",
1071 xfer_bulk > 0);
1072 return -EINVAL;
1073 }
1074 usb_bufs = &dev->usb_ctl.digital_bufs;
1075 } else if (mode == EM28XX_ANALOG_MODE) {
1076 if ((xfer_bulk && !dev->analog_ep_bulk) ||
1077 (!xfer_bulk && !dev->analog_ep_isoc)) {
1078 em28xx_errdev("no endpoint for analog mode and transfer type %d\n",
1079 xfer_bulk > 0);
1080 return -EINVAL;
1081 }
1082 usb_bufs = &dev->usb_ctl.analog_bufs;
1083 } else {
1084 em28xx_errdev("invalid mode selected\n");
1085 return -EINVAL;
1086 }
1051 1087
1052 /* De-allocates all pending stuff */ 1088 /* De-allocates all pending stuff */
1053 em28xx_uninit_isoc(dev, mode); 1089 em28xx_uninit_usb_xfer(dev, mode);
1054 1090
1055 isoc_bufs->num_bufs = num_bufs; 1091 usb_bufs->num_bufs = num_bufs;
1056 1092
1057 isoc_bufs->urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL); 1093 usb_bufs->urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
1058 if (!isoc_bufs->urb) { 1094 if (!usb_bufs->urb) {
1059 em28xx_errdev("cannot alloc memory for usb buffers\n"); 1095 em28xx_errdev("cannot alloc memory for usb buffers\n");
1060 return -ENOMEM; 1096 return -ENOMEM;
1061 } 1097 }
1062 1098
1063 isoc_bufs->transfer_buffer = kzalloc(sizeof(void *)*num_bufs, 1099 usb_bufs->transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
1064 GFP_KERNEL); 1100 GFP_KERNEL);
1065 if (!isoc_bufs->transfer_buffer) { 1101 if (!usb_bufs->transfer_buffer) {
1066 em28xx_errdev("cannot allocate memory for usb transfer\n"); 1102 em28xx_errdev("cannot allocate memory for usb transfer\n");
1067 kfree(isoc_bufs->urb); 1103 kfree(usb_bufs->urb);
1068 return -ENOMEM; 1104 return -ENOMEM;
1069 } 1105 }
1070 1106
1071 isoc_bufs->max_pkt_size = max_pkt_size; 1107 usb_bufs->max_pkt_size = max_pkt_size;
1072 isoc_bufs->num_packets = max_packets; 1108 if (xfer_bulk)
1073 dev->isoc_ctl.vid_buf = NULL; 1109 usb_bufs->num_packets = 0;
1074 dev->isoc_ctl.vbi_buf = NULL; 1110 else
1111 usb_bufs->num_packets = packet_multiplier;
1112 dev->usb_ctl.vid_buf = NULL;
1113 dev->usb_ctl.vbi_buf = NULL;
1075 1114
1076 sb_size = isoc_bufs->num_packets * isoc_bufs->max_pkt_size; 1115 sb_size = packet_multiplier * usb_bufs->max_pkt_size;
1077 1116
1078 /* allocate urbs and transfer buffers */ 1117 /* allocate urbs and transfer buffers */
1079 for (i = 0; i < isoc_bufs->num_bufs; i++) { 1118 for (i = 0; i < usb_bufs->num_bufs; i++) {
1080 urb = usb_alloc_urb(isoc_bufs->num_packets, GFP_KERNEL); 1119 urb = usb_alloc_urb(usb_bufs->num_packets, GFP_KERNEL);
1081 if (!urb) { 1120 if (!urb) {
1082 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i); 1121 em28xx_err("cannot alloc usb_ctl.urb %i\n", i);
1083 em28xx_uninit_isoc(dev, mode); 1122 em28xx_uninit_usb_xfer(dev, mode);
1084 return -ENOMEM; 1123 return -ENOMEM;
1085 } 1124 }
1086 isoc_bufs->urb[i] = urb; 1125 usb_bufs->urb[i] = urb;
1087 1126
1088 isoc_bufs->transfer_buffer[i] = usb_alloc_coherent(dev->udev, 1127 usb_bufs->transfer_buffer[i] = usb_alloc_coherent(dev->udev,
1089 sb_size, GFP_KERNEL, &urb->transfer_dma); 1128 sb_size, GFP_KERNEL, &urb->transfer_dma);
1090 if (!isoc_bufs->transfer_buffer[i]) { 1129 if (!usb_bufs->transfer_buffer[i]) {
1091 em28xx_err("unable to allocate %i bytes for transfer" 1130 em28xx_err("unable to allocate %i bytes for transfer"
1092 " buffer %i%s\n", 1131 " buffer %i%s\n",
1093 sb_size, i, 1132 sb_size, i,
1094 in_interrupt() ? " while in int" : ""); 1133 in_interrupt() ? " while in int" : "");
1095 em28xx_uninit_isoc(dev, mode); 1134 em28xx_uninit_usb_xfer(dev, mode);
1096 return -ENOMEM; 1135 return -ENOMEM;
1097 } 1136 }
1098 memset(isoc_bufs->transfer_buffer[i], 0, sb_size); 1137 memset(usb_bufs->transfer_buffer[i], 0, sb_size);
1099 1138
1100 /* FIXME: this is a hack - should be 1139 if (xfer_bulk) { /* bulk */
1101 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK' 1140 pipe = usb_rcvbulkpipe(dev->udev,
1102 should also be using 'desc.bInterval' 1141 mode == EM28XX_ANALOG_MODE ?
1103 */ 1142 dev->analog_ep_bulk :
1104 pipe = usb_rcvisocpipe(dev->udev, 1143 dev->dvb_ep_bulk);
1105 mode == EM28XX_ANALOG_MODE ? 1144 usb_fill_bulk_urb(urb, dev->udev, pipe,
1106 EM28XX_EP_ANALOG : EM28XX_EP_DIGITAL); 1145 usb_bufs->transfer_buffer[i], sb_size,
1107 1146 em28xx_irq_callback, dev);
1108 usb_fill_int_urb(urb, dev->udev, pipe, 1147 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1109 isoc_bufs->transfer_buffer[i], sb_size, 1148 } else { /* isoc */
1110 em28xx_irq_callback, dev, 1); 1149 pipe = usb_rcvisocpipe(dev->udev,
1111 1150 mode == EM28XX_ANALOG_MODE ?
1112 urb->number_of_packets = isoc_bufs->num_packets; 1151 dev->analog_ep_isoc :
1113 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 1152 dev->dvb_ep_isoc);
1114 1153 usb_fill_int_urb(urb, dev->udev, pipe,
1115 k = 0; 1154 usb_bufs->transfer_buffer[i], sb_size,
1116 for (j = 0; j < isoc_bufs->num_packets; j++) { 1155 em28xx_irq_callback, dev, 1);
1117 urb->iso_frame_desc[j].offset = k; 1156 urb->transfer_flags = URB_ISO_ASAP |
1118 urb->iso_frame_desc[j].length = 1157 URB_NO_TRANSFER_DMA_MAP;
1119 isoc_bufs->max_pkt_size; 1158 k = 0;
1120 k += isoc_bufs->max_pkt_size; 1159 for (j = 0; j < usb_bufs->num_packets; j++) {
1160 urb->iso_frame_desc[j].offset = k;
1161 urb->iso_frame_desc[j].length =
1162 usb_bufs->max_pkt_size;
1163 k += usb_bufs->max_pkt_size;
1164 }
1121 } 1165 }
1166
1167 urb->number_of_packets = usb_bufs->num_packets;
1122 } 1168 }
1123 1169
1124 return 0; 1170 return 0;
1125} 1171}
1126EXPORT_SYMBOL_GPL(em28xx_alloc_isoc); 1172EXPORT_SYMBOL_GPL(em28xx_alloc_urbs);
1127 1173
1128/* 1174/*
1129 * Allocate URBs and start IRQ 1175 * Allocate URBs and start IRQ
1130 */ 1176 */
1131int em28xx_init_isoc(struct em28xx *dev, enum em28xx_mode mode, 1177int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
1132 int max_packets, int num_bufs, int max_pkt_size, 1178 int xfer_bulk, int num_bufs, int max_pkt_size,
1133 int (*isoc_copy) (struct em28xx *dev, struct urb *urb)) 1179 int packet_multiplier,
1180 int (*urb_data_copy) (struct em28xx *dev, struct urb *urb))
1134{ 1181{
1135 struct em28xx_dmaqueue *dma_q = &dev->vidq; 1182 struct em28xx_dmaqueue *dma_q = &dev->vidq;
1136 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq; 1183 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
1137 struct em28xx_usb_isoc_bufs *isoc_bufs; 1184 struct em28xx_usb_bufs *usb_bufs;
1138 int i; 1185 int i;
1139 int rc; 1186 int rc;
1140 int alloc; 1187 int alloc;
1141 1188
1142 em28xx_isocdbg("em28xx: called em28xx_init_isoc in mode %d\n", mode); 1189 em28xx_isocdbg("em28xx: called em28xx_init_usb_xfer in mode %d\n",
1190 mode);
1143 1191
1144 dev->isoc_ctl.isoc_copy = isoc_copy; 1192 dev->usb_ctl.urb_data_copy = urb_data_copy;
1145 1193
1146 if (mode == EM28XX_DIGITAL_MODE) { 1194 if (mode == EM28XX_DIGITAL_MODE) {
1147 isoc_bufs = &dev->isoc_ctl.digital_bufs; 1195 usb_bufs = &dev->usb_ctl.digital_bufs;
1148 /* no need to free/alloc isoc buffers in digital mode */ 1196 /* no need to free/alloc usb buffers in digital mode */
1149 alloc = 0; 1197 alloc = 0;
1150 } else { 1198 } else {
1151 isoc_bufs = &dev->isoc_ctl.analog_bufs; 1199 usb_bufs = &dev->usb_ctl.analog_bufs;
1152 alloc = 1; 1200 alloc = 1;
1153 } 1201 }
1154 1202
1155 if (alloc) { 1203 if (alloc) {
1156 rc = em28xx_alloc_isoc(dev, mode, max_packets, 1204 rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1157 num_bufs, max_pkt_size); 1205 max_pkt_size, packet_multiplier);
1158 if (rc) 1206 if (rc)
1159 return rc; 1207 return rc;
1160 } 1208 }
1161 1209
1210 if (xfer_bulk) {
1211 rc = usb_clear_halt(dev->udev, usb_bufs->urb[0]->pipe);
1212 if (rc < 0) {
1213 em28xx_err("failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1214 rc);
1215 em28xx_uninit_usb_xfer(dev, mode);
1216 return rc;
1217 }
1218 }
1219
1162 init_waitqueue_head(&dma_q->wq); 1220 init_waitqueue_head(&dma_q->wq);
1163 init_waitqueue_head(&vbi_dma_q->wq); 1221 init_waitqueue_head(&vbi_dma_q->wq);
1164 1222
1165 em28xx_capture_start(dev, 1); 1223 em28xx_capture_start(dev, 1);
1166 1224
1167 /* submit urbs and enables IRQ */ 1225 /* submit urbs and enables IRQ */
1168 for (i = 0; i < isoc_bufs->num_bufs; i++) { 1226 for (i = 0; i < usb_bufs->num_bufs; i++) {
1169 rc = usb_submit_urb(isoc_bufs->urb[i], GFP_ATOMIC); 1227 rc = usb_submit_urb(usb_bufs->urb[i], GFP_ATOMIC);
1170 if (rc) { 1228 if (rc) {
1171 em28xx_err("submit of urb %i failed (error=%i)\n", i, 1229 em28xx_err("submit of urb %i failed (error=%i)\n", i,
1172 rc); 1230 rc);
1173 em28xx_uninit_isoc(dev, mode); 1231 em28xx_uninit_usb_xfer(dev, mode);
1174 return rc; 1232 return rc;
1175 } 1233 }
1176 } 1234 }
1177 1235
1178 return 0; 1236 return 0;
1179} 1237}
1180EXPORT_SYMBOL_GPL(em28xx_init_isoc); 1238EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer);
1181 1239
1182/* 1240/*
1183 * em28xx_wake_i2c() 1241 * em28xx_wake_i2c()
diff --git a/drivers/media/usb/em28xx/em28xx-dvb.c b/drivers/media/usb/em28xx/em28xx-dvb.c
index 63f2e7070c00..a81ec2e8cc9b 100644
--- a/drivers/media/usb/em28xx/em28xx-dvb.c
+++ b/drivers/media/usb/em28xx/em28xx-dvb.c
@@ -10,6 +10,8 @@
10 10
11 (c) 2008 Aidan Thornton <makosoft@googlemail.com> 11 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12 12
13 (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
14
13 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by: 15 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
14 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au> 16 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
15 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] 17 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
@@ -25,7 +27,9 @@
25 27
26#include "em28xx.h" 28#include "em28xx.h"
27#include <media/v4l2-common.h> 29#include <media/v4l2-common.h>
28#include <media/videobuf-vmalloc.h> 30#include <dvb_demux.h>
31#include <dvb_net.h>
32#include <dmxdev.h>
29#include <media/tuner.h> 33#include <media/tuner.h>
30#include "tuner-simple.h" 34#include "tuner-simple.h"
31#include <linux/gpio.h> 35#include <linux/gpio.h>
@@ -124,34 +128,47 @@ static inline void print_err_status(struct em28xx *dev,
124 } 128 }
125} 129}
126 130
127static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb) 131static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
128{ 132{
129 int i; 133 int xfer_bulk, num_packets, i;
130 134
131 if (!dev) 135 if (!dev)
132 return 0; 136 return 0;
133 137
134 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) 138 if (dev->disconnected)
135 return 0; 139 return 0;
136 140
137 if (urb->status < 0) { 141 if (urb->status < 0)
138 print_err_status(dev, -1, urb->status); 142 print_err_status(dev, -1, urb->status);
139 if (urb->status == -ENOENT)
140 return 0;
141 }
142 143
143 for (i = 0; i < urb->number_of_packets; i++) { 144 xfer_bulk = usb_pipebulk(urb->pipe);
144 int status = urb->iso_frame_desc[i].status;
145 145
146 if (status < 0) { 146 if (xfer_bulk) /* bulk */
147 print_err_status(dev, i, status); 147 num_packets = 1;
148 if (urb->iso_frame_desc[i].status != -EPROTO) 148 else /* isoc */
149 continue; 149 num_packets = urb->number_of_packets;
150 }
151 150
152 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer + 151 for (i = 0; i < num_packets; i++) {
153 urb->iso_frame_desc[i].offset, 152 if (xfer_bulk) {
154 urb->iso_frame_desc[i].actual_length); 153 if (urb->status < 0) {
154 print_err_status(dev, i, urb->status);
155 if (urb->status != -EPROTO)
156 continue;
157 }
158 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
159 urb->actual_length);
160 } else {
161 if (urb->iso_frame_desc[i].status < 0) {
162 print_err_status(dev, i,
163 urb->iso_frame_desc[i].status);
164 if (urb->iso_frame_desc[i].status != -EPROTO)
165 continue;
166 }
167 dvb_dmx_swfilter(&dev->dvb->demux,
168 urb->transfer_buffer +
169 urb->iso_frame_desc[i].offset,
170 urb->iso_frame_desc[i].actual_length);
171 }
155 } 172 }
156 173
157 return 0; 174 return 0;
@@ -161,24 +178,40 @@ static int em28xx_start_streaming(struct em28xx_dvb *dvb)
161{ 178{
162 int rc; 179 int rc;
163 struct em28xx *dev = dvb->adapter.priv; 180 struct em28xx *dev = dvb->adapter.priv;
164 int max_dvb_packet_size; 181 int dvb_max_packet_size, packet_multiplier, dvb_alt;
182
183 if (dev->dvb_xfer_bulk) {
184 if (!dev->dvb_ep_bulk)
185 return -ENODEV;
186 dvb_max_packet_size = 512; /* USB 2.0 spec */
187 packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
188 dvb_alt = 0;
189 } else { /* isoc */
190 if (!dev->dvb_ep_isoc)
191 return -ENODEV;
192 dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
193 if (dvb_max_packet_size < 0)
194 return dvb_max_packet_size;
195 packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
196 dvb_alt = dev->dvb_alt_isoc;
197 }
165 198
166 usb_set_interface(dev->udev, 0, dev->dvb_alt); 199 usb_set_interface(dev->udev, 0, dvb_alt);
167 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE); 200 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
168 if (rc < 0) 201 if (rc < 0)
169 return rc; 202 return rc;
170 203
171 max_dvb_packet_size = dev->dvb_max_pkt_size;
172 if (max_dvb_packet_size < 0)
173 return max_dvb_packet_size;
174 dprintk(1, "Using %d buffers each with %d x %d bytes\n", 204 dprintk(1, "Using %d buffers each with %d x %d bytes\n",
175 EM28XX_DVB_NUM_BUFS, 205 EM28XX_DVB_NUM_BUFS,
176 EM28XX_DVB_MAX_PACKETS, 206 packet_multiplier,
177 max_dvb_packet_size); 207 dvb_max_packet_size);
178 208
179 return em28xx_init_isoc(dev, EM28XX_DIGITAL_MODE, 209 return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
180 EM28XX_DVB_MAX_PACKETS, EM28XX_DVB_NUM_BUFS, 210 dev->dvb_xfer_bulk,
181 max_dvb_packet_size, em28xx_dvb_isoc_copy); 211 EM28XX_DVB_NUM_BUFS,
212 dvb_max_packet_size,
213 packet_multiplier,
214 em28xx_dvb_urb_data_copy);
182} 215}
183 216
184static int em28xx_stop_streaming(struct em28xx_dvb *dvb) 217static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
@@ -714,7 +747,8 @@ static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
714}; 747};
715 748
716static const struct tda10071_config em28xx_tda10071_config = { 749static const struct tda10071_config em28xx_tda10071_config = {
717 .i2c_address = 0x55, /* (0xaa >> 1) */ 750 .demod_i2c_addr = 0x55, /* (0xaa >> 1) */
751 .tuner_i2c_addr = 0x14,
718 .i2c_wr_max = 64, 752 .i2c_wr_max = 64,
719 .ts_mode = TDA10071_TS_SERIAL, 753 .ts_mode = TDA10071_TS_SERIAL,
720 .spec_inv = 0, 754 .spec_inv = 0,
@@ -1288,7 +1322,7 @@ static int em28xx_dvb_fini(struct em28xx *dev)
1288 if (dev->dvb) { 1322 if (dev->dvb) {
1289 struct em28xx_dvb *dvb = dev->dvb; 1323 struct em28xx_dvb *dvb = dev->dvb;
1290 1324
1291 if (dev->state & DEV_DISCONNECTED) { 1325 if (dev->disconnected) {
1292 /* We cannot tell the device to sleep 1326 /* We cannot tell the device to sleep
1293 * once it has been unplugged. */ 1327 * once it has been unplugged. */
1294 if (dvb->fe[0]) 1328 if (dvb->fe[0])
diff --git a/drivers/media/usb/em28xx/em28xx-i2c.c b/drivers/media/usb/em28xx/em28xx-i2c.c
index 1683bd9d51ee..8532c1d4fd46 100644
--- a/drivers/media/usb/em28xx/em28xx-i2c.c
+++ b/drivers/media/usb/em28xx/em28xx-i2c.c
@@ -50,15 +50,18 @@ do { \
50} while (0) 50} while (0)
51 51
52/* 52/*
53 * em2800_i2c_send_max4() 53 * em2800_i2c_send_bytes()
54 * send up to 4 bytes to the i2c device 54 * send up to 4 bytes to the em2800 i2c device
55 */ 55 */
56static int em2800_i2c_send_max4(struct em28xx *dev, unsigned char addr, 56static int em2800_i2c_send_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
57 char *buf, int len)
58{ 57{
59 int ret; 58 int ret;
60 int write_timeout; 59 int write_timeout;
61 unsigned char b2[6]; 60 u8 b2[6];
61
62 if (len < 1 || len > 4)
63 return -EOPNOTSUPP;
64
62 BUG_ON(len < 1 || len > 4); 65 BUG_ON(len < 1 || len > 4);
63 b2[5] = 0x80 + len - 1; 66 b2[5] = 0x80 + len - 1;
64 b2[4] = addr; 67 b2[4] = addr;
@@ -70,165 +73,212 @@ static int em2800_i2c_send_max4(struct em28xx *dev, unsigned char addr,
70 if (len > 3) 73 if (len > 3)
71 b2[0] = buf[3]; 74 b2[0] = buf[3];
72 75
76 /* trigger write */
73 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len); 77 ret = dev->em28xx_write_regs(dev, 4 - len, &b2[4 - len], 2 + len);
74 if (ret != 2 + len) { 78 if (ret != 2 + len) {
75 em28xx_warn("writing to i2c device failed (error=%i)\n", ret); 79 em28xx_warn("failed to trigger write to i2c address 0x%x "
76 return -EIO; 80 "(error=%i)\n", addr, ret);
81 return (ret < 0) ? ret : -EIO;
77 } 82 }
78 for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0; 83 /* wait for completion */
84 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
79 write_timeout -= 5) { 85 write_timeout -= 5) {
80 ret = dev->em28xx_read_reg(dev, 0x05); 86 ret = dev->em28xx_read_reg(dev, 0x05);
81 if (ret == 0x80 + len - 1) 87 if (ret == 0x80 + len - 1) {
82 return len; 88 return len;
89 } else if (ret == 0x94 + len - 1) {
90 return -ENODEV;
91 } else if (ret < 0) {
92 em28xx_warn("failed to get i2c transfer status from "
93 "bridge register (error=%i)\n", ret);
94 return ret;
95 }
83 msleep(5); 96 msleep(5);
84 } 97 }
85 em28xx_warn("i2c write timed out\n"); 98 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
86 return -EIO; 99 return -EIO;
87} 100}
88 101
89/* 102/*
90 * em2800_i2c_send_bytes() 103 * em2800_i2c_recv_bytes()
91 */ 104 * read up to 4 bytes from the em2800 i2c device
92static int em2800_i2c_send_bytes(void *data, unsigned char addr, char *buf,
93 short len)
94{
95 char *bufPtr = buf;
96 int ret;
97 int wrcount = 0;
98 int count;
99 int maxLen = 4;
100 struct em28xx *dev = (struct em28xx *)data;
101 while (len > 0) {
102 count = (len > maxLen) ? maxLen : len;
103 ret = em2800_i2c_send_max4(dev, addr, bufPtr, count);
104 if (ret > 0) {
105 len -= count;
106 bufPtr += count;
107 wrcount += count;
108 } else
109 return (ret < 0) ? ret : -EFAULT;
110 }
111 return wrcount;
112}
113
114/*
115 * em2800_i2c_check_for_device()
116 * check if there is a i2c_device at the supplied address
117 */ 105 */
118static int em2800_i2c_check_for_device(struct em28xx *dev, unsigned char addr) 106static int em2800_i2c_recv_bytes(struct em28xx *dev, u8 addr, u8 *buf, u16 len)
119{ 107{
120 char msg; 108 u8 buf2[4];
121 int ret; 109 int ret;
122 int write_timeout; 110 int read_timeout;
123 msg = addr; 111 int i;
124 ret = dev->em28xx_write_regs(dev, 0x04, &msg, 1); 112
125 if (ret < 0) { 113 if (len < 1 || len > 4)
126 em28xx_warn("setting i2c device address failed (error=%i)\n", 114 return -EOPNOTSUPP;
127 ret); 115
128 return ret; 116 /* trigger read */
129 } 117 buf2[1] = 0x84 + len - 1;
130 msg = 0x84; 118 buf2[0] = addr;
131 ret = dev->em28xx_write_regs(dev, 0x05, &msg, 1); 119 ret = dev->em28xx_write_regs(dev, 0x04, buf2, 2);
132 if (ret < 0) { 120 if (ret != 2) {
133 em28xx_warn("preparing i2c read failed (error=%i)\n", ret); 121 em28xx_warn("failed to trigger read from i2c address 0x%x "
134 return ret; 122 "(error=%i)\n", addr, ret);
123 return (ret < 0) ? ret : -EIO;
135 } 124 }
136 for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0;
137 write_timeout -= 5) {
138 unsigned reg = dev->em28xx_read_reg(dev, 0x5);
139 125
140 if (reg == 0x94) 126 /* wait for completion */
127 for (read_timeout = EM2800_I2C_XFER_TIMEOUT; read_timeout > 0;
128 read_timeout -= 5) {
129 ret = dev->em28xx_read_reg(dev, 0x05);
130 if (ret == 0x84 + len - 1) {
131 break;
132 } else if (ret == 0x94 + len - 1) {
141 return -ENODEV; 133 return -ENODEV;
142 else if (reg == 0x84) 134 } else if (ret < 0) {
143 return 0; 135 em28xx_warn("failed to get i2c transfer status from "
136 "bridge register (error=%i)\n", ret);
137 return ret;
138 }
144 msleep(5); 139 msleep(5);
145 } 140 }
146 return -ENODEV; 141 if (ret != 0x84 + len - 1)
142 em28xx_warn("read from i2c device at 0x%x timed out\n", addr);
143
144 /* get the received message */
145 ret = dev->em28xx_read_reg_req_len(dev, 0x00, 4-len, buf2, len);
146 if (ret != len) {
147 em28xx_warn("reading from i2c device at 0x%x failed: "
148 "couldn't get the received message from the bridge "
149 "(error=%i)\n", addr, ret);
150 return (ret < 0) ? ret : -EIO;
151 }
152 for (i = 0; i < len; i++)
153 buf[i] = buf2[len - 1 - i];
154
155 return ret;
147} 156}
148 157
149/* 158/*
150 * em2800_i2c_recv_bytes() 159 * em2800_i2c_check_for_device()
151 * read from the i2c device 160 * check if there is an i2c device at the supplied address
152 */ 161 */
153static int em2800_i2c_recv_bytes(struct em28xx *dev, unsigned char addr, 162static int em2800_i2c_check_for_device(struct em28xx *dev, u8 addr)
154 char *buf, int len)
155{ 163{
164 u8 buf;
156 int ret; 165 int ret;
157 /* check for the device and set i2c read address */ 166
158 ret = em2800_i2c_check_for_device(dev, addr); 167 ret = em2800_i2c_recv_bytes(dev, addr, &buf, 1);
159 if (ret) { 168 if (ret == 1)
160 em28xx_warn 169 return 0;
161 ("preparing read at i2c address 0x%x failed (error=%i)\n", 170 return (ret < 0) ? ret : -EIO;
162 addr, ret);
163 return ret;
164 }
165 ret = dev->em28xx_read_reg_req_len(dev, 0x0, 0x3, buf, len);
166 if (ret < 0) {
167 em28xx_warn("reading from i2c device at 0x%x failed (error=%i)",
168 addr, ret);
169 return ret;
170 }
171 return ret;
172} 171}
173 172
174/* 173/*
175 * em28xx_i2c_send_bytes() 174 * em28xx_i2c_send_bytes()
176 */ 175 */
177static int em28xx_i2c_send_bytes(void *data, unsigned char addr, char *buf, 176static int em28xx_i2c_send_bytes(struct em28xx *dev, u16 addr, u8 *buf,
178 short len, int stop) 177 u16 len, int stop)
179{ 178{
180 int wrcount = 0;
181 struct em28xx *dev = (struct em28xx *)data;
182 int write_timeout, ret; 179 int write_timeout, ret;
183 180
184 wrcount = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len); 181 if (len < 1 || len > 64)
182 return -EOPNOTSUPP;
183 /* NOTE: limited by the USB ctrl message constraints
184 * Zero length reads always succeed, even if no device is connected */
185
186 /* Write to i2c device */
187 ret = dev->em28xx_write_regs_req(dev, stop ? 2 : 3, addr, buf, len);
188 if (ret != len) {
189 if (ret < 0) {
190 em28xx_warn("writing to i2c device at 0x%x failed "
191 "(error=%i)\n", addr, ret);
192 return ret;
193 } else {
194 em28xx_warn("%i bytes write to i2c device at 0x%x "
195 "requested, but %i bytes written\n",
196 len, addr, ret);
197 return -EIO;
198 }
199 }
185 200
186 /* Seems to be required after a write */ 201 /* Check success of the i2c operation */
187 for (write_timeout = EM2800_I2C_WRITE_TIMEOUT; write_timeout > 0; 202 for (write_timeout = EM2800_I2C_XFER_TIMEOUT; write_timeout > 0;
188 write_timeout -= 5) { 203 write_timeout -= 5) {
189 ret = dev->em28xx_read_reg(dev, 0x05); 204 ret = dev->em28xx_read_reg(dev, 0x05);
190 if (!ret) 205 if (ret == 0) { /* success */
191 break; 206 return len;
207 } else if (ret == 0x10) {
208 return -ENODEV;
209 } else if (ret < 0) {
210 em28xx_warn("failed to read i2c transfer status from "
211 "bridge (error=%i)\n", ret);
212 return ret;
213 }
192 msleep(5); 214 msleep(5);
215 /* NOTE: do we really have to wait for success ?
216 Never seen anything else than 0x00 or 0x10
217 (even with high payload) ... */
193 } 218 }
194 219 em28xx_warn("write to i2c device at 0x%x timed out\n", addr);
195 return wrcount; 220 return -EIO;
196} 221}
197 222
198/* 223/*
199 * em28xx_i2c_recv_bytes() 224 * em28xx_i2c_recv_bytes()
200 * read a byte from the i2c device 225 * read a byte from the i2c device
201 */ 226 */
202static int em28xx_i2c_recv_bytes(struct em28xx *dev, unsigned char addr, 227static int em28xx_i2c_recv_bytes(struct em28xx *dev, u16 addr, u8 *buf, u16 len)
203 char *buf, int len)
204{ 228{
205 int ret; 229 int ret;
230
231 if (len < 1 || len > 64)
232 return -EOPNOTSUPP;
233 /* NOTE: limited by the USB ctrl message constraints
234 * Zero length reads always succeed, even if no device is connected */
235
236 /* Read data from i2c device */
206 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len); 237 ret = dev->em28xx_read_reg_req_len(dev, 2, addr, buf, len);
238 if (ret != len) {
239 if (ret < 0) {
240 em28xx_warn("reading from i2c device at 0x%x failed "
241 "(error=%i)\n", addr, ret);
242 return ret;
243 } else {
244 em28xx_warn("%i bytes requested from i2c device at "
245 "0x%x, but %i bytes received\n",
246 len, addr, ret);
247 return -EIO;
248 }
249 }
250
251 /* Check success of the i2c operation */
252 ret = dev->em28xx_read_reg(dev, 0x05);
207 if (ret < 0) { 253 if (ret < 0) {
208 em28xx_warn("reading i2c device failed (error=%i)\n", ret); 254 em28xx_warn("failed to read i2c transfer status from "
255 "bridge (error=%i)\n", ret);
209 return ret; 256 return ret;
210 } 257 }
211 if (dev->em28xx_read_reg(dev, 0x5) != 0) 258 if (ret > 0) {
212 return -ENODEV; 259 if (ret == 0x10) {
213 return ret; 260 return -ENODEV;
261 } else {
262 em28xx_warn("unknown i2c error (status=%i)\n", ret);
263 return -EIO;
264 }
265 }
266 return len;
214} 267}
215 268
216/* 269/*
217 * em28xx_i2c_check_for_device() 270 * em28xx_i2c_check_for_device()
218 * check if there is a i2c_device at the supplied address 271 * check if there is a i2c_device at the supplied address
219 */ 272 */
220static int em28xx_i2c_check_for_device(struct em28xx *dev, unsigned char addr) 273static int em28xx_i2c_check_for_device(struct em28xx *dev, u16 addr)
221{ 274{
222 int ret; 275 int ret;
276 u8 buf;
223 277
224 ret = dev->em28xx_read_reg_req(dev, 2, addr); 278 ret = em28xx_i2c_recv_bytes(dev, addr, &buf, 1);
225 if (ret < 0) { 279 if (ret == 1)
226 em28xx_warn("reading from i2c device failed (error=%i)\n", ret); 280 return 0;
227 return ret; 281 return (ret < 0) ? ret : -EIO;
228 }
229 if (dev->em28xx_read_reg(dev, 0x5) != 0)
230 return -ENODEV;
231 return 0;
232} 282}
233 283
234/* 284/*
@@ -253,11 +303,11 @@ static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
253 rc = em2800_i2c_check_for_device(dev, addr); 303 rc = em2800_i2c_check_for_device(dev, addr);
254 else 304 else
255 rc = em28xx_i2c_check_for_device(dev, addr); 305 rc = em28xx_i2c_check_for_device(dev, addr);
256 if (rc < 0) { 306 if (rc == -ENODEV) {
257 dprintk2(2, " no device\n"); 307 if (i2c_debug >= 2)
308 printk(" no device\n");
258 return rc; 309 return rc;
259 } 310 }
260
261 } else if (msgs[i].flags & I2C_M_RD) { 311 } else if (msgs[i].flags & I2C_M_RD) {
262 /* read bytes */ 312 /* read bytes */
263 if (dev->board.is_em2800) 313 if (dev->board.is_em2800)
@@ -288,16 +338,16 @@ static int em28xx_i2c_xfer(struct i2c_adapter *i2c_adap,
288 msgs[i].len, 338 msgs[i].len,
289 i == num - 1); 339 i == num - 1);
290 } 340 }
291 if (rc < 0) 341 if (rc < 0) {
292 goto err; 342 if (i2c_debug >= 2)
343 printk(" ERROR: %i\n", rc);
344 return rc;
345 }
293 if (i2c_debug >= 2) 346 if (i2c_debug >= 2)
294 printk("\n"); 347 printk("\n");
295 } 348 }
296 349
297 return num; 350 return num;
298err:
299 dprintk2(2, " ERROR: %i\n", rc);
300 return rc;
301} 351}
302 352
303/* based on linux/sunrpc/svcauth.h and linux/hash.h 353/* based on linux/sunrpc/svcauth.h and linux/hash.h
@@ -329,7 +379,7 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
329{ 379{
330 unsigned char buf, *p = eedata; 380 unsigned char buf, *p = eedata;
331 struct em28xx_eeprom *em_eeprom = (void *)eedata; 381 struct em28xx_eeprom *em_eeprom = (void *)eedata;
332 int i, err, size = len, block; 382 int i, err, size = len, block, block_max;
333 383
334 if (dev->chip_id == CHIP_ID_EM2874 || 384 if (dev->chip_id == CHIP_ID_EM2874 ||
335 dev->chip_id == CHIP_ID_EM28174 || 385 dev->chip_id == CHIP_ID_EM28174 ||
@@ -362,9 +412,15 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
362 dev->name, err); 412 dev->name, err);
363 return err; 413 return err;
364 } 414 }
415
416 if (dev->board.is_em2800)
417 block_max = 4;
418 else
419 block_max = 64;
420
365 while (size > 0) { 421 while (size > 0) {
366 if (size > 16) 422 if (size > block_max)
367 block = 16; 423 block = block_max;
368 else 424 else
369 block = size; 425 block = size;
370 426
@@ -449,7 +505,11 @@ static int em28xx_i2c_eeprom(struct em28xx *dev, unsigned char *eedata, int len)
449 */ 505 */
450static u32 functionality(struct i2c_adapter *adap) 506static u32 functionality(struct i2c_adapter *adap)
451{ 507{
452 return I2C_FUNC_SMBUS_EMUL; 508 struct em28xx *dev = adap->algo_data;
509 u32 func_flags = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
510 if (dev->board.is_em2800)
511 func_flags &= ~I2C_FUNC_SMBUS_WRITE_BLOCK_DATA;
512 return func_flags;
453} 513}
454 514
455static struct i2c_algorithm em28xx_algo = { 515static struct i2c_algorithm em28xx_algo = {
@@ -474,6 +534,7 @@ static struct i2c_client em28xx_client_template = {
474 * incomplete list of known devices 534 * incomplete list of known devices
475 */ 535 */
476static char *i2c_devs[128] = { 536static char *i2c_devs[128] = {
537 [0x3e >> 1] = "remote IR sensor",
477 [0x4a >> 1] = "saa7113h", 538 [0x4a >> 1] = "saa7113h",
478 [0x52 >> 1] = "drxk", 539 [0x52 >> 1] = "drxk",
479 [0x60 >> 1] = "remote IR sensor", 540 [0x60 >> 1] = "remote IR sensor",
diff --git a/drivers/media/usb/em28xx/em28xx-input.c b/drivers/media/usb/em28xx/em28xx-input.c
index 660bf803c9e4..1bef990b3f18 100644
--- a/drivers/media/usb/em28xx/em28xx-input.c
+++ b/drivers/media/usb/em28xx/em28xx-input.c
@@ -40,11 +40,6 @@ MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
40 40
41#define MODULE_NAME "em28xx" 41#define MODULE_NAME "em28xx"
42 42
43#define i2cdprintk(fmt, arg...) \
44 if (ir_debug) { \
45 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
46 }
47
48#define dprintk(fmt, arg...) \ 43#define dprintk(fmt, arg...) \
49 if (ir_debug) { \ 44 if (ir_debug) { \
50 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \ 45 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
@@ -57,8 +52,8 @@ MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
57struct em28xx_ir_poll_result { 52struct em28xx_ir_poll_result {
58 unsigned int toggle_bit:1; 53 unsigned int toggle_bit:1;
59 unsigned int read_count:7; 54 unsigned int read_count:7;
60 u8 rc_address; 55
61 u8 rc_data[4]; /* 1 byte on em2860/2880, 4 on em2874 */ 56 u32 scancode;
62}; 57};
63 58
64struct em28xx_IR { 59struct em28xx_IR {
@@ -67,12 +62,17 @@ struct em28xx_IR {
67 char name[32]; 62 char name[32];
68 char phys[32]; 63 char phys[32];
69 64
70 /* poll external decoder */ 65 /* poll decoder */
71 int polling; 66 int polling;
72 struct delayed_work work; 67 struct delayed_work work;
73 unsigned int full_code:1; 68 unsigned int full_code:1;
74 unsigned int last_readcount; 69 unsigned int last_readcount;
70 u64 rc_type;
75 71
72 /* i2c slave address of external device (if used) */
73 u16 i2c_dev_addr;
74
75 int (*get_key_i2c)(struct i2c_client *, u32 *);
76 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *); 76 int (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
77}; 77};
78 78
@@ -80,21 +80,16 @@ struct em28xx_IR {
80 I2C IR based get keycodes - should be used with ir-kbd-i2c 80 I2C IR based get keycodes - should be used with ir-kbd-i2c
81 **********************************************************/ 81 **********************************************************/
82 82
83static int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 83static int em28xx_get_key_terratec(struct i2c_client *i2c_dev, u32 *ir_key)
84{ 84{
85 unsigned char b; 85 unsigned char b;
86 86
87 /* poll IR chip */ 87 /* poll IR chip */
88 if (1 != i2c_master_recv(ir->c, &b, 1)) { 88 if (1 != i2c_master_recv(i2c_dev, &b, 1))
89 i2cdprintk("read error\n");
90 return -EIO; 89 return -EIO;
91 }
92 90
93 /* it seems that 0xFE indicates that a button is still hold 91 /* it seems that 0xFE indicates that a button is still hold
94 down, while 0xff indicates that no button is hold 92 down, while 0xff indicates that no button is hold down. */
95 down. 0xfe sequences are sometimes interrupted by 0xFF */
96
97 i2cdprintk("key %02x\n", b);
98 93
99 if (b == 0xff) 94 if (b == 0xff)
100 return 0; 95 return 0;
@@ -104,18 +99,17 @@ static int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
104 return 1; 99 return 1;
105 100
106 *ir_key = b; 101 *ir_key = b;
107 *ir_raw = b;
108 return 1; 102 return 1;
109} 103}
110 104
111static int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) 105static int em28xx_get_key_em_haup(struct i2c_client *i2c_dev, u32 *ir_key)
112{ 106{
113 unsigned char buf[2]; 107 unsigned char buf[2];
114 u16 code; 108 u16 code;
115 int size; 109 int size;
116 110
117 /* poll IR chip */ 111 /* poll IR chip */
118 size = i2c_master_recv(ir->c, buf, sizeof(buf)); 112 size = i2c_master_recv(i2c_dev, buf, sizeof(buf));
119 113
120 if (size != 2) 114 if (size != 2)
121 return -EIO; 115 return -EIO;
@@ -124,8 +118,6 @@ static int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
124 if (buf[1] == 0xff) 118 if (buf[1] == 0xff)
125 return 0; 119 return 0;
126 120
127 ir->old = buf[1];
128
129 /* 121 /*
130 * Rearranges bits to the right order. 122 * Rearranges bits to the right order.
131 * The bit order were determined experimentally by using 123 * The bit order were determined experimentally by using
@@ -148,65 +140,51 @@ static int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
148 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */ 140 ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010 */
149 ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */ 141 ((buf[1] & 0x80) ? 0x0100 : 0); /* 0000 0001 */
150 142
151 i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x%02x)\n",
152 code, buf[1], buf[0]);
153
154 /* return key */ 143 /* return key */
155 *ir_key = code; 144 *ir_key = code;
156 *ir_raw = code;
157 return 1; 145 return 1;
158} 146}
159 147
160static int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key, 148static int em28xx_get_key_pinnacle_usb_grey(struct i2c_client *i2c_dev,
161 u32 *ir_raw) 149 u32 *ir_key)
162{ 150{
163 unsigned char buf[3]; 151 unsigned char buf[3];
164 152
165 /* poll IR chip */ 153 /* poll IR chip */
166 154
167 if (3 != i2c_master_recv(ir->c, buf, 3)) { 155 if (3 != i2c_master_recv(i2c_dev, buf, 3))
168 i2cdprintk("read error\n");
169 return -EIO; 156 return -EIO;
170 }
171 157
172 i2cdprintk("key %02x\n", buf[2]&0x3f);
173 if (buf[0] != 0x00) 158 if (buf[0] != 0x00)
174 return 0; 159 return 0;
175 160
176 *ir_key = buf[2]&0x3f; 161 *ir_key = buf[2]&0x3f;
177 *ir_raw = buf[2]&0x3f;
178 162
179 return 1; 163 return 1;
180} 164}
181 165
182static int em28xx_get_key_winfast_usbii_deluxe(struct IR_i2c *ir, u32 *ir_key, 166static int em28xx_get_key_winfast_usbii_deluxe(struct i2c_client *i2c_dev,
183 u32 *ir_raw) 167 u32 *ir_key)
184{ 168{
185 unsigned char subaddr, keydetect, key; 169 unsigned char subaddr, keydetect, key;
186 170
187 struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0, .buf = &subaddr, .len = 1}, 171 struct i2c_msg msg[] = { { .addr = i2c_dev->addr, .flags = 0, .buf = &subaddr, .len = 1},
188 172 { .addr = i2c_dev->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
189 { .addr = ir->c->addr, .flags = I2C_M_RD, .buf = &keydetect, .len = 1} };
190 173
191 subaddr = 0x10; 174 subaddr = 0x10;
192 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { 175 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
193 i2cdprintk("read error\n");
194 return -EIO; 176 return -EIO;
195 }
196 if (keydetect == 0x00) 177 if (keydetect == 0x00)
197 return 0; 178 return 0;
198 179
199 subaddr = 0x00; 180 subaddr = 0x00;
200 msg[1].buf = &key; 181 msg[1].buf = &key;
201 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) { 182 if (2 != i2c_transfer(i2c_dev->adapter, msg, 2))
202 i2cdprintk("read error\n"); 183 return -EIO;
203 return -EIO;
204 }
205 if (key == 0x00) 184 if (key == 0x00)
206 return 0; 185 return 0;
207 186
208 *ir_key = key; 187 *ir_key = key;
209 *ir_raw = key;
210 return 1; 188 return 1;
211} 189}
212 190
@@ -236,11 +214,8 @@ static int default_polling_getkey(struct em28xx_IR *ir,
236 /* Infrared read count (Reg 0x45[6:0] */ 214 /* Infrared read count (Reg 0x45[6:0] */
237 poll_result->read_count = (msg[0] & 0x7f); 215 poll_result->read_count = (msg[0] & 0x7f);
238 216
239 /* Remote Control Address (Reg 0x46) */ 217 /* Remote Control Address/Data (Regs 0x46/0x47) */
240 poll_result->rc_address = msg[1]; 218 poll_result->scancode = msg[1] << 8 | msg[2];
241
242 /* Remote Control Data (Reg 0x47) */
243 poll_result->rc_data[0] = msg[2];
244 219
245 return 0; 220 return 0;
246} 221}
@@ -266,13 +241,35 @@ static int em2874_polling_getkey(struct em28xx_IR *ir,
266 /* Infrared read count (Reg 0x51[6:0] */ 241 /* Infrared read count (Reg 0x51[6:0] */
267 poll_result->read_count = (msg[0] & 0x7f); 242 poll_result->read_count = (msg[0] & 0x7f);
268 243
269 /* Remote Control Address (Reg 0x52) */ 244 /*
270 poll_result->rc_address = msg[1]; 245 * Remote Control Address (Reg 0x52)
271 246 * Remote Control Data (Reg 0x53-0x55)
272 /* Remote Control Data (Reg 0x53-55) */ 247 */
273 poll_result->rc_data[0] = msg[2]; 248 switch (ir->rc_type) {
274 poll_result->rc_data[1] = msg[3]; 249 case RC_BIT_RC5:
275 poll_result->rc_data[2] = msg[4]; 250 poll_result->scancode = msg[1] << 8 | msg[2];
251 break;
252 case RC_BIT_NEC:
253 if ((msg[3] ^ msg[4]) != 0xff) /* 32 bits NEC */
254 poll_result->scancode = (msg[1] << 24) |
255 (msg[2] << 16) |
256 (msg[3] << 8) |
257 msg[4];
258 else if ((msg[1] ^ msg[2]) != 0xff) /* 24 bits NEC */
259 poll_result->scancode = (msg[1] << 16) |
260 (msg[2] << 8) |
261 msg[3];
262 else /* Normal NEC */
263 poll_result->scancode = msg[1] << 8 | msg[3];
264 break;
265 case RC_BIT_RC6_0:
266 poll_result->scancode = msg[1] << 8 | msg[2];
267 break;
268 default:
269 poll_result->scancode = (msg[1] << 24) | (msg[2] << 16) |
270 (msg[3] << 8) | msg[4];
271 break;
272 }
276 273
277 return 0; 274 return 0;
278} 275}
@@ -281,6 +278,28 @@ static int em2874_polling_getkey(struct em28xx_IR *ir,
281 Polling code for em28xx 278 Polling code for em28xx
282 **********************************************************/ 279 **********************************************************/
283 280
281static int em28xx_i2c_ir_handle_key(struct em28xx_IR *ir)
282{
283 static u32 ir_key;
284 int rc;
285 struct i2c_client client;
286
287 client.adapter = &ir->dev->i2c_adap;
288 client.addr = ir->i2c_dev_addr;
289
290 rc = ir->get_key_i2c(&client, &ir_key);
291 if (rc < 0) {
292 dprintk("ir->get_key_i2c() failed: %d\n", rc);
293 return rc;
294 }
295
296 if (rc) {
297 dprintk("%s: keycode = 0x%04x\n", __func__, ir_key);
298 rc_keydown(ir->rc, ir_key, 0);
299 }
300 return 0;
301}
302
284static void em28xx_ir_handle_key(struct em28xx_IR *ir) 303static void em28xx_ir_handle_key(struct em28xx_IR *ir)
285{ 304{
286 int result; 305 int result;
@@ -289,22 +308,21 @@ static void em28xx_ir_handle_key(struct em28xx_IR *ir)
289 /* read the registers containing the IR status */ 308 /* read the registers containing the IR status */
290 result = ir->get_key(ir, &poll_result); 309 result = ir->get_key(ir, &poll_result);
291 if (unlikely(result < 0)) { 310 if (unlikely(result < 0)) {
292 dprintk("ir->get_key() failed %d\n", result); 311 dprintk("ir->get_key() failed: %d\n", result);
293 return; 312 return;
294 } 313 }
295 314
296 if (unlikely(poll_result.read_count != ir->last_readcount)) { 315 if (unlikely(poll_result.read_count != ir->last_readcount)) {
297 dprintk("%s: toggle: %d, count: %d, key 0x%02x%02x\n", __func__, 316 dprintk("%s: toggle: %d, count: %d, key 0x%04x\n", __func__,
298 poll_result.toggle_bit, poll_result.read_count, 317 poll_result.toggle_bit, poll_result.read_count,
299 poll_result.rc_address, poll_result.rc_data[0]); 318 poll_result.scancode);
300 if (ir->full_code) 319 if (ir->full_code)
301 rc_keydown(ir->rc, 320 rc_keydown(ir->rc,
302 poll_result.rc_address << 8 | 321 poll_result.scancode,
303 poll_result.rc_data[0],
304 poll_result.toggle_bit); 322 poll_result.toggle_bit);
305 else 323 else
306 rc_keydown(ir->rc, 324 rc_keydown(ir->rc,
307 poll_result.rc_data[0], 325 poll_result.scancode & 0xff,
308 poll_result.toggle_bit); 326 poll_result.toggle_bit);
309 327
310 if (ir->dev->chip_id == CHIP_ID_EM2874 || 328 if (ir->dev->chip_id == CHIP_ID_EM2874 ||
@@ -324,7 +342,10 @@ static void em28xx_ir_work(struct work_struct *work)
324{ 342{
325 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work); 343 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
326 344
327 em28xx_ir_handle_key(ir); 345 if (ir->i2c_dev_addr) /* external i2c device */
346 em28xx_i2c_ir_handle_key(ir);
347 else /* internal device */
348 em28xx_ir_handle_key(ir);
328 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling)); 349 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
329} 350}
330 351
@@ -345,93 +366,107 @@ static void em28xx_ir_stop(struct rc_dev *rc)
345 cancel_delayed_work_sync(&ir->work); 366 cancel_delayed_work_sync(&ir->work);
346} 367}
347 368
348static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type) 369static int em2860_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
349{ 370{
350 int rc = 0;
351 struct em28xx_IR *ir = rc_dev->priv; 371 struct em28xx_IR *ir = rc_dev->priv;
352 struct em28xx *dev = ir->dev; 372 struct em28xx *dev = ir->dev;
353 u8 ir_config = EM2874_IR_RC5;
354
355 /* Adjust xclk based o IR table for RC5/NEC tables */
356 373
374 /* Adjust xclk based on IR table for RC5/NEC tables */
357 if (*rc_type & RC_BIT_RC5) { 375 if (*rc_type & RC_BIT_RC5) {
358 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE; 376 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
359 ir->full_code = 1; 377 ir->full_code = 1;
360 *rc_type = RC_BIT_RC5; 378 *rc_type = RC_BIT_RC5;
361 } else if (*rc_type & RC_BIT_NEC) { 379 } else if (*rc_type & RC_BIT_NEC) {
362 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE; 380 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
363 ir_config = EM2874_IR_NEC;
364 ir->full_code = 1; 381 ir->full_code = 1;
365 *rc_type = RC_BIT_NEC; 382 *rc_type = RC_BIT_NEC;
366 } else if (*rc_type != RC_BIT_UNKNOWN) 383 } else if (*rc_type & RC_BIT_UNKNOWN) {
367 rc = -EINVAL; 384 *rc_type = RC_BIT_UNKNOWN;
385 } else {
386 *rc_type = ir->rc_type;
387 return -EINVAL;
388 }
389 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
390 EM28XX_XCLK_IR_RC5_MODE);
391
392 ir->rc_type = *rc_type;
368 393
394 return 0;
395}
396
397static int em2874_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
398{
399 struct em28xx_IR *ir = rc_dev->priv;
400 struct em28xx *dev = ir->dev;
401 u8 ir_config = EM2874_IR_RC5;
402
403 /* Adjust xclk and set type based on IR table for RC5/NEC/RC6 tables */
404 if (*rc_type & RC_BIT_RC5) {
405 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
406 ir->full_code = 1;
407 *rc_type = RC_BIT_RC5;
408 } else if (*rc_type & RC_BIT_NEC) {
409 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
410 ir_config = EM2874_IR_NEC | EM2874_IR_NEC_NO_PARITY;
411 ir->full_code = 1;
412 *rc_type = RC_BIT_NEC;
413 } else if (*rc_type & RC_BIT_RC6_0) {
414 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
415 ir_config = EM2874_IR_RC6_MODE_0;
416 ir->full_code = 1;
417 *rc_type = RC_BIT_RC6_0;
418 } else if (*rc_type & RC_BIT_UNKNOWN) {
419 *rc_type = RC_BIT_UNKNOWN;
420 } else {
421 *rc_type = ir->rc_type;
422 return -EINVAL;
423 }
424 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
369 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk, 425 em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
370 EM28XX_XCLK_IR_RC5_MODE); 426 EM28XX_XCLK_IR_RC5_MODE);
371 427
428 ir->rc_type = *rc_type;
429
430 return 0;
431}
432static int em28xx_ir_change_protocol(struct rc_dev *rc_dev, u64 *rc_type)
433{
434 struct em28xx_IR *ir = rc_dev->priv;
435 struct em28xx *dev = ir->dev;
436
372 /* Setup the proper handler based on the chip */ 437 /* Setup the proper handler based on the chip */
373 switch (dev->chip_id) { 438 switch (dev->chip_id) {
374 case CHIP_ID_EM2860: 439 case CHIP_ID_EM2860:
375 case CHIP_ID_EM2883: 440 case CHIP_ID_EM2883:
376 ir->get_key = default_polling_getkey; 441 return em2860_ir_change_protocol(rc_dev, rc_type);
377 break;
378 case CHIP_ID_EM2884: 442 case CHIP_ID_EM2884:
379 case CHIP_ID_EM2874: 443 case CHIP_ID_EM2874:
380 case CHIP_ID_EM28174: 444 case CHIP_ID_EM28174:
381 ir->get_key = em2874_polling_getkey; 445 return em2874_ir_change_protocol(rc_dev, rc_type);
382 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
383 break;
384 default: 446 default:
385 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n", 447 printk("Unrecognized em28xx chip id 0x%02x: IR not supported\n",
386 dev->chip_id); 448 dev->chip_id);
387 rc = -EINVAL; 449 return -EINVAL;
388 } 450 }
389
390 return rc;
391} 451}
392 452
393static void em28xx_register_i2c_ir(struct em28xx *dev) 453static int em28xx_probe_i2c_ir(struct em28xx *dev)
394{ 454{
455 int i = 0;
395 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */ 456 /* Leadtek winfast tv USBII deluxe can find a non working IR-device */
396 /* at address 0x18, so if that address is needed for another board in */ 457 /* at address 0x18, so if that address is needed for another board in */
397 /* the future, please put it after 0x1f. */ 458 /* the future, please put it after 0x1f. */
398 struct i2c_board_info info;
399 const unsigned short addr_list[] = { 459 const unsigned short addr_list[] = {
400 0x1f, 0x30, 0x47, I2C_CLIENT_END 460 0x1f, 0x30, 0x47, I2C_CLIENT_END
401 }; 461 };
402 462
403 memset(&info, 0, sizeof(struct i2c_board_info)); 463 while (addr_list[i] != I2C_CLIENT_END) {
404 memset(&dev->init_data, 0, sizeof(dev->init_data)); 464 if (i2c_probe_func_quick_read(&dev->i2c_adap, addr_list[i]) == 1)
405 strlcpy(info.type, "ir_video", I2C_NAME_SIZE); 465 return addr_list[i];
406 466 i++;
407 /* detect & configure */
408 switch (dev->model) {
409 case EM2800_BOARD_TERRATEC_CINERGY_200:
410 case EM2820_BOARD_TERRATEC_CINERGY_250:
411 dev->init_data.ir_codes = RC_MAP_EM_TERRATEC;
412 dev->init_data.get_key = em28xx_get_key_terratec;
413 dev->init_data.name = "i2c IR (EM28XX Terratec)";
414 break;
415 case EM2820_BOARD_PINNACLE_USB_2:
416 dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
417 dev->init_data.get_key = em28xx_get_key_pinnacle_usb_grey;
418 dev->init_data.name = "i2c IR (EM28XX Pinnacle PCTV)";
419 break;
420 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
421 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
422 dev->init_data.get_key = em28xx_get_key_em_haup;
423 dev->init_data.name = "i2c IR (EM2840 Hauppauge)";
424 break;
425 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
426 dev->init_data.ir_codes = RC_MAP_WINFAST_USBII_DELUXE;
427 dev->init_data.get_key = em28xx_get_key_winfast_usbii_deluxe;
428 dev->init_data.name = "i2c IR (EM2820 Winfast TV USBII Deluxe)";
429 break;
430 } 467 }
431 468
432 if (dev->init_data.name) 469 return -ENODEV;
433 info.platform_data = &dev->init_data;
434 i2c_new_probed_device(&dev->i2c_adap, &info, addr_list, NULL);
435} 470}
436 471
437/********************************************************** 472/**********************************************************
@@ -527,8 +562,21 @@ static int em28xx_ir_init(struct em28xx *dev)
527 struct rc_dev *rc; 562 struct rc_dev *rc;
528 int err = -ENOMEM; 563 int err = -ENOMEM;
529 u64 rc_type; 564 u64 rc_type;
565 u16 i2c_rc_dev_addr = 0;
566
567 if (dev->board.has_snapshot_button)
568 em28xx_register_snapshot_button(dev);
569
570 if (dev->board.has_ir_i2c) {
571 i2c_rc_dev_addr = em28xx_probe_i2c_ir(dev);
572 if (!i2c_rc_dev_addr) {
573 dev->board.has_ir_i2c = 0;
574 em28xx_warn("No i2c IR remote control device found.\n");
575 return -ENODEV;
576 }
577 }
530 578
531 if (dev->board.ir_codes == NULL) { 579 if (dev->board.ir_codes == NULL && !dev->board.has_ir_i2c) {
532 /* No remote control support */ 580 /* No remote control support */
533 em28xx_warn("Remote control support is not available for " 581 em28xx_warn("Remote control support is not available for "
534 "this card.\n"); 582 "this card.\n");
@@ -538,35 +586,77 @@ static int em28xx_ir_init(struct em28xx *dev)
538 ir = kzalloc(sizeof(*ir), GFP_KERNEL); 586 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
539 rc = rc_allocate_device(); 587 rc = rc_allocate_device();
540 if (!ir || !rc) 588 if (!ir || !rc)
541 goto err_out_free; 589 goto error;
542 590
543 /* record handles to ourself */ 591 /* record handles to ourself */
544 ir->dev = dev; 592 ir->dev = dev;
545 dev->ir = ir; 593 dev->ir = ir;
546 ir->rc = rc; 594 ir->rc = rc;
547 595
548 /*
549 * em2874 supports more protocols. For now, let's just announce
550 * the two protocols that were already tested
551 */
552 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
553 rc->priv = ir; 596 rc->priv = ir;
554 rc->change_protocol = em28xx_ir_change_protocol;
555 rc->open = em28xx_ir_start; 597 rc->open = em28xx_ir_start;
556 rc->close = em28xx_ir_stop; 598 rc->close = em28xx_ir_stop;
557 599
558 /* By default, keep protocol field untouched */ 600 if (dev->board.has_ir_i2c) { /* external i2c device */
559 rc_type = RC_BIT_UNKNOWN; 601 switch (dev->model) {
560 err = em28xx_ir_change_protocol(rc, &rc_type); 602 case EM2800_BOARD_TERRATEC_CINERGY_200:
561 if (err) 603 case EM2820_BOARD_TERRATEC_CINERGY_250:
562 goto err_out_free; 604 rc->map_name = RC_MAP_EM_TERRATEC;
605 ir->get_key_i2c = em28xx_get_key_terratec;
606 break;
607 case EM2820_BOARD_PINNACLE_USB_2:
608 rc->map_name = RC_MAP_PINNACLE_GREY;
609 ir->get_key_i2c = em28xx_get_key_pinnacle_usb_grey;
610 break;
611 case EM2820_BOARD_HAUPPAUGE_WINTV_USB_2:
612 rc->map_name = RC_MAP_HAUPPAUGE;
613 ir->get_key_i2c = em28xx_get_key_em_haup;
614 rc->allowed_protos = RC_BIT_RC5;
615 break;
616 case EM2820_BOARD_LEADTEK_WINFAST_USBII_DELUXE:
617 rc->map_name = RC_MAP_WINFAST_USBII_DELUXE;
618 ir->get_key_i2c = em28xx_get_key_winfast_usbii_deluxe;
619 break;
620 default:
621 err = -ENODEV;
622 goto error;
623 }
624
625 ir->i2c_dev_addr = i2c_rc_dev_addr;
626 } else { /* internal device */
627 switch (dev->chip_id) {
628 case CHIP_ID_EM2860:
629 case CHIP_ID_EM2883:
630 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
631 ir->get_key = default_polling_getkey;
632 break;
633 case CHIP_ID_EM2884:
634 case CHIP_ID_EM2874:
635 case CHIP_ID_EM28174:
636 ir->get_key = em2874_polling_getkey;
637 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC |
638 RC_BIT_RC6_0;
639 break;
640 default:
641 err = -ENODEV;
642 goto error;
643 }
644
645 rc->change_protocol = em28xx_ir_change_protocol;
646 rc->map_name = dev->board.ir_codes;
647
648 /* By default, keep protocol field untouched */
649 rc_type = RC_BIT_UNKNOWN;
650 err = em28xx_ir_change_protocol(rc, &rc_type);
651 if (err)
652 goto error;
653 }
563 654
564 /* This is how often we ask the chip for IR information */ 655 /* This is how often we ask the chip for IR information */
565 ir->polling = 100; /* ms */ 656 ir->polling = 100; /* ms */
566 657
567 /* init input device */ 658 /* init input device */
568 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", 659 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)", dev->name);
569 dev->name);
570 660
571 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys)); 661 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
572 strlcat(ir->phys, "/input0", sizeof(ir->phys)); 662 strlcat(ir->phys, "/input0", sizeof(ir->phys));
@@ -578,28 +668,17 @@ static int em28xx_ir_init(struct em28xx *dev)
578 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); 668 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
579 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct); 669 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
580 rc->dev.parent = &dev->udev->dev; 670 rc->dev.parent = &dev->udev->dev;
581 rc->map_name = dev->board.ir_codes;
582 rc->driver_name = MODULE_NAME; 671 rc->driver_name = MODULE_NAME;
583 672
584 /* all done */ 673 /* all done */
585 err = rc_register_device(rc); 674 err = rc_register_device(rc);
586 if (err) 675 if (err)
587 goto err_out_stop; 676 goto error;
588
589 em28xx_register_i2c_ir(dev);
590
591#if defined(CONFIG_MODULES) && defined(MODULE)
592 if (dev->board.has_ir_i2c)
593 request_module("ir-kbd-i2c");
594#endif
595 if (dev->board.has_snapshot_button)
596 em28xx_register_snapshot_button(dev);
597 677
598 return 0; 678 return 0;
599 679
600 err_out_stop: 680error:
601 dev->ir = NULL; 681 dev->ir = NULL;
602 err_out_free:
603 rc_free_device(rc); 682 rc_free_device(rc);
604 kfree(ir); 683 kfree(ir);
605 return err; 684 return err;
diff --git a/drivers/media/usb/em28xx/em28xx-reg.h b/drivers/media/usb/em28xx/em28xx-reg.h
index 6ff368297f6e..885089e22bcd 100644
--- a/drivers/media/usb/em28xx/em28xx-reg.h
+++ b/drivers/media/usb/em28xx/em28xx-reg.h
@@ -13,9 +13,9 @@
13#define EM_GPO_3 (1 << 3) 13#define EM_GPO_3 (1 << 3)
14 14
15/* em28xx endpoints */ 15/* em28xx endpoints */
16#define EM28XX_EP_ANALOG 0x82 16/* 0x82: (always ?) analog */
17#define EM28XX_EP_AUDIO 0x83 17#define EM28XX_EP_AUDIO 0x83
18#define EM28XX_EP_DIGITAL 0x84 18/* 0x84: digital or analog */
19 19
20/* em2800 registers */ 20/* em2800 registers */
21#define EM2800_R08_AUDIOSRC 0x08 21#define EM2800_R08_AUDIOSRC 0x08
@@ -177,6 +177,7 @@
177 177
178/* em2874 IR config register (0x50) */ 178/* em2874 IR config register (0x50) */
179#define EM2874_IR_NEC 0x00 179#define EM2874_IR_NEC 0x00
180#define EM2874_IR_NEC_NO_PARITY 0x01
180#define EM2874_IR_RC5 0x04 181#define EM2874_IR_RC5 0x04
181#define EM2874_IR_RC6_MODE_0 0x08 182#define EM2874_IR_RC6_MODE_0 0x08
182#define EM2874_IR_RC6_MODE_6A 0x0b 183#define EM2874_IR_RC6_MODE_6A 0x0b
diff --git a/drivers/media/usb/em28xx/em28xx-vbi.c b/drivers/media/usb/em28xx/em28xx-vbi.c
index 2b4c9cba2d67..39f39c527c13 100644
--- a/drivers/media/usb/em28xx/em28xx-vbi.c
+++ b/drivers/media/usb/em28xx/em28xx-vbi.c
@@ -41,105 +41,72 @@ MODULE_PARM_DESC(vbi_debug, "enable debug messages [vbi]");
41 41
42/* ------------------------------------------------------------------ */ 42/* ------------------------------------------------------------------ */
43 43
44static void 44static int vbi_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
45free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) 45 unsigned int *nbuffers, unsigned int *nplanes,
46 unsigned int sizes[], void *alloc_ctxs[])
46{ 47{
47 struct em28xx_fh *fh = vq->priv_data; 48 struct em28xx *dev = vb2_get_drv_priv(vq);
48 struct em28xx *dev = fh->dev; 49 unsigned long size;
49 unsigned long flags = 0;
50 if (in_interrupt())
51 BUG();
52
53 /* We used to wait for the buffer to finish here, but this didn't work
54 because, as we were keeping the state as VIDEOBUF_QUEUED,
55 videobuf_queue_cancel marked it as finished for us.
56 (Also, it could wedge forever if the hardware was misconfigured.)
57
58 This should be safe; by the time we get here, the buffer isn't
59 queued anymore. If we ever start marking the buffers as
60 VIDEOBUF_ACTIVE, it won't be, though.
61 */
62 spin_lock_irqsave(&dev->slock, flags);
63 if (dev->isoc_ctl.vbi_buf == buf)
64 dev->isoc_ctl.vbi_buf = NULL;
65 spin_unlock_irqrestore(&dev->slock, flags);
66 50
67 videobuf_vmalloc_free(&buf->vb); 51 if (fmt)
68 buf->vb.state = VIDEOBUF_NEEDS_INIT; 52 size = fmt->fmt.pix.sizeimage;
69} 53 else
54 size = dev->vbi_width * dev->vbi_height * 2;
70 55
71static int 56 if (0 == *nbuffers)
72vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 57 *nbuffers = 32;
73{ 58 if (*nbuffers < 2)
74 struct em28xx_fh *fh = q->priv_data; 59 *nbuffers = 2;
75 struct em28xx *dev = fh->dev; 60 if (*nbuffers > 32)
61 *nbuffers = 32;
76 62
77 *size = dev->vbi_width * dev->vbi_height * 2; 63 *nplanes = 1;
64 sizes[0] = size;
78 65
79 if (0 == *count)
80 *count = vbibufs;
81 if (*count < 2)
82 *count = 2;
83 if (*count > 32)
84 *count = 32;
85 return 0; 66 return 0;
86} 67}
87 68
88static int 69static int vbi_buffer_prepare(struct vb2_buffer *vb)
89vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
90 enum v4l2_field field)
91{ 70{
92 struct em28xx_fh *fh = q->priv_data; 71 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
93 struct em28xx *dev = fh->dev;
94 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 72 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
95 int rc = 0; 73 unsigned long size;
96 74
97 buf->vb.size = dev->vbi_width * dev->vbi_height * 2; 75 size = dev->vbi_width * dev->vbi_height * 2;
98 76
99 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 77 if (vb2_plane_size(vb, 0) < size) {
78 printk(KERN_INFO "%s data will not fit into plane (%lu < %lu)\n",
79 __func__, vb2_plane_size(vb, 0), size);
100 return -EINVAL; 80 return -EINVAL;
101
102 buf->vb.width = dev->vbi_width;
103 buf->vb.height = dev->vbi_height;
104 buf->vb.field = field;
105
106 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
107 rc = videobuf_iolock(q, &buf->vb, NULL);
108 if (rc < 0)
109 goto fail;
110 } 81 }
82 vb2_set_plane_payload(&buf->vb, 0, size);
111 83
112 buf->vb.state = VIDEOBUF_PREPARED;
113 return 0; 84 return 0;
114
115fail:
116 free_buffer(q, buf);
117 return rc;
118} 85}
119 86
120static void 87static void
121vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 88vbi_buffer_queue(struct vb2_buffer *vb)
122{
123 struct em28xx_buffer *buf = container_of(vb,
124 struct em28xx_buffer,
125 vb);
126 struct em28xx_fh *fh = vq->priv_data;
127 struct em28xx *dev = fh->dev;
128 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
129
130 buf->vb.state = VIDEOBUF_QUEUED;
131 list_add_tail(&buf->vb.queue, &vbiq->active);
132}
133
134static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
135{ 89{
90 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
136 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 91 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
137 free_buffer(q, buf); 92 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
93 unsigned long flags = 0;
94
95 buf->mem = vb2_plane_vaddr(vb, 0);
96 buf->length = vb2_plane_size(vb, 0);
97
98 spin_lock_irqsave(&dev->slock, flags);
99 list_add_tail(&buf->list, &vbiq->active);
100 spin_unlock_irqrestore(&dev->slock, flags);
138} 101}
139 102
140struct videobuf_queue_ops em28xx_vbi_qops = { 103
141 .buf_setup = vbi_setup, 104struct vb2_ops em28xx_vbi_qops = {
142 .buf_prepare = vbi_prepare, 105 .queue_setup = vbi_queue_setup,
143 .buf_queue = vbi_queue, 106 .buf_prepare = vbi_buffer_prepare,
144 .buf_release = vbi_release, 107 .buf_queue = vbi_buffer_queue,
108 .start_streaming = em28xx_start_analog_streaming,
109 .stop_streaming = em28xx_stop_vbi_streaming,
110 .wait_prepare = vb2_ops_wait_prepare,
111 .wait_finish = vb2_ops_wait_finish,
145}; 112};
diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c
index 1e553d357380..32bd7de5dec1 100644
--- a/drivers/media/usb/em28xx/em28xx-video.c
+++ b/drivers/media/usb/em28xx/em28xx-video.c
@@ -6,6 +6,7 @@
6 Markus Rechberger <mrechberger@gmail.com> 6 Markus Rechberger <mrechberger@gmail.com>
7 Mauro Carvalho Chehab <mchehab@infradead.org> 7 Mauro Carvalho Chehab <mchehab@infradead.org>
8 Sascha Sommer <saschasommer@freenet.de> 8 Sascha Sommer <saschasommer@freenet.de>
9 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
9 10
10 Some parts based on SN9C10x PC Camera Controllers GPL driver made 11 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it> 12 by Luca Risolia <luca.risolia@studio.unibo.it>
@@ -39,6 +40,7 @@
39#include "em28xx.h" 40#include "em28xx.h"
40#include <media/v4l2-common.h> 41#include <media/v4l2-common.h>
41#include <media/v4l2-ioctl.h> 42#include <media/v4l2-ioctl.h>
43#include <media/v4l2-event.h>
42#include <media/v4l2-chip-ident.h> 44#include <media/v4l2-chip-ident.h>
43#include <media/msp3400.h> 45#include <media/msp3400.h>
44#include <media/tuner.h> 46#include <media/tuner.h>
@@ -74,9 +76,9 @@ MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL"); 76MODULE_LICENSE("GPL");
75MODULE_VERSION(EM28XX_VERSION); 77MODULE_VERSION(EM28XX_VERSION);
76 78
77static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 79static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
78static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 80static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; 81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
80 82
81module_param_array(video_nr, int, NULL, 0444); 83module_param_array(video_nr, int, NULL, 0444);
82module_param_array(vbi_nr, int, NULL, 0444); 84module_param_array(vbi_nr, int, NULL, 0444);
@@ -124,101 +126,50 @@ static struct em28xx_fmt format[] = {
124 }, 126 },
125}; 127};
126 128
127/* supported controls */
128/* Common to all boards */
129static struct v4l2_queryctrl ac97_qctrl[] = {
130 {
131 .id = V4L2_CID_AUDIO_VOLUME,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Volume",
134 .minimum = 0x0,
135 .maximum = 0x1f,
136 .step = 0x1,
137 .default_value = 0x1f,
138 .flags = V4L2_CTRL_FLAG_SLIDER,
139 }, {
140 .id = V4L2_CID_AUDIO_MUTE,
141 .type = V4L2_CTRL_TYPE_BOOLEAN,
142 .name = "Mute",
143 .minimum = 0,
144 .maximum = 1,
145 .step = 1,
146 .default_value = 1,
147 .flags = 0,
148 }
149};
150
151/* ------------------------------------------------------------------ 129/* ------------------------------------------------------------------
152 DMA and thread functions 130 DMA and thread functions
153 ------------------------------------------------------------------*/ 131 ------------------------------------------------------------------*/
154 132
155/* 133/*
156 * Announces that a buffer were filled and request the next 134 * Finish the current buffer
157 */ 135 */
158static inline void buffer_filled(struct em28xx *dev, 136static inline void finish_buffer(struct em28xx *dev,
159 struct em28xx_dmaqueue *dma_q, 137 struct em28xx_buffer *buf)
160 struct em28xx_buffer *buf)
161{ 138{
162 /* Advice that buffer was filled */ 139 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
163 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
164 buf->vb.state = VIDEOBUF_DONE;
165 buf->vb.field_count++;
166 do_gettimeofday(&buf->vb.ts);
167 140
168 dev->isoc_ctl.vid_buf = NULL; 141 buf->vb.v4l2_buf.sequence = dev->field_count++;
142 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
143 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
169 144
170 list_del(&buf->vb.queue); 145 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
171 wake_up(&buf->vb.done);
172}
173
174static inline void vbi_buffer_filled(struct em28xx *dev,
175 struct em28xx_dmaqueue *dma_q,
176 struct em28xx_buffer *buf)
177{
178 /* Advice that buffer was filled */
179 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
180
181 buf->vb.state = VIDEOBUF_DONE;
182 buf->vb.field_count++;
183 do_gettimeofday(&buf->vb.ts);
184
185 dev->isoc_ctl.vbi_buf = NULL;
186
187 list_del(&buf->vb.queue);
188 wake_up(&buf->vb.done);
189} 146}
190 147
191/* 148/*
192 * Identify the buffer header type and properly handles 149 * Copy picture data from USB buffer to videobuf buffer
193 */ 150 */
194static void em28xx_copy_video(struct em28xx *dev, 151static void em28xx_copy_video(struct em28xx *dev,
195 struct em28xx_dmaqueue *dma_q,
196 struct em28xx_buffer *buf, 152 struct em28xx_buffer *buf,
197 unsigned char *p, 153 unsigned char *usb_buf,
198 unsigned char *outp, unsigned long len) 154 unsigned long len)
199{ 155{
200 void *fieldstart, *startwrite, *startread; 156 void *fieldstart, *startwrite, *startread;
201 int linesdone, currlinedone, offset, lencopy, remain; 157 int linesdone, currlinedone, offset, lencopy, remain;
202 int bytesperline = dev->width << 1; 158 int bytesperline = dev->width << 1;
203 159
204 if (dma_q->pos + len > buf->vb.size) 160 if (buf->pos + len > buf->length)
205 len = buf->vb.size - dma_q->pos; 161 len = buf->length - buf->pos;
206 162
207 startread = p; 163 startread = usb_buf;
208 remain = len; 164 remain = len;
209 165
210 if (dev->progressive) 166 if (dev->progressive || buf->top_field)
211 fieldstart = outp; 167 fieldstart = buf->vb_buf;
212 else { 168 else /* interlaced mode, even nr. of lines */
213 /* Interlaces two half frames */ 169 fieldstart = buf->vb_buf + bytesperline;
214 if (buf->top_field)
215 fieldstart = outp;
216 else
217 fieldstart = outp + bytesperline;
218 }
219 170
220 linesdone = dma_q->pos / bytesperline; 171 linesdone = buf->pos / bytesperline;
221 currlinedone = dma_q->pos % bytesperline; 172 currlinedone = buf->pos % bytesperline;
222 173
223 if (dev->progressive) 174 if (dev->progressive)
224 offset = linesdone * bytesperline + currlinedone; 175 offset = linesdone * bytesperline + currlinedone;
@@ -229,11 +180,12 @@ static void em28xx_copy_video(struct em28xx *dev,
229 lencopy = bytesperline - currlinedone; 180 lencopy = bytesperline - currlinedone;
230 lencopy = lencopy > remain ? remain : lencopy; 181 lencopy = lencopy > remain ? remain : lencopy;
231 182
232 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { 183 if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
233 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", 184 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
234 ((char *)startwrite + lencopy) - 185 ((char *)startwrite + lencopy) -
235 ((char *)outp + buf->vb.size)); 186 ((char *)buf->vb_buf + buf->length));
236 remain = (char *)outp + buf->vb.size - (char *)startwrite; 187 remain = (char *)buf->vb_buf + buf->length -
188 (char *)startwrite;
237 lencopy = remain; 189 lencopy = remain;
238 } 190 }
239 if (lencopy <= 0) 191 if (lencopy <= 0)
@@ -243,21 +195,24 @@ static void em28xx_copy_video(struct em28xx *dev,
243 remain -= lencopy; 195 remain -= lencopy;
244 196
245 while (remain > 0) { 197 while (remain > 0) {
246 startwrite += lencopy + bytesperline; 198 if (dev->progressive)
199 startwrite += lencopy;
200 else
201 startwrite += lencopy + bytesperline;
247 startread += lencopy; 202 startread += lencopy;
248 if (bytesperline > remain) 203 if (bytesperline > remain)
249 lencopy = remain; 204 lencopy = remain;
250 else 205 else
251 lencopy = bytesperline; 206 lencopy = bytesperline;
252 207
253 if ((char *)startwrite + lencopy > (char *)outp + 208 if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
254 buf->vb.size) { 209 buf->length) {
255 em28xx_isocdbg("Overflow of %zi bytes past buffer end" 210 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
256 "(2)\n", 211 "(2)\n",
257 ((char *)startwrite + lencopy) - 212 ((char *)startwrite + lencopy) -
258 ((char *)outp + buf->vb.size)); 213 ((char *)buf->vb_buf + buf->length));
259 lencopy = remain = (char *)outp + buf->vb.size - 214 lencopy = remain = (char *)buf->vb_buf + buf->length -
260 (char *)startwrite; 215 (char *)startwrite;
261 } 216 }
262 if (lencopy <= 0) 217 if (lencopy <= 0)
263 break; 218 break;
@@ -267,57 +222,29 @@ static void em28xx_copy_video(struct em28xx *dev,
267 remain -= lencopy; 222 remain -= lencopy;
268 } 223 }
269 224
270 dma_q->pos += len; 225 buf->pos += len;
271} 226}
272 227
228/*
229 * Copy VBI data from USB buffer to videobuf buffer
230 */
273static void em28xx_copy_vbi(struct em28xx *dev, 231static void em28xx_copy_vbi(struct em28xx *dev,
274 struct em28xx_dmaqueue *dma_q, 232 struct em28xx_buffer *buf,
275 struct em28xx_buffer *buf, 233 unsigned char *usb_buf,
276 unsigned char *p, 234 unsigned long len)
277 unsigned char *outp, unsigned long len)
278{ 235{
279 void *startwrite, *startread; 236 unsigned int offset;
280 int offset;
281 int bytesperline;
282
283 if (dev == NULL) {
284 em28xx_isocdbg("dev is null\n");
285 return;
286 }
287 bytesperline = dev->vbi_width;
288 237
289 if (dma_q == NULL) { 238 if (buf->pos + len > buf->length)
290 em28xx_isocdbg("dma_q is null\n"); 239 len = buf->length - buf->pos;
291 return;
292 }
293 if (buf == NULL) {
294 return;
295 }
296 if (p == NULL) {
297 em28xx_isocdbg("p is null\n");
298 return;
299 }
300 if (outp == NULL) {
301 em28xx_isocdbg("outp is null\n");
302 return;
303 }
304
305 if (dma_q->pos + len > buf->vb.size)
306 len = buf->vb.size - dma_q->pos;
307
308 startread = p;
309
310 startwrite = outp + dma_q->pos;
311 offset = dma_q->pos;
312 240
241 offset = buf->pos;
313 /* Make sure the bottom field populates the second half of the frame */ 242 /* Make sure the bottom field populates the second half of the frame */
314 if (buf->top_field == 0) { 243 if (buf->top_field == 0)
315 startwrite += bytesperline * dev->vbi_height; 244 offset += dev->vbi_width * dev->vbi_height;
316 offset += bytesperline * dev->vbi_height;
317 }
318 245
319 memcpy(startwrite, startread, len); 246 memcpy(buf->vb_buf + offset, usb_buf, len);
320 dma_q->pos += len; 247 buf->pos += len;
321} 248}
322 249
323static inline void print_err_status(struct em28xx *dev, 250static inline void print_err_status(struct em28xx *dev,
@@ -360,470 +287,444 @@ static inline void print_err_status(struct em28xx *dev,
360} 287}
361 288
362/* 289/*
363 * video-buf generic routine to get the next available buffer 290 * get the next available buffer from dma queue
364 */ 291 */
365static inline void get_next_buf(struct em28xx_dmaqueue *dma_q, 292static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
366 struct em28xx_buffer **buf) 293 struct em28xx_dmaqueue *dma_q)
367{ 294{
368 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); 295 struct em28xx_buffer *buf;
369 char *outp;
370 296
371 if (list_empty(&dma_q->active)) { 297 if (list_empty(&dma_q->active)) {
372 em28xx_isocdbg("No active queue to serve\n"); 298 em28xx_isocdbg("No active queue to serve\n");
373 dev->isoc_ctl.vid_buf = NULL; 299 return NULL;
374 *buf = NULL;
375 return;
376 } 300 }
377 301
378 /* Get the next buffer */ 302 /* Get the next buffer */
379 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); 303 buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
380
381 /* Cleans up buffer - Useful for testing for frame/URB loss */ 304 /* Cleans up buffer - Useful for testing for frame/URB loss */
382 outp = videobuf_to_vmalloc(&(*buf)->vb); 305 list_del(&buf->list);
383 memset(outp, 0, (*buf)->vb.size); 306 buf->pos = 0;
307 buf->vb_buf = buf->mem;
384 308
385 dev->isoc_ctl.vid_buf = *buf; 309 return buf;
386
387 return;
388} 310}
389 311
390/* 312/*
391 * video-buf generic routine to get the next available VBI buffer 313 * Finish the current buffer if completed and prepare for the next field
392 */ 314 */
393static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q, 315static struct em28xx_buffer *
394 struct em28xx_buffer **buf) 316finish_field_prepare_next(struct em28xx *dev,
395{ 317 struct em28xx_buffer *buf,
396 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq); 318 struct em28xx_dmaqueue *dma_q)
397 char *outp; 319{
398 320 if (dev->progressive || dev->top_field) { /* Brand new frame */
399 if (list_empty(&dma_q->active)) { 321 if (buf != NULL)
400 em28xx_isocdbg("No active queue to serve\n"); 322 finish_buffer(dev, buf);
401 dev->isoc_ctl.vbi_buf = NULL; 323 buf = get_next_buf(dev, dma_q);
402 *buf = NULL; 324 }
403 return; 325 if (buf != NULL) {
326 buf->top_field = dev->top_field;
327 buf->pos = 0;
404 } 328 }
405 329
406 /* Get the next buffer */ 330 return buf;
407 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
408 /* Cleans up buffer - Useful for testing for frame/URB loss */
409 outp = videobuf_to_vmalloc(&(*buf)->vb);
410 memset(outp, 0x00, (*buf)->vb.size);
411
412 dev->isoc_ctl.vbi_buf = *buf;
413
414 return;
415} 331}
416 332
417/* 333/*
418 * Controls the isoc copy of each urb packet 334 * Process data packet according to the em2710/em2750/em28xx frame data format
419 */ 335 */
420static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb) 336static inline void process_frame_data_em28xx(struct em28xx *dev,
337 unsigned char *data_pkt,
338 unsigned int data_len)
421{ 339{
422 struct em28xx_buffer *buf; 340 struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
341 struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
423 struct em28xx_dmaqueue *dma_q = &dev->vidq; 342 struct em28xx_dmaqueue *dma_q = &dev->vidq;
424 unsigned char *outp = NULL; 343 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
425 int i, len = 0, rc = 1;
426 unsigned char *p;
427
428 if (!dev)
429 return 0;
430
431 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
432 return 0;
433
434 if (urb->status < 0) {
435 print_err_status(dev, -1, urb->status);
436 if (urb->status == -ENOENT)
437 return 0;
438 }
439
440 buf = dev->isoc_ctl.vid_buf;
441 if (buf != NULL)
442 outp = videobuf_to_vmalloc(&buf->vb);
443
444 for (i = 0; i < urb->number_of_packets; i++) {
445 int status = urb->iso_frame_desc[i].status;
446 344
447 if (status < 0) { 345 /* capture type 0 = vbi start
448 print_err_status(dev, i, status); 346 capture type 1 = vbi in progress
449 if (urb->iso_frame_desc[i].status != -EPROTO) 347 capture type 2 = video start
450 continue; 348 capture type 3 = video in progress */
349 if (data_len >= 4) {
350 /* NOTE: Headers are always 4 bytes and
351 * never split across packets */
352 if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
353 data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
354 /* Continuation */
355 data_pkt += 4;
356 data_len -= 4;
357 } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
358 /* Field start (VBI mode) */
359 dev->capture_type = 0;
360 dev->vbi_read = 0;
361 em28xx_isocdbg("VBI START HEADER !!!\n");
362 dev->top_field = !(data_pkt[2] & 1);
363 data_pkt += 4;
364 data_len -= 4;
365 } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
366 /* Field start (VBI disabled) */
367 dev->capture_type = 2;
368 em28xx_isocdbg("VIDEO START HEADER !!!\n");
369 dev->top_field = !(data_pkt[2] & 1);
370 data_pkt += 4;
371 data_len -= 4;
451 } 372 }
373 }
374 /* NOTE: With bulk transfers, intermediate data packets
375 * have no continuation header */
452 376
453 len = urb->iso_frame_desc[i].actual_length - 4; 377 if (dev->capture_type == 0) {
378 vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
379 dev->usb_ctl.vbi_buf = vbi_buf;
380 dev->capture_type = 1;
381 }
454 382
455 if (urb->iso_frame_desc[i].actual_length <= 0) { 383 if (dev->capture_type == 1) {
456 /* em28xx_isocdbg("packet %d is empty",i); - spammy */ 384 int vbi_size = dev->vbi_width * dev->vbi_height;
457 continue; 385 int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
458 } 386 (vbi_size - dev->vbi_read) : data_len;
459 if (urb->iso_frame_desc[i].actual_length >
460 dev->max_pkt_size) {
461 em28xx_isocdbg("packet bigger than packet size");
462 continue;
463 }
464 387
465 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 388 /* Copy VBI data */
389 if (vbi_buf != NULL)
390 em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
391 dev->vbi_read += vbi_data_len;
466 392
467 /* FIXME: incomplete buffer checks where removed to make 393 if (vbi_data_len < data_len) {
468 logic simpler. Impacts of those changes should be evaluated 394 /* Continue with copying video data */
469 */ 395 dev->capture_type = 2;
470 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { 396 data_pkt += vbi_data_len;
471 em28xx_isocdbg("VBI HEADER!!!\n"); 397 data_len -= vbi_data_len;
472 /* FIXME: Should add vbi copy */
473 continue;
474 } 398 }
475 if (p[0] == 0x22 && p[1] == 0x5a) { 399 }
476 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
477 len, (p[2] & 1) ? "odd" : "even");
478
479 if (dev->progressive || !(p[2] & 1)) {
480 if (buf != NULL)
481 buffer_filled(dev, dma_q, buf);
482 get_next_buf(dma_q, &buf);
483 if (buf == NULL)
484 outp = NULL;
485 else
486 outp = videobuf_to_vmalloc(&buf->vb);
487 }
488
489 if (buf != NULL) {
490 if (p[2] & 1)
491 buf->top_field = 0;
492 else
493 buf->top_field = 1;
494 }
495 400
496 dma_q->pos = 0; 401 if (dev->capture_type == 2) {
497 } 402 buf = finish_field_prepare_next(dev, buf, dma_q);
498 if (buf != NULL) { 403 dev->usb_ctl.vid_buf = buf;
499 if (p[0] != 0x88 && p[0] != 0x22) { 404 dev->capture_type = 3;
500 em28xx_isocdbg("frame is not complete\n");
501 len += 4;
502 } else {
503 p += 4;
504 }
505 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
506 }
507 } 405 }
508 return rc; 406
407 if (dev->capture_type == 3 && buf != NULL && data_len > 0)
408 em28xx_copy_video(dev, buf, data_pkt, data_len);
509} 409}
510 410
511/* Version of isoc handler that takes into account a mixture of video and 411/* Processes and copies the URB data content (video and VBI data) */
512 VBI data */ 412static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
513static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
514{ 413{
515 struct em28xx_buffer *buf, *vbi_buf; 414 int xfer_bulk, num_packets, i;
516 struct em28xx_dmaqueue *dma_q = &dev->vidq; 415 unsigned char *usb_data_pkt;
517 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq; 416 unsigned int usb_data_len;
518 unsigned char *outp = NULL;
519 unsigned char *vbioutp = NULL;
520 int i, len = 0, rc = 1;
521 unsigned char *p;
522 int vbi_size;
523 417
524 if (!dev) 418 if (!dev)
525 return 0; 419 return 0;
526 420
527 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) 421 if (dev->disconnected)
528 return 0; 422 return 0;
529 423
530 if (urb->status < 0) { 424 if (urb->status < 0)
531 print_err_status(dev, -1, urb->status); 425 print_err_status(dev, -1, urb->status);
532 if (urb->status == -ENOENT)
533 return 0;
534 }
535 426
536 buf = dev->isoc_ctl.vid_buf; 427 xfer_bulk = usb_pipebulk(urb->pipe);
537 if (buf != NULL)
538 outp = videobuf_to_vmalloc(&buf->vb);
539 428
540 vbi_buf = dev->isoc_ctl.vbi_buf; 429 if (xfer_bulk) /* bulk */
541 if (vbi_buf != NULL) 430 num_packets = 1;
542 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb); 431 else /* isoc */
432 num_packets = urb->number_of_packets;
543 433
544 for (i = 0; i < urb->number_of_packets; i++) { 434 for (i = 0; i < num_packets; i++) {
545 int status = urb->iso_frame_desc[i].status; 435 if (xfer_bulk) { /* bulk */
436 usb_data_len = urb->actual_length;
546 437
547 if (status < 0) { 438 usb_data_pkt = urb->transfer_buffer;
548 print_err_status(dev, i, status); 439 } else { /* isoc */
549 if (urb->iso_frame_desc[i].status != -EPROTO) 440 if (urb->iso_frame_desc[i].status < 0) {
441 print_err_status(dev, i,
442 urb->iso_frame_desc[i].status);
443 if (urb->iso_frame_desc[i].status != -EPROTO)
444 continue;
445 }
446
447 usb_data_len = urb->iso_frame_desc[i].actual_length;
448 if (usb_data_len > dev->max_pkt_size) {
449 em28xx_isocdbg("packet bigger than packet size");
550 continue; 450 continue;
551 } 451 }
552 452
553 len = urb->iso_frame_desc[i].actual_length; 453 usb_data_pkt = urb->transfer_buffer +
554 if (urb->iso_frame_desc[i].actual_length <= 0) { 454 urb->iso_frame_desc[i].offset;
555 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
556 continue;
557 }
558 if (urb->iso_frame_desc[i].actual_length >
559 dev->max_pkt_size) {
560 em28xx_isocdbg("packet bigger than packet size");
561 continue;
562 } 455 }
563 456
564 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; 457 if (usb_data_len == 0) {
565 458 /* NOTE: happens very often with isoc transfers */
566 /* capture type 0 = vbi start 459 /* em28xx_usbdbg("packet %d is empty",i); - spammy */
567 capture type 1 = video start 460 continue;
568 capture type 2 = video in progress */
569 if (p[0] == 0x33 && p[1] == 0x95) {
570 dev->capture_type = 0;
571 dev->vbi_read = 0;
572 em28xx_isocdbg("VBI START HEADER!!!\n");
573 dev->cur_field = p[2];
574 p += 4;
575 len -= 4;
576 } else if (p[0] == 0x88 && p[1] == 0x88 &&
577 p[2] == 0x88 && p[3] == 0x88) {
578 /* continuation */
579 p += 4;
580 len -= 4;
581 } else if (p[0] == 0x22 && p[1] == 0x5a) {
582 /* start video */
583 p += 4;
584 len -= 4;
585 } 461 }
586 462
587 vbi_size = dev->vbi_width * dev->vbi_height; 463 process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
588 464 }
589 if (dev->capture_type == 0) { 465 return 1;
590 if (dev->vbi_read >= vbi_size) { 466}
591 /* We've already read all the VBI data, so
592 treat the rest as video */
593 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
594 } else if ((dev->vbi_read + len) < vbi_size) {
595 /* This entire frame is VBI data */
596 if (dev->vbi_read == 0 &&
597 (!(dev->cur_field & 1))) {
598 /* Brand new frame */
599 if (vbi_buf != NULL)
600 vbi_buffer_filled(dev,
601 vbi_dma_q,
602 vbi_buf);
603 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
604 if (vbi_buf == NULL)
605 vbioutp = NULL;
606 else
607 vbioutp = videobuf_to_vmalloc(
608 &vbi_buf->vb);
609 }
610
611 if (dev->vbi_read == 0) {
612 vbi_dma_q->pos = 0;
613 if (vbi_buf != NULL) {
614 if (dev->cur_field & 1)
615 vbi_buf->top_field = 0;
616 else
617 vbi_buf->top_field = 1;
618 }
619 }
620
621 dev->vbi_read += len;
622 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
623 vbioutp, len);
624 } else {
625 /* Some of this frame is VBI data and some is
626 video data */
627 int vbi_data_len = vbi_size - dev->vbi_read;
628 dev->vbi_read += vbi_data_len;
629 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
630 vbioutp, vbi_data_len);
631 dev->capture_type = 1;
632 p += vbi_data_len;
633 len -= vbi_data_len;
634 }
635 }
636 467
637 if (dev->capture_type == 1) {
638 dev->capture_type = 2;
639 if (dev->progressive || !(dev->cur_field & 1)) {
640 if (buf != NULL)
641 buffer_filled(dev, dma_q, buf);
642 get_next_buf(dma_q, &buf);
643 if (buf == NULL)
644 outp = NULL;
645 else
646 outp = videobuf_to_vmalloc(&buf->vb);
647 }
648 if (buf != NULL) {
649 if (dev->cur_field & 1)
650 buf->top_field = 0;
651 else
652 buf->top_field = 1;
653 }
654 468
655 dma_q->pos = 0; 469static int get_ressource(enum v4l2_buf_type f_type)
656 } 470{
471 switch (f_type) {
472 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
473 return EM28XX_RESOURCE_VIDEO;
474 case V4L2_BUF_TYPE_VBI_CAPTURE:
475 return EM28XX_RESOURCE_VBI;
476 default:
477 BUG();
478 return 0;
479 }
480}
657 481
658 if (buf != NULL && dev->capture_type == 2) { 482/* Usage lock check functions */
659 if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 && 483static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
660 p[2] == 0x88 && p[3] == 0x88) { 484{
661 p += 4; 485 int res_type = get_ressource(f_type);
662 len -= 4;
663 }
664 if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
665 em28xx_isocdbg("Video frame %d, len=%i, %s\n",
666 p[2], len, (p[2] & 1) ?
667 "odd" : "even");
668 p += 4;
669 len -= 4;
670 }
671 486
672 if (len > 0) 487 /* is it free? */
673 em28xx_copy_video(dev, dma_q, buf, p, outp, 488 if (dev->resources & res_type) {
674 len); 489 /* no, someone else uses it */
675 } 490 return -EBUSY;
676 } 491 }
677 return rc; 492
493 /* it's free, grab it */
494 dev->resources |= res_type;
495 em28xx_videodbg("res: get %d\n", res_type);
496 return 0;
678} 497}
679 498
499static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
500{
501 int res_type = get_ressource(f_type);
502
503 dev->resources &= ~res_type;
504 em28xx_videodbg("res: put %d\n", res_type);
505}
680 506
681/* ------------------------------------------------------------------ 507/* ------------------------------------------------------------------
682 Videobuf operations 508 Videobuf2 operations
683 ------------------------------------------------------------------*/ 509 ------------------------------------------------------------------*/
684 510
685static int 511static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
686buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) 512 unsigned int *nbuffers, unsigned int *nplanes,
513 unsigned int sizes[], void *alloc_ctxs[])
687{ 514{
688 struct em28xx_fh *fh = vq->priv_data; 515 struct em28xx *dev = vb2_get_drv_priv(vq);
689 struct em28xx *dev = fh->dev; 516 unsigned long size;
690 struct v4l2_frequency f;
691 517
692 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) 518 if (fmt)
693 >> 3; 519 size = fmt->fmt.pix.sizeimage;
694 520 else
695 if (0 == *count) 521 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
696 *count = EM28XX_DEF_BUF;
697 522
698 if (*count < EM28XX_MIN_BUF) 523 if (size == 0)
699 *count = EM28XX_MIN_BUF; 524 return -EINVAL;
700 525
701 /* Ask tuner to go to analog or radio mode */ 526 if (0 == *nbuffers)
702 memset(&f, 0, sizeof(f)); 527 *nbuffers = 32;
703 f.frequency = dev->ctl_freq;
704 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
705 528
706 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); 529 *nplanes = 1;
530 sizes[0] = size;
707 531
708 return 0; 532 return 0;
709} 533}
710 534
711/* This is called *without* dev->slock held; please keep it that way */ 535static int
712static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) 536buffer_prepare(struct vb2_buffer *vb)
713{ 537{
714 struct em28xx_fh *fh = vq->priv_data; 538 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
715 struct em28xx *dev = fh->dev; 539 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
716 unsigned long flags = 0; 540 unsigned long size;
717 if (in_interrupt())
718 BUG();
719 541
720 /* We used to wait for the buffer to finish here, but this didn't work 542 em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
721 because, as we were keeping the state as VIDEOBUF_QUEUED,
722 videobuf_queue_cancel marked it as finished for us.
723 (Also, it could wedge forever if the hardware was misconfigured.)
724 543
725 This should be safe; by the time we get here, the buffer isn't 544 size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
726 queued anymore. If we ever start marking the buffers as
727 VIDEOBUF_ACTIVE, it won't be, though.
728 */
729 spin_lock_irqsave(&dev->slock, flags);
730 if (dev->isoc_ctl.vid_buf == buf)
731 dev->isoc_ctl.vid_buf = NULL;
732 spin_unlock_irqrestore(&dev->slock, flags);
733 545
734 videobuf_vmalloc_free(&buf->vb); 546 if (vb2_plane_size(vb, 0) < size) {
735 buf->vb.state = VIDEOBUF_NEEDS_INIT; 547 em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
548 __func__, vb2_plane_size(vb, 0), size);
549 return -EINVAL;
550 }
551 vb2_set_plane_payload(&buf->vb, 0, size);
552
553 return 0;
736} 554}
737 555
738static int 556int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
739buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
740 enum v4l2_field field)
741{ 557{
742 struct em28xx_fh *fh = vq->priv_data; 558 struct em28xx *dev = vb2_get_drv_priv(vq);
743 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); 559 struct v4l2_frequency f;
744 struct em28xx *dev = fh->dev; 560 int rc = 0;
745 int rc = 0, urb_init = 0;
746 561
747 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth 562 em28xx_videodbg("%s\n", __func__);
748 + 7) >> 3;
749 563
750 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 564 /* Make sure streaming is not already in progress for this type
751 return -EINVAL; 565 of filehandle (e.g. video, vbi) */
566 rc = res_get(dev, vq->type);
567 if (rc)
568 return rc;
569
570 if (dev->streaming_users++ == 0) {
571 /* First active streaming user, so allocate all the URBs */
752 572
753 buf->vb.width = dev->width; 573 /* Allocate the USB bandwidth */
754 buf->vb.height = dev->height; 574 em28xx_set_alternate(dev);
755 buf->vb.field = field; 575
576 /* Needed, since GPIO might have disabled power of
577 some i2c device
578 */
579 em28xx_wake_i2c(dev);
756 580
757 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 581 dev->capture_type = -1;
758 rc = videobuf_iolock(vq, &buf->vb, NULL); 582 rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
583 dev->analog_xfer_bulk,
584 EM28XX_NUM_BUFS,
585 dev->max_pkt_size,
586 dev->packet_multiplier,
587 em28xx_urb_data_copy);
759 if (rc < 0) 588 if (rc < 0)
760 goto fail; 589 goto fail;
761 }
762 590
763 if (!dev->isoc_ctl.analog_bufs.num_bufs) 591 /*
764 urb_init = 1; 592 * djh: it's not clear whether this code is still needed. I'm
593 * leaving it in here for now entirely out of concern for
594 * backward compatibility (the old code did it)
595 */
765 596
766 if (urb_init) { 597 /* Ask tuner to go to analog or radio mode */
767 if (em28xx_vbi_supported(dev) == 1) 598 memset(&f, 0, sizeof(f));
768 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE, 599 f.frequency = dev->ctl_freq;
769 EM28XX_NUM_PACKETS, 600 if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
770 EM28XX_NUM_BUFS, 601 f.type = V4L2_TUNER_RADIO;
771 dev->max_pkt_size,
772 em28xx_isoc_copy_vbi);
773 else 602 else
774 rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE, 603 f.type = V4L2_TUNER_ANALOG_TV;
775 EM28XX_NUM_PACKETS, 604 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
776 EM28XX_NUM_BUFS,
777 dev->max_pkt_size,
778 em28xx_isoc_copy);
779 if (rc < 0)
780 goto fail;
781 } 605 }
782 606
783 buf->vb.state = VIDEOBUF_PREPARED;
784 return 0;
785
786fail: 607fail:
787 free_buffer(vq, buf);
788 return rc; 608 return rc;
789} 609}
790 610
791static void 611static int em28xx_stop_streaming(struct vb2_queue *vq)
792buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
793{ 612{
794 struct em28xx_buffer *buf = container_of(vb, 613 struct em28xx *dev = vb2_get_drv_priv(vq);
795 struct em28xx_buffer, 614 struct em28xx_dmaqueue *vidq = &dev->vidq;
796 vb); 615 unsigned long flags = 0;
797 struct em28xx_fh *fh = vq->priv_data; 616
798 struct em28xx *dev = fh->dev; 617 em28xx_videodbg("%s\n", __func__);
799 struct em28xx_dmaqueue *vidq = &dev->vidq; 618
619 res_free(dev, vq->type);
620
621 if (dev->streaming_users-- == 1) {
622 /* Last active user, so shutdown all the URBS */
623 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
624 }
800 625
801 buf->vb.state = VIDEOBUF_QUEUED; 626 spin_lock_irqsave(&dev->slock, flags);
802 list_add_tail(&buf->vb.queue, &vidq->active); 627 while (!list_empty(&vidq->active)) {
628 struct em28xx_buffer *buf;
629 buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
630 list_del(&buf->list);
631 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
632 }
633 dev->usb_ctl.vid_buf = NULL;
634 spin_unlock_irqrestore(&dev->slock, flags);
803 635
636 return 0;
804} 637}
805 638
806static void buffer_release(struct videobuf_queue *vq, 639int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
807 struct videobuf_buffer *vb)
808{ 640{
809 struct em28xx_buffer *buf = container_of(vb, 641 struct em28xx *dev = vb2_get_drv_priv(vq);
810 struct em28xx_buffer, 642 struct em28xx_dmaqueue *vbiq = &dev->vbiq;
811 vb); 643 unsigned long flags = 0;
812 struct em28xx_fh *fh = vq->priv_data; 644
813 struct em28xx *dev = (struct em28xx *)fh->dev; 645 em28xx_videodbg("%s\n", __func__);
814 646
815 em28xx_isocdbg("em28xx: called buffer_release\n"); 647 res_free(dev, vq->type);
816 648
817 free_buffer(vq, buf); 649 if (dev->streaming_users-- == 1) {
650 /* Last active user, so shutdown all the URBS */
651 em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
652 }
653
654 spin_lock_irqsave(&dev->slock, flags);
655 while (!list_empty(&vbiq->active)) {
656 struct em28xx_buffer *buf;
657 buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
658 list_del(&buf->list);
659 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
660 }
661 dev->usb_ctl.vbi_buf = NULL;
662 spin_unlock_irqrestore(&dev->slock, flags);
663
664 return 0;
818} 665}
819 666
820static struct videobuf_queue_ops em28xx_video_qops = { 667static void
821 .buf_setup = buffer_setup, 668buffer_queue(struct vb2_buffer *vb)
669{
670 struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
671 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
672 struct em28xx_dmaqueue *vidq = &dev->vidq;
673 unsigned long flags = 0;
674
675 em28xx_videodbg("%s\n", __func__);
676 buf->mem = vb2_plane_vaddr(vb, 0);
677 buf->length = vb2_plane_size(vb, 0);
678
679 spin_lock_irqsave(&dev->slock, flags);
680 list_add_tail(&buf->list, &vidq->active);
681 spin_unlock_irqrestore(&dev->slock, flags);
682}
683
684static struct vb2_ops em28xx_video_qops = {
685 .queue_setup = queue_setup,
822 .buf_prepare = buffer_prepare, 686 .buf_prepare = buffer_prepare,
823 .buf_queue = buffer_queue, 687 .buf_queue = buffer_queue,
824 .buf_release = buffer_release, 688 .start_streaming = em28xx_start_analog_streaming,
689 .stop_streaming = em28xx_stop_streaming,
690 .wait_prepare = vb2_ops_wait_prepare,
691 .wait_finish = vb2_ops_wait_finish,
825}; 692};
826 693
694int em28xx_vb2_setup(struct em28xx *dev)
695{
696 int rc;
697 struct vb2_queue *q;
698
699 /* Setup Videobuf2 for Video capture */
700 q = &dev->vb_vidq;
701 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
702 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
703 q->drv_priv = dev;
704 q->buf_struct_size = sizeof(struct em28xx_buffer);
705 q->ops = &em28xx_video_qops;
706 q->mem_ops = &vb2_vmalloc_memops;
707
708 rc = vb2_queue_init(q);
709 if (rc < 0)
710 return rc;
711
712 /* Setup Videobuf2 for VBI capture */
713 q = &dev->vb_vbiq;
714 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
715 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
716 q->drv_priv = dev;
717 q->buf_struct_size = sizeof(struct em28xx_buffer);
718 q->ops = &em28xx_vbi_qops;
719 q->mem_ops = &vb2_vmalloc_memops;
720
721 rc = vb2_queue_init(q);
722 if (rc < 0)
723 return rc;
724
725 return 0;
726}
727
827/********************* v4l2 interface **************************************/ 728/********************* v4l2 interface **************************************/
828 729
829static void video_mux(struct em28xx *dev, int index) 730static void video_mux(struct em28xx *dev, int index)
@@ -856,143 +757,54 @@ static void video_mux(struct em28xx *dev, int index)
856 em28xx_audio_analog_set(dev); 757 em28xx_audio_analog_set(dev);
857} 758}
858 759
859/* Usage lock check functions */ 760void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
860static int res_get(struct em28xx_fh *fh, unsigned int bit)
861{
862 struct em28xx *dev = fh->dev;
863
864 if (fh->resources & bit)
865 /* have it already allocated */
866 return 1;
867
868 /* is it free? */
869 if (dev->resources & bit) {
870 /* no, someone else uses it */
871 return 0;
872 }
873 /* it's free, grab it */
874 fh->resources |= bit;
875 dev->resources |= bit;
876 em28xx_videodbg("res: get %d\n", bit);
877 return 1;
878}
879
880static int res_check(struct em28xx_fh *fh, unsigned int bit)
881{
882 return fh->resources & bit;
883}
884
885static int res_locked(struct em28xx *dev, unsigned int bit)
886{
887 return dev->resources & bit;
888}
889
890static void res_free(struct em28xx_fh *fh, unsigned int bits)
891{ 761{
892 struct em28xx *dev = fh->dev; 762 struct em28xx *dev = priv;
893
894 BUG_ON((fh->resources & bits) != bits);
895
896 fh->resources &= ~bits;
897 dev->resources &= ~bits;
898 em28xx_videodbg("res: put %d\n", bits);
899}
900
901static int get_ressource(struct em28xx_fh *fh)
902{
903 switch (fh->type) {
904 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
905 return EM28XX_RESOURCE_VIDEO;
906 case V4L2_BUF_TYPE_VBI_CAPTURE:
907 return EM28XX_RESOURCE_VBI;
908 default:
909 BUG();
910 return 0;
911 }
912}
913
914/*
915 * ac97_queryctrl()
916 * return the ac97 supported controls
917 */
918static int ac97_queryctrl(struct v4l2_queryctrl *qc)
919{
920 int i;
921 763
922 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) { 764 /*
923 if (qc->id && qc->id == ac97_qctrl[i].id) { 765 * In the case of non-AC97 volume controls, we still need
924 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc)); 766 * to do some setups at em28xx, in order to mute/unmute
925 return 0; 767 * and to adjust audio volume. However, the value ranges
926 } 768 * should be checked by the corresponding V4L subdriver.
927 } 769 */
928
929 /* Control is not ac97 related */
930 return 1;
931}
932
933/*
934 * ac97_get_ctrl()
935 * return the current values for ac97 mute and volume
936 */
937static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
938{
939 switch (ctrl->id) { 770 switch (ctrl->id) {
940 case V4L2_CID_AUDIO_MUTE: 771 case V4L2_CID_AUDIO_MUTE:
941 ctrl->value = dev->mute; 772 dev->mute = ctrl->val;
942 return 0; 773 em28xx_audio_analog_set(dev);
774 break;
943 case V4L2_CID_AUDIO_VOLUME: 775 case V4L2_CID_AUDIO_VOLUME:
944 ctrl->value = dev->volume; 776 dev->volume = ctrl->val;
945 return 0; 777 em28xx_audio_analog_set(dev);
946 default: 778 break;
947 /* Control is not ac97 related */
948 return 1;
949 } 779 }
950} 780}
951 781
952/* 782static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
953 * ac97_set_ctrl()
954 * set values for ac97 mute and volume
955 */
956static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
957{ 783{
958 int i; 784 struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
959
960 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
961 if (ctrl->id == ac97_qctrl[i].id)
962 goto handle;
963
964 /* Announce that hasn't handle it */
965 return 1;
966
967handle:
968 if (ctrl->value < ac97_qctrl[i].minimum ||
969 ctrl->value > ac97_qctrl[i].maximum)
970 return -ERANGE;
971 785
972 switch (ctrl->id) { 786 switch (ctrl->id) {
973 case V4L2_CID_AUDIO_MUTE: 787 case V4L2_CID_AUDIO_MUTE:
974 dev->mute = ctrl->value; 788 dev->mute = ctrl->val;
975 break; 789 break;
976 case V4L2_CID_AUDIO_VOLUME: 790 case V4L2_CID_AUDIO_VOLUME:
977 dev->volume = ctrl->value; 791 dev->volume = ctrl->val;
978 break; 792 break;
979 } 793 }
980 794
981 return em28xx_audio_analog_set(dev); 795 return em28xx_audio_analog_set(dev);
982} 796}
983 797
798const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
799 .s_ctrl = em28xx_s_ctrl,
800};
801
984static int check_dev(struct em28xx *dev) 802static int check_dev(struct em28xx *dev)
985{ 803{
986 if (dev->state & DEV_DISCONNECTED) { 804 if (dev->disconnected) {
987 em28xx_errdev("v4l2 ioctl: device not present\n"); 805 em28xx_errdev("v4l2 ioctl: device not present\n");
988 return -ENODEV; 806 return -ENODEV;
989 } 807 }
990
991 if (dev->state & DEV_MISCONFIGURED) {
992 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
993 "close and open it again\n");
994 return -EIO;
995 }
996 return 0; 808 return 0;
997} 809}
998 810
@@ -1072,8 +884,11 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1072 /* the em2800 can only scale down to 50% */ 884 /* the em2800 can only scale down to 50% */
1073 height = height > (3 * maxh / 4) ? maxh : maxh / 2; 885 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1074 width = width > (3 * maxw / 4) ? maxw : maxw / 2; 886 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1075 /* MaxPacketSize for em2800 is too small to capture at full resolution 887 /*
1076 * use half of maxw as the scaler can only scale to 50% */ 888 * MaxPacketSize for em2800 is too small to capture at full
889 * resolution use half of maxw as the scaler can only scale
890 * to 50%
891 */
1077 if (width == maxw && height == maxh) 892 if (width == maxw && height == maxh)
1078 width /= 2; 893 width /= 2;
1079 } else { 894 } else {
@@ -1091,7 +906,7 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1091 f->fmt.pix.width = width; 906 f->fmt.pix.width = width;
1092 f->fmt.pix.height = height; 907 f->fmt.pix.height = height;
1093 f->fmt.pix.pixelformat = fmt->fourcc; 908 f->fmt.pix.pixelformat = fmt->fourcc;
1094 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3; 909 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
1095 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height; 910 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1096 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 911 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1097 if (dev->progressive) 912 if (dev->progressive)
@@ -1119,7 +934,6 @@ static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1119 /* set new image size */ 934 /* set new image size */
1120 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 935 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1121 936
1122 em28xx_set_alternate(dev);
1123 em28xx_resolution_set(dev); 937 em28xx_resolution_set(dev);
1124 938
1125 return 0; 939 return 0;
@@ -1128,21 +942,13 @@ static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1128static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 942static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1129 struct v4l2_format *f) 943 struct v4l2_format *f)
1130{ 944{
1131 struct em28xx_fh *fh = priv; 945 struct em28xx *dev = video_drvdata(file);
1132 struct em28xx *dev = fh->dev;
1133 int rc;
1134 946
1135 rc = check_dev(dev); 947 if (dev->streaming_users > 0)
1136 if (rc < 0) 948 return -EBUSY;
1137 return rc;
1138 949
1139 vidioc_try_fmt_vid_cap(file, priv, f); 950 vidioc_try_fmt_vid_cap(file, priv, f);
1140 951
1141 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1142 em28xx_errdev("%s queue busy\n", __func__);
1143 return -EBUSY;
1144 }
1145
1146 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat, 952 return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1147 f->fmt.pix.width, f->fmt.pix.height); 953 f->fmt.pix.width, f->fmt.pix.height);
1148} 954}
@@ -1153,6 +959,8 @@ static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1153 struct em28xx *dev = fh->dev; 959 struct em28xx *dev = fh->dev;
1154 int rc; 960 int rc;
1155 961
962 if (dev->board.is_webcam)
963 return -ENOTTY;
1156 rc = check_dev(dev); 964 rc = check_dev(dev);
1157 if (rc < 0) 965 if (rc < 0)
1158 return rc; 966 return rc;
@@ -1168,6 +976,8 @@ static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
1168 struct em28xx *dev = fh->dev; 976 struct em28xx *dev = fh->dev;
1169 int rc; 977 int rc;
1170 978
979 if (dev->board.is_webcam)
980 return -ENOTTY;
1171 rc = check_dev(dev); 981 rc = check_dev(dev);
1172 if (rc < 0) 982 if (rc < 0)
1173 return rc; 983 return rc;
@@ -1184,15 +994,22 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1184 struct v4l2_format f; 994 struct v4l2_format f;
1185 int rc; 995 int rc;
1186 996
997 if (dev->board.is_webcam)
998 return -ENOTTY;
999 if (*norm == dev->norm)
1000 return 0;
1187 rc = check_dev(dev); 1001 rc = check_dev(dev);
1188 if (rc < 0) 1002 if (rc < 0)
1189 return rc; 1003 return rc;
1190 1004
1005 if (dev->streaming_users > 0)
1006 return -EBUSY;
1007
1191 dev->norm = *norm; 1008 dev->norm = *norm;
1192 1009
1193 /* Adjusts width/height, if needed */ 1010 /* Adjusts width/height, if needed */
1194 f.fmt.pix.width = dev->width; 1011 f.fmt.pix.width = 720;
1195 f.fmt.pix.height = dev->height; 1012 f.fmt.pix.height = (*norm & V4L2_STD_525_60) ? 480 : 576;
1196 vidioc_try_fmt_vid_cap(file, priv, &f); 1013 vidioc_try_fmt_vid_cap(file, priv, &f);
1197 1014
1198 /* set new image size */ 1015 /* set new image size */
@@ -1216,6 +1033,7 @@ static int vidioc_g_parm(struct file *file, void *priv,
1216 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1033 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1217 return -EINVAL; 1034 return -EINVAL;
1218 1035
1036 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1219 if (dev->board.is_webcam) 1037 if (dev->board.is_webcam)
1220 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0, 1038 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1221 video, g_parm, p); 1039 video, g_parm, p);
@@ -1233,11 +1051,12 @@ static int vidioc_s_parm(struct file *file, void *priv,
1233 struct em28xx *dev = fh->dev; 1051 struct em28xx *dev = fh->dev;
1234 1052
1235 if (!dev->board.is_webcam) 1053 if (!dev->board.is_webcam)
1236 return -EINVAL; 1054 return -ENOTTY;
1237 1055
1238 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1056 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1239 return -EINVAL; 1057 return -EINVAL;
1240 1058
1059 p->parm.capture.readbuffers = EM28XX_MIN_BUF;
1241 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p); 1060 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1242} 1061}
1243 1062
@@ -1276,6 +1095,9 @@ static int vidioc_enum_input(struct file *file, void *priv,
1276 i->type = V4L2_INPUT_TYPE_TUNER; 1095 i->type = V4L2_INPUT_TYPE_TUNER;
1277 1096
1278 i->std = dev->vdev->tvnorms; 1097 i->std = dev->vdev->tvnorms;
1098 /* webcams do not have the STD API */
1099 if (dev->board.is_webcam)
1100 i->capabilities = 0;
1279 1101
1280 return 0; 1102 return 0;
1281} 1103}
@@ -1375,131 +1197,6 @@ static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio
1375 return 0; 1197 return 0;
1376} 1198}
1377 1199
1378static int vidioc_queryctrl(struct file *file, void *priv,
1379 struct v4l2_queryctrl *qc)
1380{
1381 struct em28xx_fh *fh = priv;
1382 struct em28xx *dev = fh->dev;
1383 int id = qc->id;
1384 int rc;
1385
1386 rc = check_dev(dev);
1387 if (rc < 0)
1388 return rc;
1389
1390 memset(qc, 0, sizeof(*qc));
1391
1392 qc->id = id;
1393
1394 /* enumerate AC97 controls */
1395 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1396 rc = ac97_queryctrl(qc);
1397 if (!rc)
1398 return 0;
1399 }
1400
1401 /* enumerate V4L2 device controls */
1402 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1403
1404 if (qc->type)
1405 return 0;
1406 else
1407 return -EINVAL;
1408}
1409
1410/*
1411 * FIXME: This is an indirect way to check if a control exists at a
1412 * subdev. Instead of that hack, maybe the better would be to change all
1413 * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
1414 */
1415static int check_subdev_ctrl(struct em28xx *dev, int id)
1416{
1417 struct v4l2_queryctrl qc;
1418
1419 memset(&qc, 0, sizeof(qc));
1420 qc.id = id;
1421
1422 /* enumerate V4L2 device controls */
1423 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
1424
1425 if (qc.type)
1426 return 0;
1427 else
1428 return -EINVAL;
1429}
1430
1431static int vidioc_g_ctrl(struct file *file, void *priv,
1432 struct v4l2_control *ctrl)
1433{
1434 struct em28xx_fh *fh = priv;
1435 struct em28xx *dev = fh->dev;
1436 int rc;
1437
1438 rc = check_dev(dev);
1439 if (rc < 0)
1440 return rc;
1441 rc = 0;
1442
1443 /* Set an AC97 control */
1444 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1445 rc = ac97_get_ctrl(dev, ctrl);
1446 else
1447 rc = 1;
1448
1449 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1450 if (rc == 1) {
1451 if (check_subdev_ctrl(dev, ctrl->id))
1452 return -EINVAL;
1453
1454 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1455 rc = 0;
1456 }
1457
1458 return rc;
1459}
1460
1461static int vidioc_s_ctrl(struct file *file, void *priv,
1462 struct v4l2_control *ctrl)
1463{
1464 struct em28xx_fh *fh = priv;
1465 struct em28xx *dev = fh->dev;
1466 int rc;
1467
1468 rc = check_dev(dev);
1469 if (rc < 0)
1470 return rc;
1471
1472 /* Set an AC97 control */
1473 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1474 rc = ac97_set_ctrl(dev, ctrl);
1475 else
1476 rc = 1;
1477
1478 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
1479 if (rc == 1) {
1480 rc = check_subdev_ctrl(dev, ctrl->id);
1481 if (!rc)
1482 v4l2_device_call_all(&dev->v4l2_dev, 0,
1483 core, s_ctrl, ctrl);
1484 /*
1485 * In the case of non-AC97 volume controls, we still need
1486 * to do some setups at em28xx, in order to mute/unmute
1487 * and to adjust audio volume. However, the value ranges
1488 * should be checked by the corresponding V4L subdriver.
1489 */
1490 switch (ctrl->id) {
1491 case V4L2_CID_AUDIO_MUTE:
1492 dev->mute = ctrl->value;
1493 rc = em28xx_audio_analog_set(dev);
1494 break;
1495 case V4L2_CID_AUDIO_VOLUME:
1496 dev->volume = ctrl->value;
1497 rc = em28xx_audio_analog_set(dev);
1498 }
1499 }
1500 return (rc < 0) ? rc : 0;
1501}
1502
1503static int vidioc_g_tuner(struct file *file, void *priv, 1200static int vidioc_g_tuner(struct file *file, void *priv,
1504 struct v4l2_tuner *t) 1201 struct v4l2_tuner *t)
1505{ 1202{
@@ -1515,7 +1212,6 @@ static int vidioc_g_tuner(struct file *file, void *priv,
1515 return -EINVAL; 1212 return -EINVAL;
1516 1213
1517 strcpy(t->name, "Tuner"); 1214 strcpy(t->name, "Tuner");
1518 t->type = V4L2_TUNER_ANALOG_TV;
1519 1215
1520 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); 1216 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1521 return 0; 1217 return 0;
@@ -1545,7 +1241,9 @@ static int vidioc_g_frequency(struct file *file, void *priv,
1545 struct em28xx_fh *fh = priv; 1241 struct em28xx_fh *fh = priv;
1546 struct em28xx *dev = fh->dev; 1242 struct em28xx *dev = fh->dev;
1547 1243
1548 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; 1244 if (0 != f->tuner)
1245 return -EINVAL;
1246
1549 f->frequency = dev->ctl_freq; 1247 f->frequency = dev->ctl_freq;
1550 return 0; 1248 return 0;
1551} 1249}
@@ -1564,13 +1262,9 @@ static int vidioc_s_frequency(struct file *file, void *priv,
1564 if (0 != f->tuner) 1262 if (0 != f->tuner)
1565 return -EINVAL; 1263 return -EINVAL;
1566 1264
1567 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1568 return -EINVAL;
1569 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1570 return -EINVAL;
1571
1572 dev->ctl_freq = f->frequency;
1573 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f); 1265 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
1266 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
1267 dev->ctl_freq = f->frequency;
1574 1268
1575 return 0; 1269 return 0;
1576} 1270}
@@ -1596,6 +1290,14 @@ static int vidioc_g_chip_ident(struct file *file, void *priv,
1596 1290
1597 chip->ident = V4L2_IDENT_NONE; 1291 chip->ident = V4L2_IDENT_NONE;
1598 chip->revision = 0; 1292 chip->revision = 0;
1293 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
1294 if (v4l2_chip_match_host(&chip->match))
1295 chip->ident = V4L2_IDENT_NONE;
1296 return 0;
1297 }
1298 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1299 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1300 return -EINVAL;
1599 1301
1600 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip); 1302 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1601 1303
@@ -1704,72 +1406,10 @@ static int vidioc_cropcap(struct file *file, void *priv,
1704 return 0; 1406 return 0;
1705} 1407}
1706 1408
1707static int vidioc_streamon(struct file *file, void *priv,
1708 enum v4l2_buf_type type)
1709{
1710 struct em28xx_fh *fh = priv;
1711 struct em28xx *dev = fh->dev;
1712 int rc = -EINVAL;
1713
1714 rc = check_dev(dev);
1715 if (rc < 0)
1716 return rc;
1717
1718 if (unlikely(type != fh->type))
1719 return -EINVAL;
1720
1721 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1722 fh, type, fh->resources, dev->resources);
1723
1724 if (unlikely(!res_get(fh, get_ressource(fh))))
1725 return -EBUSY;
1726
1727 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1728 rc = videobuf_streamon(&fh->vb_vidq);
1729 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1730 rc = videobuf_streamon(&fh->vb_vbiq);
1731
1732 return rc;
1733}
1734
1735static int vidioc_streamoff(struct file *file, void *priv,
1736 enum v4l2_buf_type type)
1737{
1738 struct em28xx_fh *fh = priv;
1739 struct em28xx *dev = fh->dev;
1740 int rc;
1741
1742 rc = check_dev(dev);
1743 if (rc < 0)
1744 return rc;
1745
1746 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1747 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1748 return -EINVAL;
1749 if (type != fh->type)
1750 return -EINVAL;
1751
1752 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1753 fh, type, fh->resources, dev->resources);
1754
1755 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1756 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
1757 videobuf_streamoff(&fh->vb_vidq);
1758 res_free(fh, EM28XX_RESOURCE_VIDEO);
1759 }
1760 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1761 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
1762 videobuf_streamoff(&fh->vb_vbiq);
1763 res_free(fh, EM28XX_RESOURCE_VBI);
1764 }
1765 }
1766
1767 return 0;
1768}
1769
1770static int vidioc_querycap(struct file *file, void *priv, 1409static int vidioc_querycap(struct file *file, void *priv,
1771 struct v4l2_capability *cap) 1410 struct v4l2_capability *cap)
1772{ 1411{
1412 struct video_device *vdev = video_devdata(file);
1773 struct em28xx_fh *fh = priv; 1413 struct em28xx_fh *fh = priv;
1774 struct em28xx *dev = fh->dev; 1414 struct em28xx *dev = fh->dev;
1775 1415
@@ -1777,20 +1417,26 @@ static int vidioc_querycap(struct file *file, void *priv,
1777 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); 1417 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1778 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 1418 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1779 1419
1780 cap->capabilities = 1420 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1781 V4L2_CAP_SLICED_VBI_CAPTURE | 1421 cap->device_caps = V4L2_CAP_READWRITE |
1782 V4L2_CAP_VIDEO_CAPTURE | 1422 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1783 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; 1423 else if (vdev->vfl_type == VFL_TYPE_RADIO)
1784 1424 cap->device_caps = V4L2_CAP_RADIO;
1785 if (dev->vbi_dev) 1425 else
1786 cap->capabilities |= V4L2_CAP_VBI_CAPTURE; 1426 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
1787 1427
1788 if (dev->audio_mode.has_audio) 1428 if (dev->audio_mode.has_audio)
1789 cap->capabilities |= V4L2_CAP_AUDIO; 1429 cap->device_caps |= V4L2_CAP_AUDIO;
1790 1430
1791 if (dev->tuner_type != TUNER_ABSENT) 1431 if (dev->tuner_type != TUNER_ABSENT)
1792 cap->capabilities |= V4L2_CAP_TUNER; 1432 cap->device_caps |= V4L2_CAP_TUNER;
1793 1433
1434 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1435 V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1436 if (dev->vbi_dev)
1437 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1438 if (dev->radio_dev)
1439 cap->capabilities |= V4L2_CAP_RADIO;
1794 return 0; 1440 return 0;
1795} 1441}
1796 1442
@@ -1845,46 +1491,6 @@ static int vidioc_enum_framesizes(struct file *file, void *priv,
1845 return 0; 1491 return 0;
1846} 1492}
1847 1493
1848/* Sliced VBI ioctls */
1849static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
1850 struct v4l2_format *f)
1851{
1852 struct em28xx_fh *fh = priv;
1853 struct em28xx *dev = fh->dev;
1854 int rc;
1855
1856 rc = check_dev(dev);
1857 if (rc < 0)
1858 return rc;
1859
1860 f->fmt.sliced.service_set = 0;
1861 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1862
1863 if (f->fmt.sliced.service_set == 0)
1864 rc = -EINVAL;
1865
1866 return rc;
1867}
1868
1869static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
1870 struct v4l2_format *f)
1871{
1872 struct em28xx_fh *fh = priv;
1873 struct em28xx *dev = fh->dev;
1874 int rc;
1875
1876 rc = check_dev(dev);
1877 if (rc < 0)
1878 return rc;
1879
1880 v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
1881
1882 if (f->fmt.sliced.service_set == 0)
1883 return -EINVAL;
1884
1885 return 0;
1886}
1887
1888/* RAW VBI ioctls */ 1494/* RAW VBI ioctls */
1889 1495
1890static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, 1496static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
@@ -1900,6 +1506,7 @@ static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1900 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1506 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1901 format->fmt.vbi.count[0] = dev->vbi_height; 1507 format->fmt.vbi.count[0] = dev->vbi_height;
1902 format->fmt.vbi.count[1] = dev->vbi_height; 1508 format->fmt.vbi.count[1] = dev->vbi_height;
1509 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1903 1510
1904 /* Varies by video standard (NTSC, PAL, etc.) */ 1511 /* Varies by video standard (NTSC, PAL, etc.) */
1905 if (dev->norm & V4L2_STD_525_60) { 1512 if (dev->norm & V4L2_STD_525_60) {
@@ -1928,6 +1535,7 @@ static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1928 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; 1535 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1929 format->fmt.vbi.count[0] = dev->vbi_height; 1536 format->fmt.vbi.count[0] = dev->vbi_height;
1930 format->fmt.vbi.count[1] = dev->vbi_height; 1537 format->fmt.vbi.count[1] = dev->vbi_height;
1538 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1931 1539
1932 /* Varies by video standard (NTSC, PAL, etc.) */ 1540 /* Varies by video standard (NTSC, PAL, etc.) */
1933 if (dev->norm & V4L2_STD_525_60) { 1541 if (dev->norm & V4L2_STD_525_60) {
@@ -1943,100 +1551,10 @@ static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1943 return 0; 1551 return 0;
1944} 1552}
1945 1553
1946static int vidioc_reqbufs(struct file *file, void *priv,
1947 struct v4l2_requestbuffers *rb)
1948{
1949 struct em28xx_fh *fh = priv;
1950 struct em28xx *dev = fh->dev;
1951 int rc;
1952
1953 rc = check_dev(dev);
1954 if (rc < 0)
1955 return rc;
1956
1957 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1958 return videobuf_reqbufs(&fh->vb_vidq, rb);
1959 else
1960 return videobuf_reqbufs(&fh->vb_vbiq, rb);
1961}
1962
1963static int vidioc_querybuf(struct file *file, void *priv,
1964 struct v4l2_buffer *b)
1965{
1966 struct em28xx_fh *fh = priv;
1967 struct em28xx *dev = fh->dev;
1968 int rc;
1969
1970 rc = check_dev(dev);
1971 if (rc < 0)
1972 return rc;
1973
1974 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1975 return videobuf_querybuf(&fh->vb_vidq, b);
1976 else {
1977 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1978 the videobuf framework, but we probably shouldn't be
1979 returning a buffer larger than that which was asked for.
1980 At a minimum, it causes a crash in zvbi since it does
1981 a memcpy based on the source buffer length */
1982 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1983 b->length = dev->vbi_width * dev->vbi_height * 2;
1984
1985 return result;
1986 }
1987}
1988
1989static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1990{
1991 struct em28xx_fh *fh = priv;
1992 struct em28xx *dev = fh->dev;
1993 int rc;
1994
1995 rc = check_dev(dev);
1996 if (rc < 0)
1997 return rc;
1998
1999 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2000 return videobuf_qbuf(&fh->vb_vidq, b);
2001 else
2002 return videobuf_qbuf(&fh->vb_vbiq, b);
2003}
2004
2005static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
2006{
2007 struct em28xx_fh *fh = priv;
2008 struct em28xx *dev = fh->dev;
2009 int rc;
2010
2011 rc = check_dev(dev);
2012 if (rc < 0)
2013 return rc;
2014
2015 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2016 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
2017 O_NONBLOCK);
2018 else
2019 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
2020 O_NONBLOCK);
2021}
2022
2023/* ----------------------------------------------------------- */ 1554/* ----------------------------------------------------------- */
2024/* RADIO ESPECIFIC IOCTLS */ 1555/* RADIO ESPECIFIC IOCTLS */
2025/* ----------------------------------------------------------- */ 1556/* ----------------------------------------------------------- */
2026 1557
2027static int radio_querycap(struct file *file, void *priv,
2028 struct v4l2_capability *cap)
2029{
2030 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2031
2032 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
2033 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
2034 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
2035
2036 cap->capabilities = V4L2_CAP_TUNER;
2037 return 0;
2038}
2039
2040static int radio_g_tuner(struct file *file, void *priv, 1558static int radio_g_tuner(struct file *file, void *priv,
2041 struct v4l2_tuner *t) 1559 struct v4l2_tuner *t)
2042{ 1560{
@@ -2053,26 +1571,6 @@ static int radio_g_tuner(struct file *file, void *priv,
2053 return 0; 1571 return 0;
2054} 1572}
2055 1573
2056static int radio_enum_input(struct file *file, void *priv,
2057 struct v4l2_input *i)
2058{
2059 if (i->index != 0)
2060 return -EINVAL;
2061 strcpy(i->name, "Radio");
2062 i->type = V4L2_INPUT_TYPE_TUNER;
2063
2064 return 0;
2065}
2066
2067static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2068{
2069 if (unlikely(a->index))
2070 return -EINVAL;
2071
2072 strcpy(a->name, "Radio");
2073 return 0;
2074}
2075
2076static int radio_s_tuner(struct file *file, void *priv, 1574static int radio_s_tuner(struct file *file, void *priv,
2077 struct v4l2_tuner *t) 1575 struct v4l2_tuner *t)
2078{ 1576{
@@ -2086,48 +1584,16 @@ static int radio_s_tuner(struct file *file, void *priv,
2086 return 0; 1584 return 0;
2087} 1585}
2088 1586
2089static int radio_s_audio(struct file *file, void *fh,
2090 const struct v4l2_audio *a)
2091{
2092 return 0;
2093}
2094
2095static int radio_s_input(struct file *file, void *fh, unsigned int i)
2096{
2097 return 0;
2098}
2099
2100static int radio_queryctrl(struct file *file, void *priv,
2101 struct v4l2_queryctrl *qc)
2102{
2103 int i;
2104
2105 if (qc->id < V4L2_CID_BASE ||
2106 qc->id >= V4L2_CID_LASTP1)
2107 return -EINVAL;
2108
2109 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2110 if (qc->id && qc->id == ac97_qctrl[i].id) {
2111 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
2112 return 0;
2113 }
2114 }
2115
2116 return -EINVAL;
2117}
2118
2119/* 1587/*
2120 * em28xx_v4l2_open() 1588 * em28xx_v4l2_open()
2121 * inits the device and starts isoc transfer 1589 * inits the device and starts isoc transfer
2122 */ 1590 */
2123static int em28xx_v4l2_open(struct file *filp) 1591static int em28xx_v4l2_open(struct file *filp)
2124{ 1592{
2125 int errCode = 0, radio = 0;
2126 struct video_device *vdev = video_devdata(filp); 1593 struct video_device *vdev = video_devdata(filp);
2127 struct em28xx *dev = video_drvdata(filp); 1594 struct em28xx *dev = video_drvdata(filp);
2128 enum v4l2_buf_type fh_type = 0; 1595 enum v4l2_buf_type fh_type = 0;
2129 struct em28xx_fh *fh; 1596 struct em28xx_fh *fh;
2130 enum v4l2_field field;
2131 1597
2132 switch (vdev->vfl_type) { 1598 switch (vdev->vfl_type) {
2133 case VFL_TYPE_GRABBER: 1599 case VFL_TYPE_GRABBER:
@@ -2136,9 +1602,6 @@ static int em28xx_v4l2_open(struct file *filp)
2136 case VFL_TYPE_VBI: 1602 case VFL_TYPE_VBI:
2137 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; 1603 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
2138 break; 1604 break;
2139 case VFL_TYPE_RADIO:
2140 radio = 1;
2141 break;
2142 } 1605 }
2143 1606
2144 em28xx_videodbg("open dev=%s type=%s users=%d\n", 1607 em28xx_videodbg("open dev=%s type=%s users=%d\n",
@@ -2154,14 +1617,13 @@ static int em28xx_v4l2_open(struct file *filp)
2154 mutex_unlock(&dev->lock); 1617 mutex_unlock(&dev->lock);
2155 return -ENOMEM; 1618 return -ENOMEM;
2156 } 1619 }
1620 v4l2_fh_init(&fh->fh, vdev);
2157 fh->dev = dev; 1621 fh->dev = dev;
2158 fh->radio = radio;
2159 fh->type = fh_type; 1622 fh->type = fh_type;
2160 filp->private_data = fh; 1623 filp->private_data = fh;
2161 1624
2162 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { 1625 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
2163 em28xx_set_mode(dev, EM28XX_ANALOG_MODE); 1626 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
2164 em28xx_set_alternate(dev);
2165 em28xx_resolution_set(dev); 1627 em28xx_resolution_set(dev);
2166 1628
2167 /* Needed, since GPIO might have disabled power of 1629 /* Needed, since GPIO might have disabled power of
@@ -2170,31 +1632,18 @@ static int em28xx_v4l2_open(struct file *filp)
2170 em28xx_wake_i2c(dev); 1632 em28xx_wake_i2c(dev);
2171 1633
2172 } 1634 }
2173 if (fh->radio) { 1635
1636 if (vdev->vfl_type == VFL_TYPE_RADIO) {
2174 em28xx_videodbg("video_open: setting radio device\n"); 1637 em28xx_videodbg("video_open: setting radio device\n");
2175 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio); 1638 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
2176 } 1639 }
2177 1640
2178 dev->users++; 1641 dev->users++;
2179 1642
2180 if (dev->progressive)
2181 field = V4L2_FIELD_NONE;
2182 else
2183 field = V4L2_FIELD_INTERLACED;
2184
2185 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2186 NULL, &dev->slock,
2187 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2188 sizeof(struct em28xx_buffer), fh, &dev->lock);
2189
2190 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2191 NULL, &dev->slock,
2192 V4L2_BUF_TYPE_VBI_CAPTURE,
2193 V4L2_FIELD_SEQ_TB,
2194 sizeof(struct em28xx_buffer), fh, &dev->lock);
2195 mutex_unlock(&dev->lock); 1643 mutex_unlock(&dev->lock);
1644 v4l2_fh_add(&fh->fh);
2196 1645
2197 return errCode; 1646 return 0;
2198} 1647}
2199 1648
2200/* 1649/*
@@ -2248,25 +1697,16 @@ static int em28xx_v4l2_close(struct file *filp)
2248 em28xx_videodbg("users=%d\n", dev->users); 1697 em28xx_videodbg("users=%d\n", dev->users);
2249 1698
2250 mutex_lock(&dev->lock); 1699 mutex_lock(&dev->lock);
2251 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) { 1700 vb2_fop_release(filp);
2252 videobuf_stop(&fh->vb_vidq);
2253 res_free(fh, EM28XX_RESOURCE_VIDEO);
2254 }
2255
2256 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2257 videobuf_stop(&fh->vb_vbiq);
2258 res_free(fh, EM28XX_RESOURCE_VBI);
2259 }
2260 1701
2261 if (dev->users == 1) { 1702 if (dev->users == 1) {
2262 /* the device is already disconnect, 1703 /* the device is already disconnect,
2263 free the remaining resources */ 1704 free the remaining resources */
2264 if (dev->state & DEV_DISCONNECTED) { 1705 if (dev->disconnected) {
2265 em28xx_release_resources(dev); 1706 em28xx_release_resources(dev);
2266 kfree(dev->alt_max_pkt_size); 1707 kfree(dev->alt_max_pkt_size_isoc);
2267 mutex_unlock(&dev->lock); 1708 mutex_unlock(&dev->lock);
2268 kfree(dev); 1709 kfree(dev);
2269 kfree(fh);
2270 return 0; 1710 return 0;
2271 } 1711 }
2272 1712
@@ -2274,7 +1714,6 @@ static int em28xx_v4l2_close(struct file *filp)
2274 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0); 1714 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
2275 1715
2276 /* do this before setting alternate! */ 1716 /* do this before setting alternate! */
2277 em28xx_uninit_isoc(dev, EM28XX_ANALOG_MODE);
2278 em28xx_set_mode(dev, EM28XX_SUSPEND); 1717 em28xx_set_mode(dev, EM28XX_SUSPEND);
2279 1718
2280 /* set alternate 0 */ 1719 /* set alternate 0 */
@@ -2287,129 +1726,18 @@ static int em28xx_v4l2_close(struct file *filp)
2287 } 1726 }
2288 } 1727 }
2289 1728
2290 videobuf_mmap_free(&fh->vb_vidq);
2291 videobuf_mmap_free(&fh->vb_vbiq);
2292 kfree(fh);
2293 dev->users--; 1729 dev->users--;
2294 mutex_unlock(&dev->lock); 1730 mutex_unlock(&dev->lock);
2295 return 0; 1731 return 0;
2296} 1732}
2297 1733
2298/*
2299 * em28xx_v4l2_read()
2300 * will allocate buffers when called for the first time
2301 */
2302static ssize_t
2303em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
2304 loff_t *pos)
2305{
2306 struct em28xx_fh *fh = filp->private_data;
2307 struct em28xx *dev = fh->dev;
2308 int rc;
2309
2310 rc = check_dev(dev);
2311 if (rc < 0)
2312 return rc;
2313
2314 if (mutex_lock_interruptible(&dev->lock))
2315 return -ERESTARTSYS;
2316 /* FIXME: read() is not prepared to allow changing the video
2317 resolution while streaming. Seems a bug at em28xx_set_fmt
2318 */
2319
2320 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2321 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2322 rc = -EBUSY;
2323 else
2324 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2325 filp->f_flags & O_NONBLOCK);
2326 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2327 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2328 rc = -EBUSY;
2329 else
2330 rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2331 filp->f_flags & O_NONBLOCK);
2332 }
2333 mutex_unlock(&dev->lock);
2334
2335 return rc;
2336}
2337
2338/*
2339 * em28xx_poll()
2340 * will allocate buffers when called for the first time
2341 */
2342static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
2343{
2344 struct em28xx_fh *fh = filp->private_data;
2345 struct em28xx *dev = fh->dev;
2346 int rc;
2347
2348 rc = check_dev(dev);
2349 if (rc < 0)
2350 return rc;
2351
2352 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2353 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2354 return POLLERR;
2355 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
2356 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2357 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2358 return POLLERR;
2359 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
2360 } else {
2361 return POLLERR;
2362 }
2363}
2364
2365static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
2366{
2367 struct em28xx_fh *fh = filp->private_data;
2368 struct em28xx *dev = fh->dev;
2369 unsigned int res;
2370
2371 mutex_lock(&dev->lock);
2372 res = em28xx_poll(filp, wait);
2373 mutex_unlock(&dev->lock);
2374 return res;
2375}
2376
2377/*
2378 * em28xx_v4l2_mmap()
2379 */
2380static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2381{
2382 struct em28xx_fh *fh = filp->private_data;
2383 struct em28xx *dev = fh->dev;
2384 int rc;
2385
2386 rc = check_dev(dev);
2387 if (rc < 0)
2388 return rc;
2389
2390 if (mutex_lock_interruptible(&dev->lock))
2391 return -ERESTARTSYS;
2392 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2393 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2394 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2395 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
2396 mutex_unlock(&dev->lock);
2397
2398 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2399 (unsigned long)vma->vm_start,
2400 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2401 rc);
2402
2403 return rc;
2404}
2405
2406static const struct v4l2_file_operations em28xx_v4l_fops = { 1734static const struct v4l2_file_operations em28xx_v4l_fops = {
2407 .owner = THIS_MODULE, 1735 .owner = THIS_MODULE,
2408 .open = em28xx_v4l2_open, 1736 .open = em28xx_v4l2_open,
2409 .release = em28xx_v4l2_close, 1737 .release = em28xx_v4l2_close,
2410 .read = em28xx_v4l2_read, 1738 .read = vb2_fop_read,
2411 .poll = em28xx_v4l2_poll, 1739 .poll = vb2_fop_poll,
2412 .mmap = em28xx_v4l2_mmap, 1740 .mmap = vb2_fop_mmap,
2413 .unlocked_ioctl = video_ioctl2, 1741 .unlocked_ioctl = video_ioctl2,
2414}; 1742};
2415 1743
@@ -2420,19 +1748,20 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2420 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1748 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2421 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1749 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2422 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 1750 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1751 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2423 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap, 1752 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
2424 .vidioc_enum_framesizes = vidioc_enum_framesizes, 1753 .vidioc_enum_framesizes = vidioc_enum_framesizes,
2425 .vidioc_g_audio = vidioc_g_audio, 1754 .vidioc_g_audio = vidioc_g_audio,
2426 .vidioc_s_audio = vidioc_s_audio, 1755 .vidioc_s_audio = vidioc_s_audio,
2427 .vidioc_cropcap = vidioc_cropcap, 1756 .vidioc_cropcap = vidioc_cropcap,
2428 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap, 1757
2429 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 1758 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2430 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, 1759 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2431 1760 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2432 .vidioc_reqbufs = vidioc_reqbufs, 1761 .vidioc_querybuf = vb2_ioctl_querybuf,
2433 .vidioc_querybuf = vidioc_querybuf, 1762 .vidioc_qbuf = vb2_ioctl_qbuf,
2434 .vidioc_qbuf = vidioc_qbuf, 1763 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2435 .vidioc_dqbuf = vidioc_dqbuf, 1764
2436 .vidioc_g_std = vidioc_g_std, 1765 .vidioc_g_std = vidioc_g_std,
2437 .vidioc_querystd = vidioc_querystd, 1766 .vidioc_querystd = vidioc_querystd,
2438 .vidioc_s_std = vidioc_s_std, 1767 .vidioc_s_std = vidioc_s_std,
@@ -2441,15 +1770,14 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2441 .vidioc_enum_input = vidioc_enum_input, 1770 .vidioc_enum_input = vidioc_enum_input,
2442 .vidioc_g_input = vidioc_g_input, 1771 .vidioc_g_input = vidioc_g_input,
2443 .vidioc_s_input = vidioc_s_input, 1772 .vidioc_s_input = vidioc_s_input,
2444 .vidioc_queryctrl = vidioc_queryctrl, 1773 .vidioc_streamon = vb2_ioctl_streamon,
2445 .vidioc_g_ctrl = vidioc_g_ctrl, 1774 .vidioc_streamoff = vb2_ioctl_streamoff,
2446 .vidioc_s_ctrl = vidioc_s_ctrl,
2447 .vidioc_streamon = vidioc_streamon,
2448 .vidioc_streamoff = vidioc_streamoff,
2449 .vidioc_g_tuner = vidioc_g_tuner, 1775 .vidioc_g_tuner = vidioc_g_tuner,
2450 .vidioc_s_tuner = vidioc_s_tuner, 1776 .vidioc_s_tuner = vidioc_s_tuner,
2451 .vidioc_g_frequency = vidioc_g_frequency, 1777 .vidioc_g_frequency = vidioc_g_frequency,
2452 .vidioc_s_frequency = vidioc_s_frequency, 1778 .vidioc_s_frequency = vidioc_s_frequency,
1779 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1780 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2453#ifdef CONFIG_VIDEO_ADV_DEBUG 1781#ifdef CONFIG_VIDEO_ADV_DEBUG
2454 .vidioc_g_register = vidioc_g_register, 1782 .vidioc_g_register = vidioc_g_register,
2455 .vidioc_s_register = vidioc_s_register, 1783 .vidioc_s_register = vidioc_s_register,
@@ -2459,11 +1787,10 @@ static const struct v4l2_ioctl_ops video_ioctl_ops = {
2459 1787
2460static const struct video_device em28xx_video_template = { 1788static const struct video_device em28xx_video_template = {
2461 .fops = &em28xx_v4l_fops, 1789 .fops = &em28xx_v4l_fops,
2462 .release = video_device_release, 1790 .release = video_device_release_empty,
2463 .ioctl_ops = &video_ioctl_ops, 1791 .ioctl_ops = &video_ioctl_ops,
2464 1792
2465 .tvnorms = V4L2_STD_ALL, 1793 .tvnorms = V4L2_STD_ALL,
2466 .current_norm = V4L2_STD_PAL,
2467}; 1794};
2468 1795
2469static const struct v4l2_file_operations radio_fops = { 1796static const struct v4l2_file_operations radio_fops = {
@@ -2474,18 +1801,13 @@ static const struct v4l2_file_operations radio_fops = {
2474}; 1801};
2475 1802
2476static const struct v4l2_ioctl_ops radio_ioctl_ops = { 1803static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2477 .vidioc_querycap = radio_querycap, 1804 .vidioc_querycap = vidioc_querycap,
2478 .vidioc_g_tuner = radio_g_tuner, 1805 .vidioc_g_tuner = radio_g_tuner,
2479 .vidioc_enum_input = radio_enum_input,
2480 .vidioc_g_audio = radio_g_audio,
2481 .vidioc_s_tuner = radio_s_tuner, 1806 .vidioc_s_tuner = radio_s_tuner,
2482 .vidioc_s_audio = radio_s_audio,
2483 .vidioc_s_input = radio_s_input,
2484 .vidioc_queryctrl = radio_queryctrl,
2485 .vidioc_g_ctrl = vidioc_g_ctrl,
2486 .vidioc_s_ctrl = vidioc_s_ctrl,
2487 .vidioc_g_frequency = vidioc_g_frequency, 1807 .vidioc_g_frequency = vidioc_g_frequency,
2488 .vidioc_s_frequency = vidioc_s_frequency, 1808 .vidioc_s_frequency = vidioc_s_frequency,
1809 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1810 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2489#ifdef CONFIG_VIDEO_ADV_DEBUG 1811#ifdef CONFIG_VIDEO_ADV_DEBUG
2490 .vidioc_g_register = vidioc_g_register, 1812 .vidioc_g_register = vidioc_g_register,
2491 .vidioc_s_register = vidioc_s_register, 1813 .vidioc_s_register = vidioc_s_register,
@@ -2514,9 +1836,11 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2514 1836
2515 *vfd = *template; 1837 *vfd = *template;
2516 vfd->v4l2_dev = &dev->v4l2_dev; 1838 vfd->v4l2_dev = &dev->v4l2_dev;
2517 vfd->release = video_device_release;
2518 vfd->debug = video_debug; 1839 vfd->debug = video_debug;
2519 vfd->lock = &dev->lock; 1840 vfd->lock = &dev->lock;
1841 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1842 if (dev->board.is_webcam)
1843 vfd->tvnorms = 0;
2520 1844
2521 snprintf(vfd->name, sizeof(vfd->name), "%s %s", 1845 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2522 dev->name, type_name); 1846 dev->name, type_name);
@@ -2527,7 +1851,7 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2527 1851
2528int em28xx_register_analog_devices(struct em28xx *dev) 1852int em28xx_register_analog_devices(struct em28xx *dev)
2529{ 1853{
2530 u8 val; 1854 u8 val;
2531 int ret; 1855 int ret;
2532 unsigned int maxw; 1856 unsigned int maxw;
2533 1857
@@ -2535,7 +1859,7 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2535 dev->name, EM28XX_VERSION); 1859 dev->name, EM28XX_VERSION);
2536 1860
2537 /* set default norm */ 1861 /* set default norm */
2538 dev->norm = em28xx_video_template.current_norm; 1862 dev->norm = V4L2_STD_PAL;
2539 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm); 1863 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
2540 dev->interlaced = EM28XX_INTERLACED_DEFAULT; 1864 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
2541 1865
@@ -2543,10 +1867,10 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2543 dev->format = &format[0]; 1867 dev->format = &format[0];
2544 1868
2545 maxw = norm_maxw(dev); 1869 maxw = norm_maxw(dev);
2546 /* MaxPacketSize for em2800 is too small to capture at full resolution 1870 /* MaxPacketSize for em2800 is too small to capture at full resolution
2547 * use half of maxw as the scaler can only scale to 50% */ 1871 * use half of maxw as the scaler can only scale to 50% */
2548 if (dev->board.is_em2800) 1872 if (dev->board.is_em2800)
2549 maxw /= 2; 1873 maxw /= 2;
2550 1874
2551 em28xx_set_video_format(dev, format[0].fourcc, 1875 em28xx_set_video_format(dev, format[0].fourcc,
2552 maxw, norm_maxh(dev)); 1876 maxw, norm_maxh(dev));
@@ -2572,6 +1896,8 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2572 em28xx_errdev("cannot allocate video_device.\n"); 1896 em28xx_errdev("cannot allocate video_device.\n");
2573 return -ENODEV; 1897 return -ENODEV;
2574 } 1898 }
1899 dev->vdev->queue = &dev->vb_vidq;
1900 dev->vdev->queue->lock = &dev->vb_queue_lock;
2575 1901
2576 /* register v4l2 video video_device */ 1902 /* register v4l2 video video_device */
2577 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER, 1903 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
@@ -2587,6 +1913,9 @@ int em28xx_register_analog_devices(struct em28xx *dev)
2587 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, 1913 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2588 "vbi"); 1914 "vbi");
2589 1915
1916 dev->vbi_dev->queue = &dev->vb_vbiq;
1917 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1918
2590 /* register v4l2 vbi video_device */ 1919 /* register v4l2 vbi video_device */
2591 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, 1920 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2592 vbi_nr[dev->devno]); 1921 vbi_nr[dev->devno]);
diff --git a/drivers/media/usb/em28xx/em28xx.h b/drivers/media/usb/em28xx/em28xx.h
index 86e90d86da6d..5f0b2c59e846 100644
--- a/drivers/media/usb/em28xx/em28xx.h
+++ b/drivers/media/usb/em28xx/em28xx.h
@@ -4,6 +4,7 @@
4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com> 4 Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
5 Ludovico Cavedon <cavedon@sssup.it> 5 Ludovico Cavedon <cavedon@sssup.it>
6 Mauro Carvalho Chehab <mchehab@infradead.org> 6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
7 8
8 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de> 9 Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
9 10
@@ -30,13 +31,12 @@
30#include <linux/mutex.h> 31#include <linux/mutex.h>
31#include <linux/videodev2.h> 32#include <linux/videodev2.h>
32 33
33#include <media/videobuf-vmalloc.h> 34#include <media/videobuf2-vmalloc.h>
34#include <media/v4l2-device.h> 35#include <media/v4l2-device.h>
36#include <media/v4l2-ctrls.h>
37#include <media/v4l2-fh.h>
35#include <media/ir-kbd-i2c.h> 38#include <media/ir-kbd-i2c.h>
36#include <media/rc-core.h> 39#include <media/rc-core.h>
37#if defined(CONFIG_VIDEO_EM28XX_DVB) || defined(CONFIG_VIDEO_EM28XX_DVB_MODULE)
38#include <media/videobuf-dvb.h>
39#endif
40#include "tuner-xc2028.h" 40#include "tuner-xc2028.h"
41#include "xc5000.h" 41#include "xc5000.h"
42#include "em28xx-reg.h" 42#include "em28xx-reg.h"
@@ -157,12 +157,18 @@
157#define EM28XX_NUM_BUFS 5 157#define EM28XX_NUM_BUFS 5
158#define EM28XX_DVB_NUM_BUFS 5 158#define EM28XX_DVB_NUM_BUFS 5
159 159
160/* number of packets for each buffer 160/* isoc transfers: number of packets for each buffer
161 windows requests only 64 packets .. so we better do the same 161 windows requests only 64 packets .. so we better do the same
162 this is what I found out for all alternate numbers there! 162 this is what I found out for all alternate numbers there!
163 */ 163 */
164#define EM28XX_NUM_PACKETS 64 164#define EM28XX_NUM_ISOC_PACKETS 64
165#define EM28XX_DVB_MAX_PACKETS 64 165#define EM28XX_DVB_NUM_ISOC_PACKETS 64
166
167/* bulk transfers: transfer buffer size = packet size * packet multiplier
168 USB 2.0 spec says bulk packet size is always 512 bytes
169 */
170#define EM28XX_BULK_PACKET_MULTIPLIER 384
171#define EM28XX_DVB_BULK_PACKET_MULTIPLIER 384
166 172
167#define EM28XX_INTERLACED_DEFAULT 1 173#define EM28XX_INTERLACED_DEFAULT 1
168 174
@@ -187,12 +193,8 @@
187 Interval: 125us 193 Interval: 125us
188*/ 194*/
189 195
190/* time to wait when stopping the isoc transfer */
191#define EM28XX_URB_TIMEOUT \
192 msecs_to_jiffies(EM28XX_NUM_BUFS * EM28XX_NUM_PACKETS)
193
194/* time in msecs to wait for i2c writes to finish */ 196/* time in msecs to wait for i2c writes to finish */
195#define EM2800_I2C_WRITE_TIMEOUT 20 197#define EM2800_I2C_XFER_TIMEOUT 20
196 198
197enum em28xx_mode { 199enum em28xx_mode {
198 EM28XX_SUSPEND, 200 EM28XX_SUSPEND,
@@ -203,7 +205,7 @@ enum em28xx_mode {
203 205
204struct em28xx; 206struct em28xx;
205 207
206struct em28xx_usb_isoc_bufs { 208struct em28xx_usb_bufs {
207 /* max packet size of isoc transaction */ 209 /* max packet size of isoc transaction */
208 int max_pkt_size; 210 int max_pkt_size;
209 211
@@ -213,26 +215,26 @@ struct em28xx_usb_isoc_bufs {
213 /* number of allocated urbs */ 215 /* number of allocated urbs */
214 int num_bufs; 216 int num_bufs;
215 217
216 /* urb for isoc transfers */ 218 /* urb for isoc/bulk transfers */
217 struct urb **urb; 219 struct urb **urb;
218 220
219 /* transfer buffers for isoc transfer */ 221 /* transfer buffers for isoc/bulk transfer */
220 char **transfer_buffer; 222 char **transfer_buffer;
221}; 223};
222 224
223struct em28xx_usb_isoc_ctl { 225struct em28xx_usb_ctl {
224 /* isoc transfer buffers for analog mode */ 226 /* isoc/bulk transfer buffers for analog mode */
225 struct em28xx_usb_isoc_bufs analog_bufs; 227 struct em28xx_usb_bufs analog_bufs;
226 228
227 /* isoc transfer buffers for digital mode */ 229 /* isoc/bulk transfer buffers for digital mode */
228 struct em28xx_usb_isoc_bufs digital_bufs; 230 struct em28xx_usb_bufs digital_bufs;
229 231
230 /* Stores already requested buffers */ 232 /* Stores already requested buffers */
231 struct em28xx_buffer *vid_buf; 233 struct em28xx_buffer *vid_buf;
232 struct em28xx_buffer *vbi_buf; 234 struct em28xx_buffer *vbi_buf;
233 235
234 /* isoc urb callback */ 236 /* copy data from URB */
235 int (*isoc_copy) (struct em28xx *dev, struct urb *urb); 237 int (*urb_data_copy) (struct em28xx *dev, struct urb *urb);
236 238
237}; 239};
238 240
@@ -247,19 +249,26 @@ struct em28xx_fmt {
247/* buffer for one video frame */ 249/* buffer for one video frame */
248struct em28xx_buffer { 250struct em28xx_buffer {
249 /* common v4l buffer stuff -- must be first */ 251 /* common v4l buffer stuff -- must be first */
250 struct videobuf_buffer vb; 252 struct vb2_buffer vb;
253 struct list_head list;
251 254
252 struct list_head frame; 255 void *mem;
256 unsigned int length;
253 int top_field; 257 int top_field;
258
259 /* counter to control buffer fill */
260 unsigned int pos;
261 /* NOTE; in interlaced mode, this value is reset to zero at
262 * the start of each new field (not frame !) */
263
264 /* pointer to vmalloc memory address in vb */
265 char *vb_buf;
254}; 266};
255 267
256struct em28xx_dmaqueue { 268struct em28xx_dmaqueue {
257 struct list_head active; 269 struct list_head active;
258 270
259 wait_queue_head_t wq; 271 wait_queue_head_t wq;
260
261 /* Counters to control buffer fill */
262 int pos;
263}; 272};
264 273
265/* inputs */ 274/* inputs */
@@ -430,13 +439,6 @@ struct em28xx_eeprom {
430 u8 string_idx_table; 439 u8 string_idx_table;
431}; 440};
432 441
433/* device states */
434enum em28xx_dev_state {
435 DEV_INITIALIZED = 0x01,
436 DEV_DISCONNECTED = 0x02,
437 DEV_MISCONFIGURED = 0x04,
438};
439
440#define EM28XX_AUDIO_BUFS 5 442#define EM28XX_AUDIO_BUFS 5
441#define EM28XX_NUM_AUDIO_PACKETS 64 443#define EM28XX_NUM_AUDIO_PACKETS 64
442#define EM28XX_AUDIO_MAX_PACKET_SIZE 196 /* static value */ 444#define EM28XX_AUDIO_MAX_PACKET_SIZE 196 /* static value */
@@ -469,12 +471,8 @@ struct em28xx_audio {
469struct em28xx; 471struct em28xx;
470 472
471struct em28xx_fh { 473struct em28xx_fh {
474 struct v4l2_fh fh;
472 struct em28xx *dev; 475 struct em28xx *dev;
473 int radio;
474 unsigned int resources;
475
476 struct videobuf_queue vb_vidq;
477 struct videobuf_queue vb_vbiq;
478 476
479 enum v4l2_buf_type type; 477 enum v4l2_buf_type type;
480}; 478};
@@ -487,9 +485,14 @@ struct em28xx {
487 int devno; /* marks the number of this device */ 485 int devno; /* marks the number of this device */
488 enum em28xx_chip_id chip_id; 486 enum em28xx_chip_id chip_id;
489 487
488 unsigned char disconnected:1; /* device has been diconnected */
489
490 int audio_ifnum; 490 int audio_ifnum;
491 491
492 struct v4l2_device v4l2_dev; 492 struct v4l2_device v4l2_dev;
493 struct v4l2_ctrl_handler ctrl_handler;
494 /* provides ac97 mute and volume overrides */
495 struct v4l2_ctrl_handler ac97_ctrl_handler;
493 struct em28xx_board board; 496 struct em28xx_board board;
494 497
495 /* Webcam specific fields */ 498 /* Webcam specific fields */
@@ -497,7 +500,7 @@ struct em28xx {
497 int sensor_xres, sensor_yres; 500 int sensor_xres, sensor_yres;
498 int sensor_xtal; 501 int sensor_xtal;
499 502
500 /* Allows progressive (e. g. non-interlaced) mode */ 503 /* Progressive (non-interlaced) mode */
501 int progressive; 504 int progressive;
502 505
503 /* Vinmode/Vinctl used at the driver */ 506 /* Vinmode/Vinctl used at the driver */
@@ -532,6 +535,7 @@ struct em28xx {
532 struct i2c_client i2c_client; 535 struct i2c_client i2c_client;
533 /* video for linux */ 536 /* video for linux */
534 int users; /* user count for exclusive use */ 537 int users; /* user count for exclusive use */
538 int streaming_users; /* Number of actively streaming users */
535 struct video_device *vdev; /* video for linux device struct */ 539 struct video_device *vdev; /* video for linux device struct */
536 v4l2_std_id norm; /* selected tv norm */ 540 v4l2_std_id norm; /* selected tv norm */
537 int ctl_freq; /* selected frequency */ 541 int ctl_freq; /* selected frequency */
@@ -554,13 +558,10 @@ struct em28xx {
554 558
555 struct em28xx_audio adev; 559 struct em28xx_audio adev;
556 560
557 /* states */ 561 /* capture state tracking */
558 enum em28xx_dev_state state;
559
560 /* vbi related state tracking */
561 int capture_type; 562 int capture_type;
563 unsigned char top_field:1;
562 int vbi_read; 564 int vbi_read;
563 unsigned char cur_field;
564 unsigned int vbi_width; 565 unsigned int vbi_width;
565 unsigned int vbi_height; /* lines per field */ 566 unsigned int vbi_height; /* lines per field */
566 567
@@ -574,6 +575,12 @@ struct em28xx {
574 struct video_device *vbi_dev; 575 struct video_device *vbi_dev;
575 struct video_device *radio_dev; 576 struct video_device *radio_dev;
576 577
578 /* Videobuf2 */
579 struct vb2_queue vb_vidq;
580 struct vb2_queue vb_vbiq;
581 struct mutex vb_queue_lock;
582 struct mutex vb_vbi_queue_lock;
583
577 /* resources in use */ 584 /* resources in use */
578 unsigned int resources; 585 unsigned int resources;
579 586
@@ -582,17 +589,31 @@ struct em28xx {
582 /* Isoc control struct */ 589 /* Isoc control struct */
583 struct em28xx_dmaqueue vidq; 590 struct em28xx_dmaqueue vidq;
584 struct em28xx_dmaqueue vbiq; 591 struct em28xx_dmaqueue vbiq;
585 struct em28xx_usb_isoc_ctl isoc_ctl; 592 struct em28xx_usb_ctl usb_ctl;
586 spinlock_t slock; 593 spinlock_t slock;
587 594
595 unsigned int field_count;
596 unsigned int vbi_field_count;
597
588 /* usb transfer */ 598 /* usb transfer */
589 struct usb_device *udev; /* the usb device */ 599 struct usb_device *udev; /* the usb device */
590 int alt; /* alternate */ 600 u8 analog_ep_isoc; /* address of isoc endpoint for analog */
591 int max_pkt_size; /* max packet size of isoc transaction */ 601 u8 analog_ep_bulk; /* address of bulk endpoint for analog */
592 int num_alt; /* Number of alternative settings */ 602 u8 dvb_ep_isoc; /* address of isoc endpoint for DVB */
593 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */ 603 u8 dvb_ep_bulk; /* address of bulk endpoint for DVC */
594 int dvb_alt; /* alternate for DVB */ 604 int alt; /* alternate setting */
595 unsigned int dvb_max_pkt_size; /* wMaxPacketSize for DVB */ 605 int max_pkt_size; /* max packet size of the selected ep at alt */
606 int packet_multiplier; /* multiplier for wMaxPacketSize, used for
607 URB buffer size definition */
608 int num_alt; /* number of alternative settings */
609 unsigned int *alt_max_pkt_size_isoc; /* array of isoc wMaxPacketSize */
610 unsigned int analog_xfer_bulk:1; /* use bulk instead of isoc
611 transfers for analog */
612 int dvb_alt_isoc; /* alternate setting for DVB isoc transfers */
613 unsigned int dvb_max_pkt_size_isoc; /* isoc max packet size of the
614 selected DVB ep at dvb_alt */
615 unsigned int dvb_xfer_bulk:1; /* use bulk instead of isoc
616 transfers for DVB */
596 char urb_buf[URB_MAX_CTRL_SIZE]; /* urb control msg buffer */ 617 char urb_buf[URB_MAX_CTRL_SIZE]; /* urb control msg buffer */
597 618
598 /* helper funcs that call usb_control_msg */ 619 /* helper funcs that call usb_control_msg */
@@ -619,9 +640,6 @@ struct em28xx {
619 struct delayed_work sbutton_query_work; 640 struct delayed_work sbutton_query_work;
620 641
621 struct em28xx_dvb *dvb; 642 struct em28xx_dvb *dvb;
622
623 /* I2C keyboard data */
624 struct IR_i2c_init_data init_data;
625}; 643};
626 644
627struct em28xx_ops { 645struct em28xx_ops {
@@ -666,12 +684,14 @@ int em28xx_vbi_supported(struct em28xx *dev);
666int em28xx_set_outfmt(struct em28xx *dev); 684int em28xx_set_outfmt(struct em28xx *dev);
667int em28xx_resolution_set(struct em28xx *dev); 685int em28xx_resolution_set(struct em28xx *dev);
668int em28xx_set_alternate(struct em28xx *dev); 686int em28xx_set_alternate(struct em28xx *dev);
669int em28xx_alloc_isoc(struct em28xx *dev, enum em28xx_mode mode, 687int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
670 int max_packets, int num_bufs, int max_pkt_size); 688 int num_bufs, int max_pkt_size, int packet_multiplier);
671int em28xx_init_isoc(struct em28xx *dev, enum em28xx_mode mode, 689int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
672 int max_packets, int num_bufs, int max_pkt_size, 690 int xfer_bulk,
673 int (*isoc_copy) (struct em28xx *dev, struct urb *urb)); 691 int num_bufs, int max_pkt_size, int packet_multiplier,
674void em28xx_uninit_isoc(struct em28xx *dev, enum em28xx_mode mode); 692 int (*urb_data_copy)
693 (struct em28xx *dev, struct urb *urb));
694void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode);
675void em28xx_stop_urbs(struct em28xx *dev); 695void em28xx_stop_urbs(struct em28xx *dev);
676int em28xx_isoc_dvb_max_packetsize(struct em28xx *dev); 696int em28xx_isoc_dvb_max_packetsize(struct em28xx *dev);
677int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode); 697int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode);
@@ -683,8 +703,13 @@ void em28xx_init_extension(struct em28xx *dev);
683void em28xx_close_extension(struct em28xx *dev); 703void em28xx_close_extension(struct em28xx *dev);
684 704
685/* Provided by em28xx-video.c */ 705/* Provided by em28xx-video.c */
706int em28xx_vb2_setup(struct em28xx *dev);
686int em28xx_register_analog_devices(struct em28xx *dev); 707int em28xx_register_analog_devices(struct em28xx *dev);
687void em28xx_release_analog_resources(struct em28xx *dev); 708void em28xx_release_analog_resources(struct em28xx *dev);
709void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv);
710int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count);
711int em28xx_stop_vbi_streaming(struct vb2_queue *vq);
712extern const struct v4l2_ctrl_ops em28xx_ctrl_ops;
688 713
689/* Provided by em28xx-cards.c */ 714/* Provided by em28xx-cards.c */
690extern int em2800_variant_detect(struct usb_device *udev, int model); 715extern int em2800_variant_detect(struct usb_device *udev, int model);
@@ -695,7 +720,7 @@ int em28xx_tuner_callback(void *ptr, int component, int command, int arg);
695void em28xx_release_resources(struct em28xx *dev); 720void em28xx_release_resources(struct em28xx *dev);
696 721
697/* Provided by em28xx-vbi.c */ 722/* Provided by em28xx-vbi.c */
698extern struct videobuf_queue_ops em28xx_vbi_qops; 723extern struct vb2_ops em28xx_vbi_qops;
699 724
700/* printk macros */ 725/* printk macros */
701 726