This source file includes following definitions.
- __isp_stat_buf_sync_magic
- isp_stat_buf_sync_magic_for_device
- isp_stat_buf_sync_magic_for_cpu
- isp_stat_buf_check_magic
- isp_stat_buf_insert_magic
- isp_stat_buf_sync_for_device
- isp_stat_buf_sync_for_cpu
- isp_stat_buf_clear
- __isp_stat_buf_find
- isp_stat_buf_find_oldest
- isp_stat_buf_find_oldest_or_empty
- isp_stat_buf_queue
- isp_stat_buf_next
- isp_stat_buf_release
- isp_stat_buf_get
- isp_stat_bufs_free
- isp_stat_bufs_alloc_one
- isp_stat_bufs_alloc
- isp_stat_queue_event
- omap3isp_stat_request_statistics
- omap3isp_stat_request_statistics_time32
- omap3isp_stat_config
- isp_stat_buf_process
- omap3isp_stat_pcr_busy
- omap3isp_stat_busy
- isp_stat_pcr_enable
- omap3isp_stat_suspend
- omap3isp_stat_resume
- isp_stat_try_enable
- omap3isp_stat_isr_frame_sync
- omap3isp_stat_sbl_overflow
- omap3isp_stat_enable
- omap3isp_stat_s_stream
- __stat_isr
- omap3isp_stat_isr
- omap3isp_stat_dma_isr
- omap3isp_stat_subscribe_event
- omap3isp_stat_unsubscribe_event
- omap3isp_stat_unregister_entities
- omap3isp_stat_register_entities
- isp_stat_init_entities
- omap3isp_stat_init
- omap3isp_stat_cleanup
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 #include <linux/dma-mapping.h>
  16 #include <linux/slab.h>
  17 #include <linux/timekeeping.h>
  18 #include <linux/uaccess.h>
  19 
  20 #include "isp.h"
  21 
  22 #define ISP_STAT_USES_DMAENGINE(stat)   ((stat)->dma_ch != NULL)
  23 
  24 
  25 
  26 
  27 
  28 #define MAGIC_SIZE              16
  29 #define MAGIC_NUM               0x55
  30 
  31 
  32 #define AF_EXTRA_DATA           OMAP3ISP_AF_PAXEL_SIZE
  33 
  34 
  35 
  36 
  37 
  38 
  39 
  40 
  41 
  42 
  43 
  44 
  45 
  46 
  47 
  48 
  49 
  50 #define NUM_H3A_RECOVER_BUFS    10
  51 
  52 
  53 
  54 
  55 
  56 #define IS_H3A_AF(stat)         ((stat) == &(stat)->isp->isp_af)
  57 #define IS_H3A_AEWB(stat)       ((stat) == &(stat)->isp->isp_aewb)
  58 #define IS_H3A(stat)            (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
  59 
  60 static void __isp_stat_buf_sync_magic(struct ispstat *stat,
  61                                       struct ispstat_buffer *buf,
  62                                       u32 buf_size, enum dma_data_direction dir,
  63                                       void (*dma_sync)(struct device *,
  64                                         dma_addr_t, unsigned long, size_t,
  65                                         enum dma_data_direction))
  66 {
  67         
  68         dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
  69         dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
  70                  buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
  71 }
  72 
  73 static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
  74                                                struct ispstat_buffer *buf,
  75                                                u32 buf_size,
  76                                                enum dma_data_direction dir)
  77 {
  78         if (ISP_STAT_USES_DMAENGINE(stat))
  79                 return;
  80 
  81         __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
  82                                   dma_sync_single_range_for_device);
  83 }
  84 
  85 static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
  86                                             struct ispstat_buffer *buf,
  87                                             u32 buf_size,
  88                                             enum dma_data_direction dir)
  89 {
  90         if (ISP_STAT_USES_DMAENGINE(stat))
  91                 return;
  92 
  93         __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
  94                                   dma_sync_single_range_for_cpu);
  95 }
  96 
  97 static int isp_stat_buf_check_magic(struct ispstat *stat,
  98                                     struct ispstat_buffer *buf)
  99 {
 100         const u32 buf_size = IS_H3A_AF(stat) ?
 101                              buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
 102         u8 *w;
 103         u8 *end;
 104         int ret = -EINVAL;
 105 
 106         isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
 107 
 108         
 109         for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
 110                 if (likely(*w != MAGIC_NUM))
 111                         ret = 0;
 112 
 113         if (ret) {
 114                 dev_dbg(stat->isp->dev,
 115                         "%s: beginning magic check does not match.\n",
 116                         stat->subdev.name);
 117                 return ret;
 118         }
 119 
 120         
 121         for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
 122              w < end; w++) {
 123                 if (unlikely(*w != MAGIC_NUM)) {
 124                         dev_dbg(stat->isp->dev,
 125                                 "%s: ending magic check does not match.\n",
 126                                 stat->subdev.name);
 127                         return -EINVAL;
 128                 }
 129         }
 130 
 131         isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
 132                                            DMA_FROM_DEVICE);
 133 
 134         return 0;
 135 }
 136 
 137 static void isp_stat_buf_insert_magic(struct ispstat *stat,
 138                                       struct ispstat_buffer *buf)
 139 {
 140         const u32 buf_size = IS_H3A_AF(stat) ?
 141                              stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
 142 
 143         isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
 144 
 145         
 146 
 147 
 148 
 149 
 150 
 151         memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
 152         memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
 153 
 154         isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
 155                                            DMA_BIDIRECTIONAL);
 156 }
 157 
 158 static void isp_stat_buf_sync_for_device(struct ispstat *stat,
 159                                          struct ispstat_buffer *buf)
 160 {
 161         if (ISP_STAT_USES_DMAENGINE(stat))
 162                 return;
 163 
 164         dma_sync_sg_for_device(stat->isp->dev, buf->sgt.sgl,
 165                                buf->sgt.nents, DMA_FROM_DEVICE);
 166 }
 167 
 168 static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
 169                                       struct ispstat_buffer *buf)
 170 {
 171         if (ISP_STAT_USES_DMAENGINE(stat))
 172                 return;
 173 
 174         dma_sync_sg_for_cpu(stat->isp->dev, buf->sgt.sgl,
 175                             buf->sgt.nents, DMA_FROM_DEVICE);
 176 }
 177 
 178 static void isp_stat_buf_clear(struct ispstat *stat)
 179 {
 180         int i;
 181 
 182         for (i = 0; i < STAT_MAX_BUFS; i++)
 183                 stat->buf[i].empty = 1;
 184 }
 185 
 186 static struct ispstat_buffer *
 187 __isp_stat_buf_find(struct ispstat *stat, int look_empty)
 188 {
 189         struct ispstat_buffer *found = NULL;
 190         int i;
 191 
 192         for (i = 0; i < STAT_MAX_BUFS; i++) {
 193                 struct ispstat_buffer *curr = &stat->buf[i];
 194 
 195                 
 196 
 197 
 198 
 199                 if (curr == stat->locked_buf || curr == stat->active_buf)
 200                         continue;
 201 
 202                 
 203                 if (!look_empty && curr->empty)
 204                         continue;
 205 
 206                 
 207                 if (curr->empty) {
 208                         found = curr;
 209                         break;
 210                 }
 211 
 212                 
 213                 if (!found ||
 214                     (s32)curr->frame_number - (s32)found->frame_number < 0)
 215                         found = curr;
 216         }
 217 
 218         return found;
 219 }
 220 
 221 static inline struct ispstat_buffer *
 222 isp_stat_buf_find_oldest(struct ispstat *stat)
 223 {
 224         return __isp_stat_buf_find(stat, 0);
 225 }
 226 
 227 static inline struct ispstat_buffer *
 228 isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
 229 {
 230         return __isp_stat_buf_find(stat, 1);
 231 }
 232 
 233 static int isp_stat_buf_queue(struct ispstat *stat)
 234 {
 235         if (!stat->active_buf)
 236                 return STAT_NO_BUF;
 237 
 238         ktime_get_ts64(&stat->active_buf->ts);
 239 
 240         stat->active_buf->buf_size = stat->buf_size;
 241         if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
 242                 dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
 243                         stat->subdev.name);
 244                 return STAT_NO_BUF;
 245         }
 246         stat->active_buf->config_counter = stat->config_counter;
 247         stat->active_buf->frame_number = stat->frame_number;
 248         stat->active_buf->empty = 0;
 249         stat->active_buf = NULL;
 250 
 251         return STAT_BUF_DONE;
 252 }
 253 
 254 
 255 static void isp_stat_buf_next(struct ispstat *stat)
 256 {
 257         if (unlikely(stat->active_buf))
 258                 
 259                 dev_dbg(stat->isp->dev,
 260                         "%s: new buffer requested without queuing active one.\n",
 261                         stat->subdev.name);
 262         else
 263                 stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
 264 }
 265 
 266 static void isp_stat_buf_release(struct ispstat *stat)
 267 {
 268         unsigned long flags;
 269 
 270         isp_stat_buf_sync_for_device(stat, stat->locked_buf);
 271         spin_lock_irqsave(&stat->isp->stat_lock, flags);
 272         stat->locked_buf = NULL;
 273         spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 274 }
 275 
 276 
 277 static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
 278                                                struct omap3isp_stat_data *data)
 279 {
 280         int rval = 0;
 281         unsigned long flags;
 282         struct ispstat_buffer *buf;
 283 
 284         spin_lock_irqsave(&stat->isp->stat_lock, flags);
 285 
 286         while (1) {
 287                 buf = isp_stat_buf_find_oldest(stat);
 288                 if (!buf) {
 289                         spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 290                         dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
 291                                 stat->subdev.name);
 292                         return ERR_PTR(-EBUSY);
 293                 }
 294                 if (isp_stat_buf_check_magic(stat, buf)) {
 295                         dev_dbg(stat->isp->dev,
 296                                 "%s: current buffer has corrupted data\n.",
 297                                 stat->subdev.name);
 298                         
 299                         buf->empty = 1;
 300                 } else {
 301                         
 302                         break;
 303                 }
 304         }
 305 
 306         stat->locked_buf = buf;
 307 
 308         spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 309 
 310         if (buf->buf_size > data->buf_size) {
 311                 dev_warn(stat->isp->dev,
 312                          "%s: userspace's buffer size is not enough.\n",
 313                          stat->subdev.name);
 314                 isp_stat_buf_release(stat);
 315                 return ERR_PTR(-EINVAL);
 316         }
 317 
 318         isp_stat_buf_sync_for_cpu(stat, buf);
 319 
 320         rval = copy_to_user(data->buf,
 321                             buf->virt_addr,
 322                             buf->buf_size);
 323 
 324         if (rval) {
 325                 dev_info(stat->isp->dev,
 326                          "%s: failed copying %d bytes of stat data\n",
 327                          stat->subdev.name, rval);
 328                 buf = ERR_PTR(-EFAULT);
 329                 isp_stat_buf_release(stat);
 330         }
 331 
 332         return buf;
 333 }
 334 
 335 static void isp_stat_bufs_free(struct ispstat *stat)
 336 {
 337         struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
 338                            ? NULL : stat->isp->dev;
 339         unsigned int i;
 340 
 341         for (i = 0; i < STAT_MAX_BUFS; i++) {
 342                 struct ispstat_buffer *buf = &stat->buf[i];
 343 
 344                 if (!buf->virt_addr)
 345                         continue;
 346 
 347                 sg_free_table(&buf->sgt);
 348 
 349                 dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
 350                                   buf->dma_addr);
 351 
 352                 buf->dma_addr = 0;
 353                 buf->virt_addr = NULL;
 354                 buf->empty = 1;
 355         }
 356 
 357         dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
 358                 stat->subdev.name);
 359 
 360         stat->buf_alloc_size = 0;
 361         stat->active_buf = NULL;
 362 }
 363 
 364 static int isp_stat_bufs_alloc_one(struct device *dev,
 365                                    struct ispstat_buffer *buf,
 366                                    unsigned int size)
 367 {
 368         int ret;
 369 
 370         buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
 371                                             GFP_KERNEL);
 372         if (!buf->virt_addr)
 373                 return -ENOMEM;
 374 
 375         ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
 376                               size);
 377         if (ret < 0) {
 378                 dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
 379                 buf->virt_addr = NULL;
 380                 buf->dma_addr = 0;
 381                 return ret;
 382         }
 383 
 384         return 0;
 385 }
 386 
 387 
 388 
 389 
 390 
 391 
 392 
 393 
 394 
 395 
 396 
 397 
 398 
 399 
 400 
 401 
 402 
 403 static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
 404 {
 405         struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
 406                            ? NULL : stat->isp->dev;
 407         unsigned long flags;
 408         unsigned int i;
 409 
 410         spin_lock_irqsave(&stat->isp->stat_lock, flags);
 411 
 412         BUG_ON(stat->locked_buf != NULL);
 413 
 414         
 415         if (stat->buf_alloc_size >= size) {
 416                 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 417                 return 0;
 418         }
 419 
 420         if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
 421                 dev_info(stat->isp->dev,
 422                          "%s: trying to allocate memory when busy\n",
 423                          stat->subdev.name);
 424                 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 425                 return -EBUSY;
 426         }
 427 
 428         spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 429 
 430         isp_stat_bufs_free(stat);
 431 
 432         stat->buf_alloc_size = size;
 433 
 434         for (i = 0; i < STAT_MAX_BUFS; i++) {
 435                 struct ispstat_buffer *buf = &stat->buf[i];
 436                 int ret;
 437 
 438                 ret = isp_stat_bufs_alloc_one(dev, buf, size);
 439                 if (ret < 0) {
 440                         dev_err(stat->isp->dev,
 441                                 "%s: Failed to allocate DMA buffer %u\n",
 442                                 stat->subdev.name, i);
 443                         isp_stat_bufs_free(stat);
 444                         return ret;
 445                 }
 446 
 447                 buf->empty = 1;
 448 
 449                 dev_dbg(stat->isp->dev,
 450                         "%s: buffer[%u] allocated. dma=%pad virt=%p",
 451                         stat->subdev.name, i, &buf->dma_addr, buf->virt_addr);
 452         }
 453 
 454         return 0;
 455 }
 456 
 457 static void isp_stat_queue_event(struct ispstat *stat, int err)
 458 {
 459         struct video_device *vdev = stat->subdev.devnode;
 460         struct v4l2_event event;
 461         struct omap3isp_stat_event_status *status = (void *)event.u.data;
 462 
 463         memset(&event, 0, sizeof(event));
 464         if (!err) {
 465                 status->frame_number = stat->frame_number;
 466                 status->config_counter = stat->config_counter;
 467         } else {
 468                 status->buf_err = 1;
 469         }
 470         event.type = stat->event_type;
 471         v4l2_event_queue(vdev, &event);
 472 }
 473 
 474 
 475 
 476 
 477 
 478 
 479 
 480 
 481 int omap3isp_stat_request_statistics(struct ispstat *stat,
 482                                      struct omap3isp_stat_data *data)
 483 {
 484         struct ispstat_buffer *buf;
 485 
 486         if (stat->state != ISPSTAT_ENABLED) {
 487                 dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
 488                         stat->subdev.name);
 489                 return -EINVAL;
 490         }
 491 
 492         mutex_lock(&stat->ioctl_lock);
 493         buf = isp_stat_buf_get(stat, data);
 494         if (IS_ERR(buf)) {
 495                 mutex_unlock(&stat->ioctl_lock);
 496                 return PTR_ERR(buf);
 497         }
 498 
 499         data->ts.tv_sec = buf->ts.tv_sec;
 500         data->ts.tv_usec = buf->ts.tv_nsec / NSEC_PER_USEC;
 501         data->config_counter = buf->config_counter;
 502         data->frame_number = buf->frame_number;
 503         data->buf_size = buf->buf_size;
 504 
 505         buf->empty = 1;
 506         isp_stat_buf_release(stat);
 507         mutex_unlock(&stat->ioctl_lock);
 508 
 509         return 0;
 510 }
 511 
 512 int omap3isp_stat_request_statistics_time32(struct ispstat *stat,
 513                                         struct omap3isp_stat_data_time32 *data)
 514 {
 515         struct omap3isp_stat_data data64;
 516         int ret;
 517 
 518         ret = omap3isp_stat_request_statistics(stat, &data64);
 519         if (ret)
 520                 return ret;
 521 
 522         data->ts.tv_sec = data64.ts.tv_sec;
 523         data->ts.tv_usec = data64.ts.tv_usec;
 524         memcpy(&data->buf, &data64.buf, sizeof(*data) - sizeof(data->ts));
 525 
 526         return 0;
 527 }
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 
 536 
 537 int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
 538 {
 539         int ret;
 540         unsigned long irqflags;
 541         struct ispstat_generic_config *user_cfg = new_conf;
 542         u32 buf_size = user_cfg->buf_size;
 543 
 544         mutex_lock(&stat->ioctl_lock);
 545 
 546         dev_dbg(stat->isp->dev,
 547                 "%s: configuring module with buffer size=0x%08lx\n",
 548                 stat->subdev.name, (unsigned long)buf_size);
 549 
 550         ret = stat->ops->validate_params(stat, new_conf);
 551         if (ret) {
 552                 mutex_unlock(&stat->ioctl_lock);
 553                 dev_dbg(stat->isp->dev, "%s: configuration values are invalid.\n",
 554                         stat->subdev.name);
 555                 return ret;
 556         }
 557 
 558         if (buf_size != user_cfg->buf_size)
 559                 dev_dbg(stat->isp->dev,
 560                         "%s: driver has corrected buffer size request to 0x%08lx\n",
 561                         stat->subdev.name,
 562                         (unsigned long)user_cfg->buf_size);
 563 
 564         
 565 
 566 
 567 
 568 
 569 
 570 
 571 
 572 
 573 
 574 
 575         if (IS_H3A(stat)) {
 576                 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
 577                 if (IS_H3A_AF(stat))
 578                         
 579 
 580 
 581 
 582                         buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
 583                 if (stat->recover_priv) {
 584                         struct ispstat_generic_config *recover_cfg =
 585                                 stat->recover_priv;
 586                         buf_size += recover_cfg->buf_size *
 587                                     NUM_H3A_RECOVER_BUFS;
 588                 }
 589                 buf_size = PAGE_ALIGN(buf_size);
 590         } else { 
 591                 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
 592         }
 593 
 594         ret = isp_stat_bufs_alloc(stat, buf_size);
 595         if (ret) {
 596                 mutex_unlock(&stat->ioctl_lock);
 597                 return ret;
 598         }
 599 
 600         spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 601         stat->ops->set_params(stat, new_conf);
 602         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 603 
 604         
 605 
 606 
 607 
 608         user_cfg->config_counter = stat->config_counter + stat->inc_config;
 609 
 610         
 611         stat->configured = 1;
 612         dev_dbg(stat->isp->dev,
 613                 "%s: module has been successfully configured.\n",
 614                 stat->subdev.name);
 615 
 616         mutex_unlock(&stat->ioctl_lock);
 617 
 618         return 0;
 619 }
 620 
 621 
 622 
 623 
 624 
 625 
 626 
 627 static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
 628 {
 629         int ret = STAT_NO_BUF;
 630 
 631         if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
 632             buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
 633                 ret = isp_stat_buf_queue(stat);
 634                 isp_stat_buf_next(stat);
 635         }
 636 
 637         return ret;
 638 }
 639 
 640 int omap3isp_stat_pcr_busy(struct ispstat *stat)
 641 {
 642         return stat->ops->busy(stat);
 643 }
 644 
 645 int omap3isp_stat_busy(struct ispstat *stat)
 646 {
 647         return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
 648                 (stat->state != ISPSTAT_DISABLED);
 649 }
 650 
 651 
 652 
 653 
 654 
 655 
 656 
 657 
 658 static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
 659 {
 660         if ((stat->state != ISPSTAT_ENABLING &&
 661              stat->state != ISPSTAT_ENABLED) && pcr_enable)
 662                 
 663                 return;
 664 
 665         stat->ops->enable(stat, pcr_enable);
 666         if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
 667                 stat->state = ISPSTAT_DISABLED;
 668         else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
 669                 stat->state = ISPSTAT_ENABLED;
 670 }
 671 
 672 void omap3isp_stat_suspend(struct ispstat *stat)
 673 {
 674         unsigned long flags;
 675 
 676         spin_lock_irqsave(&stat->isp->stat_lock, flags);
 677 
 678         if (stat->state != ISPSTAT_DISABLED)
 679                 stat->ops->enable(stat, 0);
 680         if (stat->state == ISPSTAT_ENABLED)
 681                 stat->state = ISPSTAT_SUSPENDED;
 682 
 683         spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 684 }
 685 
 686 void omap3isp_stat_resume(struct ispstat *stat)
 687 {
 688         
 689         if (stat->state == ISPSTAT_SUSPENDED)
 690                 stat->state = ISPSTAT_ENABLING;
 691 }
 692 
 693 static void isp_stat_try_enable(struct ispstat *stat)
 694 {
 695         unsigned long irqflags;
 696 
 697         if (stat->priv == NULL)
 698                 
 699                 return;
 700 
 701         spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 702         if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
 703             stat->buf_alloc_size) {
 704                 
 705 
 706 
 707 
 708                 stat->update = 1;
 709                 isp_stat_buf_next(stat);
 710                 stat->ops->setup_regs(stat, stat->priv);
 711                 isp_stat_buf_insert_magic(stat, stat->active_buf);
 712 
 713                 
 714 
 715 
 716 
 717 
 718 
 719                 if (!IS_H3A(stat))
 720                         atomic_set(&stat->buf_err, 0);
 721 
 722                 isp_stat_pcr_enable(stat, 1);
 723                 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 724                 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
 725                         stat->subdev.name);
 726         } else {
 727                 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 728         }
 729 }
 730 
 731 void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
 732 {
 733         isp_stat_try_enable(stat);
 734 }
 735 
 736 void omap3isp_stat_sbl_overflow(struct ispstat *stat)
 737 {
 738         unsigned long irqflags;
 739 
 740         spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 741         
 742 
 743 
 744 
 745         atomic_set(&stat->buf_err, 2);
 746 
 747         
 748 
 749 
 750 
 751 
 752 
 753         if (stat->recover_priv)
 754                 stat->sbl_ovl_recover = 1;
 755         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 756 }
 757 
 758 
 759 
 760 
 761 
 762 
 763 
 764 
 765 int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
 766 {
 767         unsigned long irqflags;
 768 
 769         dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
 770                 stat->subdev.name, enable ? "enable" : "disable");
 771 
 772         
 773         mutex_lock(&stat->ioctl_lock);
 774 
 775         spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 776 
 777         if (!stat->configured && enable) {
 778                 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 779                 mutex_unlock(&stat->ioctl_lock);
 780                 dev_dbg(stat->isp->dev,
 781                         "%s: cannot enable module as it's never been successfully configured so far.\n",
 782                         stat->subdev.name);
 783                 return -EINVAL;
 784         }
 785 
 786         if (enable) {
 787                 if (stat->state == ISPSTAT_DISABLING)
 788                         
 789                         stat->state = ISPSTAT_ENABLED;
 790                 else if (stat->state == ISPSTAT_DISABLED)
 791                         
 792                         stat->state = ISPSTAT_ENABLING;
 793         } else {
 794                 if (stat->state == ISPSTAT_ENABLING) {
 795                         
 796                         stat->state = ISPSTAT_DISABLED;
 797                 } else if (stat->state == ISPSTAT_ENABLED) {
 798                         
 799                         stat->state = ISPSTAT_DISABLING;
 800                         isp_stat_buf_clear(stat);
 801                 }
 802         }
 803 
 804         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 805         mutex_unlock(&stat->ioctl_lock);
 806 
 807         return 0;
 808 }
 809 
 810 int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
 811 {
 812         struct ispstat *stat = v4l2_get_subdevdata(subdev);
 813 
 814         if (enable) {
 815                 
 816 
 817 
 818 
 819                 isp_stat_try_enable(stat);
 820         } else {
 821                 unsigned long flags;
 822                 
 823                 omap3isp_stat_enable(stat, 0);
 824                 spin_lock_irqsave(&stat->isp->stat_lock, flags);
 825                 stat->ops->enable(stat, 0);
 826                 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
 827 
 828                 
 829 
 830 
 831 
 832 
 833 
 834 
 835 
 836 
 837 
 838 
 839                 if (!omap3isp_stat_pcr_busy(stat))
 840                         omap3isp_stat_isr(stat);
 841 
 842                 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
 843                         stat->subdev.name);
 844         }
 845 
 846         return 0;
 847 }
 848 
 849 
 850 
 851 
 852 static void __stat_isr(struct ispstat *stat, int from_dma)
 853 {
 854         int ret = STAT_BUF_DONE;
 855         int buf_processing;
 856         unsigned long irqflags;
 857         struct isp_pipeline *pipe;
 858 
 859         
 860 
 861 
 862 
 863 
 864         spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 865         if (stat->state == ISPSTAT_DISABLED) {
 866                 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 867                 return;
 868         }
 869         buf_processing = stat->buf_processing;
 870         stat->buf_processing = 1;
 871         stat->ops->enable(stat, 0);
 872 
 873         if (buf_processing && !from_dma) {
 874                 if (stat->state == ISPSTAT_ENABLED) {
 875                         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 876                         dev_err(stat->isp->dev,
 877                                 "%s: interrupt occurred when module was still processing a buffer.\n",
 878                                 stat->subdev.name);
 879                         ret = STAT_NO_BUF;
 880                         goto out;
 881                 } else {
 882                         
 883 
 884 
 885 
 886 
 887 
 888 
 889                         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 890                         return;
 891                 }
 892         }
 893         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 894 
 895         
 896         if (!omap3isp_stat_pcr_busy(stat)) {
 897                 if (!from_dma && stat->ops->buf_process)
 898                         
 899                         ret = stat->ops->buf_process(stat);
 900                 if (ret == STAT_BUF_WAITING_DMA)
 901                         
 902                         return;
 903 
 904                 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
 905 
 906                 
 907 
 908 
 909 
 910 
 911                 if (stat->state == ISPSTAT_DISABLING) {
 912                         stat->state = ISPSTAT_DISABLED;
 913                         spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 914                         stat->buf_processing = 0;
 915                         return;
 916                 }
 917                 pipe = to_isp_pipeline(&stat->subdev.entity);
 918                 stat->frame_number = atomic_read(&pipe->frame_number);
 919 
 920                 
 921 
 922 
 923 
 924 
 925                 ret = isp_stat_buf_process(stat, ret);
 926 
 927                 if (likely(!stat->sbl_ovl_recover)) {
 928                         stat->ops->setup_regs(stat, stat->priv);
 929                 } else {
 930                         
 931 
 932 
 933 
 934 
 935                         stat->update = 1;
 936                         stat->ops->setup_regs(stat, stat->recover_priv);
 937                         stat->sbl_ovl_recover = 0;
 938 
 939                         
 940 
 941 
 942 
 943                         stat->update = 1;
 944                 }
 945 
 946                 isp_stat_buf_insert_magic(stat, stat->active_buf);
 947 
 948                 
 949 
 950 
 951 
 952 
 953 
 954 
 955 
 956 
 957 
 958 
 959                 isp_stat_pcr_enable(stat, 1);
 960                 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
 961         } else {
 962                 
 963 
 964 
 965 
 966 
 967 
 968 
 969 
 970 
 971 
 972                 if (stat->ops->buf_process)
 973                         
 974 
 975 
 976 
 977 
 978 
 979                         atomic_set(&stat->buf_err, 1);
 980 
 981                 ret = STAT_NO_BUF;
 982                 dev_dbg(stat->isp->dev,
 983                         "%s: cannot process buffer, device is busy.\n",
 984                         stat->subdev.name);
 985         }
 986 
 987 out:
 988         stat->buf_processing = 0;
 989         isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
 990 }
 991 
 992 void omap3isp_stat_isr(struct ispstat *stat)
 993 {
 994         __stat_isr(stat, 0);
 995 }
 996 
 997 void omap3isp_stat_dma_isr(struct ispstat *stat)
 998 {
 999         __stat_isr(stat, 1);
1000 }
1001 
1002 int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
1003                                   struct v4l2_fh *fh,
1004                                   struct v4l2_event_subscription *sub)
1005 {
1006         struct ispstat *stat = v4l2_get_subdevdata(subdev);
1007 
1008         if (sub->type != stat->event_type)
1009                 return -EINVAL;
1010 
1011         return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
1012 }
1013 
1014 int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
1015                                     struct v4l2_fh *fh,
1016                                     struct v4l2_event_subscription *sub)
1017 {
1018         return v4l2_event_unsubscribe(fh, sub);
1019 }
1020 
1021 void omap3isp_stat_unregister_entities(struct ispstat *stat)
1022 {
1023         v4l2_device_unregister_subdev(&stat->subdev);
1024 }
1025 
1026 int omap3isp_stat_register_entities(struct ispstat *stat,
1027                                     struct v4l2_device *vdev)
1028 {
1029         stat->subdev.dev = vdev->mdev->dev;
1030 
1031         return v4l2_device_register_subdev(vdev, &stat->subdev);
1032 }
1033 
1034 static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1035                                   const struct v4l2_subdev_ops *sd_ops)
1036 {
1037         struct v4l2_subdev *subdev = &stat->subdev;
1038         struct media_entity *me = &subdev->entity;
1039 
1040         v4l2_subdev_init(subdev, sd_ops);
1041         snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1042         subdev->grp_id = BIT(16);       
1043         subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1044         v4l2_set_subdevdata(subdev, stat);
1045 
1046         stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
1047         me->ops = NULL;
1048 
1049         return media_entity_pads_init(me, 1, &stat->pad);
1050 }
1051 
1052 int omap3isp_stat_init(struct ispstat *stat, const char *name,
1053                        const struct v4l2_subdev_ops *sd_ops)
1054 {
1055         int ret;
1056 
1057         stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1058         if (!stat->buf)
1059                 return -ENOMEM;
1060 
1061         isp_stat_buf_clear(stat);
1062         mutex_init(&stat->ioctl_lock);
1063         atomic_set(&stat->buf_err, 0);
1064 
1065         ret = isp_stat_init_entities(stat, name, sd_ops);
1066         if (ret < 0) {
1067                 mutex_destroy(&stat->ioctl_lock);
1068                 kfree(stat->buf);
1069         }
1070 
1071         return ret;
1072 }
1073 
1074 void omap3isp_stat_cleanup(struct ispstat *stat)
1075 {
1076         media_entity_cleanup(&stat->subdev.entity);
1077         mutex_destroy(&stat->ioctl_lock);
1078         isp_stat_bufs_free(stat);
1079         kfree(stat->buf);
1080         kfree(stat->priv);
1081         kfree(stat->recover_priv);
1082 }