V4L/DVB (8818): gspca: Reinitialize the device on resume.
[safe/jmp/linux-2.6] / drivers / media / video / zoran_card.c
index 33b32f3..d842a7c 100644 (file)
 #include "zoran_device.h"
 #include "zoran_procfs.h"
 
-#define I2C_NAME(x) (x)->name
-
 extern const struct zoran_format zoran_formats[];
 
 static int card[BUZ_MAX] = { -1, -1, -1, -1 };
-module_param_array(card, int, NULL, 0);
+module_param_array(card, int, NULL, 0444);
 MODULE_PARM_DESC(card, "The type of card");
 
 static int encoder[BUZ_MAX] = { -1, -1, -1, -1 };
-module_param_array(encoder, int, NULL, 0);
+module_param_array(encoder, int, NULL, 0444);
 MODULE_PARM_DESC(encoder, "i2c TV encoder");
 
 static int decoder[BUZ_MAX] = { -1, -1, -1, -1 };
-module_param_array(decoder, int, NULL, 0);
+module_param_array(decoder, int, NULL, 0444);
 MODULE_PARM_DESC(decoder, "i2c TV decoder");
 
 /*
@@ -83,30 +81,32 @@ MODULE_PARM_DESC(decoder, "i2c TV decoder");
    or set in in a VIDIOCSFBUF ioctl
  */
 
-static unsigned long vidmem = 0;       /* Video memory base address */
-module_param(vidmem, ulong, 0);
+static unsigned long vidmem;   /* default = 0 - Video memory base address */
+module_param(vidmem, ulong, 0444);
+MODULE_PARM_DESC(vidmem, "Default video memory base address");
 
 /*
    Default input and video norm at startup of the driver.
 */
 
-static int default_input = 0;  /* 0=Composite, 1=S-Video */
-module_param(default_input, int, 0);
+static unsigned int default_input;     /* default 0 = Composite, 1 = S-Video */
+module_param(default_input, uint, 0444);
 MODULE_PARM_DESC(default_input,
                 "Default input (0=Composite, 1=S-Video, 2=Internal)");
 
 static int default_mux = 1;    /* 6 Eyes input selection */
-module_param(default_mux, int, 0);
+module_param(default_mux, int, 0644);
 MODULE_PARM_DESC(default_mux,
                 "Default 6 Eyes mux setting (Input selection)");
 
-static int default_norm = 0;   /* 0=PAL, 1=NTSC 2=SECAM */
-module_param(default_norm, int, 0);
+static int default_norm;       /* default 0 = PAL, 1 = NTSC 2 = SECAM */
+module_param(default_norm, int, 0444);
 MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
 
-static int video_nr = -1;      /* /dev/videoN, -1 for autodetect */
-module_param(video_nr, int, 0);
-MODULE_PARM_DESC(video_nr, "video device number");
+/* /dev/videoN, -1 for autodetect */
+static int video_nr[BUZ_MAX] = {-1, -1, -1, -1};
+module_param_array(video_nr, int, NULL, 0444);
+MODULE_PARM_DESC(video_nr, "video device number (-1=Auto)");
 
 /*
    Number and size of grab buffers for Video 4 Linux
@@ -127,28 +127,27 @@ MODULE_PARM_DESC(video_nr, "video device number");
 
 int v4l_nbufs = 2;
 int v4l_bufsize = 128;         /* Everybody should be able to work with this setting */
-module_param(v4l_nbufs, int, 0);
+module_param(v4l_nbufs, int, 0644);
 MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
-module_param(v4l_bufsize, int, 0);
+module_param(v4l_bufsize, int, 0644);
 MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
 
 int jpg_nbufs = 32;
 int jpg_bufsize = 512;         /* max size for 100% quality full-PAL frame */
-module_param(jpg_nbufs, int, 0);
+module_param(jpg_nbufs, int, 0644);
 MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
-module_param(jpg_bufsize, int, 0);
+module_param(jpg_bufsize, int, 0644);
 MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
 
 int pass_through = 0;          /* 1=Pass through TV signal when device is not used */
                                /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
-module_param(pass_through, int, 0);
+module_param(pass_through, int, 0644);
 MODULE_PARM_DESC(pass_through,
                 "Pass TV signal through to TV-out when idling");
 
-static int debug = 1;
-int *zr_debug = &debug;
-module_param(debug, int, 0);
-MODULE_PARM_DESC(debug, "Debug level (0-4)");
+int zr36067_debug = 1;
+module_param_named(debug, zr36067_debug, int, 0644);
+MODULE_PARM_DESC(debug, "Debug level (0-5)");
 
 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
 MODULE_AUTHOR("Serguei Miridonov");
@@ -161,14 +160,8 @@ static struct pci_device_id zr36067_pci_tbl[] = {
 };
 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
 
-#define dprintk(num, format, args...) \
-       do { \
-               if (*zr_debug >= num) \
-                       printk(format, ##args); \
-       } while (0)
-
 int zoran_num;                 /* number of Buzs in use */
-struct zoran zoran[BUZ_MAX];
+struct zoran *zoran[BUZ_MAX];
 
 /* videocodec bus functions ZR36060 */
 static u32
@@ -362,16 +355,14 @@ i2cid_to_modulename (u16 i2c_id)
        case I2C_DRIVERID_BT856:
                name = "bt856";
                break;
+       case I2C_DRIVERID_BT866:
+               name = "bt866";
+               break;
        case I2C_DRIVERID_VPX3220:
                name = "vpx3220";
                break;
-/*     case I2C_DRIVERID_VPX3224:
-               name = "vpx3224";
-               break;
-       case I2C_DRIVERID_MSE3000:
-               name = "mse3000";
-               break;*/
-       default:
+       case I2C_DRIVERID_KS0127:
+               name = "ks0127";
                break;
        }
 
@@ -393,8 +384,6 @@ codecid_to_modulename (u16 codecid)
        case CODEC_TYPE_ZR36016:
                name = "zr36016";
                break;
-       default:
-               break;
        }
 
        return name;
@@ -435,7 +424,6 @@ static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
                .type = DC10_old,
                .name = "DC10(old)",
                .i2c_decoder = I2C_DRIVERID_VPX3220,
-               /*.i2c_encoder = I2C_DRIVERID_MSE3000,*/
                .video_codec = CODEC_TYPE_ZR36050,
                .video_vfe = CODEC_TYPE_ZR36016,
 
@@ -814,43 +802,36 @@ clientunreg_unlock_and_return:
        return res;
 }
 
-static struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
+static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
        .setsda = zoran_i2c_setsda,
        .setscl = zoran_i2c_setscl,
        .getsda = zoran_i2c_getsda,
        .getscl = zoran_i2c_getscl,
        .udelay = 10,
-       .mdelay = 0,
        .timeout = 100,
 };
 
-static struct i2c_adapter zoran_i2c_adapter_template = {
-       .name = "zr36057",
-       .id = I2C_HW_B_ZR36067,
-       .algo = NULL,
-       .client_register = zoran_i2c_client_register,
-       .client_unregister = zoran_i2c_client_unregister,
-};
-
 static int
 zoran_register_i2c (struct zoran *zr)
 {
        memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
               sizeof(struct i2c_algo_bit_data));
        zr->i2c_algo.data = zr;
-       memcpy(&zr->i2c_adapter, &zoran_i2c_adapter_template,
-              sizeof(struct i2c_adapter));
-       strncpy(I2C_NAME(&zr->i2c_adapter), ZR_DEVNAME(zr),
-               sizeof(I2C_NAME(&zr->i2c_adapter)) - 1);
+       zr->i2c_adapter.id = I2C_HW_B_ZR36067;
+       zr->i2c_adapter.client_register = zoran_i2c_client_register;
+       zr->i2c_adapter.client_unregister = zoran_i2c_client_unregister;
+       strlcpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
+               sizeof(zr->i2c_adapter.name));
        i2c_set_adapdata(&zr->i2c_adapter, zr);
        zr->i2c_adapter.algo_data = &zr->i2c_algo;
+       zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
        return i2c_bit_add_bus(&zr->i2c_adapter);
 }
 
 static void
 zoran_unregister_i2c (struct zoran *zr)
 {
-       i2c_bit_del_bus((&zr->i2c_adapter));
+       i2c_del_adapter(&zr->i2c_adapter);
 }
 
 /* Check a zoran_params struct for correctness, insert default params */
@@ -1075,7 +1056,7 @@ test_interrupts (struct zoran *zr)
        if (timeout) {
                dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
        }
-       if (*zr_debug > 1)
+       if (zr36067_debug > 1)
                print_interrupts(zr);
        btwrite(icr, ZR36057_ICR);
 }
@@ -1121,7 +1102,14 @@ zr36057_init (struct zoran *zr)
                zr->timing = zr->card.tvn[zr->norm];
        }
 
-       zr->input = default_input = (default_input ? 1 : 0);
+       if (default_input > zr->card.inputs-1) {
+               dprintk(1,
+                       KERN_WARNING
+                       "%s: default_input value %d out of range (0-%d)\n",
+                       ZR_DEVNAME(zr), default_input, zr->card.inputs-1);
+               default_input = 0;
+       }
+       zr->input = default_input;
 
        /* Should the following be reset at every open ? */
        zr->hue = 32768;
@@ -1145,7 +1133,7 @@ zr36057_init (struct zoran *zr)
                goto exit_free;
        }
        for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
-               zr->stat_com[j] = 1;    /* mark as unavailable to zr36057 */
+               zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
        }
 
        /*
@@ -1153,12 +1141,12 @@ zr36057_init (struct zoran *zr)
         */
        memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
        strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
-       err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr);
+       err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr[zr->id]);
        if (err < 0)
                goto exit_unregister;
 
        zoran_init_hardware(zr);
-       if (*zr_debug > 2)
+       if (zr36067_debug > 2)
                detect_guest_activity(zr);
        test_interrupts(zr);
        if (!pass_through) {
@@ -1182,7 +1170,7 @@ static void
 zoran_release (struct zoran *zr)
 {
        if (!zr->initialized)
-               return;
+               goto exit_free;
        /* unregister videocodec bus */
        if (zr->codec) {
                struct videocodec_master *master = zr->codec->master_data;
@@ -1210,6 +1198,8 @@ zoran_release (struct zoran *zr)
        iounmap(zr->zr36057_mem);
        pci_disable_device(zr->pci_dev);
        video_unregister_device(zr->video_dev);
+exit_free:
+       kfree(zr);
 }
 
 void
@@ -1233,8 +1223,14 @@ zoran_setup_videocodec (struct zoran *zr,
                return m;
        }
 
-       m->magic = 0L; /* magic not used */
-       m->type = VID_HARDWARE_ZR36067;
+       /* magic and type are unused for master struct. Makes sense only at
+          codec structs.
+          In the past, .type were initialized to the old V4L1 .hardware
+          value, as VID_HARDWARE_ZR36067
+        */
+       m->magic = 0L;
+       m->type = 0;
+
        m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
        strncpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
        m->data = zr;
@@ -1262,7 +1258,7 @@ zoran_setup_videocodec (struct zoran *zr,
 }
 
 /*
- *   Scan for a Buz card (actually for the PCI contoler ZR36057),
+ *   Scan for a Buz card (actually for the PCI controller ZR36057),
  *   request the irq and map the io memory
  */
 static int __devinit
@@ -1279,12 +1275,16 @@ find_zr36057 (void)
 
        zoran_num = 0;
        while (zoran_num < BUZ_MAX &&
-              (dev =
-               pci_find_device(PCI_VENDOR_ID_ZORAN,
-                               PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
+              (dev = pci_get_device(PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, dev)) != NULL) {
                card_num = card[zoran_num];
-               zr = &zoran[zoran_num];
-               memset(zr, 0, sizeof(struct zoran));    // Just in case if previous cycle failed
+               zr = kzalloc(sizeof(struct zoran), GFP_KERNEL);
+               if (!zr) {
+                       dprintk(1,
+                               KERN_ERR
+                               "%s: find_zr36057() - kzalloc failed\n",
+                               ZORAN_NAME);
+                       continue;
+               }
                zr->pci_dev = dev;
                //zr->zr36057_mem = NULL;
                zr->id = zoran_num;
@@ -1292,7 +1292,7 @@ find_zr36057 (void)
                spin_lock_init(&zr->spinlock);
                mutex_init(&zr->resource_lock);
                if (pci_enable_device(dev))
-                       continue;
+                       goto zr_free_mem;
                zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
                pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION,
                                     &zr->revision);
@@ -1308,7 +1308,7 @@ find_zr36057 (void)
                                        KERN_ERR
                                        "%s: find_zr36057() - no card specified, please use the card=X insmod option\n",
                                        ZR_DEVNAME(zr));
-                               continue;
+                               goto zr_free_mem;
                        }
                } else {
                        int i;
@@ -1347,7 +1347,7 @@ find_zr36057 (void)
                                                KERN_ERR
                                                "%s: find_zr36057() - unknown card\n",
                                                ZR_DEVNAME(zr));
-                                       continue;
+                                       goto zr_free_mem;
                                }
                        }
                }
@@ -1357,7 +1357,7 @@ find_zr36057 (void)
                                KERN_ERR
                                "%s: find_zr36057() - invalid cardnum %d\n",
                                ZR_DEVNAME(zr), card_num);
-                       continue;
+                       goto zr_free_mem;
                }
 
                /* even though we make this a non pointer and thus
@@ -1375,12 +1375,12 @@ find_zr36057 (void)
                                KERN_ERR
                                "%s: find_zr36057() - ioremap failed\n",
                                ZR_DEVNAME(zr));
-                       continue;
+                       goto zr_free_mem;
                }
 
                result = request_irq(zr->pci_dev->irq,
                                     zoran_irq,
-                                    SA_SHIRQ | SA_INTERRUPT,
+                                    IRQF_SHARED | IRQF_DISABLED,
                                     ZR_DEVNAME(zr),
                                     (void *) zr);
                if (result < 0) {
@@ -1542,8 +1542,9 @@ find_zr36057 (void)
                                goto zr_detach_vfe;
                        }
                }
-
-               zoran_num++;
+               /* Success so keep the pci_dev referenced */
+               pci_dev_get(zr->pci_dev);
+               zoran[zoran_num++] = zr;
                continue;
 
                // Init errors
@@ -1562,8 +1563,13 @@ find_zr36057 (void)
                free_irq(zr->pci_dev->irq, zr);
              zr_unmap:
                iounmap(zr->zr36057_mem);
+             zr_free_mem:
+               kfree(zr);
                continue;
        }
+       if (dev)        /* Clean up ref count on early exit */
+               pci_dev_put(dev);
+
        if (zoran_num == 0) {
                dprintk(1, KERN_INFO "No known MJPEG cards found.\n");
        }
@@ -1618,21 +1624,21 @@ init_dc10_cards (void)
        }
 
        /* random nonsense */
-       dprintk(5, KERN_DEBUG "Jotti is een held!\n");
+       dprintk(6, KERN_DEBUG "Jotti is een held!\n");
 
        /* some mainboards might not do PCI-PCI data transfer well */
-       if (pci_pci_problems & PCIPCI_FAIL) {
+       if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
                dprintk(1,
                        KERN_WARNING
-                       "%s: chipset may not support reliable PCI-PCI DMA\n",
+                       "%s: chipset does not support reliable PCI-PCI DMA\n",
                        ZORAN_NAME);
        }
 
        /* take care of Natoma chipset and a revision 1 zr36057 */
        for (i = 0; i < zoran_num; i++) {
-               struct zoran *zr = &zoran[i];
+               struct zoran *zr = zoran[i];
 
-               if (pci_pci_problems & PCIPCI_NATOMA && zr->revision <= 1) {
+               if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
                        zr->jpg_buffers.need_contiguous = 1;
                        dprintk(1,
                                KERN_INFO
@@ -1642,7 +1648,7 @@ init_dc10_cards (void)
 
                if (zr36057_init(zr) < 0) {
                        for (i = 0; i < zoran_num; i++)
-                               zoran_release(&zoran[i]);
+                               zoran_release(zoran[i]);
                        return -EIO;
                }
                zoran_proc_init(zr);
@@ -1657,7 +1663,7 @@ unload_dc10_cards (void)
        int i;
 
        for (i = 0; i < zoran_num; i++)
-               zoran_release(&zoran[i]);
+               zoran_release(zoran[i]);
 }
 
 module_init(init_dc10_cards);