aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorDean Anderson <dean@sensoray.com>2008-08-25 12:58:55 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2008-10-12 07:36:53 -0400
commit14d962602c8bf86e63c9b9272be1f0360d0a448a (patch)
tree28e84f98740961f4ba4043e6ffdd374cdb1e6ec8 /drivers
parentee7aa9f821b27674f36fc09413914b68f7ad97ca (diff)
V4L/DVB (8752): s2255drv: firmware improvement patch
Fix for reloading firmware when removing and reloading driver Handshaking for firmware loading and changing modes. Removes the restriction of one user per channel at a time. JPEG capture mode added. Signed-off-by: Dean Anderson <dean@sensoray.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/media/video/s2255drv.c535
1 files changed, 297 insertions, 238 deletions
diff --git a/drivers/media/video/s2255drv.c b/drivers/media/video/s2255drv.c
index de39cf0890fc..af19bb346ef1 100644
--- a/drivers/media/video/s2255drv.c
+++ b/drivers/media/video/s2255drv.c
@@ -67,20 +67,21 @@
67/* USB endpoint number for configuring the device */ 67/* USB endpoint number for configuring the device */
68#define S2255_CONFIG_EP 2 68#define S2255_CONFIG_EP 2
69/* maximum time for DSP to start responding after last FW word loaded(ms) */ 69/* maximum time for DSP to start responding after last FW word loaded(ms) */
70#define S2255_DSP_BOOTTIME 400 70#define S2255_DSP_BOOTTIME 800
71/* maximum time to wait for firmware to load (ms) */ 71/* maximum time to wait for firmware to load (ms) */
72#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME) 72#define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME)
73#define S2255_DEF_BUFS 16 73#define S2255_DEF_BUFS 16
74#define S2255_SETMODE_TIMEOUT 500
74#define MAX_CHANNELS 4 75#define MAX_CHANNELS 4
75#define FRAME_MARKER 0x2255DA4AL 76#define S2255_MARKER_FRAME 0x2255DA4AL
76#define MAX_PIPE_USBBLOCK (40 * 1024) 77#define S2255_MARKER_RESPONSE 0x2255ACACL
77#define DEFAULT_PIPE_USBBLOCK (16 * 1024) 78#define S2255_USB_XFER_SIZE (16 * 1024)
78#define MAX_CHANNELS 4 79#define MAX_CHANNELS 4
79#define MAX_PIPE_BUFFERS 1 80#define MAX_PIPE_BUFFERS 1
80#define SYS_FRAMES 4 81#define SYS_FRAMES 4
81/* maximum size is PAL full size plus room for the marker header(s) */ 82/* maximum size is PAL full size plus room for the marker header(s) */
82#define SYS_FRAMES_MAXSIZE (720 * 288 * 2 * 2 + 4096) 83#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)
83#define DEF_USB_BLOCK (4096) 84#define DEF_USB_BLOCK S2255_USB_XFER_SIZE
84#define LINE_SZ_4CIFS_NTSC 640 85#define LINE_SZ_4CIFS_NTSC 640
85#define LINE_SZ_2CIFS_NTSC 640 86#define LINE_SZ_2CIFS_NTSC 640
86#define LINE_SZ_1CIFS_NTSC 320 87#define LINE_SZ_1CIFS_NTSC 320
@@ -108,6 +109,9 @@
108#define COLOR_YUVPL 1 /* YUV planar */ 109#define COLOR_YUVPL 1 /* YUV planar */
109#define COLOR_YUVPK 2 /* YUV packed */ 110#define COLOR_YUVPK 2 /* YUV packed */
110#define COLOR_Y8 4 /* monochrome */ 111#define COLOR_Y8 4 /* monochrome */
112#define COLOR_JPG 5 /* JPEG */
113#define MASK_COLOR 0xff
114#define MASK_JPG_QUALITY 0xff00
111 115
112/* frame decimation. Not implemented by V4L yet(experimental in V4L) */ 116/* frame decimation. Not implemented by V4L yet(experimental in V4L) */
113#define FDEC_1 1 /* capture every frame. default */ 117#define FDEC_1 1 /* capture every frame. default */
@@ -148,16 +152,14 @@ struct s2255_mode {
148 u32 restart; /* if DSP requires restart */ 152 u32 restart; /* if DSP requires restart */
149}; 153};
150 154
151/* frame structure */
152#define FRAME_STATE_UNUSED 0
153#define FRAME_STATE_FILLING 1
154#define FRAME_STATE_FULL 2
155 155
156#define S2255_READ_IDLE 0
157#define S2255_READ_FRAME 1
156 158
159/* frame structure */
157struct s2255_framei { 160struct s2255_framei {
158 unsigned long size; 161 unsigned long size;
159 162 unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/
160 unsigned long ulState; /* ulState ==0 unused, 1 being filled, 2 full */
161 void *lpvbits; /* image data */ 163 void *lpvbits; /* image data */
162 unsigned long cur_size; /* current data copied to it */ 164 unsigned long cur_size; /* current data copied to it */
163}; 165};
@@ -188,6 +190,10 @@ struct s2255_dmaqueue {
188#define S2255_FW_FAILED 3 190#define S2255_FW_FAILED 3
189#define S2255_FW_DISCONNECTING 4 191#define S2255_FW_DISCONNECTING 4
190 192
193#define S2255_FW_MARKER 0x22552f2f
194/* 2255 read states */
195#define S2255_READ_IDLE 0
196#define S2255_READ_FRAME 1
191struct s2255_fw { 197struct s2255_fw {
192 int fw_loaded; 198 int fw_loaded;
193 int fw_size; 199 int fw_size;
@@ -195,7 +201,6 @@ struct s2255_fw {
195 atomic_t fw_state; 201 atomic_t fw_state;
196 void *pfw_data; 202 void *pfw_data;
197 wait_queue_head_t wait_fw; 203 wait_queue_head_t wait_fw;
198 struct timer_list dsp_wait;
199 const struct firmware *fw; 204 const struct firmware *fw;
200}; 205};
201 206
@@ -242,10 +247,20 @@ struct s2255_dev {
242 int last_frame[MAX_CHANNELS]; 247 int last_frame[MAX_CHANNELS];
243 u32 cc; /* current channel */ 248 u32 cc; /* current channel */
244 int b_acquire[MAX_CHANNELS]; 249 int b_acquire[MAX_CHANNELS];
250 /* allocated image size */
245 unsigned long req_image_size[MAX_CHANNELS]; 251 unsigned long req_image_size[MAX_CHANNELS];
252 /* received packet size */
253 unsigned long pkt_size[MAX_CHANNELS];
246 int bad_payload[MAX_CHANNELS]; 254 int bad_payload[MAX_CHANNELS];
247 unsigned long frame_count[MAX_CHANNELS]; 255 unsigned long frame_count[MAX_CHANNELS];
248 int frame_ready; 256 int frame_ready;
257 /* if JPEG image */
258 int jpg_size[MAX_CHANNELS];
259 /* if channel configured to default state */
260 int chn_configured[MAX_CHANNELS];
261 wait_queue_head_t wait_setmode[MAX_CHANNELS];
262 int setmode_ready[MAX_CHANNELS];
263 int chn_ready;
249 struct kref kref; 264 struct kref kref;
250 spinlock_t slock; 265 spinlock_t slock;
251}; 266};
@@ -306,12 +321,16 @@ static void s2255_stop_readpipe(struct s2255_dev *dev);
306static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn); 321static int s2255_start_acquire(struct s2255_dev *dev, unsigned long chn);
307static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn); 322static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn);
308static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf, 323static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
309 int chn); 324 int chn, int jpgsize);
310static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn, 325static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
311 struct s2255_mode *mode); 326 struct s2255_mode *mode);
312static int s2255_board_shutdown(struct s2255_dev *dev); 327static int s2255_board_shutdown(struct s2255_dev *dev);
313static void s2255_exit_v4l(struct s2255_dev *dev); 328static void s2255_exit_v4l(struct s2255_dev *dev);
314static void s2255_fwload_start(struct s2255_dev *dev); 329static void s2255_fwload_start(struct s2255_dev *dev, int reset);
330static void s2255_destroy(struct kref *kref);
331static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req,
332 u16 index, u16 value, void *buf,
333 s32 buf_len, int bOut);
315 334
316#define dprintk(level, fmt, arg...) \ 335#define dprintk(level, fmt, arg...) \
317 do { \ 336 do { \
@@ -407,6 +426,10 @@ static const struct s2255_fmt formats[] = {
407 .fourcc = V4L2_PIX_FMT_UYVY, 426 .fourcc = V4L2_PIX_FMT_UYVY,
408 .depth = 16 427 .depth = 16
409 }, { 428 }, {
429 .name = "JPG",
430 .fourcc = V4L2_PIX_FMT_JPEG,
431 .depth = 24
432 }, {
410 .name = "8bpp GREY", 433 .name = "8bpp GREY",
411 .fourcc = V4L2_PIX_FMT_GREY, 434 .fourcc = V4L2_PIX_FMT_GREY,
412 .depth = 8 435 .depth = 8
@@ -464,6 +487,13 @@ static void planar422p_to_yuv_packed(const unsigned char *in,
464 return; 487 return;
465} 488}
466 489
490void s2255_reset_dsppower(struct s2255_dev *dev)
491{
492 s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1);
493 msleep(10);
494 s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1);
495 return;
496}
467 497
468/* kickstarts the firmware loading. from probe 498/* kickstarts the firmware loading. from probe
469 */ 499 */
@@ -480,18 +510,6 @@ static void s2255_timer(unsigned long user_data)
480 } 510 }
481} 511}
482 512
483/* called when DSP is up and running. DSP is guaranteed to
484 be running after S2255_DSP_BOOTTIME */
485static void s2255_dsp_running(unsigned long user_data)
486{
487 struct s2255_fw *data = (struct s2255_fw *)user_data;
488 dprintk(1, "dsp running\n");
489 atomic_set(&data->fw_state, S2255_FW_SUCCESS);
490 wake_up(&data->wait_fw);
491 printk(KERN_INFO "s2255: firmware loaded successfully\n");
492 return;
493}
494
495 513
496/* this loads the firmware asynchronously. 514/* this loads the firmware asynchronously.
497 Originally this was done synchroously in probe. 515 Originally this was done synchroously in probe.
@@ -549,19 +567,14 @@ static void s2255_fwchunk_complete(struct urb *urb)
549 } 567 }
550 data->fw_loaded += len; 568 data->fw_loaded += len;
551 } else { 569 } else {
552 init_timer(&data->dsp_wait);
553 data->dsp_wait.function = s2255_dsp_running;
554 data->dsp_wait.data = (unsigned long)data;
555 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT); 570 atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
556 mod_timer(&data->dsp_wait, msecs_to_jiffies(S2255_DSP_BOOTTIME)
557 + jiffies);
558 } 571 }
559 dprintk(100, "2255 complete done\n"); 572 dprintk(100, "2255 complete done\n");
560 return; 573 return;
561 574
562} 575}
563 576
564static int s2255_got_frame(struct s2255_dev *dev, int chn) 577static int s2255_got_frame(struct s2255_dev *dev, int chn, int jpgsize)
565{ 578{
566 struct s2255_dmaqueue *dma_q = &dev->vidq[chn]; 579 struct s2255_dmaqueue *dma_q = &dev->vidq[chn];
567 struct s2255_buffer *buf; 580 struct s2255_buffer *buf;
@@ -586,8 +599,7 @@ static int s2255_got_frame(struct s2255_dev *dev, int chn)
586 list_del(&buf->vb.queue); 599 list_del(&buf->vb.queue);
587 do_gettimeofday(&buf->vb.ts); 600 do_gettimeofday(&buf->vb.ts);
588 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i); 601 dprintk(100, "[%p/%d] wakeup\n", buf, buf->vb.i);
589 602 s2255_fillbuff(dev, buf, dma_q->channel, jpgsize);
590 s2255_fillbuff(dev, buf, dma_q->channel);
591 wake_up(&buf->vb.done); 603 wake_up(&buf->vb.done);
592 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i); 604 dprintk(2, "wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
593unlock: 605unlock:
@@ -621,7 +633,7 @@ static const struct s2255_fmt *format_by_fourcc(int fourcc)
621 * 633 *
622 */ 634 */
623static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf, 635static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
624 int chn) 636 int chn, int jpgsize)
625{ 637{
626 int pos = 0; 638 int pos = 0;
627 struct timeval ts; 639 struct timeval ts;
@@ -649,6 +661,10 @@ static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
649 case V4L2_PIX_FMT_GREY: 661 case V4L2_PIX_FMT_GREY:
650 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height); 662 memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height);
651 break; 663 break;
664 case V4L2_PIX_FMT_JPEG:
665 buf->vb.size = jpgsize;
666 memcpy(vbuf, tmpbuf, buf->vb.size);
667 break;
652 case V4L2_PIX_FMT_YUV422P: 668 case V4L2_PIX_FMT_YUV422P:
653 memcpy(vbuf, tmpbuf, 669 memcpy(vbuf, tmpbuf,
654 buf->vb.width * buf->vb.height * 2); 670 buf->vb.width * buf->vb.height * 2);
@@ -657,9 +673,6 @@ static void s2255_fillbuff(struct s2255_dev *dev, struct s2255_buffer *buf,
657 printk(KERN_DEBUG "s2255: unknown format?\n"); 673 printk(KERN_DEBUG "s2255: unknown format?\n");
658 } 674 }
659 dev->last_frame[chn] = -1; 675 dev->last_frame[chn] = -1;
660 /* done with the frame, free it */
661 frm->ulState = 0;
662 dprintk(4, "freeing buffer\n");
663 } else { 676 } else {
664 printk(KERN_ERR "s2255: =======no frame\n"); 677 printk(KERN_ERR "s2255: =======no frame\n");
665 return; 678 return;
@@ -1021,6 +1034,9 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1021 case V4L2_PIX_FMT_GREY: 1034 case V4L2_PIX_FMT_GREY:
1022 fh->mode.color = COLOR_Y8; 1035 fh->mode.color = COLOR_Y8;
1023 break; 1036 break;
1037 case V4L2_PIX_FMT_JPEG:
1038 fh->mode.color = COLOR_JPG | (50 << 8);
1039 break;
1024 case V4L2_PIX_FMT_YUV422P: 1040 case V4L2_PIX_FMT_YUV422P:
1025 fh->mode.color = COLOR_YUVPL; 1041 fh->mode.color = COLOR_YUVPL;
1026 break; 1042 break;
@@ -1139,7 +1155,7 @@ static u32 get_transfer_size(struct s2255_mode *mode)
1139 } 1155 }
1140 } 1156 }
1141 outImageSize = linesPerFrame * pixelsPerLine; 1157 outImageSize = linesPerFrame * pixelsPerLine;
1142 if (mode->color != COLOR_Y8) { 1158 if ((mode->color & MASK_COLOR) != COLOR_Y8) {
1143 /* 2 bytes/pixel if not monochrome */ 1159 /* 2 bytes/pixel if not monochrome */
1144 outImageSize *= 2; 1160 outImageSize *= 2;
1145 } 1161 }
@@ -1185,6 +1201,7 @@ static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1185 u32 *buffer; 1201 u32 *buffer;
1186 unsigned long chn_rev; 1202 unsigned long chn_rev;
1187 1203
1204 mutex_lock(&dev->lock);
1188 chn_rev = G_chnmap[chn]; 1205 chn_rev = G_chnmap[chn];
1189 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale); 1206 dprintk(3, "mode scale [%ld] %p %d\n", chn, mode, mode->scale);
1190 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn], 1207 dprintk(3, "mode scale [%ld] %p %d\n", chn, &dev->mode[chn],
@@ -1199,6 +1216,7 @@ static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1199 buffer = kzalloc(512, GFP_KERNEL); 1216 buffer = kzalloc(512, GFP_KERNEL);
1200 if (buffer == NULL) { 1217 if (buffer == NULL) {
1201 dev_err(&dev->udev->dev, "out of mem\n"); 1218 dev_err(&dev->udev->dev, "out of mem\n");
1219 mutex_unlock(&dev->lock);
1202 return -ENOMEM; 1220 return -ENOMEM;
1203 } 1221 }
1204 1222
@@ -1214,12 +1232,20 @@ static int s2255_set_mode(struct s2255_dev *dev, unsigned long chn,
1214 dprintk(1, "set mode done chn %lu, %d\n", chn, res); 1232 dprintk(1, "set mode done chn %lu, %d\n", chn, res);
1215 1233
1216 /* wait at least 3 frames before continuing */ 1234 /* wait at least 3 frames before continuing */
1217 if (mode->restart) 1235 if (mode->restart) {
1218 msleep(125); 1236 dev->setmode_ready[chn] = 0;
1237 wait_event_timeout(dev->wait_setmode[chn],
1238 (dev->setmode_ready[chn] != 0),
1239 msecs_to_jiffies(S2255_SETMODE_TIMEOUT));
1240 if (dev->setmode_ready[chn] != 1) {
1241 printk(KERN_DEBUG "s2255: no set mode response\n");
1242 res = -EFAULT;
1243 }
1244 }
1219 1245
1220 /* clear the restart flag */ 1246 /* clear the restart flag */
1221 dev->mode[chn].restart = 0; 1247 dev->mode[chn].restart = 0;
1222 1248 mutex_unlock(&dev->lock);
1223 return res; 1249 return res;
1224} 1250}
1225 1251
@@ -1270,7 +1296,7 @@ static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1270 dev->cur_frame[chn] = 0; 1296 dev->cur_frame[chn] = 0;
1271 dev->frame_count[chn] = 0; 1297 dev->frame_count[chn] = 0;
1272 for (j = 0; j < SYS_FRAMES; j++) { 1298 for (j = 0; j < SYS_FRAMES; j++) {
1273 dev->buffer[chn].frame[j].ulState = 0; 1299 dev->buffer[chn].frame[j].ulState = S2255_READ_IDLE;
1274 dev->buffer[chn].frame[j].cur_size = 0; 1300 dev->buffer[chn].frame[j].cur_size = 0;
1275 } 1301 }
1276 res = videobuf_streamon(&fh->vb_vidq); 1302 res = videobuf_streamon(&fh->vb_vidq);
@@ -1455,6 +1481,7 @@ static int s2255_open(struct inode *inode, struct file *file)
1455 enum v4l2_buf_type type = 0; 1481 enum v4l2_buf_type type = 0;
1456 int i = 0; 1482 int i = 0;
1457 int cur_channel = -1; 1483 int cur_channel = -1;
1484 int state;
1458 dprintk(1, "s2255: open called (minor=%d)\n", minor); 1485 dprintk(1, "s2255: open called (minor=%d)\n", minor);
1459 1486
1460 lock_kernel(); 1487 lock_kernel();
@@ -1471,47 +1498,77 @@ static int s2255_open(struct inode *inode, struct file *file)
1471 1498
1472 if ((NULL == dev) || (cur_channel == -1)) { 1499 if ((NULL == dev) || (cur_channel == -1)) {
1473 unlock_kernel(); 1500 unlock_kernel();
1474 dprintk(1, "s2255: openv4l no dev\n"); 1501 printk(KERN_INFO "s2255: openv4l no dev\n");
1475 return -ENODEV; 1502 return -ENODEV;
1476 } 1503 }
1477 1504
1505 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_DISCONNECTING) {
1506 unlock_kernel();
1507 printk(KERN_INFO "disconnecting\n");
1508 return -ENODEV;
1509 }
1510 kref_get(&dev->kref);
1478 mutex_lock(&dev->open_lock); 1511 mutex_lock(&dev->open_lock);
1479 1512
1480 dev->users[cur_channel]++; 1513 dev->users[cur_channel]++;
1481 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]); 1514 dprintk(4, "s2255: open_handles %d\n", dev->users[cur_channel]);
1482 1515
1483 if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_FAILED) { 1516 switch (atomic_read(&dev->fw_data->fw_state)) {
1517 case S2255_FW_FAILED:
1484 err("2255 firmware load failed. retrying.\n"); 1518 err("2255 firmware load failed. retrying.\n");
1485 s2255_fwload_start(dev); 1519 s2255_fwload_start(dev, 1);
1486 wait_event_timeout(dev->fw_data->wait_fw, 1520 wait_event_timeout(dev->fw_data->wait_fw,
1487 (atomic_read(&dev->fw_data->fw_state) 1521 ((atomic_read(&dev->fw_data->fw_state)
1488 != S2255_FW_NOTLOADED), 1522 == S2255_FW_SUCCESS) ||
1523 (atomic_read(&dev->fw_data->fw_state)
1524 == S2255_FW_DISCONNECTING)),
1489 msecs_to_jiffies(S2255_LOAD_TIMEOUT)); 1525 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1490 if (atomic_read(&dev->fw_data->fw_state) 1526 break;
1491 != S2255_FW_SUCCESS) { 1527 case S2255_FW_NOTLOADED:
1492 printk(KERN_INFO "2255 FW load failed.\n"); 1528 case S2255_FW_LOADED_DSPWAIT:
1493 dev->users[cur_channel]--;
1494 mutex_unlock(&dev->open_lock);
1495 unlock_kernel();
1496 return -EFAULT;
1497 }
1498 } else if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_NOTLOADED) {
1499 /* give S2255_LOAD_TIMEOUT time for firmware to load in case 1529 /* give S2255_LOAD_TIMEOUT time for firmware to load in case
1500 driver loaded and then device immediately opened */ 1530 driver loaded and then device immediately opened */
1501 printk(KERN_INFO "%s waiting for firmware load\n", __func__); 1531 printk(KERN_INFO "%s waiting for firmware load\n", __func__);
1502 wait_event_timeout(dev->fw_data->wait_fw, 1532 wait_event_timeout(dev->fw_data->wait_fw,
1503 (atomic_read(&dev->fw_data->fw_state) 1533 ((atomic_read(&dev->fw_data->fw_state)
1504 != S2255_FW_NOTLOADED), 1534 == S2255_FW_SUCCESS) ||
1505 msecs_to_jiffies(S2255_LOAD_TIMEOUT)); 1535 (atomic_read(&dev->fw_data->fw_state)
1506 if (atomic_read(&dev->fw_data->fw_state) 1536 == S2255_FW_DISCONNECTING)),
1507 != S2255_FW_SUCCESS) { 1537 msecs_to_jiffies(S2255_LOAD_TIMEOUT));
1508 printk(KERN_INFO "2255 firmware not loaded" 1538 break;
1509 "try again\n"); 1539 case S2255_FW_SUCCESS:
1510 dev->users[cur_channel]--; 1540 default:
1511 mutex_unlock(&dev->open_lock); 1541 break;
1512 unlock_kernel(); 1542 }
1513 return -EBUSY; 1543 state = atomic_read(&dev->fw_data->fw_state);
1544 if (state != S2255_FW_SUCCESS) {
1545 int rc;
1546 switch (state) {
1547 case S2255_FW_FAILED:
1548 printk(KERN_INFO "2255 FW load failed. %d\n", state);
1549 rc = -ENODEV;
1550 break;
1551 case S2255_FW_DISCONNECTING:
1552 printk(KERN_INFO "%s: disconnecting\n", __func__);
1553 rc = -ENODEV;
1554 break;
1555 case S2255_FW_LOADED_DSPWAIT:
1556 case S2255_FW_NOTLOADED:
1557 printk(KERN_INFO "%s: firmware not loaded yet"
1558 "please try again later\n",
1559 __func__);
1560 rc = -EAGAIN;
1561 break;
1562 default:
1563 printk(KERN_INFO "%s: unknown state\n", __func__);
1564 rc = -EFAULT;
1565 break;
1514 } 1566 }
1567 dev->users[cur_channel]--;
1568 mutex_unlock(&dev->open_lock);
1569 kref_put(&dev->kref, s2255_destroy);
1570 unlock_kernel();
1571 return rc;
1515 } 1572 }
1516 1573
1517 /* allocate + initialize per filehandle data */ 1574 /* allocate + initialize per filehandle data */
@@ -1519,6 +1576,7 @@ static int s2255_open(struct inode *inode, struct file *file)
1519 if (NULL == fh) { 1576 if (NULL == fh) {
1520 dev->users[cur_channel]--; 1577 dev->users[cur_channel]--;
1521 mutex_unlock(&dev->open_lock); 1578 mutex_unlock(&dev->open_lock);
1579 kref_put(&dev->kref, s2255_destroy);
1522 unlock_kernel(); 1580 unlock_kernel();
1523 return -ENOMEM; 1581 return -ENOMEM;
1524 } 1582 }
@@ -1533,6 +1591,13 @@ static int s2255_open(struct inode *inode, struct file *file)
1533 fh->height = NUM_LINES_4CIFS_NTSC * 2; 1591 fh->height = NUM_LINES_4CIFS_NTSC * 2;
1534 fh->channel = cur_channel; 1592 fh->channel = cur_channel;
1535 1593
1594 /* configure channel to default state */
1595 if (!dev->chn_configured[cur_channel]) {
1596 s2255_set_mode(dev, cur_channel, &fh->mode);
1597 dev->chn_configured[cur_channel] = 1;
1598 }
1599
1600
1536 /* Put all controls at a sane state */ 1601 /* Put all controls at a sane state */
1537 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++) 1602 for (i = 0; i < ARRAY_SIZE(s2255_qctrl); i++)
1538 qctl_regs[i] = s2255_qctrl[i].default_value; 1603 qctl_regs[i] = s2255_qctrl[i].default_value;
@@ -1551,7 +1616,6 @@ static int s2255_open(struct inode *inode, struct file *file)
1551 V4L2_FIELD_INTERLACED, 1616 V4L2_FIELD_INTERLACED,
1552 sizeof(struct s2255_buffer), fh); 1617 sizeof(struct s2255_buffer), fh);
1553 1618
1554 kref_get(&dev->kref);
1555 mutex_unlock(&dev->open_lock); 1619 mutex_unlock(&dev->open_lock);
1556 unlock_kernel(); 1620 unlock_kernel();
1557 return 0; 1621 return 0;
@@ -1575,30 +1639,24 @@ static unsigned int s2255_poll(struct file *file,
1575static void s2255_destroy(struct kref *kref) 1639static void s2255_destroy(struct kref *kref)
1576{ 1640{
1577 struct s2255_dev *dev = to_s2255_dev(kref); 1641 struct s2255_dev *dev = to_s2255_dev(kref);
1642 struct list_head *list;
1643 int i;
1578 if (!dev) { 1644 if (!dev) {
1579 printk(KERN_ERR "s2255drv: kref problem\n"); 1645 printk(KERN_ERR "s2255drv: kref problem\n");
1580 return; 1646 return;
1581 } 1647 }
1582
1583 /*
1584 * Wake up any firmware load waiting (only done in .open,
1585 * which holds the open_lock mutex)
1586 */
1587 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING); 1648 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
1588 wake_up(&dev->fw_data->wait_fw); 1649 wake_up(&dev->fw_data->wait_fw);
1589 1650 for (i = 0; i < MAX_CHANNELS; i++) {
1590 /* prevent s2255_disconnect from racing s2255_open */ 1651 dev->setmode_ready[i] = 1;
1652 wake_up(&dev->wait_setmode[i]);
1653 }
1591 mutex_lock(&dev->open_lock); 1654 mutex_lock(&dev->open_lock);
1655 /* reset the DSP so firmware can be reload next time */
1656 s2255_reset_dsppower(dev);
1592 s2255_exit_v4l(dev); 1657 s2255_exit_v4l(dev);
1593 /*
1594 * device unregistered so no longer possible to open. open_mutex
1595 * can be unlocked and timers deleted afterwards.
1596 */
1597 mutex_unlock(&dev->open_lock);
1598
1599 /* board shutdown stops the read pipe if it is running */ 1658 /* board shutdown stops the read pipe if it is running */
1600 s2255_board_shutdown(dev); 1659 s2255_board_shutdown(dev);
1601
1602 /* make sure firmware still not trying to load */ 1660 /* make sure firmware still not trying to load */
1603 del_timer(&dev->timer); /* only started in .probe and .open */ 1661 del_timer(&dev->timer); /* only started in .probe and .open */
1604 1662
@@ -1608,23 +1666,19 @@ static void s2255_destroy(struct kref *kref)
1608 usb_free_urb(dev->fw_data->fw_urb); 1666 usb_free_urb(dev->fw_data->fw_urb);
1609 dev->fw_data->fw_urb = NULL; 1667 dev->fw_data->fw_urb = NULL;
1610 } 1668 }
1611
1612 /*
1613 * delete the dsp_wait timer, which sets the firmware
1614 * state on completion. This is done before fw_data
1615 * is freed below.
1616 */
1617
1618 del_timer(&dev->fw_data->dsp_wait); /* only started in .open */
1619
1620 if (dev->fw_data->fw) 1669 if (dev->fw_data->fw)
1621 release_firmware(dev->fw_data->fw); 1670 release_firmware(dev->fw_data->fw);
1622 kfree(dev->fw_data->pfw_data); 1671 kfree(dev->fw_data->pfw_data);
1623 kfree(dev->fw_data); 1672 kfree(dev->fw_data);
1624
1625 usb_put_dev(dev->udev); 1673 usb_put_dev(dev->udev);
1626 dprintk(1, "%s", __func__); 1674 dprintk(1, "%s", __func__);
1627 kfree(dev); 1675 kfree(dev);
1676
1677 while (!list_empty(&s2255_devlist)) {
1678 list = s2255_devlist.next;
1679 list_del(list);
1680 }
1681 mutex_unlock(&dev->open_lock);
1628} 1682}
1629 1683
1630static int s2255_close(struct inode *inode, struct file *file) 1684static int s2255_close(struct inode *inode, struct file *file)
@@ -1760,18 +1814,16 @@ static int s2255_probe_v4l(struct s2255_dev *dev)
1760 1814
1761static void s2255_exit_v4l(struct s2255_dev *dev) 1815static void s2255_exit_v4l(struct s2255_dev *dev)
1762{ 1816{
1763 struct list_head *list; 1817
1764 int i; 1818 int i;
1765 /* unregister the video devices */
1766 while (!list_empty(&s2255_devlist)) {
1767 list = s2255_devlist.next;
1768 list_del(list);
1769 }
1770 for (i = 0; i < MAX_CHANNELS; i++) { 1819 for (i = 0; i < MAX_CHANNELS; i++) {
1771 if (-1 != dev->vdev[i]->minor) 1820 if (-1 != dev->vdev[i]->minor) {
1772 video_unregister_device(dev->vdev[i]); 1821 video_unregister_device(dev->vdev[i]);
1773 else 1822 printk(KERN_INFO "s2255 unregistered\n");
1823 } else {
1774 video_device_release(dev->vdev[i]); 1824 video_device_release(dev->vdev[i]);
1825 printk(KERN_INFO "s2255 released\n");
1826 }
1775 } 1827 }
1776} 1828}
1777 1829
@@ -1781,134 +1833,123 @@ static void s2255_exit_v4l(struct s2255_dev *dev)
1781 * function again). 1833 * function again).
1782 * 1834 *
1783 * Received frame structure: 1835 * Received frame structure:
1784 * bytes 0-3: marker : 0x2255DA4AL (FRAME_MARKER) 1836 * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME)
1785 * bytes 4-7: channel: 0-3 1837 * bytes 4-7: channel: 0-3
1786 * bytes 8-11: payload size: size of the frame 1838 * bytes 8-11: payload size: size of the frame
1787 * bytes 12-payloadsize+12: frame data 1839 * bytes 12-payloadsize+12: frame data
1788 */ 1840 */
1789static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info) 1841static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1790{ 1842{
1791 static int dbgsync; /* = 0; */
1792 char *pdest; 1843 char *pdest;
1793 u32 offset = 0; 1844 u32 offset = 0;
1794 int bsync = 0; 1845 int bframe = 0;
1795 int btrunc = 0;
1796 char *psrc; 1846 char *psrc;
1797 unsigned long copy_size; 1847 unsigned long copy_size;
1798 unsigned long size; 1848 unsigned long size;
1799 s32 idx = -1; 1849 s32 idx = -1;
1800 struct s2255_framei *frm; 1850 struct s2255_framei *frm;
1801 unsigned char *pdata; 1851 unsigned char *pdata;
1802 unsigned long cur_size; 1852
1803 int bsearch = 0;
1804 struct s2255_bufferi *buf;
1805 dprintk(100, "buffer to user\n"); 1853 dprintk(100, "buffer to user\n");
1806 1854
1807 idx = dev->cur_frame[dev->cc]; 1855 idx = dev->cur_frame[dev->cc];
1808 buf = &dev->buffer[dev->cc]; 1856 frm = &dev->buffer[dev->cc].frame[idx];
1809 frm = &buf->frame[idx];
1810
1811 if (frm->ulState == 0) {
1812 frm->ulState = 1;
1813 frm->cur_size = 0;
1814 bsearch = 1;
1815 } else if (frm->ulState == 2) {
1816 /* system frame was not freed */
1817 dprintk(2, "sys frame not free. overrun ringbuf\n");
1818 bsearch = 1;
1819 frm->ulState = 1;
1820 frm->cur_size = 0;
1821 }
1822
1823 if (bsearch) {
1824 if (*(s32 *) pipe_info->transfer_buffer != FRAME_MARKER) {
1825 u32 jj;
1826 if (dbgsync == 0) {
1827 dprintk(3, "not synched, discarding all packets"
1828 "until marker\n");
1829 1857
1830 dbgsync++; 1858 if (frm->ulState == S2255_READ_IDLE) {
1831 } 1859 int jj;
1832 pdata = (unsigned char *)pipe_info->transfer_buffer; 1860 unsigned int cc;
1833 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); 1861 s32 *pdword;
1834 jj++) { 1862 int payload;
1835 if (*(s32 *) pdata == FRAME_MARKER) { 1863 /* search for marker codes */
1836 int cc; 1864 pdata = (unsigned char *)pipe_info->transfer_buffer;
1837 dprintk(3, 1865 for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) {
1838 "found frame marker at offset:" 1866 switch (*(s32 *) pdata) {
1839 " %d [%x %x]\n", jj, pdata[0], 1867 case S2255_MARKER_FRAME:
1840 pdata[1]); 1868 pdword = (s32 *)pdata;
1841 offset = jj; 1869 dprintk(4, "found frame marker at offset:"
1842 bsync = 1; 1870 " %d [%x %x]\n", jj, pdata[0],
1843 cc = *(u32 *) (pdata + sizeof(u32)); 1871 pdata[1]);
1844 if (cc >= MAX_CHANNELS) { 1872 offset = jj + PREFIX_SIZE;
1845 printk(KERN_ERR 1873 bframe = 1;
1846 "bad channel\n"); 1874 cc = pdword[1];
1847 return -EINVAL; 1875 if (cc >= MAX_CHANNELS) {
1848 } 1876 printk(KERN_ERR
1849 /* reverse it */ 1877 "bad channel\n");
1850 dev->cc = G_chnmap[cc]; 1878 return -EINVAL;
1879 }
1880 /* reverse it */
1881 dev->cc = G_chnmap[cc];
1882 payload = pdword[3];
1883 if (payload > dev->req_image_size[dev->cc]) {
1884 dev->bad_payload[dev->cc]++;
1885 /* discard the bad frame */
1886 return -EINVAL;
1887 }
1888 dev->pkt_size[dev->cc] = payload;
1889 dev->jpg_size[dev->cc] = pdword[4];
1890 break;
1891 case S2255_MARKER_RESPONSE:
1892 pdword = (s32 *)pdata;
1893 pdata += DEF_USB_BLOCK;
1894 jj += DEF_USB_BLOCK;
1895 if (pdword[1] >= MAX_CHANNELS)
1896 break;
1897 cc = G_chnmap[pdword[1]];
1898 if (!(cc >= 0 && cc < MAX_CHANNELS))
1899 break;
1900 switch (pdword[2]) {
1901 case 0x01:
1902 /* check if channel valid */
1903 /* set mode ready */
1904 dev->setmode_ready[cc] = 1;
1905 wake_up(&dev->wait_setmode[cc]);
1906 dprintk(5, "setmode ready %d\n", cc);
1851 break; 1907 break;
1908 case 0x10:
1909
1910 dev->chn_ready |= (1 << cc);
1911 if ((dev->chn_ready & 0x0f) != 0x0f)
1912 break;
1913 /* all channels ready */
1914 printk(KERN_INFO "s2255: fw loaded\n");
1915 atomic_set(&dev->fw_data->fw_state,
1916 S2255_FW_SUCCESS);
1917 wake_up(&dev->fw_data->wait_fw);
1918 break;
1919 default:
1920 printk(KERN_INFO "s2255 unknwn resp\n");
1852 } 1921 }
1922 default:
1853 pdata++; 1923 pdata++;
1924 break;
1854 } 1925 }
1855 if (bsync == 0) 1926 if (bframe)
1856 return -EINVAL; 1927 break;
1857 } else { 1928 } /* for */
1858 u32 *pword; 1929 if (!bframe)
1859 u32 payload; 1930 return -EINVAL;
1860 int cc;
1861 dbgsync = 0;
1862 bsync = 1;
1863 pword = (u32 *) pipe_info->transfer_buffer;
1864 cc = pword[1];
1865
1866 if (cc >= MAX_CHANNELS) {
1867 printk("invalid channel found. "
1868 "throwing out data!\n");
1869 return -EINVAL;
1870 }
1871 dev->cc = G_chnmap[cc];
1872 payload = pword[2];
1873 if (payload != dev->req_image_size[dev->cc]) {
1874 dprintk(1, "[%d][%d]unexpected payload: %d"
1875 "required: %lu \n", cc, dev->cc,
1876 payload, dev->req_image_size[dev->cc]);
1877 dev->bad_payload[dev->cc]++;
1878 /* discard the bad frame */
1879 return -EINVAL;
1880 }
1881
1882 }
1883 }
1884 /* search done. now find out if should be acquiring
1885 on this channel */
1886 if (!dev->b_acquire[dev->cc]) {
1887 frm->ulState = 0;
1888 return -EINVAL;
1889 } 1931 }
1890 1932
1933
1891 idx = dev->cur_frame[dev->cc]; 1934 idx = dev->cur_frame[dev->cc];
1892 frm = &dev->buffer[dev->cc].frame[idx]; 1935 frm = &dev->buffer[dev->cc].frame[idx];
1893 1936
1894 if (frm->ulState == 0) { 1937 /* search done. now find out if should be acquiring on this channel */
1895 frm->ulState = 1; 1938 if (!dev->b_acquire[dev->cc]) {
1896 frm->cur_size = 0; 1939 /* we found a frame, but this channel is turned off */
1897 } else if (frm->ulState == 2) { 1940 frm->ulState = S2255_READ_IDLE;
1898 /* system frame ring buffer overrun */ 1941 return -EINVAL;
1899 dprintk(2, "sys frame overrun. overwriting frame %d %d\n",
1900 dev->cc, idx);
1901 frm->ulState = 1;
1902 frm->cur_size = 0;
1903 } 1942 }
1904 1943
1905 if (bsync) { 1944 if (frm->ulState == S2255_READ_IDLE) {
1906 /* skip the marker 512 bytes (and offset if out of sync) */ 1945 frm->ulState = S2255_READ_FRAME;
1907 psrc = (u8 *)pipe_info->transfer_buffer + offset + PREFIX_SIZE; 1946 frm->cur_size = 0;
1908 } else {
1909 psrc = (u8 *)pipe_info->transfer_buffer;
1910 } 1947 }
1911 1948
1949 /* skip the marker 512 bytes (and offset if out of sync) */
1950 psrc = (u8 *)pipe_info->transfer_buffer + offset;
1951
1952
1912 if (frm->lpvbits == NULL) { 1953 if (frm->lpvbits == NULL) {
1913 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d", 1954 dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d",
1914 frm, dev, dev->cc, idx); 1955 frm, dev, dev->cc, idx);
@@ -1917,33 +1958,20 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1917 1958
1918 pdest = frm->lpvbits + frm->cur_size; 1959 pdest = frm->lpvbits + frm->cur_size;
1919 1960
1920 if (bsync) { 1961 copy_size = (pipe_info->cur_transfer_size - offset);
1921 copy_size =
1922 (pipe_info->cur_transfer_size - offset) - PREFIX_SIZE;
1923 if (copy_size > pipe_info->cur_transfer_size) {
1924 printk("invalid copy size, overflow!\n");
1925 return -ENOMEM;
1926 }
1927 } else {
1928 copy_size = pipe_info->cur_transfer_size;
1929 }
1930 1962
1931 cur_size = frm->cur_size; 1963 size = dev->pkt_size[dev->cc] - PREFIX_SIZE;
1932 size = dev->req_image_size[dev->cc];
1933 1964
1934 if ((copy_size + cur_size) > size) { 1965 /* sanity check on pdest */
1935 copy_size = size - cur_size; 1966 if ((copy_size + frm->cur_size) < dev->req_image_size[dev->cc])
1936 btrunc = 1; 1967 memcpy(pdest, psrc, copy_size);
1937 }
1938 1968
1939 memcpy(pdest, psrc, copy_size);
1940 cur_size += copy_size;
1941 frm->cur_size += copy_size; 1969 frm->cur_size += copy_size;
1942 dprintk(50, "cur_size size %lu size %lu \n", cur_size, size); 1970 dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size);
1971
1972 if (frm->cur_size >= size) {
1943 1973
1944 if (cur_size >= (size - PREFIX_SIZE)) {
1945 u32 cc = dev->cc; 1974 u32 cc = dev->cc;
1946 frm->ulState = 2;
1947 dprintk(2, "****************[%d]Buffer[%d]full*************\n", 1975 dprintk(2, "****************[%d]Buffer[%d]full*************\n",
1948 cc, idx); 1976 cc, idx);
1949 dev->last_frame[cc] = dev->cur_frame[cc]; 1977 dev->last_frame[cc] = dev->cur_frame[cc];
@@ -1952,16 +1980,13 @@ static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info)
1952 if ((dev->cur_frame[cc] == SYS_FRAMES) || 1980 if ((dev->cur_frame[cc] == SYS_FRAMES) ||
1953 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames)) 1981 (dev->cur_frame[cc] == dev->buffer[cc].dwFrames))
1954 dev->cur_frame[cc] = 0; 1982 dev->cur_frame[cc] = 0;
1955 1983 /* frame ready */
1956 /* signal the semaphore for this channel */
1957 if (dev->b_acquire[cc]) 1984 if (dev->b_acquire[cc])
1958 s2255_got_frame(dev, cc); 1985 s2255_got_frame(dev, cc, dev->jpg_size[cc]);
1959 dev->frame_count[cc]++; 1986 dev->frame_count[cc]++;
1960 } 1987 frm->ulState = S2255_READ_IDLE;
1961 /* frame was truncated */ 1988 frm->cur_size = 0;
1962 if (btrunc) { 1989
1963 /* return more data to process */
1964 return EAGAIN;
1965 } 1990 }
1966 /* done successfully */ 1991 /* done successfully */
1967 return 0; 1992 return 0;
@@ -1980,8 +2005,8 @@ static void s2255_read_video_callback(struct s2255_dev *dev,
1980 } 2005 }
1981 /* otherwise copy to the system buffers */ 2006 /* otherwise copy to the system buffers */
1982 res = save_frame(dev, pipe_info); 2007 res = save_frame(dev, pipe_info);
1983 if (res == EAGAIN) 2008 if (res != 0)
1984 save_frame(dev, pipe_info); 2009 dprintk(4, "s2255: read callback failed\n");
1985 2010
1986 dprintk(50, "callback read video done\n"); 2011 dprintk(50, "callback read video done\n");
1987 return; 2012 return;
@@ -2101,11 +2126,9 @@ static int s2255_board_init(struct s2255_dev *dev)
2101 2126
2102 memset(pipe, 0, sizeof(*pipe)); 2127 memset(pipe, 0, sizeof(*pipe));
2103 pipe->dev = dev; 2128 pipe->dev = dev;
2104 pipe->cur_transfer_size = DEFAULT_PIPE_USBBLOCK; 2129 pipe->cur_transfer_size = S2255_USB_XFER_SIZE;
2105 pipe->max_transfer_size = MAX_PIPE_USBBLOCK; 2130 pipe->max_transfer_size = S2255_USB_XFER_SIZE;
2106 2131
2107 if (pipe->cur_transfer_size > pipe->max_transfer_size)
2108 pipe->cur_transfer_size = pipe->max_transfer_size;
2109 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size, 2132 pipe->transfer_buffer = kzalloc(pipe->max_transfer_size,
2110 GFP_KERNEL); 2133 GFP_KERNEL);
2111 if (pipe->transfer_buffer == NULL) { 2134 if (pipe->transfer_buffer == NULL) {
@@ -2329,7 +2352,7 @@ static int s2255_stop_acquire(struct s2255_dev *dev, unsigned long chn)
2329 kfree(buffer); 2352 kfree(buffer);
2330 dev->b_acquire[chn] = 0; 2353 dev->b_acquire[chn] = 0;
2331 2354
2332 return 0; 2355 return res;
2333} 2356}
2334 2357
2335static void s2255_stop_readpipe(struct s2255_dev *dev) 2358static void s2255_stop_readpipe(struct s2255_dev *dev)
@@ -2365,8 +2388,10 @@ static void s2255_stop_readpipe(struct s2255_dev *dev)
2365 return; 2388 return;
2366} 2389}
2367 2390
2368static void s2255_fwload_start(struct s2255_dev *dev) 2391static void s2255_fwload_start(struct s2255_dev *dev, int reset)
2369{ 2392{
2393 if (reset)
2394 s2255_reset_dsppower(dev);
2370 dev->fw_data->fw_size = dev->fw_data->fw->size; 2395 dev->fw_data->fw_size = dev->fw_data->fw->size;
2371 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED); 2396 atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED);
2372 memcpy(dev->fw_data->pfw_data, 2397 memcpy(dev->fw_data->pfw_data,
@@ -2389,6 +2414,8 @@ static int s2255_probe(struct usb_interface *interface,
2389 struct usb_endpoint_descriptor *endpoint; 2414 struct usb_endpoint_descriptor *endpoint;
2390 int i; 2415 int i;
2391 int retval = -ENOMEM; 2416 int retval = -ENOMEM;
2417 __le32 *pdata;
2418 int fw_size;
2392 2419
2393 dprintk(2, "s2255: probe\n"); 2420 dprintk(2, "s2255: probe\n");
2394 2421
@@ -2443,6 +2470,8 @@ static int s2255_probe(struct usb_interface *interface,
2443 dev->timer.data = (unsigned long)dev->fw_data; 2470 dev->timer.data = (unsigned long)dev->fw_data;
2444 2471
2445 init_waitqueue_head(&dev->fw_data->wait_fw); 2472 init_waitqueue_head(&dev->fw_data->wait_fw);
2473 for (i = 0; i < MAX_CHANNELS; i++)
2474 init_waitqueue_head(&dev->wait_setmode[i]);
2446 2475
2447 2476
2448 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL); 2477 dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL);
@@ -2462,16 +2491,30 @@ static int s2255_probe(struct usb_interface *interface,
2462 printk(KERN_ERR "sensoray 2255 failed to get firmware\n"); 2491 printk(KERN_ERR "sensoray 2255 failed to get firmware\n");
2463 goto error; 2492 goto error;
2464 } 2493 }
2494 /* check the firmware is valid */
2495 fw_size = dev->fw_data->fw->size;
2496 pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8];
2465 2497
2498 if (*pdata != S2255_FW_MARKER) {
2499 printk(KERN_INFO "Firmware invalid.\n");
2500 retval = -ENODEV;
2501 goto error;
2502 } else {
2503 /* make sure firmware is the latest */
2504 __le32 *pRel;
2505 pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4];
2506 printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel);
2507 }
2466 /* loads v4l specific */ 2508 /* loads v4l specific */
2467 s2255_probe_v4l(dev); 2509 s2255_probe_v4l(dev);
2510 usb_reset_device(dev->udev);
2468 /* load 2255 board specific */ 2511 /* load 2255 board specific */
2469 s2255_board_init(dev); 2512 s2255_board_init(dev);
2470 2513
2471 dprintk(4, "before probe done %p\n", dev); 2514 dprintk(4, "before probe done %p\n", dev);
2472 spin_lock_init(&dev->slock); 2515 spin_lock_init(&dev->slock);
2473 2516
2474 s2255_fwload_start(dev); 2517 s2255_fwload_start(dev, 0);
2475 dev_info(&interface->dev, "Sensoray 2255 detected\n"); 2518 dev_info(&interface->dev, "Sensoray 2255 detected\n");
2476 return 0; 2519 return 0;
2477error: 2520error:
@@ -2482,14 +2525,30 @@ error:
2482static void s2255_disconnect(struct usb_interface *interface) 2525static void s2255_disconnect(struct usb_interface *interface)
2483{ 2526{
2484 struct s2255_dev *dev = NULL; 2527 struct s2255_dev *dev = NULL;
2528 int i;
2485 dprintk(1, "s2255: disconnect interface %p\n", interface); 2529 dprintk(1, "s2255: disconnect interface %p\n", interface);
2486 dev = usb_get_intfdata(interface); 2530 dev = usb_get_intfdata(interface);
2531
2532 /*
2533 * wake up any of the timers to allow open_lock to be
2534 * acquired sooner
2535 */
2536 atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING);
2537 wake_up(&dev->fw_data->wait_fw);
2538 for (i = 0; i < MAX_CHANNELS; i++) {
2539 dev->setmode_ready[i] = 1;
2540 wake_up(&dev->wait_setmode[i]);
2541 }
2542
2543 mutex_lock(&dev->open_lock);
2544 usb_set_intfdata(interface, NULL);
2545 mutex_unlock(&dev->open_lock);
2546
2487 if (dev) { 2547 if (dev) {
2488 kref_put(&dev->kref, s2255_destroy); 2548 kref_put(&dev->kref, s2255_destroy);
2489 dprintk(1, "s2255drv: disconnect\n"); 2549 dprintk(1, "s2255drv: disconnect\n");
2490 dev_info(&interface->dev, "s2255usb now disconnected\n"); 2550 dev_info(&interface->dev, "s2255usb now disconnected\n");
2491 } 2551 }
2492 usb_set_intfdata(interface, NULL);
2493} 2552}
2494 2553
2495static struct usb_driver s2255_driver = { 2554static struct usb_driver s2255_driver = {