aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorJeff Garzik <jgarzik@pobox.com>2006-02-02 01:12:54 -0500
committerJeff Garzik <jgarzik@pobox.com>2006-02-02 01:12:54 -0500
commit18ee3610040a4c008ce08a40a5dd025241cc7e97 (patch)
tree32a996a5123726b63c31a1522f230933fb967a32 /drivers/usb
parente4e7b89280d1d666e2c09e5ad36cf071796c4c7e (diff)
parentb4103333d7904310d34de18d85e51e3d74f00a3b (diff)
Merge branch 'master'
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Makefile1
-rw-r--r--drivers/usb/atm/cxacru.c92
-rw-r--r--drivers/usb/atm/speedtch.c167
-rw-r--r--drivers/usb/atm/ueagle-atm.c96
-rw-r--r--drivers/usb/atm/usbatm.c574
-rw-r--r--drivers/usb/atm/usbatm.h59
-rw-r--r--drivers/usb/atm/xusbatm.c137
-rw-r--r--drivers/usb/class/cdc-acm.c9
-rw-r--r--drivers/usb/class/usblp.c71
-rw-r--r--drivers/usb/core/message.c1
-rw-r--r--drivers/usb/core/urb.c1
-rw-r--r--drivers/usb/gadget/inode.c8
-rw-r--r--drivers/usb/gadget/net2280.c1
-rw-r--r--drivers/usb/gadget/zero.c8
-rw-r--r--drivers/usb/host/ehci-pci.c64
-rw-r--r--drivers/usb/host/ehci-sched.c4
-rw-r--r--drivers/usb/host/isp116x-hcd.c21
-rw-r--r--drivers/usb/host/ohci-au1xxx.c2
-rw-r--r--drivers/usb/host/pci-quirks.c106
-rw-r--r--drivers/usb/host/uhci-q.c4
-rw-r--r--drivers/usb/input/hid-core.c9
-rw-r--r--drivers/usb/input/hiddev.c5
-rw-r--r--drivers/usb/input/touchkitusb.c3
-rw-r--r--drivers/usb/input/yealink.c48
-rw-r--r--drivers/usb/media/Kconfig17
-rw-r--r--drivers/usb/media/Makefile2
-rw-r--r--drivers/usb/media/et61x251.h220
-rw-r--r--drivers/usb/media/et61x251_core.c2605
-rw-r--r--drivers/usb/media/et61x251_sensor.h115
-rw-r--r--drivers/usb/media/et61x251_tas5130d1b.c137
-rw-r--r--drivers/usb/media/ov511.c196
-rw-r--r--drivers/usb/media/pwc/pwc-ctrl.c264
-rw-r--r--drivers/usb/media/sn9c102.h50
-rw-r--r--drivers/usb/media/sn9c102_core.c1681
-rw-r--r--drivers/usb/media/sn9c102_hv7131d.c2
-rw-r--r--drivers/usb/media/sn9c102_mi0343.c2
-rw-r--r--drivers/usb/media/sn9c102_ov7630.c8
-rw-r--r--drivers/usb/media/sn9c102_pas106b.c2
-rw-r--r--drivers/usb/media/sn9c102_sensor.h85
-rw-r--r--drivers/usb/media/sn9c102_tas5110c1b.c2
-rw-r--r--drivers/usb/media/sn9c102_tas5130d1b.c2
-rw-r--r--drivers/usb/media/w9968cf.c128
-rw-r--r--drivers/usb/media/w9968cf.h1
-rw-r--r--drivers/usb/media/w9968cf_vpp.h3
-rw-r--r--drivers/usb/misc/auerswald.c2
-rw-r--r--drivers/usb/misc/ldusb.c2
-rw-r--r--drivers/usb/net/asix.c4
-rw-r--r--drivers/usb/serial/cp2101.c14
-rw-r--r--drivers/usb/serial/ftdi_sio.c6
-rw-r--r--drivers/usb/serial/ftdi_sio.h23
-rw-r--r--drivers/usb/serial/pl2303.c4
-rw-r--r--drivers/usb/serial/pl2303.h7
-rw-r--r--drivers/usb/storage/initializers.c73
-rw-r--r--drivers/usb/storage/initializers.h1
-rw-r--r--drivers/usb/storage/libusual.c2
-rw-r--r--drivers/usb/storage/unusual_devs.h15
-rw-r--r--drivers/usb/usb-skeleton.c2
57 files changed, 5196 insertions, 1972 deletions
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 3639c3f8d357..36e476dd9123 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_USB_XPAD) += input/
38 38
39obj-$(CONFIG_USB_DABUSB) += media/ 39obj-$(CONFIG_USB_DABUSB) += media/
40obj-$(CONFIG_USB_DSBR) += media/ 40obj-$(CONFIG_USB_DSBR) += media/
41obj-$(CONFIG_USB_ET61X251) += media/
41obj-$(CONFIG_USB_IBMCAM) += media/ 42obj-$(CONFIG_USB_IBMCAM) += media/
42obj-$(CONFIG_USB_KONICAWC) += media/ 43obj-$(CONFIG_USB_KONICAWC) += media/
43obj-$(CONFIG_USB_OV511) += media/ 44obj-$(CONFIG_USB_OV511) += media/
diff --git a/drivers/usb/atm/cxacru.c b/drivers/usb/atm/cxacru.c
index af0a41e7870e..04631dcbabbc 100644
--- a/drivers/usb/atm/cxacru.c
+++ b/drivers/usb/atm/cxacru.c
@@ -36,6 +36,7 @@
36#include <linux/init.h> 36#include <linux/init.h>
37#include <linux/device.h> /* FIXME: linux/firmware.h should include it itself */ 37#include <linux/device.h> /* FIXME: linux/firmware.h should include it itself */
38#include <linux/firmware.h> 38#include <linux/firmware.h>
39#include <linux/mutex.h>
39 40
40#include "usbatm.h" 41#include "usbatm.h"
41 42
@@ -160,7 +161,7 @@ struct cxacru_data {
160 struct work_struct poll_work; 161 struct work_struct poll_work;
161 162
162 /* contol handles */ 163 /* contol handles */
163 struct semaphore cm_serialize; 164 struct mutex cm_serialize;
164 u8 *rcv_buf; 165 u8 *rcv_buf;
165 u8 *snd_buf; 166 u8 *snd_buf;
166 struct urb *rcv_urb; 167 struct urb *rcv_urb;
@@ -219,7 +220,7 @@ static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
219 goto fail; 220 goto fail;
220 } 221 }
221 222
222 down(&instance->cm_serialize); 223 mutex_lock(&instance->cm_serialize);
223 224
224 /* submit reading urb before the writing one */ 225 /* submit reading urb before the writing one */
225 init_completion(&instance->rcv_done); 226 init_completion(&instance->rcv_done);
@@ -288,7 +289,7 @@ static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
288 ret = offd; 289 ret = offd;
289 dbg("cm %#x", cm); 290 dbg("cm %#x", cm);
290fail: 291fail:
291 up(&instance->cm_serialize); 292 mutex_unlock(&instance->cm_serialize);
292 return ret; 293 return ret;
293} 294}
294 295
@@ -352,7 +353,6 @@ static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
352 struct atm_dev *atm_dev) 353 struct atm_dev *atm_dev)
353{ 354{
354 struct cxacru_data *instance = usbatm_instance->driver_data; 355 struct cxacru_data *instance = usbatm_instance->driver_data;
355 struct device *dev = &usbatm_instance->usb_intf->dev;
356 /* 356 /*
357 struct atm_dev *atm_dev = usbatm_instance->atm_dev; 357 struct atm_dev *atm_dev = usbatm_instance->atm_dev;
358 */ 358 */
@@ -364,14 +364,14 @@ static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
364 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0, 364 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
365 atm_dev->esi, sizeof(atm_dev->esi)); 365 atm_dev->esi, sizeof(atm_dev->esi));
366 if (ret < 0) { 366 if (ret < 0) {
367 dev_err(dev, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret); 367 atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
368 return ret; 368 return ret;
369 } 369 }
370 370
371 /* start ADSL */ 371 /* start ADSL */
372 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0); 372 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
373 if (ret < 0) { 373 if (ret < 0) {
374 dev_err(dev, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret); 374 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
375 return ret; 375 return ret;
376 } 376 }
377 377
@@ -383,13 +383,13 @@ static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
383static void cxacru_poll_status(struct cxacru_data *instance) 383static void cxacru_poll_status(struct cxacru_data *instance)
384{ 384{
385 u32 buf[CXINF_MAX] = {}; 385 u32 buf[CXINF_MAX] = {};
386 struct device *dev = &instance->usbatm->usb_intf->dev; 386 struct usbatm_data *usbatm = instance->usbatm;
387 struct atm_dev *atm_dev = instance->usbatm->atm_dev; 387 struct atm_dev *atm_dev = usbatm->atm_dev;
388 int ret; 388 int ret;
389 389
390 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX); 390 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
391 if (ret < 0) { 391 if (ret < 0) {
392 dev_warn(dev, "poll status: error %d\n", ret); 392 atm_warn(usbatm, "poll status: error %d\n", ret);
393 goto reschedule; 393 goto reschedule;
394 } 394 }
395 395
@@ -400,50 +400,50 @@ static void cxacru_poll_status(struct cxacru_data *instance)
400 switch (instance->line_status) { 400 switch (instance->line_status) {
401 case 0: 401 case 0:
402 atm_dev->signal = ATM_PHY_SIG_LOST; 402 atm_dev->signal = ATM_PHY_SIG_LOST;
403 dev_info(dev, "ADSL line: down\n"); 403 atm_info(usbatm, "ADSL line: down\n");
404 break; 404 break;
405 405
406 case 1: 406 case 1:
407 atm_dev->signal = ATM_PHY_SIG_LOST; 407 atm_dev->signal = ATM_PHY_SIG_LOST;
408 dev_info(dev, "ADSL line: attemtping to activate\n"); 408 atm_info(usbatm, "ADSL line: attempting to activate\n");
409 break; 409 break;
410 410
411 case 2: 411 case 2:
412 atm_dev->signal = ATM_PHY_SIG_LOST; 412 atm_dev->signal = ATM_PHY_SIG_LOST;
413 dev_info(dev, "ADSL line: training\n"); 413 atm_info(usbatm, "ADSL line: training\n");
414 break; 414 break;
415 415
416 case 3: 416 case 3:
417 atm_dev->signal = ATM_PHY_SIG_LOST; 417 atm_dev->signal = ATM_PHY_SIG_LOST;
418 dev_info(dev, "ADSL line: channel analysis\n"); 418 atm_info(usbatm, "ADSL line: channel analysis\n");
419 break; 419 break;
420 420
421 case 4: 421 case 4:
422 atm_dev->signal = ATM_PHY_SIG_LOST; 422 atm_dev->signal = ATM_PHY_SIG_LOST;
423 dev_info(dev, "ADSL line: exchange\n"); 423 atm_info(usbatm, "ADSL line: exchange\n");
424 break; 424 break;
425 425
426 case 5: 426 case 5:
427 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424; 427 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
428 atm_dev->signal = ATM_PHY_SIG_FOUND; 428 atm_dev->signal = ATM_PHY_SIG_FOUND;
429 429
430 dev_info(dev, "ADSL line: up (%d kb/s down | %d kb/s up)\n", 430 atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
431 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]); 431 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
432 break; 432 break;
433 433
434 case 6: 434 case 6:
435 atm_dev->signal = ATM_PHY_SIG_LOST; 435 atm_dev->signal = ATM_PHY_SIG_LOST;
436 dev_info(dev, "ADSL line: waiting\n"); 436 atm_info(usbatm, "ADSL line: waiting\n");
437 break; 437 break;
438 438
439 case 7: 439 case 7:
440 atm_dev->signal = ATM_PHY_SIG_LOST; 440 atm_dev->signal = ATM_PHY_SIG_LOST;
441 dev_info(dev, "ADSL line: initializing\n"); 441 atm_info(usbatm, "ADSL line: initializing\n");
442 break; 442 break;
443 443
444 default: 444 default:
445 atm_dev->signal = ATM_PHY_SIG_UNKNOWN; 445 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
446 dev_info(dev, "Unknown line state %02x\n", instance->line_status); 446 atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
447 break; 447 break;
448 } 448 }
449reschedule: 449reschedule:
@@ -504,8 +504,8 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
504{ 504{
505 int ret; 505 int ret;
506 int off; 506 int off;
507 struct usb_device *usb_dev = instance->usbatm->usb_dev; 507 struct usbatm_data *usbatm = instance->usbatm;
508 struct device *dev = &instance->usbatm->usb_intf->dev; 508 struct usb_device *usb_dev = usbatm->usb_dev;
509 u16 signature[] = { usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct }; 509 u16 signature[] = { usb_dev->descriptor.idVendor, usb_dev->descriptor.idProduct };
510 u32 val; 510 u32 val;
511 511
@@ -515,7 +515,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
515 val = cpu_to_le32(instance->modem_type->pll_f_clk); 515 val = cpu_to_le32(instance->modem_type->pll_f_clk);
516 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4); 516 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
517 if (ret) { 517 if (ret) {
518 dev_err(dev, "FirmwarePllFClkValue failed: %d\n", ret); 518 usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
519 return; 519 return;
520 } 520 }
521 521
@@ -523,7 +523,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
523 val = cpu_to_le32(instance->modem_type->pll_b_clk); 523 val = cpu_to_le32(instance->modem_type->pll_b_clk);
524 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4); 524 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
525 if (ret) { 525 if (ret) {
526 dev_err(dev, "FirmwarePllBClkValue failed: %d\n", ret); 526 usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
527 return; 527 return;
528 } 528 }
529 529
@@ -531,14 +531,14 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
531 val = cpu_to_le32(SDRAM_ENA); 531 val = cpu_to_le32(SDRAM_ENA);
532 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4); 532 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
533 if (ret) { 533 if (ret) {
534 dev_err(dev, "Enable SDRAM failed: %d\n", ret); 534 usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
535 return; 535 return;
536 } 536 }
537 537
538 /* Firmware */ 538 /* Firmware */
539 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size); 539 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
540 if (ret) { 540 if (ret) {
541 dev_err(dev, "Firmware upload failed: %d\n", ret); 541 usb_err(usbatm, "Firmware upload failed: %d\n", ret);
542 return; 542 return;
543 } 543 }
544 544
@@ -546,7 +546,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
546 if (instance->modem_type->boot_rom_patch) { 546 if (instance->modem_type->boot_rom_patch) {
547 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size); 547 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
548 if (ret) { 548 if (ret) {
549 dev_err(dev, "Boot ROM patching failed: %d\n", ret); 549 usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
550 return; 550 return;
551 } 551 }
552 } 552 }
@@ -554,7 +554,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
554 /* Signature */ 554 /* Signature */
555 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4); 555 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
556 if (ret) { 556 if (ret) {
557 dev_err(dev, "Signature storing failed: %d\n", ret); 557 usb_err(usbatm, "Signature storing failed: %d\n", ret);
558 return; 558 return;
559 } 559 }
560 560
@@ -566,7 +566,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
566 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0); 566 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
567 } 567 }
568 if (ret) { 568 if (ret) {
569 dev_err(dev, "Passing control to firmware failed: %d\n", ret); 569 usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
570 return; 570 return;
571 } 571 }
572 572
@@ -580,7 +580,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
580 580
581 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0); 581 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
582 if (ret < 0) { 582 if (ret < 0) {
583 dev_err(dev, "modem failed to initialize: %d\n", ret); 583 usb_err(usbatm, "modem failed to initialize: %d\n", ret);
584 return; 584 return;
585 } 585 }
586 586
@@ -597,7 +597,7 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
597 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET, 597 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
598 (u8 *) buf, len, NULL, 0); 598 (u8 *) buf, len, NULL, 0);
599 if (ret < 0) { 599 if (ret < 0) {
600 dev_err(dev, "load config data failed: %d\n", ret); 600 usb_err(usbatm, "load config data failed: %d\n", ret);
601 return; 601 return;
602 } 602 }
603 } 603 }
@@ -608,18 +608,19 @@ static void cxacru_upload_firmware(struct cxacru_data *instance,
608static int cxacru_find_firmware(struct cxacru_data *instance, 608static int cxacru_find_firmware(struct cxacru_data *instance,
609 char* phase, const struct firmware **fw_p) 609 char* phase, const struct firmware **fw_p)
610{ 610{
611 struct device *dev = &instance->usbatm->usb_intf->dev; 611 struct usbatm_data *usbatm = instance->usbatm;
612 struct device *dev = &usbatm->usb_intf->dev;
612 char buf[16]; 613 char buf[16];
613 614
614 sprintf(buf, "cxacru-%s.bin", phase); 615 sprintf(buf, "cxacru-%s.bin", phase);
615 dbg("cxacru_find_firmware: looking for %s", buf); 616 dbg("cxacru_find_firmware: looking for %s", buf);
616 617
617 if (request_firmware(fw_p, buf, dev)) { 618 if (request_firmware(fw_p, buf, dev)) {
618 dev_dbg(dev, "no stage %s firmware found\n", phase); 619 usb_dbg(usbatm, "no stage %s firmware found\n", phase);
619 return -ENOENT; 620 return -ENOENT;
620 } 621 }
621 622
622 dev_info(dev, "found firmware %s\n", buf); 623 usb_info(usbatm, "found firmware %s\n", buf);
623 624
624 return 0; 625 return 0;
625} 626}
@@ -627,20 +628,19 @@ static int cxacru_find_firmware(struct cxacru_data *instance,
627static int cxacru_heavy_init(struct usbatm_data *usbatm_instance, 628static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
628 struct usb_interface *usb_intf) 629 struct usb_interface *usb_intf)
629{ 630{
630 struct device *dev = &usbatm_instance->usb_intf->dev;
631 const struct firmware *fw, *bp, *cf; 631 const struct firmware *fw, *bp, *cf;
632 struct cxacru_data *instance = usbatm_instance->driver_data; 632 struct cxacru_data *instance = usbatm_instance->driver_data;
633 633
634 int ret = cxacru_find_firmware(instance, "fw", &fw); 634 int ret = cxacru_find_firmware(instance, "fw", &fw);
635 if (ret) { 635 if (ret) {
636 dev_warn(dev, "firmware (cxacru-fw.bin) unavailable (hotplug misconfiguration?)\n"); 636 usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
637 return ret; 637 return ret;
638 } 638 }
639 639
640 if (instance->modem_type->boot_rom_patch) { 640 if (instance->modem_type->boot_rom_patch) {
641 ret = cxacru_find_firmware(instance, "bp", &bp); 641 ret = cxacru_find_firmware(instance, "bp", &bp);
642 if (ret) { 642 if (ret) {
643 dev_warn(dev, "boot ROM patch (cxacru-bp.bin) unavailable (hotplug misconfiguration?)\n"); 643 usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
644 release_firmware(fw); 644 release_firmware(fw);
645 return ret; 645 return ret;
646 } 646 }
@@ -667,22 +667,19 @@ static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
667} 667}
668 668
669static int cxacru_bind(struct usbatm_data *usbatm_instance, 669static int cxacru_bind(struct usbatm_data *usbatm_instance,
670 struct usb_interface *intf, const struct usb_device_id *id, 670 struct usb_interface *intf, const struct usb_device_id *id)
671 int *need_heavy_init)
672{ 671{
673 struct cxacru_data *instance; 672 struct cxacru_data *instance;
674 struct usb_device *usb_dev = interface_to_usbdev(intf); 673 struct usb_device *usb_dev = interface_to_usbdev(intf);
675 int ret; 674 int ret;
676 675
677 /* instance init */ 676 /* instance init */
678 instance = kmalloc(sizeof(*instance), GFP_KERNEL); 677 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
679 if (!instance) { 678 if (!instance) {
680 dbg("cxacru_bind: no memory for instance data"); 679 dbg("cxacru_bind: no memory for instance data");
681 return -ENOMEM; 680 return -ENOMEM;
682 } 681 }
683 682
684 memset(instance, 0, sizeof(*instance));
685
686 instance->usbatm = usbatm_instance; 683 instance->usbatm = usbatm_instance;
687 instance->modem_type = (struct cxacru_modem_type *) id->driver_info; 684 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
688 685
@@ -721,13 +718,13 @@ static int cxacru_bind(struct usbatm_data *usbatm_instance,
721 instance->snd_buf, PAGE_SIZE, 718 instance->snd_buf, PAGE_SIZE,
722 cxacru_blocking_completion, &instance->snd_done, 4); 719 cxacru_blocking_completion, &instance->snd_done, 4);
723 720
724 init_MUTEX(&instance->cm_serialize); 721 mutex_init(&instance->cm_serialize);
725 722
726 INIT_WORK(&instance->poll_work, (void *)cxacru_poll_status, instance); 723 INIT_WORK(&instance->poll_work, (void *)cxacru_poll_status, instance);
727 724
728 usbatm_instance->driver_data = instance; 725 usbatm_instance->driver_data = instance;
729 726
730 *need_heavy_init = cxacru_card_status(instance); 727 usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
731 728
732 return 0; 729 return 0;
733 730
@@ -787,12 +784,12 @@ static const struct usb_device_id cxacru_usb_ids[] = {
787 { /* V = Conexant P = ADSL modem (Hasbani project) */ 784 { /* V = Conexant P = ADSL modem (Hasbani project) */
788 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00 785 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
789 }, 786 },
790 { /* V = Conexant P = ADSL modem (Well PTI-800 */
791 USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
792 },
793 { /* V = Conexant P = ADSL modem */ 787 { /* V = Conexant P = ADSL modem */
794 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00 788 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
795 }, 789 },
790 { /* V = Conexant P = ADSL modem (Well PTI-800) */
791 USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
792 },
796 { /* V = Conexant P = ADSL modem */ 793 { /* V = Conexant P = ADSL modem */
797 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00 794 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
798 }, 795 },
@@ -835,14 +832,13 @@ static const struct usb_device_id cxacru_usb_ids[] = {
835MODULE_DEVICE_TABLE(usb, cxacru_usb_ids); 832MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
836 833
837static struct usbatm_driver cxacru_driver = { 834static struct usbatm_driver cxacru_driver = {
838 .owner = THIS_MODULE,
839 .driver_name = cxacru_driver_name, 835 .driver_name = cxacru_driver_name,
840 .bind = cxacru_bind, 836 .bind = cxacru_bind,
841 .heavy_init = cxacru_heavy_init, 837 .heavy_init = cxacru_heavy_init,
842 .unbind = cxacru_unbind, 838 .unbind = cxacru_unbind,
843 .atm_start = cxacru_atm_start, 839 .atm_start = cxacru_atm_start,
844 .in = CXACRU_EP_DATA, 840 .bulk_in = CXACRU_EP_DATA,
845 .out = CXACRU_EP_DATA, 841 .bulk_out = CXACRU_EP_DATA,
846 .rx_padding = 3, 842 .rx_padding = 3,
847 .tx_padding = 11, 843 .tx_padding = 11,
848}; 844};
diff --git a/drivers/usb/atm/speedtch.c b/drivers/usb/atm/speedtch.c
index c1b47d74e206..7860c8a5800d 100644
--- a/drivers/usb/atm/speedtch.c
+++ b/drivers/usb/atm/speedtch.c
@@ -35,12 +35,14 @@
35#include <linux/slab.h> 35#include <linux/slab.h>
36#include <linux/stat.h> 36#include <linux/stat.h>
37#include <linux/timer.h> 37#include <linux/timer.h>
38#include <linux/types.h>
39#include <linux/usb_ch9.h>
38#include <linux/workqueue.h> 40#include <linux/workqueue.h>
39 41
40#include "usbatm.h" 42#include "usbatm.h"
41 43
42#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" 44#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
43#define DRIVER_VERSION "1.9" 45#define DRIVER_VERSION "1.10"
44#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION 46#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
45 47
46static const char speedtch_driver_name[] = "speedtch"; 48static const char speedtch_driver_name[] = "speedtch";
@@ -66,31 +68,42 @@ static const char speedtch_driver_name[] = "speedtch";
66 68
67#define RESUBMIT_DELAY 1000 /* milliseconds */ 69#define RESUBMIT_DELAY 1000 /* milliseconds */
68 70
69#define DEFAULT_ALTSETTING 1 71#define DEFAULT_BULK_ALTSETTING 1
72#define DEFAULT_ISOC_ALTSETTING 2
70#define DEFAULT_DL_512_FIRST 0 73#define DEFAULT_DL_512_FIRST 0
74#define DEFAULT_ENABLE_ISOC 0
71#define DEFAULT_SW_BUFFERING 0 75#define DEFAULT_SW_BUFFERING 0
72 76
73static int altsetting = DEFAULT_ALTSETTING; 77static unsigned int altsetting = 0; /* zero means: use the default */
74static int dl_512_first = DEFAULT_DL_512_FIRST; 78static int dl_512_first = DEFAULT_DL_512_FIRST;
79static int enable_isoc = DEFAULT_ENABLE_ISOC;
75static int sw_buffering = DEFAULT_SW_BUFFERING; 80static int sw_buffering = DEFAULT_SW_BUFFERING;
76 81
77module_param(altsetting, int, S_IRUGO | S_IWUSR); 82module_param(altsetting, uint, S_IRUGO | S_IWUSR);
78MODULE_PARM_DESC(altsetting, 83MODULE_PARM_DESC(altsetting,
79 "Alternative setting for data interface (default: " 84 "Alternative setting for data interface (bulk_default: "
80 __MODULE_STRING(DEFAULT_ALTSETTING) ")"); 85 __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
86 __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
81 87
82module_param(dl_512_first, bool, S_IRUGO | S_IWUSR); 88module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
83MODULE_PARM_DESC(dl_512_first, 89MODULE_PARM_DESC(dl_512_first,
84 "Read 512 bytes before sending firmware (default: " 90 "Read 512 bytes before sending firmware (default: "
85 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")"); 91 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
86 92
93module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(enable_isoc,
95 "Use isochronous transfers if available (default: "
96 __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
97
87module_param(sw_buffering, bool, S_IRUGO | S_IWUSR); 98module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(sw_buffering, 99MODULE_PARM_DESC(sw_buffering,
89 "Enable software buffering (default: " 100 "Enable software buffering (default: "
90 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")"); 101 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
91 102
103#define INTERFACE_DATA 1
92#define ENDPOINT_INT 0x81 104#define ENDPOINT_INT 0x81
93#define ENDPOINT_DATA 0x07 105#define ENDPOINT_BULK_DATA 0x07
106#define ENDPOINT_ISOC_DATA 0x07
94#define ENDPOINT_FIRMWARE 0x05 107#define ENDPOINT_FIRMWARE 0x05
95 108
96#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) ) 109#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
@@ -98,6 +111,8 @@ MODULE_PARM_DESC(sw_buffering,
98struct speedtch_instance_data { 111struct speedtch_instance_data {
99 struct usbatm_data *usbatm; 112 struct usbatm_data *usbatm;
100 113
114 unsigned int altsetting;
115
101 struct work_struct status_checker; 116 struct work_struct status_checker;
102 117
103 unsigned char last_status; 118 unsigned char last_status;
@@ -205,7 +220,7 @@ static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
205 buffer, 0x200, &actual_length, 2000); 220 buffer, 0x200, &actual_length, 2000);
206 221
207 if (ret < 0 && ret != -ETIMEDOUT) 222 if (ret < 0 && ret != -ETIMEDOUT)
208 usb_dbg(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret); 223 usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
209 else 224 else
210 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret); 225 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
211 } 226 }
@@ -219,7 +234,7 @@ static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
219 buffer, thislen, &actual_length, DATA_TIMEOUT); 234 buffer, thislen, &actual_length, DATA_TIMEOUT);
220 235
221 if (ret < 0) { 236 if (ret < 0) {
222 usb_dbg(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret); 237 usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
223 goto out_free; 238 goto out_free;
224 } 239 }
225 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size); 240 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
@@ -232,7 +247,7 @@ static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
232 buffer, 0x200, &actual_length, DATA_TIMEOUT); 247 buffer, 0x200, &actual_length, DATA_TIMEOUT);
233 248
234 if (ret < 0) { 249 if (ret < 0) {
235 usb_dbg(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret); 250 usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
236 goto out_free; 251 goto out_free;
237 } 252 }
238 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length); 253 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
@@ -246,7 +261,7 @@ static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
246 buffer, thislen, &actual_length, DATA_TIMEOUT); 261 buffer, thislen, &actual_length, DATA_TIMEOUT);
247 262
248 if (ret < 0) { 263 if (ret < 0) {
249 usb_dbg(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret); 264 usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
250 goto out_free; 265 goto out_free;
251 } 266 }
252 } 267 }
@@ -259,7 +274,7 @@ static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
259 buffer, 0x200, &actual_length, DATA_TIMEOUT); 274 buffer, 0x200, &actual_length, DATA_TIMEOUT);
260 275
261 if (ret < 0) { 276 if (ret < 0) {
262 usb_dbg(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret); 277 usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
263 goto out_free; 278 goto out_free;
264 } 279 }
265 280
@@ -270,6 +285,11 @@ static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
270 because we're in our own kernel thread anyway. */ 285 because we're in our own kernel thread anyway. */
271 msleep_interruptible(1000); 286 msleep_interruptible(1000);
272 287
288 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->altsetting)) < 0) {
289 usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->altsetting, ret);
290 goto out_free;
291 }
292
273 /* Enable software buffering, if requested */ 293 /* Enable software buffering, if requested */
274 if (sw_buffering) 294 if (sw_buffering)
275 speedtch_set_swbuff(instance, 1); 295 speedtch_set_swbuff(instance, 1);
@@ -285,8 +305,8 @@ out:
285 return ret; 305 return ret;
286} 306}
287 307
288static int speedtch_find_firmware(struct usb_interface *intf, int phase, 308static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
289 const struct firmware **fw_p) 309 int phase, const struct firmware **fw_p)
290{ 310{
291 struct device *dev = &intf->dev; 311 struct device *dev = &intf->dev;
292 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice); 312 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
@@ -295,24 +315,24 @@ static int speedtch_find_firmware(struct usb_interface *intf, int phase,
295 char buf[24]; 315 char buf[24];
296 316
297 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision); 317 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
298 dev_dbg(dev, "%s: looking for %s\n", __func__, buf); 318 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
299 319
300 if (request_firmware(fw_p, buf, dev)) { 320 if (request_firmware(fw_p, buf, dev)) {
301 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision); 321 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
302 dev_dbg(dev, "%s: looking for %s\n", __func__, buf); 322 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
303 323
304 if (request_firmware(fw_p, buf, dev)) { 324 if (request_firmware(fw_p, buf, dev)) {
305 sprintf(buf, "speedtch-%d.bin", phase); 325 sprintf(buf, "speedtch-%d.bin", phase);
306 dev_dbg(dev, "%s: looking for %s\n", __func__, buf); 326 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
307 327
308 if (request_firmware(fw_p, buf, dev)) { 328 if (request_firmware(fw_p, buf, dev)) {
309 dev_warn(dev, "no stage %d firmware found!\n", phase); 329 usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
310 return -ENOENT; 330 return -ENOENT;
311 } 331 }
312 } 332 }
313 } 333 }
314 334
315 dev_info(dev, "found stage %d firmware %s\n", phase, buf); 335 usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
316 336
317 return 0; 337 return 0;
318} 338}
@@ -323,15 +343,16 @@ static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface
323 struct speedtch_instance_data *instance = usbatm->driver_data; 343 struct speedtch_instance_data *instance = usbatm->driver_data;
324 int ret; 344 int ret;
325 345
326 if ((ret = speedtch_find_firmware(intf, 1, &fw1)) < 0) 346 if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
327 return ret; 347 return ret;
328 348
329 if ((ret = speedtch_find_firmware(intf, 2, &fw2)) < 0) { 349 if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
330 release_firmware(fw1); 350 release_firmware(fw1);
331 return ret; 351 return ret;
332 } 352 }
333 353
334 ret = speedtch_upload_firmware(instance, fw1, fw2); 354 if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
355 usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
335 356
336 release_firmware(fw2); 357 release_firmware(fw2);
337 release_firmware(fw1); 358 release_firmware(fw1);
@@ -428,7 +449,9 @@ static void speedtch_check_status(struct speedtch_instance_data *instance)
428 int down_speed, up_speed, ret; 449 int down_speed, up_speed, ret;
429 unsigned char status; 450 unsigned char status;
430 451
452#ifdef VERBOSE_DEBUG
431 atm_dbg(usbatm, "%s entered\n", __func__); 453 atm_dbg(usbatm, "%s entered\n", __func__);
454#endif
432 455
433 ret = speedtch_read_status(instance); 456 ret = speedtch_read_status(instance);
434 if (ret < 0) { 457 if (ret < 0) {
@@ -441,9 +464,9 @@ static void speedtch_check_status(struct speedtch_instance_data *instance)
441 464
442 status = buf[OFFSET_7]; 465 status = buf[OFFSET_7];
443 466
444 atm_dbg(usbatm, "%s: line state %02x\n", __func__, status);
445
446 if ((status != instance->last_status) || !status) { 467 if ((status != instance->last_status) || !status) {
468 atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
469
447 switch (status) { 470 switch (status) {
448 case 0: 471 case 0:
449 atm_dev->signal = ATM_PHY_SIG_LOST; 472 atm_dev->signal = ATM_PHY_SIG_LOST;
@@ -484,7 +507,7 @@ static void speedtch_check_status(struct speedtch_instance_data *instance)
484 507
485 default: 508 default:
486 atm_dev->signal = ATM_PHY_SIG_UNKNOWN; 509 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
487 atm_info(usbatm, "Unknown line state %02x\n", status); 510 atm_info(usbatm, "unknown line state %02x\n", status);
488 break; 511 break;
489 } 512 }
490 513
@@ -583,11 +606,6 @@ static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_de
583 606
584 atm_dbg(usbatm, "%s entered\n", __func__); 607 atm_dbg(usbatm, "%s entered\n", __func__);
585 608
586 if ((ret = usb_set_interface(usb_dev, 1, altsetting)) < 0) {
587 atm_dbg(usbatm, "%s: usb_set_interface returned %d!\n", __func__, ret);
588 return ret;
589 }
590
591 /* Set MAC address, it is stored in the serial number */ 609 /* Set MAC address, it is stored in the serial number */
592 memset(atm_dev->esi, 0, sizeof(atm_dev->esi)); 610 memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
593 if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) { 611 if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
@@ -678,20 +696,27 @@ static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_inte
678 696
679static int speedtch_bind(struct usbatm_data *usbatm, 697static int speedtch_bind(struct usbatm_data *usbatm,
680 struct usb_interface *intf, 698 struct usb_interface *intf,
681 const struct usb_device_id *id, 699 const struct usb_device_id *id)
682 int *need_heavy_init)
683{ 700{
684 struct usb_device *usb_dev = interface_to_usbdev(intf); 701 struct usb_device *usb_dev = interface_to_usbdev(intf);
685 struct usb_interface *cur_intf; 702 struct usb_interface *cur_intf, *data_intf;
686 struct speedtch_instance_data *instance; 703 struct speedtch_instance_data *instance;
687 int ifnum = intf->altsetting->desc.bInterfaceNumber; 704 int ifnum = intf->altsetting->desc.bInterfaceNumber;
688 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces; 705 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
689 int i, ret; 706 int i, ret;
707 int use_isoc;
690 708
691 usb_dbg(usbatm, "%s entered\n", __func__); 709 usb_dbg(usbatm, "%s entered\n", __func__);
692 710
711 /* sanity checks */
712
693 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) { 713 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
694 usb_dbg(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass); 714 usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
715 return -ENODEV;
716 }
717
718 if (!(data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA))) {
719 usb_err(usbatm, "%s: data interface not found!\n", __func__);
695 return -ENODEV; 720 return -ENODEV;
696 } 721 }
697 722
@@ -704,25 +729,71 @@ static int speedtch_bind(struct usbatm_data *usbatm,
704 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm); 729 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
705 730
706 if (ret < 0) { 731 if (ret < 0) {
707 usb_dbg(usbatm, "%s: failed to claim interface %d (%d)\n", __func__, i, ret); 732 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
708 speedtch_release_interfaces(usb_dev, i); 733 speedtch_release_interfaces(usb_dev, i);
709 return ret; 734 return ret;
710 } 735 }
711 } 736 }
712 } 737 }
713 738
714 instance = kmalloc(sizeof(*instance), GFP_KERNEL); 739 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
715 740
716 if (!instance) { 741 if (!instance) {
717 usb_dbg(usbatm, "%s: no memory for instance data!\n", __func__); 742 usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
718 ret = -ENOMEM; 743 ret = -ENOMEM;
719 goto fail_release; 744 goto fail_release;
720 } 745 }
721 746
722 memset(instance, 0, sizeof(struct speedtch_instance_data));
723
724 instance->usbatm = usbatm; 747 instance->usbatm = usbatm;
725 748
749 /* altsetting and enable_isoc may change at any moment, so take a snapshot */
750 instance->altsetting = altsetting;
751 use_isoc = enable_isoc;
752
753 if (instance->altsetting)
754 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->altsetting)) < 0) {
755 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->altsetting, ret);
756 instance->altsetting = 0; /* fall back to default */
757 }
758
759 if (!instance->altsetting && use_isoc)
760 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
761 usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
762 use_isoc = 0; /* fall back to bulk */
763 }
764
765 if (use_isoc) {
766 const struct usb_host_interface *desc = data_intf->cur_altsetting;
767 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
768 int i;
769
770 use_isoc = 0; /* fall back to bulk if endpoint not found */
771
772 for (i=0; i<desc->desc.bNumEndpoints; i++) {
773 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
774
775 if ((endpoint_desc->bEndpointAddress == target_address)) {
776 use_isoc = (endpoint_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
777 USB_ENDPOINT_XFER_ISOC;
778 break;
779 }
780 }
781
782 if (!use_isoc)
783 usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
784 }
785
786 if (!use_isoc && !instance->altsetting)
787 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
788 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
789 goto fail_free;
790 }
791
792 if (!instance->altsetting)
793 instance->altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
794
795 usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
796
726 INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance); 797 INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance);
727 798
728 instance->status_checker.timer.function = speedtch_status_poll; 799 instance->status_checker.timer.function = speedtch_status_poll;
@@ -749,13 +820,15 @@ static int speedtch_bind(struct usbatm_data *usbatm,
749 0x12, 0xc0, 0x07, 0x00, 820 0x12, 0xc0, 0x07, 0x00,
750 instance->scratch_buffer + OFFSET_7, SIZE_7, 500); 821 instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
751 822
752 *need_heavy_init = (ret != SIZE_7); 823 usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
753 824
754 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, need_heavy_init ? "not" : "already"); 825 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
755 826
756 if (*need_heavy_init) 827 if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
757 if ((ret = usb_reset_device(usb_dev)) < 0) 828 if ((ret = usb_reset_device(usb_dev)) < 0) {
829 usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
758 goto fail_free; 830 goto fail_free;
831 }
759 832
760 usbatm->driver_data = instance; 833 usbatm->driver_data = instance;
761 834
@@ -787,15 +860,15 @@ static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *in
787***********/ 860***********/
788 861
789static struct usbatm_driver speedtch_usbatm_driver = { 862static struct usbatm_driver speedtch_usbatm_driver = {
790 .owner = THIS_MODULE,
791 .driver_name = speedtch_driver_name, 863 .driver_name = speedtch_driver_name,
792 .bind = speedtch_bind, 864 .bind = speedtch_bind,
793 .heavy_init = speedtch_heavy_init, 865 .heavy_init = speedtch_heavy_init,
794 .unbind = speedtch_unbind, 866 .unbind = speedtch_unbind,
795 .atm_start = speedtch_atm_start, 867 .atm_start = speedtch_atm_start,
796 .atm_stop = speedtch_atm_stop, 868 .atm_stop = speedtch_atm_stop,
797 .in = ENDPOINT_DATA, 869 .bulk_in = ENDPOINT_BULK_DATA,
798 .out = ENDPOINT_DATA 870 .bulk_out = ENDPOINT_BULK_DATA,
871 .isoc_in = ENDPOINT_ISOC_DATA
799}; 872};
800 873
801static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 874static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
diff --git a/drivers/usb/atm/ueagle-atm.c b/drivers/usb/atm/ueagle-atm.c
index 7d2a679989ed..830d2c982670 100644
--- a/drivers/usb/atm/ueagle-atm.c
+++ b/drivers/usb/atm/ueagle-atm.c
@@ -63,11 +63,12 @@
63#include <linux/ctype.h> 63#include <linux/ctype.h>
64#include <linux/kthread.h> 64#include <linux/kthread.h>
65#include <linux/version.h> 65#include <linux/version.h>
66#include <linux/mutex.h>
66#include <asm/unaligned.h> 67#include <asm/unaligned.h>
67 68
68#include "usbatm.h" 69#include "usbatm.h"
69 70
70#define EAGLEUSBVERSION "ueagle 1.1" 71#define EAGLEUSBVERSION "ueagle 1.2"
71 72
72 73
73/* 74/*
@@ -358,16 +359,19 @@ struct intr_pkt {
358#define INTR_PKT_SIZE 28 359#define INTR_PKT_SIZE 28
359 360
360static struct usb_driver uea_driver; 361static struct usb_driver uea_driver;
361static DECLARE_MUTEX(uea_semaphore); 362static DEFINE_MUTEX(uea_mutex);
362static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III"}; 363static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III"};
363 364
364static int modem_index; 365static int modem_index;
365static unsigned int debug; 366static unsigned int debug;
367static int use_iso[NB_MODEM] = {[0 ... (NB_MODEM - 1)] = 1};
366static int sync_wait[NB_MODEM]; 368static int sync_wait[NB_MODEM];
367static char *cmv_file[NB_MODEM]; 369static char *cmv_file[NB_MODEM];
368 370
369module_param(debug, uint, 0644); 371module_param(debug, uint, 0644);
370MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)"); 372MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
373module_param_array(use_iso, bool, NULL, 0644);
374MODULE_PARM_DESC(use_iso, "use isochronous usb pipe for incoming traffic");
371module_param_array(sync_wait, bool, NULL, 0644); 375module_param_array(sync_wait, bool, NULL, 0644);
372MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM"); 376MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
373module_param_array(cmv_file, charp, NULL, 0644); 377module_param_array(cmv_file, charp, NULL, 0644);
@@ -628,8 +632,7 @@ static int request_dsp(struct uea_softc *sc)
628 dsp_name = FW_DIR "DSPep.bin"; 632 dsp_name = FW_DIR "DSPep.bin";
629 } 633 }
630 634
631 ret = request_firmware(&sc->dsp_firm, 635 ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
632 dsp_name, &sc->usb_dev->dev);
633 if (ret < 0) { 636 if (ret < 0) {
634 uea_err(INS_TO_USBDEV(sc), 637 uea_err(INS_TO_USBDEV(sc),
635 "requesting firmware %s failed with error %d\n", 638 "requesting firmware %s failed with error %d\n",
@@ -744,7 +747,6 @@ static inline int wait_cmv_ack(struct uea_softc *sc)
744 return ret; 747 return ret;
745 748
746 return (ret == 0) ? -ETIMEDOUT : 0; 749 return (ret == 0) ? -ETIMEDOUT : 0;
747
748} 750}
749 751
750#define UCDC_SEND_ENCAPSULATED_COMMAND 0x00 752#define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
@@ -935,6 +937,7 @@ static int uea_stat(struct uea_softc *sc)
935 * ADI930 don't support it (-EPIPE error). 937 * ADI930 don't support it (-EPIPE error).
936 */ 938 */
937 if (UEA_CHIP_VERSION(sc) != ADI930 939 if (UEA_CHIP_VERSION(sc) != ADI930
940 && !use_iso[sc->modem_index]
938 && sc->stats.phy.dsrate != (data >> 16) * 32) { 941 && sc->stats.phy.dsrate != (data >> 16) * 32) {
939 /* Original timming from ADI(used in windows driver) 942 /* Original timming from ADI(used in windows driver)
940 * 0x20ffff>>16 * 32 = 32 * 32 = 1Mbits 943 * 0x20ffff>>16 * 32 = 32 * 32 = 1Mbits
@@ -1010,7 +1013,7 @@ static int request_cmvs(struct uea_softc *sc,
1010 int ret, size; 1013 int ret, size;
1011 u8 *data; 1014 u8 *data;
1012 char *file; 1015 char *file;
1013 static char cmv_name[256] = FW_DIR; 1016 char cmv_name[FIRMWARE_NAME_MAX]; /* 30 bytes stack variable */
1014 1017
1015 if (cmv_file[sc->modem_index] == NULL) { 1018 if (cmv_file[sc->modem_index] == NULL) {
1016 if (UEA_CHIP_VERSION(sc) == ADI930) 1019 if (UEA_CHIP_VERSION(sc) == ADI930)
@@ -1184,8 +1187,7 @@ static int load_XILINX_firmware(struct uea_softc *sc)
1184 } 1187 }
1185 } 1188 }
1186 1189
1187 /* finish to send the fpga 1190 /* finish to send the fpga */
1188 */
1189 ret = uea_request(sc, 0xe, 1, 0, NULL); 1191 ret = uea_request(sc, 0xe, 1, 0, NULL);
1190 if (ret < 0) { 1192 if (ret < 0) {
1191 uea_err(INS_TO_USBDEV(sc), 1193 uea_err(INS_TO_USBDEV(sc),
@@ -1193,9 +1195,7 @@ static int load_XILINX_firmware(struct uea_softc *sc)
1193 goto err1; 1195 goto err1;
1194 } 1196 }
1195 1197
1196 /* 1198 /* Tell the modem we finish : de-assert reset */
1197 * Tell the modem we finish : de-assert reset
1198 */
1199 value = 0; 1199 value = 0;
1200 ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value); 1200 ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1201 if (ret < 0) 1201 if (ret < 0)
@@ -1209,6 +1209,7 @@ err0:
1209 return ret; 1209 return ret;
1210} 1210}
1211 1211
1212/* The modem send us an ack. First with check if it right */
1212static void uea_dispatch_cmv(struct uea_softc *sc, struct cmv* cmv) 1213static void uea_dispatch_cmv(struct uea_softc *sc, struct cmv* cmv)
1213{ 1214{
1214 uea_enters(INS_TO_USBDEV(sc)); 1215 uea_enters(INS_TO_USBDEV(sc));
@@ -1268,23 +1269,19 @@ bad1:
1268 */ 1269 */
1269static void uea_intr(struct urb *urb, struct pt_regs *regs) 1270static void uea_intr(struct urb *urb, struct pt_regs *regs)
1270{ 1271{
1271 struct uea_softc *sc = (struct uea_softc *)urb->context; 1272 struct uea_softc *sc = urb->context;
1272 struct intr_pkt *intr; 1273 struct intr_pkt *intr = urb->transfer_buffer;
1273 uea_enters(INS_TO_USBDEV(sc)); 1274 uea_enters(INS_TO_USBDEV(sc));
1274 1275
1275 if (urb->status < 0) { 1276 if (unlikely(urb->status < 0)) {
1276 uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n", 1277 uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
1277 urb->status); 1278 urb->status);
1278 return; 1279 return;
1279 } 1280 }
1280 1281
1281 intr = (struct intr_pkt *) urb->transfer_buffer;
1282
1283 /* device-to-host interrupt */ 1282 /* device-to-host interrupt */
1284 if (intr->bType != 0x08 || sc->booting) { 1283 if (intr->bType != 0x08 || sc->booting) {
1285 uea_err(INS_TO_USBDEV(sc), "wrong intr\n"); 1284 uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
1286 // rebooting ?
1287 // sc->reset = 1;
1288 goto resubmit; 1285 goto resubmit;
1289 } 1286 }
1290 1287
@@ -1300,7 +1297,7 @@ static void uea_intr(struct urb *urb, struct pt_regs *regs)
1300 break; 1297 break;
1301 1298
1302 default: 1299 default:
1303 uea_err(INS_TO_USBDEV(sc), "unknown intr %u\n", 1300 uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
1304 le16_to_cpu(intr->wInterrupt)); 1301 le16_to_cpu(intr->wInterrupt));
1305 } 1302 }
1306 1303
@@ -1379,7 +1376,7 @@ static void uea_stop(struct uea_softc *sc)
1379 int ret; 1376 int ret;
1380 uea_enters(INS_TO_USBDEV(sc)); 1377 uea_enters(INS_TO_USBDEV(sc));
1381 ret = kthread_stop(sc->kthread); 1378 ret = kthread_stop(sc->kthread);
1382 uea_info(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret); 1379 uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
1383 1380
1384 /* stop any pending boot process */ 1381 /* stop any pending boot process */
1385 flush_scheduled_work(); 1382 flush_scheduled_work();
@@ -1418,13 +1415,13 @@ static ssize_t read_status(struct device *dev, struct device_attribute *attr,
1418 int ret = -ENODEV; 1415 int ret = -ENODEV;
1419 struct uea_softc *sc; 1416 struct uea_softc *sc;
1420 1417
1421 down(&uea_semaphore); 1418 mutex_lock(&uea_mutex);
1422 sc = dev_to_uea(dev); 1419 sc = dev_to_uea(dev);
1423 if (!sc) 1420 if (!sc)
1424 goto out; 1421 goto out;
1425 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state); 1422 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
1426out: 1423out:
1427 up(&uea_semaphore); 1424 mutex_unlock(&uea_mutex);
1428 return ret; 1425 return ret;
1429} 1426}
1430 1427
@@ -1434,14 +1431,14 @@ static ssize_t reboot(struct device *dev, struct device_attribute *attr,
1434 int ret = -ENODEV; 1431 int ret = -ENODEV;
1435 struct uea_softc *sc; 1432 struct uea_softc *sc;
1436 1433
1437 down(&uea_semaphore); 1434 mutex_lock(&uea_mutex);
1438 sc = dev_to_uea(dev); 1435 sc = dev_to_uea(dev);
1439 if (!sc) 1436 if (!sc)
1440 goto out; 1437 goto out;
1441 sc->reset = 1; 1438 sc->reset = 1;
1442 ret = count; 1439 ret = count;
1443out: 1440out:
1444 up(&uea_semaphore); 1441 mutex_unlock(&uea_mutex);
1445 return ret; 1442 return ret;
1446} 1443}
1447 1444
@@ -1453,7 +1450,7 @@ static ssize_t read_human_status(struct device *dev, struct device_attribute *at
1453 int ret = -ENODEV; 1450 int ret = -ENODEV;
1454 struct uea_softc *sc; 1451 struct uea_softc *sc;
1455 1452
1456 down(&uea_semaphore); 1453 mutex_lock(&uea_mutex);
1457 sc = dev_to_uea(dev); 1454 sc = dev_to_uea(dev);
1458 if (!sc) 1455 if (!sc)
1459 goto out; 1456 goto out;
@@ -1473,7 +1470,7 @@ static ssize_t read_human_status(struct device *dev, struct device_attribute *at
1473 break; 1470 break;
1474 } 1471 }
1475out: 1472out:
1476 up(&uea_semaphore); 1473 mutex_unlock(&uea_mutex);
1477 return ret; 1474 return ret;
1478} 1475}
1479 1476
@@ -1485,7 +1482,7 @@ static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
1485 int ret = -ENODEV; 1482 int ret = -ENODEV;
1486 struct uea_softc *sc; 1483 struct uea_softc *sc;
1487 1484
1488 down(&uea_semaphore); 1485 mutex_lock(&uea_mutex);
1489 sc = dev_to_uea(dev); 1486 sc = dev_to_uea(dev);
1490 if (!sc) 1487 if (!sc)
1491 goto out; 1488 goto out;
@@ -1497,7 +1494,7 @@ static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
1497 else 1494 else
1498 ret = sprintf(buf, "GOOD\n"); 1495 ret = sprintf(buf, "GOOD\n");
1499out: 1496out:
1500 up(&uea_semaphore); 1497 mutex_unlock(&uea_mutex);
1501 return ret; 1498 return ret;
1502} 1499}
1503 1500
@@ -1511,7 +1508,7 @@ static ssize_t read_##name(struct device *dev, \
1511 int ret = -ENODEV; \ 1508 int ret = -ENODEV; \
1512 struct uea_softc *sc; \ 1509 struct uea_softc *sc; \
1513 \ 1510 \
1514 down(&uea_semaphore); \ 1511 mutex_lock(&uea_mutex); \
1515 sc = dev_to_uea(dev); \ 1512 sc = dev_to_uea(dev); \
1516 if (!sc) \ 1513 if (!sc) \
1517 goto out; \ 1514 goto out; \
@@ -1519,7 +1516,7 @@ static ssize_t read_##name(struct device *dev, \
1519 if (reset) \ 1516 if (reset) \
1520 sc->stats.phy.name = 0; \ 1517 sc->stats.phy.name = 0; \
1521out: \ 1518out: \
1522 up(&uea_semaphore); \ 1519 mutex_unlock(&uea_mutex); \
1523 return ret; \ 1520 return ret; \
1524} \ 1521} \
1525 \ 1522 \
@@ -1617,7 +1614,7 @@ static void create_fs_entries(struct uea_softc *sc, struct usb_interface *intf)
1617} 1614}
1618 1615
1619static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf, 1616static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
1620 const struct usb_device_id *id, int *heavy) 1617 const struct usb_device_id *id)
1621{ 1618{
1622 struct usb_device *usb = interface_to_usbdev(intf); 1619 struct usb_device *usb = interface_to_usbdev(intf);
1623 struct uea_softc *sc; 1620 struct uea_softc *sc;
@@ -1629,16 +1626,14 @@ static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
1629 if (ifnum != UEA_INTR_IFACE_NO) 1626 if (ifnum != UEA_INTR_IFACE_NO)
1630 return -ENODEV; 1627 return -ENODEV;
1631 1628
1632 *heavy = sync_wait[modem_index]; 1629 usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
1633 1630
1634 /* interface 1 is for outbound traffic */ 1631 /* interface 1 is for outbound traffic */
1635 ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO); 1632 ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
1636 if (ret < 0) 1633 if (ret < 0)
1637 return ret; 1634 return ret;
1638 1635
1639 /* ADI930 has only 2 interfaces and inbound traffic 1636 /* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
1640 * is on interface 1
1641 */
1642 if (UEA_CHIP_VERSION(id) != ADI930) { 1637 if (UEA_CHIP_VERSION(id) != ADI930) {
1643 /* interface 2 is for inbound traffic */ 1638 /* interface 2 is for inbound traffic */
1644 ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO); 1639 ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
@@ -1658,6 +1653,25 @@ static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
1658 sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0; 1653 sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
1659 sc->driver_info = id->driver_info; 1654 sc->driver_info = id->driver_info;
1660 1655
1656 /* ADI930 don't support iso */
1657 if (UEA_CHIP_VERSION(id) != ADI930 && use_iso[sc->modem_index]) {
1658 int i;
1659
1660 /* try set fastest alternate for inbound traffic interface */
1661 for (i = FASTEST_ISO_INTF; i > 0; i--)
1662 if (usb_set_interface(usb, UEA_DS_IFACE_NO, i) == 0)
1663 break;
1664
1665 if (i > 0) {
1666 uea_dbg(usb, "set alternate %d for 2 interface\n", i);
1667 uea_info(usb, "using iso mode\n");
1668 usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
1669 } else {
1670 uea_err(usb, "setting any alternate failed for "
1671 "2 interface, using bulk mode\n");
1672 }
1673 }
1674
1661 ret = uea_boot(sc); 1675 ret = uea_boot(sc);
1662 if (ret < 0) { 1676 if (ret < 0) {
1663 kfree(sc); 1677 kfree(sc);
@@ -1701,13 +1715,13 @@ static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
1701 1715
1702static struct usbatm_driver uea_usbatm_driver = { 1716static struct usbatm_driver uea_usbatm_driver = {
1703 .driver_name = "ueagle-atm", 1717 .driver_name = "ueagle-atm",
1704 .owner = THIS_MODULE,
1705 .bind = uea_bind, 1718 .bind = uea_bind,
1706 .atm_start = uea_atm_open, 1719 .atm_start = uea_atm_open,
1707 .unbind = uea_unbind, 1720 .unbind = uea_unbind,
1708 .heavy_init = uea_heavy, 1721 .heavy_init = uea_heavy,
1709 .in = UEA_BULK_DATA_PIPE, 1722 .bulk_in = UEA_BULK_DATA_PIPE,
1710 .out = UEA_BULK_DATA_PIPE, 1723 .bulk_out = UEA_BULK_DATA_PIPE,
1724 .isoc_in = UEA_ISO_DATA_PIPE,
1711}; 1725};
1712 1726
1713static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id) 1727static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
@@ -1738,9 +1752,9 @@ static void uea_disconnect(struct usb_interface *intf)
1738 * Pre-firmware device has one interface 1752 * Pre-firmware device has one interface
1739 */ 1753 */
1740 if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) { 1754 if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
1741 down(&uea_semaphore); 1755 mutex_lock(&uea_mutex);
1742 usbatm_usb_disconnect(intf); 1756 usbatm_usb_disconnect(intf);
1743 up(&uea_semaphore); 1757 mutex_unlock(&uea_mutex);
1744 uea_info(usb, "ADSL device removed\n"); 1758 uea_info(usb, "ADSL device removed\n");
1745 } 1759 }
1746 1760
diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c
index 7af1883d4bf9..c1211fc037d9 100644
--- a/drivers/usb/atm/usbatm.c
+++ b/drivers/usb/atm/usbatm.c
@@ -72,6 +72,7 @@
72#include <linux/kernel.h> 72#include <linux/kernel.h>
73#include <linux/module.h> 73#include <linux/module.h>
74#include <linux/moduleparam.h> 74#include <linux/moduleparam.h>
75#include <linux/netdevice.h>
75#include <linux/proc_fs.h> 76#include <linux/proc_fs.h>
76#include <linux/sched.h> 77#include <linux/sched.h>
77#include <linux/signal.h> 78#include <linux/signal.h>
@@ -91,19 +92,18 @@ static int usbatm_print_packet(const unsigned char *data, int len);
91#endif 92#endif
92 93
93#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" 94#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
94#define DRIVER_VERSION "1.9" 95#define DRIVER_VERSION "1.10"
95#define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION 96#define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
96 97
97static const char usbatm_driver_name[] = "usbatm"; 98static const char usbatm_driver_name[] = "usbatm";
98 99
99#define UDSL_MAX_RCV_URBS 16 100#define UDSL_MAX_RCV_URBS 16
100#define UDSL_MAX_SND_URBS 16 101#define UDSL_MAX_SND_URBS 16
101#define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */ 102#define UDSL_MAX_BUF_SIZE 64 * 1024 /* bytes */
102#define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */
103#define UDSL_DEFAULT_RCV_URBS 4 103#define UDSL_DEFAULT_RCV_URBS 4
104#define UDSL_DEFAULT_SND_URBS 4 104#define UDSL_DEFAULT_SND_URBS 4
105#define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */ 105#define UDSL_DEFAULT_RCV_BUF_SIZE 64 * ATM_CELL_SIZE /* bytes */
106#define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */ 106#define UDSL_DEFAULT_SND_BUF_SIZE 64 * ATM_CELL_SIZE /* bytes */
107 107
108#define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD) 108#define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
109 109
@@ -111,8 +111,8 @@ static const char usbatm_driver_name[] = "usbatm";
111 111
112static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS; 112static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
113static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS; 113static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
114static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE; 114static unsigned int rcv_buf_bytes = UDSL_DEFAULT_RCV_BUF_SIZE;
115static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE; 115static unsigned int snd_buf_bytes = UDSL_DEFAULT_SND_BUF_SIZE;
116 116
117module_param(num_rcv_urbs, uint, S_IRUGO); 117module_param(num_rcv_urbs, uint, S_IRUGO);
118MODULE_PARM_DESC(num_rcv_urbs, 118MODULE_PARM_DESC(num_rcv_urbs,
@@ -126,15 +126,15 @@ MODULE_PARM_DESC(num_snd_urbs,
126 __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: " 126 __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
127 __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")"); 127 __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
128 128
129module_param(rcv_buf_size, uint, S_IRUGO); 129module_param(rcv_buf_bytes, uint, S_IRUGO);
130MODULE_PARM_DESC(rcv_buf_size, 130MODULE_PARM_DESC(rcv_buf_bytes,
131 "Size of the buffers used for reception in ATM cells (range: 1-" 131 "Size of the buffers used for reception, in bytes (range: 1-"
132 __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: " 132 __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
133 __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")"); 133 __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
134 134
135module_param(snd_buf_size, uint, S_IRUGO); 135module_param(snd_buf_bytes, uint, S_IRUGO);
136MODULE_PARM_DESC(snd_buf_size, 136MODULE_PARM_DESC(snd_buf_bytes,
137 "Size of the buffers used for transmission in ATM cells (range: 1-" 137 "Size of the buffers used for transmission, in bytes (range: 1-"
138 __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: " 138 __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: "
139 __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")"); 139 __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
140 140
@@ -166,10 +166,10 @@ struct usbatm_control {
166 166
167/* ATM */ 167/* ATM */
168 168
169static void usbatm_atm_dev_close(struct atm_dev *dev); 169static void usbatm_atm_dev_close(struct atm_dev *atm_dev);
170static int usbatm_atm_open(struct atm_vcc *vcc); 170static int usbatm_atm_open(struct atm_vcc *vcc);
171static void usbatm_atm_close(struct atm_vcc *vcc); 171static void usbatm_atm_close(struct atm_vcc *vcc);
172static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg); 172static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd, void __user * arg);
173static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb); 173static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
174static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page); 174static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
175 175
@@ -199,7 +199,7 @@ static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
199 if (vcc->pop) 199 if (vcc->pop)
200 vcc->pop(vcc, skb); 200 vcc->pop(vcc, skb);
201 else 201 else
202 dev_kfree_skb(skb); 202 dev_kfree_skb_any(skb);
203} 203}
204 204
205 205
@@ -234,8 +234,9 @@ static int usbatm_submit_urb(struct urb *urb)
234 234
235 ret = usb_submit_urb(urb, GFP_ATOMIC); 235 ret = usb_submit_urb(urb, GFP_ATOMIC);
236 if (ret) { 236 if (ret) {
237 atm_dbg(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n", 237 if (printk_ratelimit())
238 __func__, urb, ret); 238 atm_warn(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
239 __func__, urb, ret);
239 240
240 /* consider all errors transient and return the buffer back to the queue */ 241 /* consider all errors transient and return the buffer back to the queue */
241 urb->status = -EAGAIN; 242 urb->status = -EAGAIN;
@@ -269,10 +270,16 @@ static void usbatm_complete(struct urb *urb, struct pt_regs *regs)
269 270
270 spin_unlock_irqrestore(&channel->lock, flags); 271 spin_unlock_irqrestore(&channel->lock, flags);
271 272
272 if (unlikely(urb->status)) 273 if (unlikely(urb->status) &&
274 (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
275 urb->status != -EILSEQ ))
276 {
277 if (printk_ratelimit())
278 atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
279 __func__, urb, urb->status);
273 /* throttle processing in case of an error */ 280 /* throttle processing in case of an error */
274 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS)); 281 mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
275 else 282 } else
276 tasklet_schedule(&channel->tasklet); 283 tasklet_schedule(&channel->tasklet);
277} 284}
278 285
@@ -284,129 +291,167 @@ static void usbatm_complete(struct urb *urb, struct pt_regs *regs)
284static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance, 291static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
285 short vpi, int vci) 292 short vpi, int vci)
286{ 293{
287 struct usbatm_vcc_data *vcc; 294 struct usbatm_vcc_data *vcc_data;
288 295
289 list_for_each_entry(vcc, &instance->vcc_list, list) 296 list_for_each_entry(vcc_data, &instance->vcc_list, list)
290 if ((vcc->vci == vci) && (vcc->vpi == vpi)) 297 if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
291 return vcc; 298 return vcc_data;
292 return NULL; 299 return NULL;
293} 300}
294 301
295static void usbatm_extract_cells(struct usbatm_data *instance, 302static void usbatm_extract_one_cell(struct usbatm_data *instance, unsigned char *source)
296 unsigned char *source, unsigned int avail_data)
297{ 303{
298 struct usbatm_vcc_data *cached_vcc = NULL;
299 struct atm_vcc *vcc; 304 struct atm_vcc *vcc;
300 struct sk_buff *sarb; 305 struct sk_buff *sarb;
301 unsigned int stride = instance->rx_channel.stride; 306 short vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
302 int vci, cached_vci = 0; 307 int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
303 short vpi, cached_vpi = 0; 308 u8 pti = ((source[3] & 0xe) >> 1);
304 u8 pti;
305 309
306 for (; avail_data >= stride; avail_data -= stride, source += stride) { 310 vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
307 vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
308 vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
309 pti = ((source[3] & 0xe) >> 1);
310 311
311 vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti); 312 if ((vci != instance->cached_vci) || (vpi != instance->cached_vpi)) {
313 instance->cached_vpi = vpi;
314 instance->cached_vci = vci;
312 315
313 if ((vci != cached_vci) || (vpi != cached_vpi)) { 316 instance->cached_vcc = usbatm_find_vcc(instance, vpi, vci);
314 cached_vpi = vpi;
315 cached_vci = vci;
316 317
317 cached_vcc = usbatm_find_vcc(instance, vpi, vci); 318 if (!instance->cached_vcc)
319 atm_rldbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
320 }
318 321
319 if (!cached_vcc) 322 if (!instance->cached_vcc)
320 atm_dbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci); 323 return;
321 }
322 324
323 if (!cached_vcc) 325 vcc = instance->cached_vcc->vcc;
324 continue;
325 326
326 vcc = cached_vcc->vcc; 327 /* OAM F5 end-to-end */
328 if (pti == ATM_PTI_E2EF5) {
329 if (printk_ratelimit())
330 atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n",
331 __func__, vpi, vci);
332 atomic_inc(&vcc->stats->rx_err);
333 return;
334 }
327 335
328 /* OAM F5 end-to-end */ 336 sarb = instance->cached_vcc->sarb;
329 if (pti == ATM_PTI_E2EF5) {
330 atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n", __func__, vpi, vci);
331 atomic_inc(&vcc->stats->rx_err);
332 continue;
333 }
334 337
335 sarb = cached_vcc->sarb; 338 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
339 atm_rldbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
340 __func__, sarb->len, vcc);
341 /* discard cells already received */
342 skb_trim(sarb, 0);
343 UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
344 }
336 345
337 if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) { 346 memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
338 atm_dbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n", 347 __skb_put(sarb, ATM_CELL_PAYLOAD);
339 __func__, sarb->len, vcc);
340 /* discard cells already received */
341 skb_trim(sarb, 0);
342 UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
343 }
344 348
345 memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD); 349 if (pti & 1) {
346 __skb_put(sarb, ATM_CELL_PAYLOAD); 350 struct sk_buff *skb;
351 unsigned int length;
352 unsigned int pdu_length;
347 353
348 if (pti & 1) { 354 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
349 struct sk_buff *skb;
350 unsigned int length;
351 unsigned int pdu_length;
352 355
353 length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5]; 356 /* guard against overflow */
357 if (length > ATM_MAX_AAL5_PDU) {
358 atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
359 __func__, length, vcc);
360 atomic_inc(&vcc->stats->rx_err);
361 goto out;
362 }
354 363
355 /* guard against overflow */ 364 pdu_length = usbatm_pdu_length(length);
356 if (length > ATM_MAX_AAL5_PDU) {
357 atm_dbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
358 __func__, length, vcc);
359 atomic_inc(&vcc->stats->rx_err);
360 goto out;
361 }
362 365
363 pdu_length = usbatm_pdu_length(length); 366 if (sarb->len < pdu_length) {
367 atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
368 __func__, pdu_length, sarb->len, vcc);
369 atomic_inc(&vcc->stats->rx_err);
370 goto out;
371 }
364 372
365 if (sarb->len < pdu_length) { 373 if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
366 atm_dbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n", 374 atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
367 __func__, pdu_length, sarb->len, vcc); 375 __func__, vcc);
368 atomic_inc(&vcc->stats->rx_err); 376 atomic_inc(&vcc->stats->rx_err);
369 goto out; 377 goto out;
370 } 378 }
371 379
372 if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) { 380 vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc);
373 atm_dbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
374 __func__, vcc);
375 atomic_inc(&vcc->stats->rx_err);
376 goto out;
377 }
378 381
379 vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc); 382 if (!(skb = dev_alloc_skb(length))) {
383 if (printk_ratelimit())
384 atm_err(instance, "%s: no memory for skb (length: %u)!\n",
385 __func__, length);
386 atomic_inc(&vcc->stats->rx_drop);
387 goto out;
388 }
380 389
381 if (!(skb = dev_alloc_skb(length))) { 390 vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize);
382 atm_dbg(instance, "%s: no memory for skb (length: %u)!\n", __func__, length);
383 atomic_inc(&vcc->stats->rx_drop);
384 goto out;
385 }
386 391
387 vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize); 392 if (!atm_charge(vcc, skb->truesize)) {
393 atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
394 __func__, skb->truesize);
395 dev_kfree_skb_any(skb);
396 goto out; /* atm_charge increments rx_drop */
397 }
388 398
389 if (!atm_charge(vcc, skb->truesize)) { 399 memcpy(skb->data, sarb->tail - pdu_length, length);
390 atm_dbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n", __func__, skb->truesize); 400 __skb_put(skb, length);
391 dev_kfree_skb(skb);
392 goto out; /* atm_charge increments rx_drop */
393 }
394 401
395 memcpy(skb->data, sarb->tail - pdu_length, length); 402 vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
396 __skb_put(skb, length); 403 __func__, skb, skb->len, skb->truesize);
397 404
398 vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u", 405 PACKETDEBUG(skb->data, skb->len);
399 __func__, skb, skb->len, skb->truesize);
400 406
401 PACKETDEBUG(skb->data, skb->len); 407 vcc->push(vcc, skb);
402 408
403 vcc->push(vcc, skb); 409 atomic_inc(&vcc->stats->rx);
410 out:
411 skb_trim(sarb, 0);
412 }
413}
404 414
405 atomic_inc(&vcc->stats->rx); 415static void usbatm_extract_cells(struct usbatm_data *instance,
406 out: 416 unsigned char *source, unsigned int avail_data)
407 skb_trim(sarb, 0); 417{
418 unsigned int stride = instance->rx_channel.stride;
419 unsigned int buf_usage = instance->buf_usage;
420
421 /* extract cells from incoming data, taking into account that
422 * the length of avail data may not be a multiple of stride */
423
424 if (buf_usage > 0) {
425 /* we have a partially received atm cell */
426 unsigned char *cell_buf = instance->cell_buf;
427 unsigned int space_left = stride - buf_usage;
428
429 UDSL_ASSERT(buf_usage <= stride);
430
431 if (avail_data >= space_left) {
432 /* add new data and process cell */
433 memcpy(cell_buf + buf_usage, source, space_left);
434 source += space_left;
435 avail_data -= space_left;
436 usbatm_extract_one_cell(instance, cell_buf);
437 instance->buf_usage = 0;
438 } else {
439 /* not enough data to fill the cell */
440 memcpy(cell_buf + buf_usage, source, avail_data);
441 instance->buf_usage = buf_usage + avail_data;
442 return;
408 } 443 }
409 } 444 }
445
446 for (; avail_data >= stride; avail_data -= stride, source += stride)
447 usbatm_extract_one_cell(instance, source);
448
449 if (avail_data > 0) {
450 /* length was not a multiple of stride -
451 * save remaining data for next call */
452 memcpy(instance->cell_buf, source, avail_data);
453 instance->buf_usage = avail_data;
454 }
410} 455}
411 456
412 457
@@ -420,14 +465,14 @@ static unsigned int usbatm_write_cells(struct usbatm_data *instance,
420{ 465{
421 struct usbatm_control *ctrl = UDSL_SKB(skb); 466 struct usbatm_control *ctrl = UDSL_SKB(skb);
422 struct atm_vcc *vcc = ctrl->atm.vcc; 467 struct atm_vcc *vcc = ctrl->atm.vcc;
423 unsigned int num_written; 468 unsigned int bytes_written;
424 unsigned int stride = instance->tx_channel.stride; 469 unsigned int stride = instance->tx_channel.stride;
425 470
426 vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space); 471 vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space);
427 UDSL_ASSERT(!(avail_space % stride)); 472 UDSL_ASSERT(!(avail_space % stride));
428 473
429 for (num_written = 0; num_written < avail_space && ctrl->len; 474 for (bytes_written = 0; bytes_written < avail_space && ctrl->len;
430 num_written += stride, target += stride) { 475 bytes_written += stride, target += stride) {
431 unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD); 476 unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
432 unsigned int left = ATM_CELL_PAYLOAD - data_len; 477 unsigned int left = ATM_CELL_PAYLOAD - data_len;
433 u8 *ptr = target; 478 u8 *ptr = target;
@@ -470,7 +515,7 @@ static unsigned int usbatm_write_cells(struct usbatm_data *instance,
470 ctrl->crc = crc32_be(ctrl->crc, ptr, left); 515 ctrl->crc = crc32_be(ctrl->crc, ptr, left);
471 } 516 }
472 517
473 return num_written; 518 return bytes_written;
474} 519}
475 520
476 521
@@ -487,16 +532,40 @@ static void usbatm_rx_process(unsigned long data)
487 vdbg("%s: processing urb 0x%p", __func__, urb); 532 vdbg("%s: processing urb 0x%p", __func__, urb);
488 533
489 if (usb_pipeisoc(urb->pipe)) { 534 if (usb_pipeisoc(urb->pipe)) {
535 unsigned char *merge_start = NULL;
536 unsigned int merge_length = 0;
537 const unsigned int packet_size = instance->rx_channel.packet_size;
490 int i; 538 int i;
491 for (i = 0; i < urb->number_of_packets; i++) 539
492 if (!urb->iso_frame_desc[i].status) 540 for (i = 0; i < urb->number_of_packets; i++) {
493 usbatm_extract_cells(instance, 541 if (!urb->iso_frame_desc[i].status) {
494 (u8 *)urb->transfer_buffer + urb->iso_frame_desc[i].offset, 542 unsigned int actual_length = urb->iso_frame_desc[i].actual_length;
495 urb->iso_frame_desc[i].actual_length); 543
496 } 544 UDSL_ASSERT(actual_length <= packet_size);
497 else 545
546 if (!merge_length)
547 merge_start = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
548 merge_length += actual_length;
549 if (merge_length && (actual_length < packet_size)) {
550 usbatm_extract_cells(instance, merge_start, merge_length);
551 merge_length = 0;
552 }
553 } else {
554 atm_rldbg(instance, "%s: status %d in frame %d!\n", __func__, urb->status, i);
555 if (merge_length)
556 usbatm_extract_cells(instance, merge_start, merge_length);
557 merge_length = 0;
558 instance->buf_usage = 0;
559 }
560 }
561
562 if (merge_length)
563 usbatm_extract_cells(instance, merge_start, merge_length);
564 } else
498 if (!urb->status) 565 if (!urb->status)
499 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length); 566 usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
567 else
568 instance->buf_usage = 0;
500 569
501 if (usbatm_submit_urb(urb)) 570 if (usbatm_submit_urb(urb))
502 return; 571 return;
@@ -514,7 +583,7 @@ static void usbatm_tx_process(unsigned long data)
514 struct sk_buff *skb = instance->current_skb; 583 struct sk_buff *skb = instance->current_skb;
515 struct urb *urb = NULL; 584 struct urb *urb = NULL;
516 const unsigned int buf_size = instance->tx_channel.buf_size; 585 const unsigned int buf_size = instance->tx_channel.buf_size;
517 unsigned int num_written = 0; 586 unsigned int bytes_written = 0;
518 u8 *buffer = NULL; 587 u8 *buffer = NULL;
519 588
520 if (!skb) 589 if (!skb)
@@ -526,16 +595,16 @@ static void usbatm_tx_process(unsigned long data)
526 if (!urb) 595 if (!urb)
527 break; /* no more senders */ 596 break; /* no more senders */
528 buffer = urb->transfer_buffer; 597 buffer = urb->transfer_buffer;
529 num_written = (urb->status == -EAGAIN) ? 598 bytes_written = (urb->status == -EAGAIN) ?
530 urb->transfer_buffer_length : 0; 599 urb->transfer_buffer_length : 0;
531 } 600 }
532 601
533 num_written += usbatm_write_cells(instance, skb, 602 bytes_written += usbatm_write_cells(instance, skb,
534 buffer + num_written, 603 buffer + bytes_written,
535 buf_size - num_written); 604 buf_size - bytes_written);
536 605
537 vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p", 606 vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p",
538 __func__, num_written, skb, urb); 607 __func__, bytes_written, skb, urb);
539 608
540 if (!UDSL_SKB(skb)->len) { 609 if (!UDSL_SKB(skb)->len) {
541 struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc; 610 struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
@@ -546,8 +615,8 @@ static void usbatm_tx_process(unsigned long data)
546 skb = skb_dequeue(&instance->sndqueue); 615 skb = skb_dequeue(&instance->sndqueue);
547 } 616 }
548 617
549 if (num_written == buf_size || (!skb && num_written)) { 618 if (bytes_written == buf_size || (!skb && bytes_written)) {
550 urb->transfer_buffer_length = num_written; 619 urb->transfer_buffer_length = bytes_written;
551 620
552 if (usbatm_submit_urb(urb)) 621 if (usbatm_submit_urb(urb))
553 break; 622 break;
@@ -593,20 +662,24 @@ static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
593 662
594 vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len); 663 vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len);
595 664
596 if (!instance) { 665 /* racy disconnection check - fine */
597 dbg("%s: NULL data!", __func__); 666 if (!instance || instance->disconnected) {
667#ifdef DEBUG
668 if (printk_ratelimit())
669 printk(KERN_DEBUG "%s: %s!\n", __func__, instance ? "disconnected" : "NULL instance");
670#endif
598 err = -ENODEV; 671 err = -ENODEV;
599 goto fail; 672 goto fail;
600 } 673 }
601 674
602 if (vcc->qos.aal != ATM_AAL5) { 675 if (vcc->qos.aal != ATM_AAL5) {
603 atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal); 676 atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
604 err = -EINVAL; 677 err = -EINVAL;
605 goto fail; 678 goto fail;
606 } 679 }
607 680
608 if (skb->len > ATM_MAX_AAL5_PDU) { 681 if (skb->len > ATM_MAX_AAL5_PDU) {
609 atm_dbg(instance, "%s: packet too long (%d vs %d)!\n", 682 atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
610 __func__, skb->len, ATM_MAX_AAL5_PDU); 683 __func__, skb->len, ATM_MAX_AAL5_PDU);
611 err = -EINVAL; 684 err = -EINVAL;
612 goto fail; 685 goto fail;
@@ -665,16 +738,16 @@ static void usbatm_put_instance(struct usbatm_data *instance)
665** ATM ** 738** ATM **
666**********/ 739**********/
667 740
668static void usbatm_atm_dev_close(struct atm_dev *dev) 741static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
669{ 742{
670 struct usbatm_data *instance = dev->dev_data; 743 struct usbatm_data *instance = atm_dev->dev_data;
671 744
672 dbg("%s", __func__); 745 dbg("%s", __func__);
673 746
674 if (!instance) 747 if (!instance)
675 return; 748 return;
676 749
677 dev->dev_data = NULL; 750 atm_dev->dev_data = NULL; /* catch bugs */
678 usbatm_put_instance(instance); /* taken in usbatm_atm_init */ 751 usbatm_put_instance(instance); /* taken in usbatm_atm_init */
679} 752}
680 753
@@ -706,15 +779,19 @@ static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *pag
706 atomic_read(&atm_dev->stats.aal5.rx_err), 779 atomic_read(&atm_dev->stats.aal5.rx_err),
707 atomic_read(&atm_dev->stats.aal5.rx_drop)); 780 atomic_read(&atm_dev->stats.aal5.rx_drop));
708 781
709 if (!left--) 782 if (!left--) {
710 switch (atm_dev->signal) { 783 if (instance->disconnected)
711 case ATM_PHY_SIG_FOUND: 784 return sprintf(page, "Disconnected\n");
712 return sprintf(page, "Line up\n"); 785 else
713 case ATM_PHY_SIG_LOST: 786 switch (atm_dev->signal) {
714 return sprintf(page, "Line down\n"); 787 case ATM_PHY_SIG_FOUND:
715 default: 788 return sprintf(page, "Line up\n");
716 return sprintf(page, "Line state unknown\n"); 789 case ATM_PHY_SIG_LOST:
717 } 790 return sprintf(page, "Line down\n");
791 default:
792 return sprintf(page, "Line state unknown\n");
793 }
794 }
718 795
719 return 0; 796 return 0;
720} 797}
@@ -735,13 +812,24 @@ static int usbatm_atm_open(struct atm_vcc *vcc)
735 atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci); 812 atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
736 813
737 /* only support AAL5 */ 814 /* only support AAL5 */
738 if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) 815 if ((vcc->qos.aal != ATM_AAL5)) {
739 || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) { 816 atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
740 atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal); 817 return -EINVAL;
818 }
819
820 /* sanity checks */
821 if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
822 atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
741 return -EINVAL; 823 return -EINVAL;
742 } 824 }
743 825
744 down(&instance->serialize); /* vs self, usbatm_atm_close */ 826 mutex_lock(&instance->serialize); /* vs self, usbatm_atm_close, usbatm_usb_disconnect */
827
828 if (instance->disconnected) {
829 atm_dbg(instance, "%s: disconnected!\n", __func__);
830 ret = -ENODEV;
831 goto fail;
832 }
745 833
746 if (usbatm_find_vcc(instance, vpi, vci)) { 834 if (usbatm_find_vcc(instance, vpi, vci)) {
747 atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci); 835 atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
@@ -749,20 +837,19 @@ static int usbatm_atm_open(struct atm_vcc *vcc)
749 goto fail; 837 goto fail;
750 } 838 }
751 839
752 if (!(new = kmalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) { 840 if (!(new = kzalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
753 atm_dbg(instance, "%s: no memory for vcc_data!\n", __func__); 841 atm_err(instance, "%s: no memory for vcc_data!\n", __func__);
754 ret = -ENOMEM; 842 ret = -ENOMEM;
755 goto fail; 843 goto fail;
756 } 844 }
757 845
758 memset(new, 0, sizeof(struct usbatm_vcc_data));
759 new->vcc = vcc; 846 new->vcc = vcc;
760 new->vpi = vpi; 847 new->vpi = vpi;
761 new->vci = vci; 848 new->vci = vci;
762 849
763 new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL); 850 new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
764 if (!new->sarb) { 851 if (!new->sarb) {
765 atm_dbg(instance, "%s: no memory for SAR buffer!\n", __func__); 852 atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
766 ret = -ENOMEM; 853 ret = -ENOMEM;
767 goto fail; 854 goto fail;
768 } 855 }
@@ -770,6 +857,9 @@ static int usbatm_atm_open(struct atm_vcc *vcc)
770 vcc->dev_data = new; 857 vcc->dev_data = new;
771 858
772 tasklet_disable(&instance->rx_channel.tasklet); 859 tasklet_disable(&instance->rx_channel.tasklet);
860 instance->cached_vcc = new;
861 instance->cached_vpi = vpi;
862 instance->cached_vci = vci;
773 list_add(&new->list, &instance->vcc_list); 863 list_add(&new->list, &instance->vcc_list);
774 tasklet_enable(&instance->rx_channel.tasklet); 864 tasklet_enable(&instance->rx_channel.tasklet);
775 865
@@ -777,7 +867,7 @@ static int usbatm_atm_open(struct atm_vcc *vcc)
777 set_bit(ATM_VF_PARTIAL, &vcc->flags); 867 set_bit(ATM_VF_PARTIAL, &vcc->flags);
778 set_bit(ATM_VF_READY, &vcc->flags); 868 set_bit(ATM_VF_READY, &vcc->flags);
779 869
780 up(&instance->serialize); 870 mutex_unlock(&instance->serialize);
781 871
782 atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new); 872 atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
783 873
@@ -785,7 +875,7 @@ static int usbatm_atm_open(struct atm_vcc *vcc)
785 875
786fail: 876fail:
787 kfree(new); 877 kfree(new);
788 up(&instance->serialize); 878 mutex_unlock(&instance->serialize);
789 return ret; 879 return ret;
790} 880}
791 881
@@ -806,9 +896,14 @@ static void usbatm_atm_close(struct atm_vcc *vcc)
806 896
807 usbatm_cancel_send(instance, vcc); 897 usbatm_cancel_send(instance, vcc);
808 898
809 down(&instance->serialize); /* vs self, usbatm_atm_open */ 899 mutex_lock(&instance->serialize); /* vs self, usbatm_atm_open, usbatm_usb_disconnect */
810 900
811 tasklet_disable(&instance->rx_channel.tasklet); 901 tasklet_disable(&instance->rx_channel.tasklet);
902 if (instance->cached_vcc == vcc_data) {
903 instance->cached_vcc = NULL;
904 instance->cached_vpi = ATM_VPI_UNSPEC;
905 instance->cached_vci = ATM_VCI_UNSPEC;
906 }
812 list_del(&vcc_data->list); 907 list_del(&vcc_data->list);
813 tasklet_enable(&instance->rx_channel.tasklet); 908 tasklet_enable(&instance->rx_channel.tasklet);
814 909
@@ -824,14 +919,21 @@ static void usbatm_atm_close(struct atm_vcc *vcc)
824 clear_bit(ATM_VF_PARTIAL, &vcc->flags); 919 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
825 clear_bit(ATM_VF_ADDR, &vcc->flags); 920 clear_bit(ATM_VF_ADDR, &vcc->flags);
826 921
827 up(&instance->serialize); 922 mutex_unlock(&instance->serialize);
828 923
829 atm_dbg(instance, "%s successful\n", __func__); 924 atm_dbg(instance, "%s successful\n", __func__);
830} 925}
831 926
832static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, 927static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
833 void __user * arg) 928 void __user * arg)
834{ 929{
930 struct usbatm_data *instance = atm_dev->dev_data;
931
932 if (!instance || instance->disconnected) {
933 dbg("%s: %s!", __func__, instance ? "disconnected" : "NULL instance");
934 return -ENODEV;
935 }
936
835 switch (cmd) { 937 switch (cmd) {
836 case ATM_QUERYLOOP: 938 case ATM_QUERYLOOP:
837 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0; 939 return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
@@ -845,10 +947,13 @@ static int usbatm_atm_init(struct usbatm_data *instance)
845 struct atm_dev *atm_dev; 947 struct atm_dev *atm_dev;
846 int ret, i; 948 int ret, i;
847 949
848 /* ATM init */ 950 /* ATM init. The ATM initialization scheme suffers from an intrinsic race
951 * condition: callbacks we register can be executed at once, before we have
952 * initialized the struct atm_dev. To protect against this, all callbacks
953 * abort if atm_dev->dev_data is NULL. */
849 atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL); 954 atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL);
850 if (!atm_dev) { 955 if (!atm_dev) {
851 usb_dbg(instance, "%s: failed to register ATM device!\n", __func__); 956 usb_err(instance, "%s: failed to register ATM device!\n", __func__);
852 return -1; 957 return -1;
853 } 958 }
854 959
@@ -862,12 +967,13 @@ static int usbatm_atm_init(struct usbatm_data *instance)
862 atm_dev->link_rate = 128 * 1000 / 424; 967 atm_dev->link_rate = 128 * 1000 / 424;
863 968
864 if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) { 969 if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
865 atm_dbg(instance, "%s: atm_start failed: %d!\n", __func__, ret); 970 atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
866 goto fail; 971 goto fail;
867 } 972 }
868 973
869 /* ready for ATM callbacks */
870 usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */ 974 usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */
975
976 /* ready for ATM callbacks */
871 mb(); 977 mb();
872 atm_dev->dev_data = instance; 978 atm_dev->dev_data = instance;
873 979
@@ -903,9 +1009,9 @@ static int usbatm_do_heavy_init(void *arg)
903 if (!ret) 1009 if (!ret)
904 ret = usbatm_atm_init(instance); 1010 ret = usbatm_atm_init(instance);
905 1011
906 down(&instance->serialize); 1012 mutex_lock(&instance->serialize);
907 instance->thread_pid = -1; 1013 instance->thread_pid = -1;
908 up(&instance->serialize); 1014 mutex_unlock(&instance->serialize);
909 1015
910 complete_and_exit(&instance->thread_exited, ret); 1016 complete_and_exit(&instance->thread_exited, ret);
911} 1017}
@@ -915,13 +1021,13 @@ static int usbatm_heavy_init(struct usbatm_data *instance)
915 int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL); 1021 int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL);
916 1022
917 if (ret < 0) { 1023 if (ret < 0) {
918 usb_dbg(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret); 1024 usb_err(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret);
919 return ret; 1025 return ret;
920 } 1026 }
921 1027
922 down(&instance->serialize); 1028 mutex_lock(&instance->serialize);
923 instance->thread_pid = ret; 1029 instance->thread_pid = ret;
924 up(&instance->serialize); 1030 mutex_unlock(&instance->serialize);
925 1031
926 wait_for_completion(&instance->thread_started); 1032 wait_for_completion(&instance->thread_started);
927 1033
@@ -951,9 +1057,9 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
951 char *buf; 1057 char *buf;
952 int error = -ENOMEM; 1058 int error = -ENOMEM;
953 int i, length; 1059 int i, length;
954 int need_heavy; 1060 unsigned int maxpacket, num_packets;
955 1061
956 dev_dbg(dev, "%s: trying driver %s with vendor=0x%x, product=0x%x, ifnum %d\n", 1062 dev_dbg(dev, "%s: trying driver %s with vendor=%04x, product=%04x, ifnum %2d\n",
957 __func__, driver->driver_name, 1063 __func__, driver->driver_name,
958 le16_to_cpu(usb_dev->descriptor.idVendor), 1064 le16_to_cpu(usb_dev->descriptor.idVendor),
959 le16_to_cpu(usb_dev->descriptor.idProduct), 1065 le16_to_cpu(usb_dev->descriptor.idProduct),
@@ -962,7 +1068,7 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
962 /* instance init */ 1068 /* instance init */
963 instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL); 1069 instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
964 if (!instance) { 1070 if (!instance) {
965 dev_dbg(dev, "%s: no memory for instance data!\n", __func__); 1071 dev_err(dev, "%s: no memory for instance data!\n", __func__);
966 return -ENOMEM; 1072 return -ENOMEM;
967 } 1073 }
968 1074
@@ -996,66 +1102,96 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
996 snprintf(buf, length, ")"); 1102 snprintf(buf, length, ")");
997 1103
998 bind: 1104 bind:
999 need_heavy = 1; 1105 if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
1000 if (driver->bind && (error = driver->bind(instance, intf, id, &need_heavy)) < 0) { 1106 dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
1001 dev_dbg(dev, "%s: bind failed: %d!\n", __func__, error);
1002 goto fail_free; 1107 goto fail_free;
1003 } 1108 }
1004 1109
1005 /* private fields */ 1110 /* private fields */
1006 1111
1007 kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */ 1112 kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
1008 init_MUTEX(&instance->serialize); 1113 mutex_init(&instance->serialize);
1009 1114
1010 instance->thread_pid = -1; 1115 instance->thread_pid = -1;
1011 init_completion(&instance->thread_started); 1116 init_completion(&instance->thread_started);
1012 init_completion(&instance->thread_exited); 1117 init_completion(&instance->thread_exited);
1013 1118
1014 INIT_LIST_HEAD(&instance->vcc_list); 1119 INIT_LIST_HEAD(&instance->vcc_list);
1120 skb_queue_head_init(&instance->sndqueue);
1015 1121
1016 usbatm_init_channel(&instance->rx_channel); 1122 usbatm_init_channel(&instance->rx_channel);
1017 usbatm_init_channel(&instance->tx_channel); 1123 usbatm_init_channel(&instance->tx_channel);
1018 tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance); 1124 tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
1019 tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance); 1125 tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
1020 instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->in);
1021 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->out);
1022 instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding; 1126 instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
1023 instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding; 1127 instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
1024 instance->rx_channel.buf_size = rcv_buf_size * instance->rx_channel.stride;
1025 instance->tx_channel.buf_size = snd_buf_size * instance->tx_channel.stride;
1026 instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance; 1128 instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
1027 1129
1028 skb_queue_head_init(&instance->sndqueue); 1130 if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
1131 instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
1132 else
1133 instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
1134
1135 instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
1136
1137 /* tx buffer size must be a positive multiple of the stride */
1138 instance->tx_channel.buf_size = max (instance->tx_channel.stride,
1139 snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
1140
1141 /* rx buffer size must be a positive multiple of the endpoint maxpacket */
1142 maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint, 0);
1143
1144 if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
1145 dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
1146 usb_pipeendpoint(instance->rx_channel.endpoint));
1147 error = -EINVAL;
1148 goto fail_unbind;
1149 }
1150
1151 num_packets = max (1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
1152
1153 if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
1154 num_packets--;
1155
1156 instance->rx_channel.buf_size = num_packets * maxpacket;
1157 instance->rx_channel.packet_size = maxpacket;
1158
1159#ifdef DEBUG
1160 for (i = 0; i < 2; i++) {
1161 struct usbatm_channel *channel = i ?
1162 &instance->tx_channel : &instance->rx_channel;
1163
1164 dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n", __func__, channel->buf_size, i ? "tx" : "rx", channel);
1165 }
1166#endif
1167
1168 /* initialize urbs */
1029 1169
1030 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { 1170 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1031 struct urb *urb;
1032 u8 *buffer; 1171 u8 *buffer;
1033 unsigned int iso_packets = 0, iso_size = 0;
1034 struct usbatm_channel *channel = i < num_rcv_urbs ? 1172 struct usbatm_channel *channel = i < num_rcv_urbs ?
1035 &instance->rx_channel : &instance->tx_channel; 1173 &instance->rx_channel : &instance->tx_channel;
1174 struct urb *urb;
1175 unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
1036 1176
1037 if (usb_pipeisoc(channel->endpoint)) { 1177 UDSL_ASSERT(!usb_pipeisoc(channel->endpoint) || usb_pipein(channel->endpoint));
1038 /* don't expect iso out endpoints */
1039 iso_size = usb_maxpacket(instance->usb_dev, channel->endpoint, 0);
1040 iso_size -= iso_size % channel->stride; /* alignment */
1041 BUG_ON(!iso_size);
1042 iso_packets = (channel->buf_size - 1) / iso_size + 1;
1043 }
1044 1178
1045 urb = usb_alloc_urb(iso_packets, GFP_KERNEL); 1179 urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
1046 if (!urb) { 1180 if (!urb) {
1047 dev_dbg(dev, "%s: no memory for urb %d!\n", __func__, i); 1181 dev_err(dev, "%s: no memory for urb %d!\n", __func__, i);
1182 error = -ENOMEM;
1048 goto fail_unbind; 1183 goto fail_unbind;
1049 } 1184 }
1050 1185
1051 instance->urbs[i] = urb; 1186 instance->urbs[i] = urb;
1052 1187
1053 buffer = kmalloc(channel->buf_size, GFP_KERNEL); 1188 /* zero the tx padding to avoid leaking information */
1189 buffer = kzalloc(channel->buf_size, GFP_KERNEL);
1054 if (!buffer) { 1190 if (!buffer) {
1055 dev_dbg(dev, "%s: no memory for buffer %d!\n", __func__, i); 1191 dev_err(dev, "%s: no memory for buffer %d!\n", __func__, i);
1192 error = -ENOMEM;
1056 goto fail_unbind; 1193 goto fail_unbind;
1057 } 1194 }
1058 memset(buffer, 0, channel->buf_size);
1059 1195
1060 usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint, 1196 usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
1061 buffer, channel->buf_size, usbatm_complete, channel); 1197 buffer, channel->buf_size, usbatm_complete, channel);
@@ -1065,9 +1201,8 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1065 urb->transfer_flags = URB_ISO_ASAP; 1201 urb->transfer_flags = URB_ISO_ASAP;
1066 urb->number_of_packets = iso_packets; 1202 urb->number_of_packets = iso_packets;
1067 for (j = 0; j < iso_packets; j++) { 1203 for (j = 0; j < iso_packets; j++) {
1068 urb->iso_frame_desc[j].offset = iso_size * j; 1204 urb->iso_frame_desc[j].offset = channel->packet_size * j;
1069 urb->iso_frame_desc[j].length = min_t(int, iso_size, 1205 urb->iso_frame_desc[j].length = channel->packet_size;
1070 channel->buf_size - urb->iso_frame_desc[j].offset);
1071 } 1206 }
1072 } 1207 }
1073 1208
@@ -1079,7 +1214,17 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1079 __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb); 1214 __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
1080 } 1215 }
1081 1216
1082 if (need_heavy && driver->heavy_init) { 1217 instance->cached_vpi = ATM_VPI_UNSPEC;
1218 instance->cached_vci = ATM_VCI_UNSPEC;
1219 instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
1220
1221 if (!instance->cell_buf) {
1222 dev_err(dev, "%s: no memory for cell buffer!\n", __func__);
1223 error = -ENOMEM;
1224 goto fail_unbind;
1225 }
1226
1227 if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
1083 error = usbatm_heavy_init(instance); 1228 error = usbatm_heavy_init(instance);
1084 } else { 1229 } else {
1085 complete(&instance->thread_exited); /* pretend that heavy_init was run */ 1230 complete(&instance->thread_exited); /* pretend that heavy_init was run */
@@ -1098,6 +1243,8 @@ int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
1098 if (instance->driver->unbind) 1243 if (instance->driver->unbind)
1099 instance->driver->unbind(instance, intf); 1244 instance->driver->unbind(instance, intf);
1100 fail_free: 1245 fail_free:
1246 kfree(instance->cell_buf);
1247
1101 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { 1248 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1102 if (instance->urbs[i]) 1249 if (instance->urbs[i])
1103 kfree(instance->urbs[i]->transfer_buffer); 1250 kfree(instance->urbs[i]->transfer_buffer);
@@ -1114,6 +1261,7 @@ void usbatm_usb_disconnect(struct usb_interface *intf)
1114{ 1261{
1115 struct device *dev = &intf->dev; 1262 struct device *dev = &intf->dev;
1116 struct usbatm_data *instance = usb_get_intfdata(intf); 1263 struct usbatm_data *instance = usb_get_intfdata(intf);
1264 struct usbatm_vcc_data *vcc_data;
1117 int i; 1265 int i;
1118 1266
1119 dev_dbg(dev, "%s entered\n", __func__); 1267 dev_dbg(dev, "%s entered\n", __func__);
@@ -1125,13 +1273,19 @@ void usbatm_usb_disconnect(struct usb_interface *intf)
1125 1273
1126 usb_set_intfdata(intf, NULL); 1274 usb_set_intfdata(intf, NULL);
1127 1275
1128 down(&instance->serialize); 1276 mutex_lock(&instance->serialize);
1277 instance->disconnected = 1;
1129 if (instance->thread_pid >= 0) 1278 if (instance->thread_pid >= 0)
1130 kill_proc(instance->thread_pid, SIGTERM, 1); 1279 kill_proc(instance->thread_pid, SIGTERM, 1);
1131 up(&instance->serialize); 1280 mutex_unlock(&instance->serialize);
1132 1281
1133 wait_for_completion(&instance->thread_exited); 1282 wait_for_completion(&instance->thread_exited);
1134 1283
1284 mutex_lock(&instance->serialize);
1285 list_for_each_entry(vcc_data, &instance->vcc_list, list)
1286 vcc_release_async(vcc_data->vcc, -EPIPE);
1287 mutex_unlock(&instance->serialize);
1288
1135 tasklet_disable(&instance->rx_channel.tasklet); 1289 tasklet_disable(&instance->rx_channel.tasklet);
1136 tasklet_disable(&instance->tx_channel.tasklet); 1290 tasklet_disable(&instance->tx_channel.tasklet);
1137 1291
@@ -1141,6 +1295,14 @@ void usbatm_usb_disconnect(struct usb_interface *intf)
1141 del_timer_sync(&instance->rx_channel.delay); 1295 del_timer_sync(&instance->rx_channel.delay);
1142 del_timer_sync(&instance->tx_channel.delay); 1296 del_timer_sync(&instance->tx_channel.delay);
1143 1297
1298 /* turn usbatm_[rt]x_process into something close to a no-op */
1299 /* no need to take the spinlock */
1300 INIT_LIST_HEAD(&instance->rx_channel.list);
1301 INIT_LIST_HEAD(&instance->tx_channel.list);
1302
1303 tasklet_enable(&instance->rx_channel.tasklet);
1304 tasklet_enable(&instance->tx_channel.tasklet);
1305
1144 if (instance->atm_dev && instance->driver->atm_stop) 1306 if (instance->atm_dev && instance->driver->atm_stop)
1145 instance->driver->atm_stop(instance, instance->atm_dev); 1307 instance->driver->atm_stop(instance, instance->atm_dev);
1146 1308
@@ -1149,19 +1311,13 @@ void usbatm_usb_disconnect(struct usb_interface *intf)
1149 1311
1150 instance->driver_data = NULL; 1312 instance->driver_data = NULL;
1151 1313
1152 /* turn usbatm_[rt]x_process into noop */
1153 /* no need to take the spinlock */
1154 INIT_LIST_HEAD(&instance->rx_channel.list);
1155 INIT_LIST_HEAD(&instance->tx_channel.list);
1156
1157 tasklet_enable(&instance->rx_channel.tasklet);
1158 tasklet_enable(&instance->tx_channel.tasklet);
1159
1160 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { 1314 for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
1161 kfree(instance->urbs[i]->transfer_buffer); 1315 kfree(instance->urbs[i]->transfer_buffer);
1162 usb_free_urb(instance->urbs[i]); 1316 usb_free_urb(instance->urbs[i]);
1163 } 1317 }
1164 1318
1319 kfree(instance->cell_buf);
1320
1165 /* ATM finalize */ 1321 /* ATM finalize */
1166 if (instance->atm_dev) 1322 if (instance->atm_dev)
1167 atm_dev_deregister(instance->atm_dev); 1323 atm_dev_deregister(instance->atm_dev);
@@ -1186,10 +1342,10 @@ static int __init usbatm_usb_init(void)
1186 1342
1187 if ((num_rcv_urbs > UDSL_MAX_RCV_URBS) 1343 if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
1188 || (num_snd_urbs > UDSL_MAX_SND_URBS) 1344 || (num_snd_urbs > UDSL_MAX_SND_URBS)
1189 || (rcv_buf_size < 1) 1345 || (rcv_buf_bytes < 1)
1190 || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE) 1346 || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
1191 || (snd_buf_size < 1) 1347 || (snd_buf_bytes < 1)
1192 || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE)) 1348 || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
1193 return -EINVAL; 1349 return -EINVAL;
1194 1350
1195 return 0; 1351 return 0;
diff --git a/drivers/usb/atm/usbatm.h b/drivers/usb/atm/usbatm.h
index 1adacd60d713..ff8551e93372 100644
--- a/drivers/usb/atm/usbatm.h
+++ b/drivers/usb/atm/usbatm.h
@@ -24,21 +24,21 @@
24#ifndef _USBATM_H_ 24#ifndef _USBATM_H_
25#define _USBATM_H_ 25#define _USBATM_H_
26 26
27#include <linux/config.h>
28
29/*
30#define VERBOSE_DEBUG
31*/
32
33#include <asm/semaphore.h> 27#include <asm/semaphore.h>
34#include <linux/atm.h> 28#include <linux/atm.h>
35#include <linux/atmdev.h> 29#include <linux/atmdev.h>
36#include <linux/completion.h> 30#include <linux/completion.h>
37#include <linux/device.h> 31#include <linux/device.h>
32#include <linux/kernel.h>
38#include <linux/kref.h> 33#include <linux/kref.h>
39#include <linux/list.h> 34#include <linux/list.h>
40#include <linux/stringify.h> 35#include <linux/stringify.h>
41#include <linux/usb.h> 36#include <linux/usb.h>
37#include <linux/mutex.h>
38
39/*
40#define VERBOSE_DEBUG
41*/
42 42
43#ifdef DEBUG 43#ifdef DEBUG
44#define UDSL_ASSERT(x) BUG_ON(!(x)) 44#define UDSL_ASSERT(x) BUG_ON(!(x))
@@ -52,8 +52,13 @@
52 dev_info(&(instance)->usb_intf->dev , format , ## arg) 52 dev_info(&(instance)->usb_intf->dev , format , ## arg)
53#define usb_warn(instance, format, arg...) \ 53#define usb_warn(instance, format, arg...) \
54 dev_warn(&(instance)->usb_intf->dev , format , ## arg) 54 dev_warn(&(instance)->usb_intf->dev , format , ## arg)
55#ifdef DEBUG
55#define usb_dbg(instance, format, arg...) \ 56#define usb_dbg(instance, format, arg...) \
56 dev_dbg(&(instance)->usb_intf->dev , format , ## arg) 57 dev_printk(KERN_DEBUG , &(instance)->usb_intf->dev , format , ## arg)
58#else
59#define usb_dbg(instance, format, arg...) \
60 do {} while (0)
61#endif
57 62
58/* FIXME: move to dev_* once ATM is driver model aware */ 63/* FIXME: move to dev_* once ATM is driver model aware */
59#define atm_printk(level, instance, format, arg...) \ 64#define atm_printk(level, instance, format, arg...) \
@@ -69,12 +74,24 @@
69#ifdef DEBUG 74#ifdef DEBUG
70#define atm_dbg(instance, format, arg...) \ 75#define atm_dbg(instance, format, arg...) \
71 atm_printk(KERN_DEBUG, instance , format , ## arg) 76 atm_printk(KERN_DEBUG, instance , format , ## arg)
77#define atm_rldbg(instance, format, arg...) \
78 if (printk_ratelimit()) \
79 atm_printk(KERN_DEBUG, instance , format , ## arg)
72#else 80#else
73#define atm_dbg(instance, format, arg...) \ 81#define atm_dbg(instance, format, arg...) \
74 do {} while (0) 82 do {} while (0)
83#define atm_rldbg(instance, format, arg...) \
84 do {} while (0)
75#endif 85#endif
76 86
77 87
88/* flags, set by mini-driver in bind() */
89
90#define UDSL_SKIP_HEAVY_INIT (1<<0)
91#define UDSL_USE_ISOC (1<<1)
92#define UDSL_IGNORE_EILSEQ (1<<2)
93
94
78/* mini driver */ 95/* mini driver */
79 96
80struct usbatm_data; 97struct usbatm_data;
@@ -86,16 +103,11 @@ struct usbatm_data;
86*/ 103*/
87 104
88struct usbatm_driver { 105struct usbatm_driver {
89 struct module *owner;
90
91 const char *driver_name; 106 const char *driver_name;
92 107
93 /* 108 /* init device ... can sleep, or cause probe() failure */
94 * init device ... can sleep, or cause probe() failure. Drivers with a heavy_init
95 * method can avoid having it called by setting need_heavy_init to zero.
96 */
97 int (*bind) (struct usbatm_data *, struct usb_interface *, 109 int (*bind) (struct usbatm_data *, struct usb_interface *,
98 const struct usb_device_id *id, int *need_heavy_init); 110 const struct usb_device_id *id);
99 111
100 /* additional device initialization that is too slow to be done in probe() */ 112 /* additional device initialization that is too slow to be done in probe() */
101 int (*heavy_init) (struct usbatm_data *, struct usb_interface *); 113 int (*heavy_init) (struct usbatm_data *, struct usb_interface *);
@@ -109,8 +121,9 @@ struct usbatm_driver {
109 /* cleanup ATM device ... can sleep, but can't fail */ 121 /* cleanup ATM device ... can sleep, but can't fail */
110 void (*atm_stop) (struct usbatm_data *, struct atm_dev *); 122 void (*atm_stop) (struct usbatm_data *, struct atm_dev *);
111 123
112 int in; /* rx endpoint */ 124 int bulk_in; /* bulk rx endpoint */
113 int out; /* tx endpoint */ 125 int isoc_in; /* isochronous rx endpoint */
126 int bulk_out; /* bulk tx endpoint */
114 127
115 unsigned rx_padding; 128 unsigned rx_padding;
116 unsigned tx_padding; 129 unsigned tx_padding;
@@ -125,6 +138,7 @@ struct usbatm_channel {
125 int endpoint; /* usb pipe */ 138 int endpoint; /* usb pipe */
126 unsigned int stride; /* ATM cell size + padding */ 139 unsigned int stride; /* ATM cell size + padding */
127 unsigned int buf_size; /* urb buffer size */ 140 unsigned int buf_size; /* urb buffer size */
141 unsigned int packet_size; /* endpoint maxpacket */
128 spinlock_t lock; 142 spinlock_t lock;
129 struct list_head list; 143 struct list_head list;
130 struct tasklet_struct tasklet; 144 struct tasklet_struct tasklet;
@@ -143,6 +157,7 @@ struct usbatm_data {
143 struct usbatm_driver *driver; 157 struct usbatm_driver *driver;
144 void *driver_data; 158 void *driver_data;
145 char driver_name[16]; 159 char driver_name[16];
160 unsigned int flags; /* set by mini-driver in bind() */
146 161
147 /* USB device */ 162 /* USB device */
148 struct usb_device *usb_dev; 163 struct usb_device *usb_dev;
@@ -157,7 +172,8 @@ struct usbatm_data {
157 ********************************/ 172 ********************************/
158 173
159 struct kref refcount; 174 struct kref refcount;
160 struct semaphore serialize; 175 struct mutex serialize;
176 int disconnected;
161 177
162 /* heavy init */ 178 /* heavy init */
163 int thread_pid; 179 int thread_pid;
@@ -171,7 +187,14 @@ struct usbatm_data {
171 struct usbatm_channel tx_channel; 187 struct usbatm_channel tx_channel;
172 188
173 struct sk_buff_head sndqueue; 189 struct sk_buff_head sndqueue;
174 struct sk_buff *current_skb; /* being emptied */ 190 struct sk_buff *current_skb; /* being emptied */
191
192 struct usbatm_vcc_data *cached_vcc;
193 int cached_vci;
194 short cached_vpi;
195
196 unsigned char *cell_buf; /* holds partial rx cell */
197 unsigned int buf_usage;
175 198
176 struct urb *urbs[0]; 199 struct urb *urbs[0];
177}; 200};
diff --git a/drivers/usb/atm/xusbatm.c b/drivers/usb/atm/xusbatm.c
index 5c76e3aaaa5e..42d6823b82b3 100644
--- a/drivers/usb/atm/xusbatm.c
+++ b/drivers/usb/atm/xusbatm.c
@@ -41,6 +41,8 @@ XUSBATM_PARM(rx_endpoint, unsigned char, byte, "rx endpoint number");
41XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number"); 41XUSBATM_PARM(tx_endpoint, unsigned char, byte, "tx endpoint number");
42XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)"); 42XUSBATM_PARM(rx_padding, unsigned char, byte, "rx padding (default 0)");
43XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)"); 43XUSBATM_PARM(tx_padding, unsigned char, byte, "tx padding (default 0)");
44XUSBATM_PARM(rx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
45XUSBATM_PARM(tx_altsetting, unsigned char, byte, "rx altsetting (default 0)");
44 46
45static const char xusbatm_driver_name[] = "xusbatm"; 47static const char xusbatm_driver_name[] = "xusbatm";
46 48
@@ -48,82 +50,118 @@ static struct usbatm_driver xusbatm_drivers[XUSBATM_DRIVERS_MAX];
48static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1]; 50static struct usb_device_id xusbatm_usb_ids[XUSBATM_DRIVERS_MAX + 1];
49static struct usb_driver xusbatm_usb_driver; 51static struct usb_driver xusbatm_usb_driver;
50 52
51static int usb_intf_has_ep(const struct usb_interface *intf, u8 ep) 53static struct usb_interface *xusbatm_find_intf (struct usb_device *usb_dev, int altsetting, u8 ep)
52{ 54{
55 struct usb_host_interface *alt;
56 struct usb_interface *intf;
53 int i, j; 57 int i, j;
54 58
55 for (i = 0; i < intf->num_altsetting; i++) { 59 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++)
56 struct usb_host_interface *alt = intf->altsetting; 60 if ((intf = usb_dev->actconfig->interface[i]) && (alt = usb_altnum_to_altsetting(intf, altsetting)))
57 for (j = 0; j < alt->desc.bNumEndpoints; j++) 61 for (j = 0; j < alt->desc.bNumEndpoints; j++)
58 if ((alt->endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) == ep) 62 if (alt->endpoint[j].desc.bEndpointAddress == ep)
59 return 1; 63 return intf;
64 return NULL;
65}
66
67static int xusbatm_capture_intf (struct usbatm_data *usbatm, struct usb_device *usb_dev,
68 struct usb_interface *intf, int altsetting, int claim)
69{
70 int ifnum = intf->altsetting->desc.bInterfaceNumber;
71 int ret;
72
73 if (claim && (ret = usb_driver_claim_interface(&xusbatm_usb_driver, intf, usbatm))) {
74 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, ifnum, ret);
75 return ret;
76 }
77 if ((ret = usb_set_interface(usb_dev, ifnum, altsetting))) {
78 usb_err(usbatm, "%s: altsetting %2d for interface %2d failed (%d)!\n", __func__, altsetting, ifnum, ret);
79 return ret;
60 } 80 }
61 return 0; 81 return 0;
62} 82}
63 83
64static int xusbatm_bind(struct usbatm_data *usbatm_instance, 84static void xusbatm_release_intf (struct usb_device *usb_dev, struct usb_interface *intf, int claimed)
65 struct usb_interface *intf, const struct usb_device_id *id, 85{
66 int *need_heavy_init) 86 if (claimed) {
87 usb_set_intfdata(intf, NULL);
88 usb_driver_release_interface(&xusbatm_usb_driver, intf);
89 }
90}
91
92static int xusbatm_bind(struct usbatm_data *usbatm,
93 struct usb_interface *intf, const struct usb_device_id *id)
67{ 94{
68 struct usb_device *usb_dev = interface_to_usbdev(intf); 95 struct usb_device *usb_dev = interface_to_usbdev(intf);
69 int drv_ix = id - xusbatm_usb_ids; 96 int drv_ix = id - xusbatm_usb_ids;
70 int rx_ep_present = usb_intf_has_ep(intf, rx_endpoint[drv_ix]); 97 int rx_alt = rx_altsetting[drv_ix];
71 int tx_ep_present = usb_intf_has_ep(intf, tx_endpoint[drv_ix]); 98 int tx_alt = tx_altsetting[drv_ix];
72 u8 searched_ep = rx_ep_present ? tx_endpoint[drv_ix] : rx_endpoint[drv_ix]; 99 struct usb_interface *rx_intf = xusbatm_find_intf(usb_dev, rx_alt, rx_endpoint[drv_ix]);
73 int i, ret; 100 struct usb_interface *tx_intf = xusbatm_find_intf(usb_dev, tx_alt, tx_endpoint[drv_ix]);
74 101 int ret;
75 usb_dbg(usbatm_instance, "%s: binding driver %d: vendor %#x product %#x" 102
76 " rx: ep %#x padd %d tx: ep %#x padd %d\n", 103 usb_dbg(usbatm, "%s: binding driver %d: vendor %04x product %04x"
104 " rx: ep %02x padd %d alt %2d tx: ep %02x padd %d alt %2d\n",
77 __func__, drv_ix, vendor[drv_ix], product[drv_ix], 105 __func__, drv_ix, vendor[drv_ix], product[drv_ix],
78 rx_endpoint[drv_ix], rx_padding[drv_ix], 106 rx_endpoint[drv_ix], rx_padding[drv_ix], rx_alt,
79 tx_endpoint[drv_ix], tx_padding[drv_ix]); 107 tx_endpoint[drv_ix], tx_padding[drv_ix], tx_alt);
108
109 if (!rx_intf || !tx_intf) {
110 if (!rx_intf)
111 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
112 __func__, rx_endpoint[drv_ix], rx_alt);
113 if (!tx_intf)
114 usb_dbg(usbatm, "%s: no interface contains endpoint %02x in altsetting %2d\n",
115 __func__, tx_endpoint[drv_ix], tx_alt);
116 return -ENODEV;
117 }
80 118
81 if (!rx_ep_present && !tx_ep_present) { 119 if ((rx_intf != intf) && (tx_intf != intf))
82 usb_dbg(usbatm_instance, "%s: intf #%d has neither rx (%#x) nor tx (%#x) endpoint\n",
83 __func__, intf->altsetting->desc.bInterfaceNumber,
84 rx_endpoint[drv_ix], tx_endpoint[drv_ix]);
85 return -ENODEV; 120 return -ENODEV;
121
122 if ((rx_intf == tx_intf) && (rx_alt != tx_alt)) {
123 usb_err(usbatm, "%s: altsettings clash on interface %2d (%2d vs %2d)!\n", __func__,
124 rx_intf->altsetting->desc.bInterfaceNumber, rx_alt, tx_alt);
125 return -EINVAL;
86 } 126 }
87 127
88 if (rx_ep_present && tx_ep_present) 128 usb_dbg(usbatm, "%s: rx If#=%2d; tx If#=%2d\n", __func__,
89 return 0; 129 rx_intf->altsetting->desc.bInterfaceNumber,
130 tx_intf->altsetting->desc.bInterfaceNumber);
90 131
91 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) { 132 if ((ret = xusbatm_capture_intf(usbatm, usb_dev, rx_intf, rx_alt, rx_intf != intf)))
92 struct usb_interface *cur_if = usb_dev->actconfig->interface[i]; 133 return ret;
93 134
94 if (cur_if != intf && usb_intf_has_ep(cur_if, searched_ep)) { 135 if ((tx_intf != rx_intf) && (ret = xusbatm_capture_intf(usbatm, usb_dev, tx_intf, tx_alt, tx_intf != intf))) {
95 ret = usb_driver_claim_interface(&xusbatm_usb_driver, 136 xusbatm_release_intf(usb_dev, rx_intf, rx_intf != intf);
96 cur_if, usbatm_instance); 137 return ret;
97 if (!ret)
98 usb_err(usbatm_instance, "%s: failed to claim interface #%d (%d)\n",
99 __func__, cur_if->altsetting->desc.bInterfaceNumber, ret);
100 return ret;
101 }
102 } 138 }
103 139
104 usb_err(usbatm_instance, "%s: no interface has endpoint %#x\n", 140 return 0;
105 __func__, searched_ep);
106 return -ENODEV;
107} 141}
108 142
109static void xusbatm_unbind(struct usbatm_data *usbatm_instance, 143static void xusbatm_unbind(struct usbatm_data *usbatm,
110 struct usb_interface *intf) 144 struct usb_interface *intf)
111{ 145{
112 struct usb_device *usb_dev = interface_to_usbdev(intf); 146 struct usb_device *usb_dev = interface_to_usbdev(intf);
113 int i; 147 int i;
114 usb_dbg(usbatm_instance, "%s entered\n", __func__); 148
149 usb_dbg(usbatm, "%s entered\n", __func__);
115 150
116 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) { 151 for(i = 0; i < usb_dev->actconfig->desc.bNumInterfaces; i++) {
117 struct usb_interface *cur_if = usb_dev->actconfig->interface[i]; 152 struct usb_interface *cur_intf = usb_dev->actconfig->interface[i];
118 usb_set_intfdata(cur_if, NULL); 153
119 usb_driver_release_interface(&xusbatm_usb_driver, cur_if); 154 if (cur_intf && (usb_get_intfdata(cur_intf) == usbatm)) {
155 usb_set_intfdata(cur_intf, NULL);
156 usb_driver_release_interface(&xusbatm_usb_driver, cur_intf);
157 }
120 } 158 }
121} 159}
122 160
123static int xusbatm_atm_start(struct usbatm_data *usbatm_instance, 161static int xusbatm_atm_start(struct usbatm_data *usbatm,
124 struct atm_dev *atm_dev) 162 struct atm_dev *atm_dev)
125{ 163{
126 atm_dbg(usbatm_instance, "%s entered\n", __func__); 164 atm_dbg(usbatm, "%s entered\n", __func__);
127 165
128 /* use random MAC as we've no way to get it from the device */ 166 /* use random MAC as we've no way to get it from the device */
129 random_ether_addr(atm_dev->esi); 167 random_ether_addr(atm_dev->esi);
@@ -161,18 +199,19 @@ static int __init xusbatm_init(void)
161 } 199 }
162 200
163 for (i = 0; i < num_vendor; i++) { 201 for (i = 0; i < num_vendor; i++) {
202 rx_endpoint[i] |= USB_DIR_IN;
203 tx_endpoint[i] &= USB_ENDPOINT_NUMBER_MASK;
204
164 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 205 xusbatm_usb_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
165 xusbatm_usb_ids[i].idVendor = vendor[i]; 206 xusbatm_usb_ids[i].idVendor = vendor[i];
166 xusbatm_usb_ids[i].idProduct = product[i]; 207 xusbatm_usb_ids[i].idProduct = product[i];
167 208
168
169 xusbatm_drivers[i].owner = THIS_MODULE;
170 xusbatm_drivers[i].driver_name = xusbatm_driver_name; 209 xusbatm_drivers[i].driver_name = xusbatm_driver_name;
171 xusbatm_drivers[i].bind = xusbatm_bind; 210 xusbatm_drivers[i].bind = xusbatm_bind;
172 xusbatm_drivers[i].unbind = xusbatm_unbind; 211 xusbatm_drivers[i].unbind = xusbatm_unbind;
173 xusbatm_drivers[i].atm_start = xusbatm_atm_start; 212 xusbatm_drivers[i].atm_start = xusbatm_atm_start;
174 xusbatm_drivers[i].in = rx_endpoint[i]; 213 xusbatm_drivers[i].bulk_in = rx_endpoint[i];
175 xusbatm_drivers[i].out = tx_endpoint[i]; 214 xusbatm_drivers[i].bulk_out = tx_endpoint[i];
176 xusbatm_drivers[i].rx_padding = rx_padding[i]; 215 xusbatm_drivers[i].rx_padding = rx_padding[i];
177 xusbatm_drivers[i].tx_padding = tx_padding[i]; 216 xusbatm_drivers[i].tx_padding = tx_padding[i];
178 } 217 }
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index b9fd39fd1b5b..97bdeb1c2181 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -1014,8 +1014,13 @@ static void acm_disconnect(struct usb_interface *intf)
1014 } 1014 }
1015 1015
1016 down(&open_sem); 1016 down(&open_sem);
1017 if (!usb_get_intfdata(intf)) {
1018 up(&open_sem);
1019 return;
1020 }
1017 acm->dev = NULL; 1021 acm->dev = NULL;
1018 usb_set_intfdata (intf, NULL); 1022 usb_set_intfdata(acm->control, NULL);
1023 usb_set_intfdata(acm->data, NULL);
1019 1024
1020 tasklet_disable(&acm->urb_task); 1025 tasklet_disable(&acm->urb_task);
1021 1026
@@ -1036,7 +1041,7 @@ static void acm_disconnect(struct usb_interface *intf)
1036 for (i = 0; i < ACM_NRB; i++) 1041 for (i = 0; i < ACM_NRB; i++)
1037 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); 1042 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
1038 1043
1039 usb_driver_release_interface(&acm_driver, acm->data); 1044 usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf);
1040 1045
1041 if (!acm->used) { 1046 if (!acm->used) {
1042 acm_tty_unregister(acm); 1047 acm_tty_unregister(acm);
diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index dba4cc026077..d34848ac30b0 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -7,6 +7,7 @@
7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> 7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
8 # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com> 8 # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com>
9 # Copyright (c) 2001 David Paschal <paschal@rcsis.com> 9 # Copyright (c) 2001 David Paschal <paschal@rcsis.com>
10 * Copyright (c) 2006 Oliver Neukum <oliver@neukum.name>
10 * 11 *
11 * USB Printer Device Class driver for USB printers and printer cables 12 * USB Printer Device Class driver for USB printers and printer cables
12 * 13 *
@@ -273,13 +274,16 @@ static void usblp_bulk_read(struct urb *urb, struct pt_regs *regs)
273{ 274{
274 struct usblp *usblp = urb->context; 275 struct usblp *usblp = urb->context;
275 276
276 if (!usblp || !usblp->dev || !usblp->used || !usblp->present) 277 if (unlikely(!usblp || !usblp->dev || !usblp->used))
277 return; 278 return;
278 279
280 if (unlikely(!usblp->present))
281 goto unplug;
279 if (unlikely(urb->status)) 282 if (unlikely(urb->status))
280 warn("usblp%d: nonzero read/write bulk status received: %d", 283 warn("usblp%d: nonzero read/write bulk status received: %d",
281 usblp->minor, urb->status); 284 usblp->minor, urb->status);
282 usblp->rcomplete = 1; 285 usblp->rcomplete = 1;
286unplug:
283 wake_up_interruptible(&usblp->wait); 287 wake_up_interruptible(&usblp->wait);
284} 288}
285 289
@@ -287,13 +291,15 @@ static void usblp_bulk_write(struct urb *urb, struct pt_regs *regs)
287{ 291{
288 struct usblp *usblp = urb->context; 292 struct usblp *usblp = urb->context;
289 293
290 if (!usblp || !usblp->dev || !usblp->used || !usblp->present) 294 if (unlikely(!usblp || !usblp->dev || !usblp->used))
291 return; 295 return;
292 296 if (unlikely(!usblp->present))
297 goto unplug;
293 if (unlikely(urb->status)) 298 if (unlikely(urb->status))
294 warn("usblp%d: nonzero read/write bulk status received: %d", 299 warn("usblp%d: nonzero read/write bulk status received: %d",
295 usblp->minor, urb->status); 300 usblp->minor, urb->status);
296 usblp->wcomplete = 1; 301 usblp->wcomplete = 1;
302unplug:
297 wake_up_interruptible(&usblp->wait); 303 wake_up_interruptible(&usblp->wait);
298} 304}
299 305
@@ -627,9 +633,8 @@ done:
627 633
628static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 634static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
629{ 635{
630 DECLARE_WAITQUEUE(wait, current);
631 struct usblp *usblp = file->private_data; 636 struct usblp *usblp = file->private_data;
632 int timeout, err = 0, transfer_length = 0; 637 int timeout, rv, err = 0, transfer_length = 0;
633 size_t writecount = 0; 638 size_t writecount = 0;
634 639
635 while (writecount < count) { 640 while (writecount < count) {
@@ -641,24 +646,11 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
641 } 646 }
642 647
643 timeout = USBLP_WRITE_TIMEOUT; 648 timeout = USBLP_WRITE_TIMEOUT;
644 add_wait_queue(&usblp->wait, &wait);
645 while ( 1==1 ) {
646 649
647 if (signal_pending(current)) { 650 rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
648 remove_wait_queue(&usblp->wait, &wait); 651 if (rv < 0)
649 return writecount ? writecount : -EINTR; 652 return writecount ? writecount : -EINTR;
650 }
651 set_current_state(TASK_INTERRUPTIBLE);
652 if (timeout && !usblp->wcomplete) {
653 timeout = schedule_timeout(timeout);
654 } else {
655 set_current_state(TASK_RUNNING);
656 break;
657 }
658 }
659 remove_wait_queue(&usblp->wait, &wait);
660 } 653 }
661
662 down (&usblp->sem); 654 down (&usblp->sem);
663 if (!usblp->present) { 655 if (!usblp->present) {
664 up (&usblp->sem); 656 up (&usblp->sem);
@@ -724,7 +716,7 @@ static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t
724static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 716static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
725{ 717{
726 struct usblp *usblp = file->private_data; 718 struct usblp *usblp = file->private_data;
727 DECLARE_WAITQUEUE(wait, current); 719 int rv;
728 720
729 if (!usblp->bidir) 721 if (!usblp->bidir)
730 return -EINVAL; 722 return -EINVAL;
@@ -742,26 +734,13 @@ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count,
742 count = -EAGAIN; 734 count = -EAGAIN;
743 goto done; 735 goto done;
744 } 736 }
745 737 up(&usblp->sem);
746 add_wait_queue(&usblp->wait, &wait); 738 rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
747 while (1==1) { 739 down(&usblp->sem);
748 if (signal_pending(current)) { 740 if (rv < 0) {
749 count = -EINTR; 741 count = -EINTR;
750 remove_wait_queue(&usblp->wait, &wait); 742 goto done;
751 goto done;
752 }
753 up (&usblp->sem);
754 set_current_state(TASK_INTERRUPTIBLE);
755 if (!usblp->rcomplete) {
756 schedule();
757 } else {
758 set_current_state(TASK_RUNNING);
759 down(&usblp->sem);
760 break;
761 }
762 down (&usblp->sem);
763 } 743 }
764 remove_wait_queue(&usblp->wait, &wait);
765 } 744 }
766 745
767 if (!usblp->present) { 746 if (!usblp->present) {
@@ -874,11 +853,10 @@ static int usblp_probe(struct usb_interface *intf,
874 853
875 /* Malloc and start initializing usblp structure so we can use it 854 /* Malloc and start initializing usblp structure so we can use it
876 * directly. */ 855 * directly. */
877 if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) { 856 if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
878 err("out of memory for usblp"); 857 err("out of memory for usblp");
879 goto abort; 858 goto abort;
880 } 859 }
881 memset(usblp, 0, sizeof(struct usblp));
882 usblp->dev = dev; 860 usblp->dev = dev;
883 init_MUTEX (&usblp->sem); 861 init_MUTEX (&usblp->sem);
884 init_waitqueue_head(&usblp->wait); 862 init_waitqueue_head(&usblp->wait);
@@ -1214,10 +1192,9 @@ static int __init usblp_init(void)
1214{ 1192{
1215 int retval; 1193 int retval;
1216 retval = usb_register(&usblp_driver); 1194 retval = usb_register(&usblp_driver);
1217 if (retval) 1195 if (!retval)
1218 goto out; 1196 info(DRIVER_VERSION ": " DRIVER_DESC);
1219 info(DRIVER_VERSION ": " DRIVER_DESC); 1197
1220out:
1221 return retval; 1198 return retval;
1222} 1199}
1223 1200
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 319de03944e7..7135e542679d 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -13,6 +13,7 @@
13#include <linux/ctype.h> 13#include <linux/ctype.h>
14#include <linux/device.h> 14#include <linux/device.h>
15#include <asm/byteorder.h> 15#include <asm/byteorder.h>
16#include <asm/scatterlist.h>
16 17
17#include "hcd.h" /* for usbcore internals */ 18#include "hcd.h" /* for usbcore internals */
18#include "usb.h" 19#include "usb.h"
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 081796726b95..dad4d8fd8180 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -468,6 +468,7 @@ int usb_unlink_urb(struct urb *urb)
468 */ 468 */
469void usb_kill_urb(struct urb *urb) 469void usb_kill_urb(struct urb *urb)
470{ 470{
471 might_sleep();
471 if (!(urb && urb->dev && urb->dev->bus && urb->dev->bus->op)) 472 if (!(urb && urb->dev && urb->dev->bus && urb->dev->bus->op))
472 return; 473 return;
473 spin_lock_irq(&urb->lock); 474 spin_lock_irq(&urb->lock);
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index 9a4edc5657aa..0aab7d24c768 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -135,6 +135,7 @@ struct dev_data {
135 setup_out_ready : 1, 135 setup_out_ready : 1,
136 setup_out_error : 1, 136 setup_out_error : 1,
137 setup_abort : 1; 137 setup_abort : 1;
138 unsigned setup_wLength;
138 139
139 /* the rest is basically write-once */ 140 /* the rest is basically write-once */
140 struct usb_config_descriptor *config, *hs_config; 141 struct usb_config_descriptor *config, *hs_config;
@@ -942,6 +943,7 @@ static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
942 } 943 }
943 req->complete = ep0_complete; 944 req->complete = ep0_complete;
944 req->length = len; 945 req->length = len;
946 req->zero = 0;
945 return 0; 947 return 0;
946} 948}
947 949
@@ -1161,10 +1163,13 @@ ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
1161 spin_unlock_irq (&dev->lock); 1163 spin_unlock_irq (&dev->lock);
1162 if (copy_from_user (dev->req->buf, buf, len)) 1164 if (copy_from_user (dev->req->buf, buf, len))
1163 retval = -EFAULT; 1165 retval = -EFAULT;
1164 else 1166 else {
1167 if (len < dev->setup_wLength)
1168 dev->req->zero = 1;
1165 retval = usb_ep_queue ( 1169 retval = usb_ep_queue (
1166 dev->gadget->ep0, dev->req, 1170 dev->gadget->ep0, dev->req,
1167 GFP_KERNEL); 1171 GFP_KERNEL);
1172 }
1168 if (retval < 0) { 1173 if (retval < 0) {
1169 spin_lock_irq (&dev->lock); 1174 spin_lock_irq (&dev->lock);
1170 clean_req (dev->gadget->ep0, dev->req); 1175 clean_req (dev->gadget->ep0, dev->req);
@@ -1483,6 +1488,7 @@ unrecognized:
1483delegate: 1488delegate:
1484 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN) 1489 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
1485 ? 1 : 0; 1490 ? 1 : 0;
1491 dev->setup_wLength = w_length;
1486 dev->setup_out_ready = 0; 1492 dev->setup_out_ready = 0;
1487 dev->setup_out_error = 0; 1493 dev->setup_out_error = 0;
1488 value = 0; 1494 value = 0;
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index c32e1f7476da..67b13ab2f3f5 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -47,6 +47,7 @@
47#include <linux/config.h> 47#include <linux/config.h>
48#include <linux/module.h> 48#include <linux/module.h>
49#include <linux/pci.h> 49#include <linux/pci.h>
50#include <linux/dma-mapping.h>
50#include <linux/kernel.h> 51#include <linux/kernel.h>
51#include <linux/delay.h> 52#include <linux/delay.h>
52#include <linux/ioport.h> 53#include <linux/ioport.h>
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index 2fc110d3ad5a..ae7a1c0f5748 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -165,8 +165,8 @@ static unsigned buflen = 4096;
165static unsigned qlen = 32; 165static unsigned qlen = 32;
166static unsigned pattern = 0; 166static unsigned pattern = 0;
167 167
168module_param (buflen, uint, S_IRUGO|S_IWUSR); 168module_param (buflen, uint, S_IRUGO);
169module_param (qlen, uint, S_IRUGO|S_IWUSR); 169module_param (qlen, uint, S_IRUGO);
170module_param (pattern, uint, S_IRUGO|S_IWUSR); 170module_param (pattern, uint, S_IRUGO|S_IWUSR);
171 171
172/* 172/*
@@ -1127,8 +1127,10 @@ zero_unbind (struct usb_gadget *gadget)
1127 DBG (dev, "unbind\n"); 1127 DBG (dev, "unbind\n");
1128 1128
1129 /* we've already been disconnected ... no i/o is active */ 1129 /* we've already been disconnected ... no i/o is active */
1130 if (dev->req) 1130 if (dev->req) {
1131 dev->req->length = USB_BUFSIZ;
1131 free_ep_req (gadget->ep0, dev->req); 1132 free_ep_req (gadget->ep0, dev->req);
1133 }
1132 del_timer_sync (&dev->resume); 1134 del_timer_sync (&dev->resume);
1133 kfree (dev); 1135 kfree (dev);
1134 set_gadget_data (gadget, NULL); 1136 set_gadget_data (gadget, NULL);
diff --git a/drivers/usb/host/ehci-pci.c b/drivers/usb/host/ehci-pci.c
index 08ca0f849dab..3a6687df5594 100644
--- a/drivers/usb/host/ehci-pci.c
+++ b/drivers/usb/host/ehci-pci.c
@@ -24,46 +24,11 @@
24 24
25/*-------------------------------------------------------------------------*/ 25/*-------------------------------------------------------------------------*/
26 26
27/* EHCI 0.96 (and later) section 5.1 says how to kick BIOS/SMM/...
28 * off the controller (maybe it can boot from highspeed USB disks).
29 */
30static int bios_handoff(struct ehci_hcd *ehci, int where, u32 cap)
31{
32 struct pci_dev *pdev = to_pci_dev(ehci_to_hcd(ehci)->self.controller);
33
34 /* always say Linux will own the hardware */
35 pci_write_config_byte(pdev, where + 3, 1);
36
37 /* maybe wait a while for BIOS to respond */
38 if (cap & (1 << 16)) {
39 int msec = 5000;
40
41 do {
42 msleep(10);
43 msec -= 10;
44 pci_read_config_dword(pdev, where, &cap);
45 } while ((cap & (1 << 16)) && msec);
46 if (cap & (1 << 16)) {
47 ehci_err(ehci, "BIOS handoff failed (%d, %08x)\n",
48 where, cap);
49 // some BIOS versions seem buggy...
50 // return 1;
51 ehci_warn(ehci, "continuing after BIOS bug...\n");
52 /* disable all SMIs, and clear "BIOS owns" flag */
53 pci_write_config_dword(pdev, where + 4, 0);
54 pci_write_config_byte(pdev, where + 2, 0);
55 } else
56 ehci_dbg(ehci, "BIOS handoff succeeded\n");
57 }
58 return 0;
59}
60
61/* called after powerup, by probe or system-pm "wakeup" */ 27/* called after powerup, by probe or system-pm "wakeup" */
62static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev) 28static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
63{ 29{
64 u32 temp; 30 u32 temp;
65 int retval; 31 int retval;
66 unsigned count = 256/4;
67 32
68 /* optional debug port, normally in the first BAR */ 33 /* optional debug port, normally in the first BAR */
69 temp = pci_find_capability(pdev, 0x0a); 34 temp = pci_find_capability(pdev, 0x0a);
@@ -84,32 +49,9 @@ static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
84 } 49 }
85 } 50 }
86 51
87 temp = HCC_EXT_CAPS(readl(&ehci->caps->hcc_params)); 52 /* we expect static quirk code to handle the "extended capabilities"
88 53 * (currently just BIOS handoff) allowed starting with EHCI 0.96
89 /* EHCI 0.96 and later may have "extended capabilities" */ 54 */
90 while (temp && count--) {
91 u32 cap;
92
93 pci_read_config_dword(pdev, temp, &cap);
94 ehci_dbg(ehci, "capability %04x at %02x\n", cap, temp);
95 switch (cap & 0xff) {
96 case 1: /* BIOS/SMM/... handoff */
97 if (bios_handoff(ehci, temp, cap) != 0)
98 return -EOPNOTSUPP;
99 break;
100 case 0: /* illegal reserved capability */
101 ehci_dbg(ehci, "illegal capability!\n");
102 cap = 0;
103 /* FALLTHROUGH */
104 default: /* unknown */
105 break;
106 }
107 temp = (cap >> 8) & 0xff;
108 }
109 if (!count) {
110 ehci_err(ehci, "bogus capabilities ... PCI problems!\n");
111 return -EIO;
112 }
113 55
114 /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */ 56 /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
115 retval = pci_set_mwi(pdev); 57 retval = pci_set_mwi(pdev);
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index 57e77374d228..ebcca9700671 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -1063,6 +1063,7 @@ sitd_slot_ok (
1063 1063
1064 /* for IN, check CSPLIT */ 1064 /* for IN, check CSPLIT */
1065 if (stream->c_usecs) { 1065 if (stream->c_usecs) {
1066 uf = uframe & 7;
1066 max_used = 100 - stream->c_usecs; 1067 max_used = 100 - stream->c_usecs;
1067 do { 1068 do {
1068 tmp = 1 << uf; 1069 tmp = 1 << uf;
@@ -1843,8 +1844,7 @@ done:
1843#else 1844#else
1844 1845
1845static inline int 1846static inline int
1846sitd_submit (struct ehci_hcd *ehci, struct urb *urb, 1847sitd_submit (struct ehci_hcd *ehci, struct urb *urb, gfp_t mem_flags)
1847 unsigned mem_flags)
1848{ 1848{
1849 ehci_dbg (ehci, "split iso support is disabled\n"); 1849 ehci_dbg (ehci, "split iso support is disabled\n");
1850 return -ENOSYS; 1850 return -ENOSYS;
diff --git a/drivers/usb/host/isp116x-hcd.c b/drivers/usb/host/isp116x-hcd.c
index 584b8dc65119..972ce04889f8 100644
--- a/drivers/usb/host/isp116x-hcd.c
+++ b/drivers/usb/host/isp116x-hcd.c
@@ -1420,20 +1420,22 @@ static int isp116x_bus_suspend(struct usb_hcd *hcd)
1420 int ret = 0; 1420 int ret = 0;
1421 1421
1422 spin_lock_irqsave(&isp116x->lock, flags); 1422 spin_lock_irqsave(&isp116x->lock, flags);
1423
1424 val = isp116x_read_reg32(isp116x, HCCONTROL); 1423 val = isp116x_read_reg32(isp116x, HCCONTROL);
1424
1425 switch (val & HCCONTROL_HCFS) { 1425 switch (val & HCCONTROL_HCFS) {
1426 case HCCONTROL_USB_OPER: 1426 case HCCONTROL_USB_OPER:
1427 spin_unlock_irqrestore(&isp116x->lock, flags);
1427 val &= (~HCCONTROL_HCFS & ~HCCONTROL_RWE); 1428 val &= (~HCCONTROL_HCFS & ~HCCONTROL_RWE);
1428 val |= HCCONTROL_USB_SUSPEND; 1429 val |= HCCONTROL_USB_SUSPEND;
1429 if (device_may_wakeup(&hcd->self.root_hub->dev)) 1430 if (device_may_wakeup(&hcd->self.root_hub->dev))
1430 val |= HCCONTROL_RWE; 1431 val |= HCCONTROL_RWE;
1431 /* Wait for usb transfers to finish */ 1432 /* Wait for usb transfers to finish */
1432 mdelay(2); 1433 msleep(2);
1434 spin_lock_irqsave(&isp116x->lock, flags);
1433 isp116x_write_reg32(isp116x, HCCONTROL, val); 1435 isp116x_write_reg32(isp116x, HCCONTROL, val);
1436 spin_unlock_irqrestore(&isp116x->lock, flags);
1434 /* Wait for devices to suspend */ 1437 /* Wait for devices to suspend */
1435 mdelay(5); 1438 msleep(5);
1436 case HCCONTROL_USB_SUSPEND:
1437 break; 1439 break;
1438 case HCCONTROL_USB_RESUME: 1440 case HCCONTROL_USB_RESUME:
1439 isp116x_write_reg32(isp116x, HCCONTROL, 1441 isp116x_write_reg32(isp116x, HCCONTROL,
@@ -1441,12 +1443,11 @@ static int isp116x_bus_suspend(struct usb_hcd *hcd)
1441 HCCONTROL_USB_RESET); 1443 HCCONTROL_USB_RESET);
1442 case HCCONTROL_USB_RESET: 1444 case HCCONTROL_USB_RESET:
1443 ret = -EBUSY; 1445 ret = -EBUSY;
1446 default: /* HCCONTROL_USB_SUSPEND */
1447 spin_unlock_irqrestore(&isp116x->lock, flags);
1444 break; 1448 break;
1445 default:
1446 ret = -EINVAL;
1447 } 1449 }
1448 1450
1449 spin_unlock_irqrestore(&isp116x->lock, flags);
1450 return ret; 1451 return ret;
1451} 1452}
1452 1453
@@ -1715,9 +1716,9 @@ static struct platform_driver isp116x_driver = {
1715 .remove = isp116x_remove, 1716 .remove = isp116x_remove,
1716 .suspend = isp116x_suspend, 1717 .suspend = isp116x_suspend,
1717 .resume = isp116x_resume, 1718 .resume = isp116x_resume,
1718 .driver = { 1719 .driver = {
1719 .name = (char *)hcd_name, 1720 .name = (char *)hcd_name,
1720 }, 1721 },
1721}; 1722};
1722 1723
1723/*-----------------------------------------------------------------*/ 1724/*-----------------------------------------------------------------*/
diff --git a/drivers/usb/host/ohci-au1xxx.c b/drivers/usb/host/ohci-au1xxx.c
index 77cd6ac07e3c..db280ca7b7a0 100644
--- a/drivers/usb/host/ohci-au1xxx.c
+++ b/drivers/usb/host/ohci-au1xxx.c
@@ -67,7 +67,7 @@ static void au1xxx_stop_hc(struct platform_device *dev)
67 ": stopping Au1xxx OHCI USB Controller\n"); 67 ": stopping Au1xxx OHCI USB Controller\n");
68 68
69 /* Disable clock */ 69 /* Disable clock */
70 au_writel(readl((void *)USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG); 70 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG);
71} 71}
72 72
73 73
diff --git a/drivers/usb/host/pci-quirks.c b/drivers/usb/host/pci-quirks.c
index 3ef2c0cdf1db..e9e5bc178cef 100644
--- a/drivers/usb/host/pci-quirks.c
+++ b/drivers/usb/host/pci-quirks.c
@@ -190,7 +190,7 @@ static void __devinit quirk_usb_handoff_ohci(struct pci_dev *pdev)
190 msleep(10); 190 msleep(10);
191 } 191 }
192 if (wait_time <= 0) 192 if (wait_time <= 0)
193 printk(KERN_WARNING "%s %s: early BIOS handoff " 193 printk(KERN_WARNING "%s %s: BIOS handoff "
194 "failed (BIOS bug ?)\n", 194 "failed (BIOS bug ?)\n",
195 pdev->dev.bus_id, "OHCI"); 195 pdev->dev.bus_id, "OHCI");
196 196
@@ -212,8 +212,9 @@ static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
212{ 212{
213 int wait_time, delta; 213 int wait_time, delta;
214 void __iomem *base, *op_reg_base; 214 void __iomem *base, *op_reg_base;
215 u32 hcc_params, val, temp; 215 u32 hcc_params, val;
216 u8 cap_length; 216 u8 offset, cap_length;
217 int count = 256/4;
217 218
218 if (!mmio_resource_enabled(pdev, 0)) 219 if (!mmio_resource_enabled(pdev, 0))
219 return; 220 return;
@@ -224,51 +225,80 @@ static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
224 225
225 cap_length = readb(base); 226 cap_length = readb(base);
226 op_reg_base = base + cap_length; 227 op_reg_base = base + cap_length;
228
229 /* EHCI 0.96 and later may have "extended capabilities"
230 * spec section 5.1 explains the bios handoff, e.g. for
231 * booting from USB disk or using a usb keyboard
232 */
227 hcc_params = readl(base + EHCI_HCC_PARAMS); 233 hcc_params = readl(base + EHCI_HCC_PARAMS);
228 hcc_params = (hcc_params >> 8) & 0xff; 234 offset = (hcc_params >> 8) & 0xff;
229 if (hcc_params) { 235 while (offset && count--) {
230 pci_read_config_dword(pdev, 236 u32 cap;
231 hcc_params + EHCI_USBLEGSUP, 237 int msec;
232 &val); 238
233 if (((val & 0xff) == 1) && (val & EHCI_USBLEGSUP_BIOS)) { 239 pci_read_config_dword(pdev, offset, &cap);
234 /* 240 switch (cap & 0xff) {
235 * Ok, BIOS is in smm mode, try to hand off... 241 case 1: /* BIOS/SMM/... handoff support */
242 if ((cap & EHCI_USBLEGSUP_BIOS)) {
243 pr_debug("%s %s: BIOS handoff\n",
244 pdev->dev.bus_id, "EHCI");
245
246 /* BIOS workaround (?): be sure the
247 * pre-Linux code receives the SMI
248 */
249 pci_read_config_dword(pdev,
250 offset + EHCI_USBLEGCTLSTS,
251 &val);
252 pci_write_config_dword(pdev,
253 offset + EHCI_USBLEGCTLSTS,
254 val | EHCI_USBLEGCTLSTS_SOOE);
255 }
256
257 /* always say Linux will own the hardware
258 * by setting EHCI_USBLEGSUP_OS.
236 */ 259 */
237 pci_read_config_dword(pdev, 260 pci_write_config_byte(pdev, offset + 3, 1);
238 hcc_params + EHCI_USBLEGCTLSTS,
239 &temp);
240 pci_write_config_dword(pdev,
241 hcc_params + EHCI_USBLEGCTLSTS,
242 temp | EHCI_USBLEGCTLSTS_SOOE);
243 val |= EHCI_USBLEGSUP_OS;
244 pci_write_config_dword(pdev,
245 hcc_params + EHCI_USBLEGSUP,
246 val);
247 261
248 wait_time = 500; 262 /* if boot firmware now owns EHCI, spin till
249 do { 263 * it hands it over.
264 */
265 msec = 5000;
266 while ((cap & EHCI_USBLEGSUP_BIOS) && (msec > 0)) {
250 msleep(10); 267 msleep(10);
251 wait_time -= 10; 268 msec -= 10;
252 pci_read_config_dword(pdev, 269 pci_read_config_dword(pdev, offset, &cap);
253 hcc_params + EHCI_USBLEGSUP, 270 }
254 &val); 271
255 } while (wait_time && (val & EHCI_USBLEGSUP_BIOS)); 272 if (cap & EHCI_USBLEGSUP_BIOS) {
256 if (!wait_time) { 273 /* well, possibly buggy BIOS... try to shut
257 /* 274 * it down, and hope nothing goes too wrong
258 * well, possibly buggy BIOS...
259 */ 275 */
260 printk(KERN_WARNING "%s %s: early BIOS handoff " 276 printk(KERN_WARNING "%s %s: BIOS handoff "
261 "failed (BIOS bug ?)\n", 277 "failed (BIOS bug ?)\n",
262 pdev->dev.bus_id, "EHCI"); 278 pdev->dev.bus_id, "EHCI");
263 pci_write_config_dword(pdev, 279 pci_write_config_byte(pdev, offset + 2, 0);
264 hcc_params + EHCI_USBLEGSUP,
265 EHCI_USBLEGSUP_OS);
266 pci_write_config_dword(pdev,
267 hcc_params + EHCI_USBLEGCTLSTS,
268 0);
269 } 280 }
281
282 /* just in case, always disable EHCI SMIs */
283 pci_write_config_dword(pdev,
284 offset + EHCI_USBLEGCTLSTS,
285 0);
286 break;
287 case 0: /* illegal reserved capability */
288 cap = 0;
289 /* FALLTHROUGH */
290 default:
291 printk(KERN_WARNING "%s %s: unrecognized "
292 "capability %02x\n",
293 pdev->dev.bus_id, "EHCI",
294 cap & 0xff);
295 break;
270 } 296 }
297 offset = (cap >> 8) & 0xff;
271 } 298 }
299 if (!count)
300 printk(KERN_DEBUG "%s %s: capability loop?\n",
301 pdev->dev.bus_id, "EHCI");
272 302
273 /* 303 /*
274 * halt EHCI & disable its interrupts in any case 304 * halt EHCI & disable its interrupts in any case
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index b6076004a437..782398045f9f 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -672,9 +672,9 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
672 /* Low-speed transfers get a different queue, and won't hog the bus. 672 /* Low-speed transfers get a different queue, and won't hog the bus.
673 * Also, some devices enumerate better without FSBR; the easiest way 673 * Also, some devices enumerate better without FSBR; the easiest way
674 * to do that is to put URBs on the low-speed queue while the device 674 * to do that is to put URBs on the low-speed queue while the device
675 * is in the DEFAULT state. */ 675 * isn't in the CONFIGURED state. */
676 if (urb->dev->speed == USB_SPEED_LOW || 676 if (urb->dev->speed == USB_SPEED_LOW ||
677 urb->dev->state == USB_STATE_DEFAULT) 677 urb->dev->state != USB_STATE_CONFIGURED)
678 skelqh = uhci->skel_ls_control_qh; 678 skelqh = uhci->skel_ls_control_qh;
679 else { 679 else {
680 skelqh = uhci->skel_fs_control_qh; 680 skelqh = uhci->skel_fs_control_qh;
diff --git a/drivers/usb/input/hid-core.c b/drivers/usb/input/hid-core.c
index a91e72c41415..6f7a684c3e07 100644
--- a/drivers/usb/input/hid-core.c
+++ b/drivers/usb/input/hid-core.c
@@ -1307,7 +1307,7 @@ void hid_init_reports(struct hid_device *hid)
1307 } 1307 }
1308 1308
1309 if (err) 1309 if (err)
1310 warn("timeout initializing reports\n"); 1310 warn("timeout initializing reports");
1311} 1311}
1312 1312
1313#define USB_VENDOR_ID_WACOM 0x056a 1313#define USB_VENDOR_ID_WACOM 0x056a
@@ -1453,6 +1453,9 @@ void hid_init_reports(struct hid_device *hid)
1453#define USB_VENDOR_ID_CHERRY 0x046a 1453#define USB_VENDOR_ID_CHERRY 0x046a
1454#define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023 1454#define USB_DEVICE_ID_CHERRY_CYMOTION 0x0023
1455 1455
1456#define USB_VENDOR_ID_HP 0x03f0
1457#define USB_DEVICE_ID_HP_USBHUB_KB 0x020c
1458
1456/* 1459/*
1457 * Alphabetically sorted blacklist by quirk type. 1460 * Alphabetically sorted blacklist by quirk type.
1458 */ 1461 */
@@ -1566,6 +1569,7 @@ static const struct hid_blacklist {
1566 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET }, 1569 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1567 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET}, 1570 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
1568 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET}, 1571 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
1572 { USB_VENDOR_ID_HP, USB_DEVICE_ID_HP_USBHUB_KB, HID_QUIRK_NOGET },
1569 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET }, 1573 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1570 1574
1571 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_POWERMOUSE, HID_QUIRK_2WHEEL_POWERMOUSE }, 1575 { USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_POWERMOUSE, HID_QUIRK_2WHEEL_POWERMOUSE },
@@ -1828,9 +1832,6 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1828 hid->urbctrl->transfer_dma = hid->ctrlbuf_dma; 1832 hid->urbctrl->transfer_dma = hid->ctrlbuf_dma;
1829 hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); 1833 hid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
1830 1834
1831 /* May be needed for some devices */
1832 usb_clear_halt(hid->dev, hid->urbin->pipe);
1833
1834 return hid; 1835 return hid;
1835 1836
1836fail: 1837fail:
diff --git a/drivers/usb/input/hiddev.c b/drivers/usb/input/hiddev.c
index 4dff8473553d..925f5aba06f5 100644
--- a/drivers/usb/input/hiddev.c
+++ b/drivers/usb/input/hiddev.c
@@ -35,7 +35,6 @@
35#include <linux/usb.h> 35#include <linux/usb.h>
36#include "hid.h" 36#include "hid.h"
37#include <linux/hiddev.h> 37#include <linux/hiddev.h>
38#include <linux/devfs_fs_kernel.h>
39 38
40#ifdef CONFIG_USB_DYNAMIC_MINORS 39#ifdef CONFIG_USB_DYNAMIC_MINORS
41#define HIDDEV_MINOR_BASE 0 40#define HIDDEV_MINOR_BASE 0
@@ -632,7 +631,7 @@ static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd
632 631
633 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) && 632 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
634 (uref_multi->num_values > HID_MAX_MULTI_USAGES || 633 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
635 uref->usage_index + uref_multi->num_values >= field->report_count)) 634 uref->usage_index + uref_multi->num_values > field->report_count))
636 goto inval; 635 goto inval;
637 } 636 }
638 637
@@ -832,12 +831,10 @@ static /* const */ struct usb_driver hiddev_driver = {
832 831
833int __init hiddev_init(void) 832int __init hiddev_init(void)
834{ 833{
835 devfs_mk_dir("usb/hid");
836 return usb_register(&hiddev_driver); 834 return usb_register(&hiddev_driver);
837} 835}
838 836
839void hiddev_exit(void) 837void hiddev_exit(void)
840{ 838{
841 usb_deregister(&hiddev_driver); 839 usb_deregister(&hiddev_driver);
842 devfs_remove("usb/hid");
843} 840}
diff --git a/drivers/usb/input/touchkitusb.c b/drivers/usb/input/touchkitusb.c
index 3b3c7b4120a2..697c5e573a11 100644
--- a/drivers/usb/input/touchkitusb.c
+++ b/drivers/usb/input/touchkitusb.c
@@ -337,6 +337,9 @@ static int touchkit_probe(struct usb_interface *intf,
337 touchkit->data, TOUCHKIT_REPORT_DATA_SIZE, 337 touchkit->data, TOUCHKIT_REPORT_DATA_SIZE,
338 touchkit_irq, touchkit, endpoint->bInterval); 338 touchkit_irq, touchkit, endpoint->bInterval);
339 339
340 touchkit->irq->transfer_dma = touchkit->data_dma;
341 touchkit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
342
340 input_register_device(touchkit->input); 343 input_register_device(touchkit->input);
341 344
342 usb_set_intfdata(intf, touchkit); 345 usb_set_intfdata(intf, touchkit);
diff --git a/drivers/usb/input/yealink.c b/drivers/usb/input/yealink.c
index 1bfc105ad4d6..37d2f0ba0319 100644
--- a/drivers/usb/input/yealink.c
+++ b/drivers/usb/input/yealink.c
@@ -59,7 +59,7 @@
59#include "map_to_7segment.h" 59#include "map_to_7segment.h"
60#include "yealink.h" 60#include "yealink.h"
61 61
62#define DRIVER_VERSION "yld-20050816" 62#define DRIVER_VERSION "yld-20051230"
63#define DRIVER_AUTHOR "Henk Vergonet" 63#define DRIVER_AUTHOR "Henk Vergonet"
64#define DRIVER_DESC "Yealink phone driver" 64#define DRIVER_DESC "Yealink phone driver"
65 65
@@ -786,16 +786,25 @@ static struct attribute_group yld_attr_group = {
786 * Linux interface and usb initialisation 786 * Linux interface and usb initialisation
787 ******************************************************************************/ 787 ******************************************************************************/
788 788
789static const struct yld_device { 789struct driver_info {
790 u16 idVendor;
791 u16 idProduct;
792 char *name; 790 char *name;
793} yld_device[] = {
794 { 0x6993, 0xb001, "Yealink usb-p1k" },
795}; 791};
796 792
797static struct usb_device_id usb_table [] = { 793static const struct driver_info info_P1K = {
798 { USB_INTERFACE_INFO(USB_CLASS_HID, 0, 0) }, 794 .name = "Yealink usb-p1k",
795};
796
797static const struct usb_device_id usb_table [] = {
798 {
799 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
800 USB_DEVICE_ID_MATCH_INT_INFO,
801 .idVendor = 0x6993,
802 .idProduct = 0xb001,
803 .bInterfaceClass = USB_CLASS_HID,
804 .bInterfaceSubClass = 0,
805 .bInterfaceProtocol = 0,
806 .driver_info = (kernel_ulong_t)&info_P1K
807 },
799 { } 808 { }
800}; 809};
801 810
@@ -842,33 +851,16 @@ static void usb_disconnect(struct usb_interface *intf)
842 usb_cleanup(yld, 0); 851 usb_cleanup(yld, 0);
843} 852}
844 853
845static int usb_match(struct usb_device *udev)
846{
847 int i;
848 u16 idVendor = le16_to_cpu(udev->descriptor.idVendor);
849 u16 idProduct = le16_to_cpu(udev->descriptor.idProduct);
850
851 for (i = 0; i < ARRAY_SIZE(yld_device); i++) {
852 if ((idVendor == yld_device[i].idVendor) &&
853 (idProduct == yld_device[i].idProduct))
854 return i;
855 }
856 return -ENODEV;
857}
858
859static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 854static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
860{ 855{
861 struct usb_device *udev = interface_to_usbdev (intf); 856 struct usb_device *udev = interface_to_usbdev (intf);
857 struct driver_info *nfo = (struct driver_info *)id->driver_info;
862 struct usb_host_interface *interface; 858 struct usb_host_interface *interface;
863 struct usb_endpoint_descriptor *endpoint; 859 struct usb_endpoint_descriptor *endpoint;
864 struct yealink_dev *yld; 860 struct yealink_dev *yld;
865 struct input_dev *input_dev; 861 struct input_dev *input_dev;
866 int ret, pipe, i; 862 int ret, pipe, i;
867 863
868 i = usb_match(udev);
869 if (i < 0)
870 return -ENODEV;
871
872 interface = intf->cur_altsetting; 864 interface = intf->cur_altsetting;
873 endpoint = &interface->endpoint[0].desc; 865 endpoint = &interface->endpoint[0].desc;
874 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) 866 if (!(endpoint->bEndpointAddress & USB_DIR_IN))
@@ -915,7 +907,7 @@ static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
915 pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress); 907 pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
916 ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); 908 ret = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
917 if (ret != USB_PKT_LEN) 909 if (ret != USB_PKT_LEN)
918 err("invalid payload size %d, expected %d", ret, USB_PKT_LEN); 910 err("invalid payload size %d, expected %zd", ret, USB_PKT_LEN);
919 911
920 /* initialise irq urb */ 912 /* initialise irq urb */
921 usb_fill_int_urb(yld->urb_irq, udev, pipe, yld->irq_data, 913 usb_fill_int_urb(yld->urb_irq, udev, pipe, yld->irq_data,
@@ -948,7 +940,7 @@ static int usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
948 strlcat(yld->phys, "/input0", sizeof(yld->phys)); 940 strlcat(yld->phys, "/input0", sizeof(yld->phys));
949 941
950 /* register settings for the input device */ 942 /* register settings for the input device */
951 input_dev->name = yld_device[i].name; 943 input_dev->name = nfo->name;
952 input_dev->phys = yld->phys; 944 input_dev->phys = yld->phys;
953 usb_to_input_id(udev, &input_dev->id); 945 usb_to_input_id(udev, &input_dev->id);
954 input_dev->cdev.dev = &intf->dev; 946 input_dev->cdev.dev = &intf->dev;
diff --git a/drivers/usb/media/Kconfig b/drivers/usb/media/Kconfig
index 21232ee2974c..0d3d2cc5d7be 100644
--- a/drivers/usb/media/Kconfig
+++ b/drivers/usb/media/Kconfig
@@ -53,6 +53,21 @@ config USB_DSBR
53 To compile this driver as a module, choose M here: the 53 To compile this driver as a module, choose M here: the
54 module will be called dsbr100. 54 module will be called dsbr100.
55 55
56config USB_ET61X251
57 tristate "USB ET61X[12]51 PC Camera Controller support"
58 depends on USB && VIDEO_DEV
59 ---help---
60 Say Y here if you want support for cameras based on Etoms ET61X151
61 or ET61X251 PC Camera Controllers.
62
63 See <file:Documentation/usb/et61x251.txt> for more informations.
64
65 This driver uses the Video For Linux API. You must say Y or M to
66 "Video For Linux" to use this driver.
67
68 To compile this driver as a module, choose M here: the
69 module will be called et61x251.
70
56config USB_IBMCAM 71config USB_IBMCAM
57 tristate "USB IBM (Xirlink) C-it Camera support" 72 tristate "USB IBM (Xirlink) C-it Camera support"
58 depends on USB && VIDEO_DEV 73 depends on USB && VIDEO_DEV
@@ -209,5 +224,3 @@ config USB_PWC
209 224
210 To compile this driver as a module, choose M here: the 225 To compile this driver as a module, choose M here: the
211 module will be called pwc. 226 module will be called pwc.
212
213
diff --git a/drivers/usb/media/Makefile b/drivers/usb/media/Makefile
index d83adffa925f..3957aa1be0f2 100644
--- a/drivers/usb/media/Makefile
+++ b/drivers/usb/media/Makefile
@@ -3,9 +3,11 @@
3# 3#
4 4
5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bcb.o sn9c102_tas5110c1b.o sn9c102_tas5130d1b.o 5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bcb.o sn9c102_tas5110c1b.o sn9c102_tas5130d1b.o
6et61x251-objs := et61x251_core.o et61x251_tas5130d1b.o
6 7
7obj-$(CONFIG_USB_DABUSB) += dabusb.o 8obj-$(CONFIG_USB_DABUSB) += dabusb.o
8obj-$(CONFIG_USB_DSBR) += dsbr100.o 9obj-$(CONFIG_USB_DSBR) += dsbr100.o
10obj-$(CONFIG_USB_ET61X251) += et61x251.o
9obj-$(CONFIG_USB_IBMCAM) += ibmcam.o usbvideo.o ultracam.o 11obj-$(CONFIG_USB_IBMCAM) += ibmcam.o usbvideo.o ultracam.o
10obj-$(CONFIG_USB_KONICAWC) += konicawc.o usbvideo.o 12obj-$(CONFIG_USB_KONICAWC) += konicawc.o usbvideo.o
11obj-$(CONFIG_USB_OV511) += ov511.o 13obj-$(CONFIG_USB_OV511) += ov511.o
diff --git a/drivers/usb/media/et61x251.h b/drivers/usb/media/et61x251.h
new file mode 100644
index 000000000000..652238f329f3
--- /dev/null
+++ b/drivers/usb/media/et61x251.h
@@ -0,0 +1,220 @@
1/***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _ET61X251_H_
22#define _ET61X251_H_
23
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
28#include <linux/device.h>
29#include <linux/list.h>
30#include <linux/spinlock.h>
31#include <linux/time.h>
32#include <linux/wait.h>
33#include <linux/types.h>
34#include <linux/param.h>
35#include <linux/rwsem.h>
36#include <asm/semaphore.h>
37
38#include "et61x251_sensor.h"
39
40/*****************************************************************************/
41
42#define ET61X251_DEBUG
43#define ET61X251_DEBUG_LEVEL 2
44#define ET61X251_MAX_DEVICES 64
45#define ET61X251_PRESERVE_IMGSCALE 0
46#define ET61X251_FORCE_MUNMAP 0
47#define ET61X251_MAX_FRAMES 32
48#define ET61X251_COMPRESSION_QUALITY 0
49#define ET61X251_URBS 2
50#define ET61X251_ISO_PACKETS 7
51#define ET61X251_ALTERNATE_SETTING 13
52#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS)
53#define ET61X251_CTRL_TIMEOUT 100
54
55/*****************************************************************************/
56
57static const struct usb_device_id et61x251_id_table[] = {
58 { USB_DEVICE(0x102c, 0x6151), },
59 { USB_DEVICE(0x102c, 0x6251), },
60 { USB_DEVICE(0x102c, 0x6253), },
61 { USB_DEVICE(0x102c, 0x6254), },
62 { USB_DEVICE(0x102c, 0x6255), },
63 { USB_DEVICE(0x102c, 0x6256), },
64 { USB_DEVICE(0x102c, 0x6257), },
65 { USB_DEVICE(0x102c, 0x6258), },
66 { USB_DEVICE(0x102c, 0x6259), },
67 { USB_DEVICE(0x102c, 0x625a), },
68 { USB_DEVICE(0x102c, 0x625b), },
69 { USB_DEVICE(0x102c, 0x625c), },
70 { USB_DEVICE(0x102c, 0x625d), },
71 { USB_DEVICE(0x102c, 0x625e), },
72 { USB_DEVICE(0x102c, 0x625f), },
73 { USB_DEVICE(0x102c, 0x6260), },
74 { USB_DEVICE(0x102c, 0x6261), },
75 { USB_DEVICE(0x102c, 0x6262), },
76 { USB_DEVICE(0x102c, 0x6263), },
77 { USB_DEVICE(0x102c, 0x6264), },
78 { USB_DEVICE(0x102c, 0x6265), },
79 { USB_DEVICE(0x102c, 0x6266), },
80 { USB_DEVICE(0x102c, 0x6267), },
81 { USB_DEVICE(0x102c, 0x6268), },
82 { USB_DEVICE(0x102c, 0x6269), },
83 { }
84};
85
86ET61X251_SENSOR_TABLE
87
88/*****************************************************************************/
89
90enum et61x251_frame_state {
91 F_UNUSED,
92 F_QUEUED,
93 F_GRABBING,
94 F_DONE,
95 F_ERROR,
96};
97
98struct et61x251_frame_t {
99 void* bufmem;
100 struct v4l2_buffer buf;
101 enum et61x251_frame_state state;
102 struct list_head frame;
103 unsigned long vma_use_count;
104};
105
106enum et61x251_dev_state {
107 DEV_INITIALIZED = 0x01,
108 DEV_DISCONNECTED = 0x02,
109 DEV_MISCONFIGURED = 0x04,
110};
111
112enum et61x251_io_method {
113 IO_NONE,
114 IO_READ,
115 IO_MMAP,
116};
117
118enum et61x251_stream_state {
119 STREAM_OFF,
120 STREAM_INTERRUPT,
121 STREAM_ON,
122};
123
124struct et61x251_sysfs_attr {
125 u8 reg, i2c_reg;
126};
127
128struct et61x251_module_param {
129 u8 force_munmap;
130};
131
132static DECLARE_MUTEX(et61x251_sysfs_lock);
133static DECLARE_RWSEM(et61x251_disconnect);
134
135struct et61x251_device {
136 struct video_device* v4ldev;
137
138 struct et61x251_sensor* sensor;
139
140 struct usb_device* usbdev;
141 struct urb* urb[ET61X251_URBS];
142 void* transfer_buffer[ET61X251_URBS];
143 u8* control_buffer;
144
145 struct et61x251_frame_t *frame_current, frame[ET61X251_MAX_FRAMES];
146 struct list_head inqueue, outqueue;
147 u32 frame_count, nbuffers, nreadbuffers;
148
149 enum et61x251_io_method io;
150 enum et61x251_stream_state stream;
151
152 struct v4l2_jpegcompression compression;
153
154 struct et61x251_sysfs_attr sysfs;
155 struct et61x251_module_param module_param;
156
157 enum et61x251_dev_state state;
158 u8 users;
159
160 struct semaphore dev_sem, fileop_sem;
161 spinlock_t queue_lock;
162 wait_queue_head_t open, wait_frame, wait_stream;
163};
164
165/*****************************************************************************/
166
167void
168et61x251_attach_sensor(struct et61x251_device* cam,
169 struct et61x251_sensor* sensor)
170{
171 cam->sensor = sensor;
172 cam->sensor->usbdev = cam->usbdev;
173}
174
175/*****************************************************************************/
176
177#undef DBG
178#undef KDBG
179#ifdef ET61X251_DEBUG
180# define DBG(level, fmt, args...) \
181do { \
182 if (debug >= (level)) { \
183 if ((level) == 1) \
184 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
185 else if ((level) == 2) \
186 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
187 else if ((level) >= 3) \
188 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
189 __FUNCTION__, __LINE__ , ## args); \
190 } \
191} while (0)
192# define KDBG(level, fmt, args...) \
193do { \
194 if (debug >= (level)) { \
195 if ((level) == 1 || (level) == 2) \
196 pr_info("et61x251: " fmt "\n", ## args); \
197 else if ((level) == 3) \
198 pr_debug("et61x251: [%s:%d] " fmt "\n", __FUNCTION__, \
199 __LINE__ , ## args); \
200 } \
201} while (0)
202# define V4LDBG(level, name, cmd) \
203do { \
204 if (debug >= (level)) \
205 v4l_print_ioctl(name, cmd); \
206} while (0)
207#else
208# define DBG(level, fmt, args...) do {;} while(0)
209# define KDBG(level, fmt, args...) do {;} while(0)
210# define V4LDBG(level, name, cmd) do {;} while(0)
211#endif
212
213#undef PDBG
214#define PDBG(fmt, args...) \
215dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args)
216
217#undef PDBGG
218#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
219
220#endif /* _ET61X251_H_ */
diff --git a/drivers/usb/media/et61x251_core.c b/drivers/usb/media/et61x251_core.c
new file mode 100644
index 000000000000..2c0171a5ad62
--- /dev/null
+++ b/drivers/usb/media/et61x251_core.c
@@ -0,0 +1,2605 @@
1/***************************************************************************
2 * V4L2 driver for ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/param.h>
25#include <linux/moduleparam.h>
26#include <linux/errno.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h>
30#include <linux/fs.h>
31#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h>
34#include <linux/ioctl.h>
35#include <linux/poll.h>
36#include <linux/stat.h>
37#include <linux/mm.h>
38#include <linux/vmalloc.h>
39#include <linux/page-flags.h>
40#include <linux/byteorder/generic.h>
41#include <asm/page.h>
42#include <asm/uaccess.h>
43
44#include "et61x251.h"
45
46/*****************************************************************************/
47
48#define ET61X251_MODULE_NAME "V4L2 driver for ET61X[12]51 " \
49 "PC Camera Controllers"
50#define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia"
51#define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
52#define ET61X251_MODULE_LICENSE "GPL"
53#define ET61X251_MODULE_VERSION "1:1.01"
54#define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 1)
55
56/*****************************************************************************/
57
58MODULE_DEVICE_TABLE(usb, et61x251_id_table);
59
60MODULE_AUTHOR(ET61X251_MODULE_AUTHOR " " ET61X251_AUTHOR_EMAIL);
61MODULE_DESCRIPTION(ET61X251_MODULE_NAME);
62MODULE_VERSION(ET61X251_MODULE_VERSION);
63MODULE_LICENSE(ET61X251_MODULE_LICENSE);
64
65static short video_nr[] = {[0 ... ET61X251_MAX_DEVICES-1] = -1};
66module_param_array(video_nr, short, NULL, 0444);
67MODULE_PARM_DESC(video_nr,
68 "\n<-1|n[,...]> Specify V4L2 minor mode number."
69 "\n -1 = use next available (default)"
70 "\n n = use minor number n (integer >= 0)"
71 "\nYou can specify up to "
72 __MODULE_STRING(ET61X251_MAX_DEVICES) " cameras this way."
73 "\nFor example:"
74 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
75 "\nthe second registered camera and use auto for the first"
76 "\none and for every other camera."
77 "\n");
78
79static short force_munmap[] = {[0 ... ET61X251_MAX_DEVICES-1] =
80 ET61X251_FORCE_MUNMAP};
81module_param_array(force_munmap, bool, NULL, 0444);
82MODULE_PARM_DESC(force_munmap,
83 "\n<0|1[,...]> Force the application to unmap previously"
84 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
85 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
86 "\nthis feature. This parameter is specific for each"
87 "\ndetected camera."
88 "\n 0 = do not force memory unmapping"
89 "\n 1 = force memory unmapping (save memory)"
90 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
91 "\n");
92
93#ifdef ET61X251_DEBUG
94static unsigned short debug = ET61X251_DEBUG_LEVEL;
95module_param(debug, ushort, 0644);
96MODULE_PARM_DESC(debug,
97 "\n<n> Debugging information level, from 0 to 3:"
98 "\n0 = none (use carefully)"
99 "\n1 = critical errors"
100 "\n2 = significant informations"
101 "\n3 = more verbose messages"
102 "\nLevel 3 is useful for testing only, when only "
103 "one device is used."
104 "\nDefault value is "__MODULE_STRING(ET61X251_DEBUG_LEVEL)"."
105 "\n");
106#endif
107
108/*****************************************************************************/
109
110static u32
111et61x251_request_buffers(struct et61x251_device* cam, u32 count,
112 enum et61x251_io_method io)
113{
114 struct v4l2_pix_format* p = &(cam->sensor->pix_format);
115 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds);
116 const size_t imagesize = cam->module_param.force_munmap ||
117 io == IO_READ ?
118 (p->width * p->height * p->priv) / 8 :
119 (r->width * r->height * p->priv) / 8;
120 void* buff = NULL;
121 u32 i;
122
123 if (count > ET61X251_MAX_FRAMES)
124 count = ET61X251_MAX_FRAMES;
125
126 cam->nbuffers = count;
127 while (cam->nbuffers > 0) {
128 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
129 break;
130 cam->nbuffers--;
131 }
132
133 for (i = 0; i < cam->nbuffers; i++) {
134 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
135 cam->frame[i].buf.index = i;
136 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
137 cam->frame[i].buf.length = imagesize;
138 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
139 cam->frame[i].buf.sequence = 0;
140 cam->frame[i].buf.field = V4L2_FIELD_NONE;
141 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
142 cam->frame[i].buf.flags = 0;
143 }
144
145 return cam->nbuffers;
146}
147
148
149static void et61x251_release_buffers(struct et61x251_device* cam)
150{
151 if (cam->nbuffers) {
152 vfree(cam->frame[0].bufmem);
153 cam->nbuffers = 0;
154 }
155 cam->frame_current = NULL;
156}
157
158
159static void et61x251_empty_framequeues(struct et61x251_device* cam)
160{
161 u32 i;
162
163 INIT_LIST_HEAD(&cam->inqueue);
164 INIT_LIST_HEAD(&cam->outqueue);
165
166 for (i = 0; i < ET61X251_MAX_FRAMES; i++) {
167 cam->frame[i].state = F_UNUSED;
168 cam->frame[i].buf.bytesused = 0;
169 }
170}
171
172
173static void et61x251_requeue_outqueue(struct et61x251_device* cam)
174{
175 struct et61x251_frame_t *i;
176
177 list_for_each_entry(i, &cam->outqueue, frame) {
178 i->state = F_QUEUED;
179 list_add(&i->frame, &cam->inqueue);
180 }
181
182 INIT_LIST_HEAD(&cam->outqueue);
183}
184
185
186static void et61x251_queue_unusedframes(struct et61x251_device* cam)
187{
188 unsigned long lock_flags;
189 u32 i;
190
191 for (i = 0; i < cam->nbuffers; i++)
192 if (cam->frame[i].state == F_UNUSED) {
193 cam->frame[i].state = F_QUEUED;
194 spin_lock_irqsave(&cam->queue_lock, lock_flags);
195 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
196 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
197 }
198}
199
200/*****************************************************************************/
201
202int et61x251_write_reg(struct et61x251_device* cam, u8 value, u16 index)
203{
204 struct usb_device* udev = cam->usbdev;
205 u8* buff = cam->control_buffer;
206 int res;
207
208 *buff = value;
209
210 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
211 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
212 if (res < 0) {
213 DBG(3, "Failed to write a register (value 0x%02X, index "
214 "0x%02X, error %d)", value, index, res);
215 return -1;
216 }
217
218 return 0;
219}
220
221
222int et61x251_read_reg(struct et61x251_device* cam, u16 index)
223{
224 struct usb_device* udev = cam->usbdev;
225 u8* buff = cam->control_buffer;
226 int res;
227
228 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
229 0, index, buff, 1, ET61X251_CTRL_TIMEOUT);
230 if (res < 0)
231 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
232 index, res);
233
234 return (res >= 0) ? (int)(*buff) : -1;
235}
236
237
238static int
239et61x251_i2c_wait(struct et61x251_device* cam, struct et61x251_sensor* sensor)
240{
241 int i, r;
242
243 for (i = 1; i <= 8; i++) {
244 if (sensor->interface == ET61X251_I2C_3WIRES) {
245 r = et61x251_read_reg(cam, 0x8e);
246 if (!(r & 0x02) && (r >= 0))
247 return 0;
248 } else {
249 r = et61x251_read_reg(cam, 0x8b);
250 if (!(r & 0x01) && (r >= 0))
251 return 0;
252 }
253 if (r < 0)
254 return -EIO;
255 udelay(8*8); /* minimum for sensors at 400kHz */
256 }
257
258 return -EBUSY;
259}
260
261
262int
263et61x251_i2c_try_read(struct et61x251_device* cam,
264 struct et61x251_sensor* sensor, u8 address)
265{
266 struct usb_device* udev = cam->usbdev;
267 u8* data = cam->control_buffer;
268 int err = 0, res;
269
270 data[0] = address;
271 data[1] = cam->sensor->i2c_slave_id;
272 data[2] = cam->sensor->rsta | 0x10;
273 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
274 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
275 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
276 if (res < 0)
277 err += res;
278
279 err += et61x251_i2c_wait(cam, sensor);
280
281 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00, 0xc1,
282 0, 0x80, data, 8, ET61X251_CTRL_TIMEOUT);
283 if (res < 0)
284 err += res;
285
286 if (err)
287 DBG(3, "I2C read failed for %s image sensor", sensor->name);
288
289 PDBGG("I2C read: address 0x%02X, value: 0x%02X", address, data[0]);
290
291 return err ? -1 : (int)data[0];
292}
293
294
295int
296et61x251_i2c_try_write(struct et61x251_device* cam,
297 struct et61x251_sensor* sensor, u8 address, u8 value)
298{
299 struct usb_device* udev = cam->usbdev;
300 u8* data = cam->control_buffer;
301 int err = 0, res;
302
303 data[0] = address;
304 data[1] = cam->sensor->i2c_slave_id;
305 data[2] = cam->sensor->rsta | 0x12;
306 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
307 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
308 if (res < 0)
309 err += res;
310
311 data[0] = value;
312 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
313 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
314 if (res < 0)
315 err += res;
316
317 err += et61x251_i2c_wait(cam, sensor);
318
319 if (err)
320 DBG(3, "I2C write failed for %s image sensor", sensor->name);
321
322 PDBGG("I2C write: address 0x%02X, value: 0x%02X", address, value);
323
324 return err ? -1 : 0;
325}
326
327
328int
329et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
330 u8 data3, u8 data4, u8 data5, u8 data6, u8 data7,
331 u8 data8, u8 address)
332{
333 struct usb_device* udev = cam->usbdev;
334 u8* data = cam->control_buffer;
335 int err = 0, res;
336
337 if (!cam->sensor)
338 return -1;
339
340 data[0] = data2;
341 data[1] = data3;
342 data[2] = data4;
343 data[3] = data5;
344 data[4] = data6;
345 data[5] = data7;
346 data[6] = data8;
347 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
348 0, 0x81, data, n-1, ET61X251_CTRL_TIMEOUT);
349 if (res < 0)
350 err += res;
351
352 data[0] = address;
353 data[1] = cam->sensor->i2c_slave_id;
354 data[2] = cam->sensor->rsta | 0x02 | (n << 4);
355 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
356 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
357 if (res < 0)
358 err += res;
359
360 /* Start writing through the serial interface */
361 data[0] = data1;
362 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
363 0, 0x80, data, 1, ET61X251_CTRL_TIMEOUT);
364 if (res < 0)
365 err += res;
366
367 err += et61x251_i2c_wait(cam, cam->sensor);
368
369 if (err)
370 DBG(3, "I2C raw write failed for %s image sensor",
371 cam->sensor->name);
372
373 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
374 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
375 " data6 = 0x%02X, data7 = 0x%02X, data8 = 0x%02X", n, address,
376 data1, data2, data3, data4, data5, data6, data7, data8);
377
378 return err ? -1 : 0;
379
380}
381
382
383int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
384{
385 if (!cam->sensor)
386 return -1;
387
388 return et61x251_i2c_try_read(cam, cam->sensor, address);
389}
390
391
392int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
393{
394 if (!cam->sensor)
395 return -1;
396
397 return et61x251_i2c_try_write(cam, cam->sensor, address, value);
398}
399
400/*****************************************************************************/
401
402static void et61x251_urb_complete(struct urb *urb, struct pt_regs* regs)
403{
404 struct et61x251_device* cam = urb->context;
405 struct et61x251_frame_t** f;
406 size_t imagesize;
407 u8 i;
408 int err = 0;
409
410 if (urb->status == -ENOENT)
411 return;
412
413 f = &cam->frame_current;
414
415 if (cam->stream == STREAM_INTERRUPT) {
416 cam->stream = STREAM_OFF;
417 if ((*f))
418 (*f)->state = F_QUEUED;
419 DBG(3, "Stream interrupted");
420 wake_up_interruptible(&cam->wait_stream);
421 }
422
423 if (cam->state & DEV_DISCONNECTED)
424 return;
425
426 if (cam->state & DEV_MISCONFIGURED) {
427 wake_up_interruptible(&cam->wait_frame);
428 return;
429 }
430
431 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
432 goto resubmit_urb;
433
434 if (!(*f))
435 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
436 frame);
437
438 imagesize = (cam->sensor->pix_format.width *
439 cam->sensor->pix_format.height *
440 cam->sensor->pix_format.priv) / 8;
441
442 for (i = 0; i < urb->number_of_packets; i++) {
443 unsigned int len, status;
444 void *pos;
445 u8* b1, * b2, sof;
446 const u8 VOID_BYTES = 6;
447 size_t imglen;
448
449 len = urb->iso_frame_desc[i].actual_length;
450 status = urb->iso_frame_desc[i].status;
451 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
452
453 if (status) {
454 DBG(3, "Error in isochronous frame");
455 (*f)->state = F_ERROR;
456 continue;
457 }
458
459 b1 = pos++;
460 b2 = pos++;
461 sof = ((*b1 & 0x3f) == 63);
462 imglen = ((*b1 & 0xc0) << 2) | *b2;
463
464 PDBGG("Isochrnous frame: length %u, #%u i, image length %zu",
465 len, i, imglen);
466
467 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
468start_of_frame:
469 if (sof) {
470 (*f)->state = F_GRABBING;
471 (*f)->buf.bytesused = 0;
472 do_gettimeofday(&(*f)->buf.timestamp);
473 pos += 22;
474 DBG(3, "SOF detected: new video frame");
475 }
476
477 if ((*f)->state == F_GRABBING) {
478 if (sof && (*f)->buf.bytesused) {
479 if (cam->sensor->pix_format.pixelformat ==
480 V4L2_PIX_FMT_ET61X251)
481 goto end_of_frame;
482 else {
483 DBG(3, "Not expected SOF detected "
484 "after %lu bytes",
485 (unsigned long)(*f)->buf.bytesused);
486 (*f)->state = F_ERROR;
487 continue;
488 }
489 }
490
491 if ((*f)->buf.bytesused + imglen > imagesize) {
492 DBG(3, "Video frame size exceeded");
493 (*f)->state = F_ERROR;
494 continue;
495 }
496
497 pos += VOID_BYTES;
498
499 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, imglen);
500 (*f)->buf.bytesused += imglen;
501
502 if ((*f)->buf.bytesused == imagesize) {
503 u32 b;
504end_of_frame:
505 b = (*f)->buf.bytesused;
506 (*f)->state = F_DONE;
507 (*f)->buf.sequence= ++cam->frame_count;
508 spin_lock(&cam->queue_lock);
509 list_move_tail(&(*f)->frame, &cam->outqueue);
510 if (!list_empty(&cam->inqueue))
511 (*f) = list_entry(cam->inqueue.next,
512 struct et61x251_frame_t,
513 frame);
514 else
515 (*f) = NULL;
516 spin_unlock(&cam->queue_lock);
517 DBG(3, "Video frame captured: : %lu bytes",
518 (unsigned long)(b));
519
520 if (!(*f))
521 goto resubmit_urb;
522
523 if (sof &&
524 cam->sensor->pix_format.pixelformat ==
525 V4L2_PIX_FMT_ET61X251)
526 goto start_of_frame;
527 }
528 }
529 }
530
531resubmit_urb:
532 urb->dev = cam->usbdev;
533 err = usb_submit_urb(urb, GFP_ATOMIC);
534 if (err < 0 && err != -EPERM) {
535 cam->state |= DEV_MISCONFIGURED;
536 DBG(1, "usb_submit_urb() failed");
537 }
538
539 wake_up_interruptible(&cam->wait_frame);
540}
541
542
543static int et61x251_start_transfer(struct et61x251_device* cam)
544{
545 struct usb_device *udev = cam->usbdev;
546 struct urb* urb;
547 const unsigned int wMaxPacketSize[] = {0, 256, 384, 512, 640, 768, 832,
548 864, 896, 920, 956, 980, 1000,
549 1022};
550 const unsigned int psz = wMaxPacketSize[ET61X251_ALTERNATE_SETTING];
551 s8 i, j;
552 int err = 0;
553
554 for (i = 0; i < ET61X251_URBS; i++) {
555 cam->transfer_buffer[i] = kzalloc(ET61X251_ISO_PACKETS * psz,
556 GFP_KERNEL);
557 if (!cam->transfer_buffer[i]) {
558 err = -ENOMEM;
559 DBG(1, "Not enough memory");
560 goto free_buffers;
561 }
562 }
563
564 for (i = 0; i < ET61X251_URBS; i++) {
565 urb = usb_alloc_urb(ET61X251_ISO_PACKETS, GFP_KERNEL);
566 cam->urb[i] = urb;
567 if (!urb) {
568 err = -ENOMEM;
569 DBG(1, "usb_alloc_urb() failed");
570 goto free_urbs;
571 }
572 urb->dev = udev;
573 urb->context = cam;
574 urb->pipe = usb_rcvisocpipe(udev, 1);
575 urb->transfer_flags = URB_ISO_ASAP;
576 urb->number_of_packets = ET61X251_ISO_PACKETS;
577 urb->complete = et61x251_urb_complete;
578 urb->transfer_buffer = cam->transfer_buffer[i];
579 urb->transfer_buffer_length = psz * ET61X251_ISO_PACKETS;
580 urb->interval = 1;
581 for (j = 0; j < ET61X251_ISO_PACKETS; j++) {
582 urb->iso_frame_desc[j].offset = psz * j;
583 urb->iso_frame_desc[j].length = psz;
584 }
585 }
586
587 err = et61x251_write_reg(cam, 0x01, 0x03);
588 err = et61x251_write_reg(cam, 0x00, 0x03);
589 err = et61x251_write_reg(cam, 0x08, 0x03);
590 if (err) {
591 err = -EIO;
592 DBG(1, "I/O hardware error");
593 goto free_urbs;
594 }
595
596 err = usb_set_interface(udev, 0, ET61X251_ALTERNATE_SETTING);
597 if (err) {
598 DBG(1, "usb_set_interface() failed");
599 goto free_urbs;
600 }
601
602 cam->frame_current = NULL;
603
604 for (i = 0; i < ET61X251_URBS; i++) {
605 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
606 if (err) {
607 for (j = i-1; j >= 0; j--)
608 usb_kill_urb(cam->urb[j]);
609 DBG(1, "usb_submit_urb() failed, error %d", err);
610 goto free_urbs;
611 }
612 }
613
614 return 0;
615
616free_urbs:
617 for (i = 0; (i < ET61X251_URBS) && cam->urb[i]; i++)
618 usb_free_urb(cam->urb[i]);
619
620free_buffers:
621 for (i = 0; (i < ET61X251_URBS) && cam->transfer_buffer[i]; i++)
622 kfree(cam->transfer_buffer[i]);
623
624 return err;
625}
626
627
628static int et61x251_stop_transfer(struct et61x251_device* cam)
629{
630 struct usb_device *udev = cam->usbdev;
631 s8 i;
632 int err = 0;
633
634 if (cam->state & DEV_DISCONNECTED)
635 return 0;
636
637 for (i = ET61X251_URBS-1; i >= 0; i--) {
638 usb_kill_urb(cam->urb[i]);
639 usb_free_urb(cam->urb[i]);
640 kfree(cam->transfer_buffer[i]);
641 }
642
643 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
644 if (err)
645 DBG(3, "usb_set_interface() failed");
646
647 return err;
648}
649
650
651static int et61x251_stream_interrupt(struct et61x251_device* cam)
652{
653 int err = 0;
654
655 cam->stream = STREAM_INTERRUPT;
656 err = wait_event_timeout(cam->wait_stream,
657 (cam->stream == STREAM_OFF) ||
658 (cam->state & DEV_DISCONNECTED),
659 ET61X251_URB_TIMEOUT);
660 if (cam->state & DEV_DISCONNECTED)
661 return -ENODEV;
662 else if (err) {
663 cam->state |= DEV_MISCONFIGURED;
664 DBG(1, "URB timeout reached. The camera is misconfigured. To "
665 "use it, close and open /dev/video%d again.",
666 cam->v4ldev->minor);
667 return err;
668 }
669
670 return 0;
671}
672
673/*****************************************************************************/
674
675#ifdef CONFIG_VIDEO_ADV_DEBUG
676static u8 et61x251_strtou8(const char* buff, size_t len, ssize_t* count)
677{
678 char str[5];
679 char* endp;
680 unsigned long val;
681
682 if (len < 4) {
683 strncpy(str, buff, len);
684 str[len+1] = '\0';
685 } else {
686 strncpy(str, buff, 4);
687 str[4] = '\0';
688 }
689
690 val = simple_strtoul(str, &endp, 0);
691
692 *count = 0;
693 if (val <= 0xff)
694 *count = (ssize_t)(endp - str);
695 if ((*count) && (len == *count+1) && (buff[*count] == '\n'))
696 *count += 1;
697
698 return (u8)val;
699}
700
701/*
702 NOTE 1: being inside one of the following methods implies that the v4l
703 device exists for sure (see kobjects and reference counters)
704 NOTE 2: buffers are PAGE_SIZE long
705*/
706
707static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
708{
709 struct et61x251_device* cam;
710 ssize_t count;
711
712 if (down_interruptible(&et61x251_sysfs_lock))
713 return -ERESTARTSYS;
714
715 cam = video_get_drvdata(to_video_device(cd));
716 if (!cam) {
717 up(&et61x251_sysfs_lock);
718 return -ENODEV;
719 }
720
721 count = sprintf(buf, "%u\n", cam->sysfs.reg);
722
723 up(&et61x251_sysfs_lock);
724
725 return count;
726}
727
728
729static ssize_t
730et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
731{
732 struct et61x251_device* cam;
733 u8 index;
734 ssize_t count;
735
736 if (down_interruptible(&et61x251_sysfs_lock))
737 return -ERESTARTSYS;
738
739 cam = video_get_drvdata(to_video_device(cd));
740 if (!cam) {
741 up(&et61x251_sysfs_lock);
742 return -ENODEV;
743 }
744
745 index = et61x251_strtou8(buf, len, &count);
746 if (index > 0x8e || !count) {
747 up(&et61x251_sysfs_lock);
748 return -EINVAL;
749 }
750
751 cam->sysfs.reg = index;
752
753 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
754 DBG(3, "Written bytes: %zd", count);
755
756 up(&et61x251_sysfs_lock);
757
758 return count;
759}
760
761
762static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
763{
764 struct et61x251_device* cam;
765 ssize_t count;
766 int val;
767
768 if (down_interruptible(&et61x251_sysfs_lock))
769 return -ERESTARTSYS;
770
771 cam = video_get_drvdata(to_video_device(cd));
772 if (!cam) {
773 up(&et61x251_sysfs_lock);
774 return -ENODEV;
775 }
776
777 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
778 up(&et61x251_sysfs_lock);
779 return -EIO;
780 }
781
782 count = sprintf(buf, "%d\n", val);
783
784 DBG(3, "Read bytes: %zd", count);
785
786 up(&et61x251_sysfs_lock);
787
788 return count;
789}
790
791
792static ssize_t
793et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
794{
795 struct et61x251_device* cam;
796 u8 value;
797 ssize_t count;
798 int err;
799
800 if (down_interruptible(&et61x251_sysfs_lock))
801 return -ERESTARTSYS;
802
803 cam = video_get_drvdata(to_video_device(cd));
804 if (!cam) {
805 up(&et61x251_sysfs_lock);
806 return -ENODEV;
807 }
808
809 value = et61x251_strtou8(buf, len, &count);
810 if (!count) {
811 up(&et61x251_sysfs_lock);
812 return -EINVAL;
813 }
814
815 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
816 if (err) {
817 up(&et61x251_sysfs_lock);
818 return -EIO;
819 }
820
821 DBG(2, "Written ET61X[12]51 reg. 0x%02X, val. 0x%02X",
822 cam->sysfs.reg, value);
823 DBG(3, "Written bytes: %zd", count);
824
825 up(&et61x251_sysfs_lock);
826
827 return count;
828}
829
830
831static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
832{
833 struct et61x251_device* cam;
834 ssize_t count;
835
836 if (down_interruptible(&et61x251_sysfs_lock))
837 return -ERESTARTSYS;
838
839 cam = video_get_drvdata(to_video_device(cd));
840 if (!cam) {
841 up(&et61x251_sysfs_lock);
842 return -ENODEV;
843 }
844
845 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
846
847 DBG(3, "Read bytes: %zd", count);
848
849 up(&et61x251_sysfs_lock);
850
851 return count;
852}
853
854
855static ssize_t
856et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
857{
858 struct et61x251_device* cam;
859 u8 index;
860 ssize_t count;
861
862 if (down_interruptible(&et61x251_sysfs_lock))
863 return -ERESTARTSYS;
864
865 cam = video_get_drvdata(to_video_device(cd));
866 if (!cam) {
867 up(&et61x251_sysfs_lock);
868 return -ENODEV;
869 }
870
871 index = et61x251_strtou8(buf, len, &count);
872 if (!count) {
873 up(&et61x251_sysfs_lock);
874 return -EINVAL;
875 }
876
877 cam->sysfs.i2c_reg = index;
878
879 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
880 DBG(3, "Written bytes: %zd", count);
881
882 up(&et61x251_sysfs_lock);
883
884 return count;
885}
886
887
888static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
889{
890 struct et61x251_device* cam;
891 ssize_t count;
892 int val;
893
894 if (down_interruptible(&et61x251_sysfs_lock))
895 return -ERESTARTSYS;
896
897 cam = video_get_drvdata(to_video_device(cd));
898 if (!cam) {
899 up(&et61x251_sysfs_lock);
900 return -ENODEV;
901 }
902
903 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) {
904 up(&et61x251_sysfs_lock);
905 return -ENOSYS;
906 }
907
908 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
909 up(&et61x251_sysfs_lock);
910 return -EIO;
911 }
912
913 count = sprintf(buf, "%d\n", val);
914
915 DBG(3, "Read bytes: %zd", count);
916
917 up(&et61x251_sysfs_lock);
918
919 return count;
920}
921
922
923static ssize_t
924et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
925{
926 struct et61x251_device* cam;
927 u8 value;
928 ssize_t count;
929 int err;
930
931 if (down_interruptible(&et61x251_sysfs_lock))
932 return -ERESTARTSYS;
933
934 cam = video_get_drvdata(to_video_device(cd));
935 if (!cam) {
936 up(&et61x251_sysfs_lock);
937 return -ENODEV;
938 }
939
940 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) {
941 up(&et61x251_sysfs_lock);
942 return -ENOSYS;
943 }
944
945 value = et61x251_strtou8(buf, len, &count);
946 if (!count) {
947 up(&et61x251_sysfs_lock);
948 return -EINVAL;
949 }
950
951 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
952 if (err) {
953 up(&et61x251_sysfs_lock);
954 return -EIO;
955 }
956
957 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
958 cam->sysfs.i2c_reg, value);
959 DBG(3, "Written bytes: %zd", count);
960
961 up(&et61x251_sysfs_lock);
962
963 return count;
964}
965
966
967static CLASS_DEVICE_ATTR(reg, S_IRUGO | S_IWUSR,
968 et61x251_show_reg, et61x251_store_reg);
969static CLASS_DEVICE_ATTR(val, S_IRUGO | S_IWUSR,
970 et61x251_show_val, et61x251_store_val);
971static CLASS_DEVICE_ATTR(i2c_reg, S_IRUGO | S_IWUSR,
972 et61x251_show_i2c_reg, et61x251_store_i2c_reg);
973static CLASS_DEVICE_ATTR(i2c_val, S_IRUGO | S_IWUSR,
974 et61x251_show_i2c_val, et61x251_store_i2c_val);
975
976
977static void et61x251_create_sysfs(struct et61x251_device* cam)
978{
979 struct video_device *v4ldev = cam->v4ldev;
980
981 video_device_create_file(v4ldev, &class_device_attr_reg);
982 video_device_create_file(v4ldev, &class_device_attr_val);
983 if (cam->sensor && cam->sensor->sysfs_ops) {
984 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
985 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
986 }
987}
988#endif /* CONFIG_VIDEO_ADV_DEBUG */
989
990/*****************************************************************************/
991
992static int
993et61x251_set_pix_format(struct et61x251_device* cam,
994 struct v4l2_pix_format* pix)
995{
996 int r, err = 0;
997
998 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
999 err += r;
1000 if (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1001 err += et61x251_write_reg(cam, r & 0xfd, 0x12);
1002 else
1003 err += et61x251_write_reg(cam, r | 0x02, 0x12);
1004
1005 return err ? -EIO : 0;
1006}
1007
1008
1009static int
1010et61x251_set_compression(struct et61x251_device* cam,
1011 struct v4l2_jpegcompression* compression)
1012{
1013 int r, err = 0;
1014
1015 if ((r = et61x251_read_reg(cam, 0x12)) < 0)
1016 err += r;
1017 if (compression->quality == 0)
1018 err += et61x251_write_reg(cam, r & 0xfb, 0x12);
1019 else
1020 err += et61x251_write_reg(cam, r | 0x04, 0x12);
1021
1022 return err ? -EIO : 0;
1023}
1024
1025
1026static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1027{
1028 int r = 0, err = 0;
1029
1030 r = et61x251_read_reg(cam, 0x12);
1031 if (r < 0)
1032 err += r;
1033
1034 if (scale == 1)
1035 err += et61x251_write_reg(cam, r & ~0x01, 0x12);
1036 else if (scale == 2)
1037 err += et61x251_write_reg(cam, r | 0x01, 0x12);
1038
1039 if (err)
1040 return -EIO;
1041
1042 PDBGG("Scaling factor: %u", scale);
1043
1044 return 0;
1045}
1046
1047
1048static int
1049et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1050{
1051 struct et61x251_sensor* s = cam->sensor;
1052 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1053 s->active_pixel.left),
1054 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
1055 s->active_pixel.top),
1056 fmw_length = (u16)(rect->width),
1057 fmw_height = (u16)(rect->height);
1058 int err = 0;
1059
1060 err += et61x251_write_reg(cam, fmw_sx & 0xff, 0x69);
1061 err += et61x251_write_reg(cam, fmw_sy & 0xff, 0x6a);
1062 err += et61x251_write_reg(cam, fmw_length & 0xff, 0x6b);
1063 err += et61x251_write_reg(cam, fmw_height & 0xff, 0x6c);
1064 err += et61x251_write_reg(cam, (fmw_sx >> 8) | ((fmw_sy & 0x300) >> 6)
1065 | ((fmw_length & 0x300) >> 4)
1066 | ((fmw_height & 0x300) >> 2), 0x6d);
1067 if (err)
1068 return -EIO;
1069
1070 PDBGG("fmw_sx, fmw_sy, fmw_length, fmw_height: %u %u %u %u",
1071 fmw_sx, fmw_sy, fmw_length, fmw_height);
1072
1073 return 0;
1074}
1075
1076
1077static int et61x251_init(struct et61x251_device* cam)
1078{
1079 struct et61x251_sensor* s = cam->sensor;
1080 struct v4l2_control ctrl;
1081 struct v4l2_queryctrl *qctrl;
1082 struct v4l2_rect* rect;
1083 u8 i = 0;
1084 int err = 0;
1085
1086 if (!(cam->state & DEV_INITIALIZED)) {
1087 init_waitqueue_head(&cam->open);
1088 qctrl = s->qctrl;
1089 rect = &(s->cropcap.defrect);
1090 cam->compression.quality = ET61X251_COMPRESSION_QUALITY;
1091 } else { /* use current values */
1092 qctrl = s->_qctrl;
1093 rect = &(s->_rect);
1094 }
1095
1096 err += et61x251_set_scale(cam, rect->width / s->pix_format.width);
1097 err += et61x251_set_crop(cam, rect);
1098 if (err)
1099 return err;
1100
1101 if (s->init) {
1102 err = s->init(cam);
1103 if (err) {
1104 DBG(3, "Sensor initialization failed");
1105 return err;
1106 }
1107 }
1108
1109 err += et61x251_set_compression(cam, &cam->compression);
1110 err += et61x251_set_pix_format(cam, &s->pix_format);
1111 if (s->set_pix_format)
1112 err += s->set_pix_format(cam, &s->pix_format);
1113 if (err)
1114 return err;
1115
1116 if (s->pix_format.pixelformat == V4L2_PIX_FMT_ET61X251)
1117 DBG(3, "Compressed video format is active, quality %d",
1118 cam->compression.quality);
1119 else
1120 DBG(3, "Uncompressed video format is active");
1121
1122 if (s->set_crop)
1123 if ((err = s->set_crop(cam, rect))) {
1124 DBG(3, "set_crop() failed");
1125 return err;
1126 }
1127
1128 if (s->set_ctrl) {
1129 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1130 if (s->qctrl[i].id != 0 &&
1131 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
1132 ctrl.id = s->qctrl[i].id;
1133 ctrl.value = qctrl[i].default_value;
1134 err = s->set_ctrl(cam, &ctrl);
1135 if (err) {
1136 DBG(3, "Set %s control failed",
1137 s->qctrl[i].name);
1138 return err;
1139 }
1140 DBG(3, "Image sensor supports '%s' control",
1141 s->qctrl[i].name);
1142 }
1143 }
1144
1145 if (!(cam->state & DEV_INITIALIZED)) {
1146 init_MUTEX(&cam->fileop_sem);
1147 spin_lock_init(&cam->queue_lock);
1148 init_waitqueue_head(&cam->wait_frame);
1149 init_waitqueue_head(&cam->wait_stream);
1150 cam->nreadbuffers = 2;
1151 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
1152 memcpy(&(s->_rect), &(s->cropcap.defrect),
1153 sizeof(struct v4l2_rect));
1154 cam->state |= DEV_INITIALIZED;
1155 }
1156
1157 DBG(2, "Initialization succeeded");
1158 return 0;
1159}
1160
1161
1162static void et61x251_release_resources(struct et61x251_device* cam)
1163{
1164 down(&et61x251_sysfs_lock);
1165
1166 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1167 video_set_drvdata(cam->v4ldev, NULL);
1168 video_unregister_device(cam->v4ldev);
1169
1170 up(&et61x251_sysfs_lock);
1171
1172 kfree(cam->control_buffer);
1173}
1174
1175/*****************************************************************************/
1176
1177static int et61x251_open(struct inode* inode, struct file* filp)
1178{
1179 struct et61x251_device* cam;
1180 int err = 0;
1181
1182 /*
1183 This is the only safe way to prevent race conditions with
1184 disconnect
1185 */
1186 if (!down_read_trylock(&et61x251_disconnect))
1187 return -ERESTARTSYS;
1188
1189 cam = video_get_drvdata(video_devdata(filp));
1190
1191 if (down_interruptible(&cam->dev_sem)) {
1192 up_read(&et61x251_disconnect);
1193 return -ERESTARTSYS;
1194 }
1195
1196 if (cam->users) {
1197 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1198 if ((filp->f_flags & O_NONBLOCK) ||
1199 (filp->f_flags & O_NDELAY)) {
1200 err = -EWOULDBLOCK;
1201 goto out;
1202 }
1203 up(&cam->dev_sem);
1204 err = wait_event_interruptible_exclusive(cam->open,
1205 cam->state & DEV_DISCONNECTED
1206 || !cam->users);
1207 if (err) {
1208 up_read(&et61x251_disconnect);
1209 return err;
1210 }
1211 if (cam->state & DEV_DISCONNECTED) {
1212 up_read(&et61x251_disconnect);
1213 return -ENODEV;
1214 }
1215 down(&cam->dev_sem);
1216 }
1217
1218
1219 if (cam->state & DEV_MISCONFIGURED) {
1220 err = et61x251_init(cam);
1221 if (err) {
1222 DBG(1, "Initialization failed again. "
1223 "I will retry on next open().");
1224 goto out;
1225 }
1226 cam->state &= ~DEV_MISCONFIGURED;
1227 }
1228
1229 if ((err = et61x251_start_transfer(cam)))
1230 goto out;
1231
1232 filp->private_data = cam;
1233 cam->users++;
1234 cam->io = IO_NONE;
1235 cam->stream = STREAM_OFF;
1236 cam->nbuffers = 0;
1237 cam->frame_count = 0;
1238 et61x251_empty_framequeues(cam);
1239
1240 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1241
1242out:
1243 up(&cam->dev_sem);
1244 up_read(&et61x251_disconnect);
1245 return err;
1246}
1247
1248
1249static int et61x251_release(struct inode* inode, struct file* filp)
1250{
1251 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1252
1253 down(&cam->dev_sem); /* prevent disconnect() to be called */
1254
1255 et61x251_stop_transfer(cam);
1256
1257 et61x251_release_buffers(cam);
1258
1259 if (cam->state & DEV_DISCONNECTED) {
1260 et61x251_release_resources(cam);
1261 up(&cam->dev_sem);
1262 kfree(cam);
1263 return 0;
1264 }
1265
1266 cam->users--;
1267 wake_up_interruptible_nr(&cam->open, 1);
1268
1269 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1270
1271 up(&cam->dev_sem);
1272
1273 return 0;
1274}
1275
1276
1277static ssize_t
1278et61x251_read(struct file* filp, char __user * buf,
1279 size_t count, loff_t* f_pos)
1280{
1281 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1282 struct et61x251_frame_t* f, * i;
1283 unsigned long lock_flags;
1284 int err = 0;
1285
1286 if (down_interruptible(&cam->fileop_sem))
1287 return -ERESTARTSYS;
1288
1289 if (cam->state & DEV_DISCONNECTED) {
1290 DBG(1, "Device not present");
1291 up(&cam->fileop_sem);
1292 return -ENODEV;
1293 }
1294
1295 if (cam->state & DEV_MISCONFIGURED) {
1296 DBG(1, "The camera is misconfigured. Close and open it "
1297 "again.");
1298 up(&cam->fileop_sem);
1299 return -EIO;
1300 }
1301
1302 if (cam->io == IO_MMAP) {
1303 DBG(3, "Close and open the device again to choose the read "
1304 "method");
1305 up(&cam->fileop_sem);
1306 return -EINVAL;
1307 }
1308
1309 if (cam->io == IO_NONE) {
1310 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1311 IO_READ)) {
1312 DBG(1, "read() failed, not enough memory");
1313 up(&cam->fileop_sem);
1314 return -ENOMEM;
1315 }
1316 cam->io = IO_READ;
1317 cam->stream = STREAM_ON;
1318 }
1319
1320 if (list_empty(&cam->inqueue)) {
1321 if (!list_empty(&cam->outqueue))
1322 et61x251_empty_framequeues(cam);
1323 et61x251_queue_unusedframes(cam);
1324 }
1325
1326 if (!count) {
1327 up(&cam->fileop_sem);
1328 return 0;
1329 }
1330
1331 if (list_empty(&cam->outqueue)) {
1332 if (filp->f_flags & O_NONBLOCK) {
1333 up(&cam->fileop_sem);
1334 return -EAGAIN;
1335 }
1336 err = wait_event_interruptible
1337 ( cam->wait_frame,
1338 (!list_empty(&cam->outqueue)) ||
1339 (cam->state & DEV_DISCONNECTED) ||
1340 (cam->state & DEV_MISCONFIGURED) );
1341 if (err) {
1342 up(&cam->fileop_sem);
1343 return err;
1344 }
1345 if (cam->state & DEV_DISCONNECTED) {
1346 up(&cam->fileop_sem);
1347 return -ENODEV;
1348 }
1349 if (cam->state & DEV_MISCONFIGURED) {
1350 up(&cam->fileop_sem);
1351 return -EIO;
1352 }
1353 }
1354
1355 f = list_entry(cam->outqueue.prev, struct et61x251_frame_t, frame);
1356
1357 if (count > f->buf.bytesused)
1358 count = f->buf.bytesused;
1359
1360 if (copy_to_user(buf, f->bufmem, count)) {
1361 err = -EFAULT;
1362 goto exit;
1363 }
1364 *f_pos += count;
1365
1366exit:
1367 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1368 list_for_each_entry(i, &cam->outqueue, frame)
1369 i->state = F_UNUSED;
1370 INIT_LIST_HEAD(&cam->outqueue);
1371 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1372
1373 et61x251_queue_unusedframes(cam);
1374
1375 PDBGG("Frame #%lu, bytes read: %zu",
1376 (unsigned long)f->buf.index, count);
1377
1378 up(&cam->fileop_sem);
1379
1380 return err ? err : count;
1381}
1382
1383
1384static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1385{
1386 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1387 struct et61x251_frame_t* f;
1388 unsigned long lock_flags;
1389 unsigned int mask = 0;
1390
1391 if (down_interruptible(&cam->fileop_sem))
1392 return POLLERR;
1393
1394 if (cam->state & DEV_DISCONNECTED) {
1395 DBG(1, "Device not present");
1396 goto error;
1397 }
1398
1399 if (cam->state & DEV_MISCONFIGURED) {
1400 DBG(1, "The camera is misconfigured. Close and open it "
1401 "again.");
1402 goto error;
1403 }
1404
1405 if (cam->io == IO_NONE) {
1406 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1407 IO_READ)) {
1408 DBG(1, "poll() failed, not enough memory");
1409 goto error;
1410 }
1411 cam->io = IO_READ;
1412 cam->stream = STREAM_ON;
1413 }
1414
1415 if (cam->io == IO_READ) {
1416 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1417 list_for_each_entry(f, &cam->outqueue, frame)
1418 f->state = F_UNUSED;
1419 INIT_LIST_HEAD(&cam->outqueue);
1420 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1421 et61x251_queue_unusedframes(cam);
1422 }
1423
1424 poll_wait(filp, &cam->wait_frame, wait);
1425
1426 if (!list_empty(&cam->outqueue))
1427 mask |= POLLIN | POLLRDNORM;
1428
1429 up(&cam->fileop_sem);
1430
1431 return mask;
1432
1433error:
1434 up(&cam->fileop_sem);
1435 return POLLERR;
1436}
1437
1438
1439static void et61x251_vm_open(struct vm_area_struct* vma)
1440{
1441 struct et61x251_frame_t* f = vma->vm_private_data;
1442 f->vma_use_count++;
1443}
1444
1445
1446static void et61x251_vm_close(struct vm_area_struct* vma)
1447{
1448 /* NOTE: buffers are not freed here */
1449 struct et61x251_frame_t* f = vma->vm_private_data;
1450 f->vma_use_count--;
1451}
1452
1453
1454static struct vm_operations_struct et61x251_vm_ops = {
1455 .open = et61x251_vm_open,
1456 .close = et61x251_vm_close,
1457};
1458
1459
1460static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1461{
1462 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1463 unsigned long size = vma->vm_end - vma->vm_start,
1464 start = vma->vm_start;
1465 void *pos;
1466 u32 i;
1467
1468 if (down_interruptible(&cam->fileop_sem))
1469 return -ERESTARTSYS;
1470
1471 if (cam->state & DEV_DISCONNECTED) {
1472 DBG(1, "Device not present");
1473 up(&cam->fileop_sem);
1474 return -ENODEV;
1475 }
1476
1477 if (cam->state & DEV_MISCONFIGURED) {
1478 DBG(1, "The camera is misconfigured. Close and open it "
1479 "again.");
1480 up(&cam->fileop_sem);
1481 return -EIO;
1482 }
1483
1484 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1485 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1486 up(&cam->fileop_sem);
1487 return -EINVAL;
1488 }
1489
1490 for (i = 0; i < cam->nbuffers; i++) {
1491 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
1492 break;
1493 }
1494 if (i == cam->nbuffers) {
1495 up(&cam->fileop_sem);
1496 return -EINVAL;
1497 }
1498
1499 vma->vm_flags |= VM_IO;
1500 vma->vm_flags |= VM_RESERVED;
1501
1502 pos = cam->frame[i].bufmem;
1503 while (size > 0) { /* size is page-aligned */
1504 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1505 up(&cam->fileop_sem);
1506 return -EAGAIN;
1507 }
1508 start += PAGE_SIZE;
1509 pos += PAGE_SIZE;
1510 size -= PAGE_SIZE;
1511 }
1512
1513 vma->vm_ops = &et61x251_vm_ops;
1514 vma->vm_private_data = &cam->frame[i];
1515
1516 et61x251_vm_open(vma);
1517
1518 up(&cam->fileop_sem);
1519
1520 return 0;
1521}
1522
1523/*****************************************************************************/
1524
1525static int
1526et61x251_vidioc_querycap(struct et61x251_device* cam, void __user * arg)
1527{
1528 struct v4l2_capability cap = {
1529 .driver = "et61x251",
1530 .version = ET61X251_MODULE_VERSION_CODE,
1531 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1532 V4L2_CAP_STREAMING,
1533 };
1534
1535 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1536 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1537 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1538 sizeof(cap.bus_info));
1539
1540 if (copy_to_user(arg, &cap, sizeof(cap)))
1541 return -EFAULT;
1542
1543 return 0;
1544}
1545
1546
1547static int
1548et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1549{
1550 struct v4l2_input i;
1551
1552 if (copy_from_user(&i, arg, sizeof(i)))
1553 return -EFAULT;
1554
1555 if (i.index)
1556 return -EINVAL;
1557
1558 memset(&i, 0, sizeof(i));
1559 strcpy(i.name, "Camera");
1560
1561 if (copy_to_user(arg, &i, sizeof(i)))
1562 return -EFAULT;
1563
1564 return 0;
1565}
1566
1567
1568static int
1569et61x251_vidioc_gs_input(struct et61x251_device* cam, void __user * arg)
1570{
1571 int index;
1572
1573 if (copy_from_user(&index, arg, sizeof(index)))
1574 return -EFAULT;
1575
1576 if (index != 0)
1577 return -EINVAL;
1578
1579 return 0;
1580}
1581
1582
1583static int
1584et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1585{
1586 struct et61x251_sensor* s = cam->sensor;
1587 struct v4l2_queryctrl qc;
1588 u8 i;
1589
1590 if (copy_from_user(&qc, arg, sizeof(qc)))
1591 return -EFAULT;
1592
1593 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1594 if (qc.id && qc.id == s->qctrl[i].id) {
1595 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1596 if (copy_to_user(arg, &qc, sizeof(qc)))
1597 return -EFAULT;
1598 return 0;
1599 }
1600
1601 return -EINVAL;
1602}
1603
1604
1605static int
1606et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1607{
1608 struct et61x251_sensor* s = cam->sensor;
1609 struct v4l2_control ctrl;
1610 int err = 0;
1611 u8 i;
1612
1613 if (!s->get_ctrl && !s->set_ctrl)
1614 return -EINVAL;
1615
1616 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1617 return -EFAULT;
1618
1619 if (!s->get_ctrl) {
1620 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1621 if (ctrl.id == s->qctrl[i].id) {
1622 ctrl.value = s->_qctrl[i].default_value;
1623 goto exit;
1624 }
1625 return -EINVAL;
1626 } else
1627 err = s->get_ctrl(cam, &ctrl);
1628
1629exit:
1630 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1631 return -EFAULT;
1632
1633 return err;
1634}
1635
1636
1637static int
1638et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1639{
1640 struct et61x251_sensor* s = cam->sensor;
1641 struct v4l2_control ctrl;
1642 u8 i;
1643 int err = 0;
1644
1645 if (!s->set_ctrl)
1646 return -EINVAL;
1647
1648 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1649 return -EFAULT;
1650
1651 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1652 if (ctrl.id == s->qctrl[i].id) {
1653 if (ctrl.value < s->qctrl[i].minimum ||
1654 ctrl.value > s->qctrl[i].maximum)
1655 return -ERANGE;
1656 ctrl.value -= ctrl.value % s->qctrl[i].step;
1657 break;
1658 }
1659
1660 if ((err = s->set_ctrl(cam, &ctrl)))
1661 return err;
1662
1663 s->_qctrl[i].default_value = ctrl.value;
1664
1665 return 0;
1666}
1667
1668
1669static int
1670et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1671{
1672 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1673
1674 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675 cc->pixelaspect.numerator = 1;
1676 cc->pixelaspect.denominator = 1;
1677
1678 if (copy_to_user(arg, cc, sizeof(*cc)))
1679 return -EFAULT;
1680
1681 return 0;
1682}
1683
1684
1685static int
1686et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1687{
1688 struct et61x251_sensor* s = cam->sensor;
1689 struct v4l2_crop crop = {
1690 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1691 };
1692
1693 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1694
1695 if (copy_to_user(arg, &crop, sizeof(crop)))
1696 return -EFAULT;
1697
1698 return 0;
1699}
1700
1701
1702static int
1703et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1704{
1705 struct et61x251_sensor* s = cam->sensor;
1706 struct v4l2_crop crop;
1707 struct v4l2_rect* rect;
1708 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1709 struct v4l2_pix_format* pix_format = &(s->pix_format);
1710 u8 scale;
1711 const enum et61x251_stream_state stream = cam->stream;
1712 const u32 nbuffers = cam->nbuffers;
1713 u32 i;
1714 int err = 0;
1715
1716 if (copy_from_user(&crop, arg, sizeof(crop)))
1717 return -EFAULT;
1718
1719 rect = &(crop.c);
1720
1721 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1722 return -EINVAL;
1723
1724 if (cam->module_param.force_munmap)
1725 for (i = 0; i < cam->nbuffers; i++)
1726 if (cam->frame[i].vma_use_count) {
1727 DBG(3, "VIDIOC_S_CROP failed. "
1728 "Unmap the buffers first.");
1729 return -EINVAL;
1730 }
1731
1732 /* Preserve R,G or B origin */
1733 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1734 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1735
1736 if (rect->width < 4)
1737 rect->width = 4;
1738 if (rect->height < 4)
1739 rect->height = 4;
1740 if (rect->width > bounds->width)
1741 rect->width = bounds->width;
1742 if (rect->height > bounds->height)
1743 rect->height = bounds->height;
1744 if (rect->left < bounds->left)
1745 rect->left = bounds->left;
1746 if (rect->top < bounds->top)
1747 rect->top = bounds->top;
1748 if (rect->left + rect->width > bounds->left + bounds->width)
1749 rect->left = bounds->left+bounds->width - rect->width;
1750 if (rect->top + rect->height > bounds->top + bounds->height)
1751 rect->top = bounds->top+bounds->height - rect->height;
1752
1753 rect->width &= ~3L;
1754 rect->height &= ~3L;
1755
1756 if (ET61X251_PRESERVE_IMGSCALE) {
1757 /* Calculate the actual scaling factor */
1758 u32 a, b;
1759 a = rect->width * rect->height;
1760 b = pix_format->width * pix_format->height;
1761 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1762 } else
1763 scale = 1;
1764
1765 if (cam->stream == STREAM_ON)
1766 if ((err = et61x251_stream_interrupt(cam)))
1767 return err;
1768
1769 if (copy_to_user(arg, &crop, sizeof(crop))) {
1770 cam->stream = stream;
1771 return -EFAULT;
1772 }
1773
1774 if (cam->module_param.force_munmap || cam->io == IO_READ)
1775 et61x251_release_buffers(cam);
1776
1777 err = et61x251_set_crop(cam, rect);
1778 if (s->set_crop)
1779 err += s->set_crop(cam, rect);
1780 err += et61x251_set_scale(cam, scale);
1781
1782 if (err) { /* atomic, no rollback in ioctl() */
1783 cam->state |= DEV_MISCONFIGURED;
1784 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1785 "use the camera, close and open /dev/video%d again.",
1786 cam->v4ldev->minor);
1787 return -EIO;
1788 }
1789
1790 s->pix_format.width = rect->width/scale;
1791 s->pix_format.height = rect->height/scale;
1792 memcpy(&(s->_rect), rect, sizeof(*rect));
1793
1794 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1795 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1796 cam->state |= DEV_MISCONFIGURED;
1797 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1798 "use the camera, close and open /dev/video%d again.",
1799 cam->v4ldev->minor);
1800 return -ENOMEM;
1801 }
1802
1803 if (cam->io == IO_READ)
1804 et61x251_empty_framequeues(cam);
1805 else if (cam->module_param.force_munmap)
1806 et61x251_requeue_outqueue(cam);
1807
1808 cam->stream = stream;
1809
1810 return 0;
1811}
1812
1813
1814static int
1815et61x251_vidioc_enum_fmt(struct et61x251_device* cam, void __user * arg)
1816{
1817 struct v4l2_fmtdesc fmtd;
1818
1819 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1820 return -EFAULT;
1821
1822 if (fmtd.index == 0) {
1823 strcpy(fmtd.description, "bayer rgb");
1824 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1825 } else if (fmtd.index == 1) {
1826 strcpy(fmtd.description, "compressed");
1827 fmtd.pixelformat = V4L2_PIX_FMT_ET61X251;
1828 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1829 } else
1830 return -EINVAL;
1831
1832 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1833 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1834
1835 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1836 return -EFAULT;
1837
1838 return 0;
1839}
1840
1841
1842static int
1843et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1844{
1845 struct v4l2_format format;
1846 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1847
1848 if (copy_from_user(&format, arg, sizeof(format)))
1849 return -EFAULT;
1850
1851 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1852 return -EINVAL;
1853
1854 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_ET61X251)
1855 ? 0 : (pfmt->width * pfmt->priv) / 8;
1856 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1857 pfmt->field = V4L2_FIELD_NONE;
1858 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1859
1860 if (copy_to_user(arg, &format, sizeof(format)))
1861 return -EFAULT;
1862
1863 return 0;
1864}
1865
1866
1867static int
1868et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1869 void __user * arg)
1870{
1871 struct et61x251_sensor* s = cam->sensor;
1872 struct v4l2_format format;
1873 struct v4l2_pix_format* pix;
1874 struct v4l2_pix_format* pfmt = &(s->pix_format);
1875 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1876 struct v4l2_rect rect;
1877 u8 scale;
1878 const enum et61x251_stream_state stream = cam->stream;
1879 const u32 nbuffers = cam->nbuffers;
1880 u32 i;
1881 int err = 0;
1882
1883 if (copy_from_user(&format, arg, sizeof(format)))
1884 return -EFAULT;
1885
1886 pix = &(format.fmt.pix);
1887
1888 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1889 return -EINVAL;
1890
1891 memcpy(&rect, &(s->_rect), sizeof(rect));
1892
1893 { /* calculate the actual scaling factor */
1894 u32 a, b;
1895 a = rect.width * rect.height;
1896 b = pix->width * pix->height;
1897 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1898 }
1899
1900 rect.width = scale * pix->width;
1901 rect.height = scale * pix->height;
1902
1903 if (rect.width < 4)
1904 rect.width = 4;
1905 if (rect.height < 4)
1906 rect.height = 4;
1907 if (rect.width > bounds->left + bounds->width - rect.left)
1908 rect.width = bounds->left + bounds->width - rect.left;
1909 if (rect.height > bounds->top + bounds->height - rect.top)
1910 rect.height = bounds->top + bounds->height - rect.top;
1911
1912 rect.width &= ~3L;
1913 rect.height &= ~3L;
1914
1915 { /* adjust the scaling factor */
1916 u32 a, b;
1917 a = rect.width * rect.height;
1918 b = pix->width * pix->height;
1919 scale = b ? (u8)((a / b) < 4 ? 1 : 2) : 1;
1920 }
1921
1922 pix->width = rect.width / scale;
1923 pix->height = rect.height / scale;
1924
1925 if (pix->pixelformat != V4L2_PIX_FMT_ET61X251 &&
1926 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
1927 pix->pixelformat = pfmt->pixelformat;
1928 pix->priv = pfmt->priv; /* bpp */
1929 pix->colorspace = pfmt->colorspace;
1930 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_ET61X251)
1931 ? 0 : (pix->width * pix->priv) / 8;
1932 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1933 pix->field = V4L2_FIELD_NONE;
1934
1935 if (cmd == VIDIOC_TRY_FMT) {
1936 if (copy_to_user(arg, &format, sizeof(format)))
1937 return -EFAULT;
1938 return 0;
1939 }
1940
1941 if (cam->module_param.force_munmap)
1942 for (i = 0; i < cam->nbuffers; i++)
1943 if (cam->frame[i].vma_use_count) {
1944 DBG(3, "VIDIOC_S_FMT failed. "
1945 "Unmap the buffers first.");
1946 return -EINVAL;
1947 }
1948
1949 if (cam->stream == STREAM_ON)
1950 if ((err = et61x251_stream_interrupt(cam)))
1951 return err;
1952
1953 if (copy_to_user(arg, &format, sizeof(format))) {
1954 cam->stream = stream;
1955 return -EFAULT;
1956 }
1957
1958 if (cam->module_param.force_munmap || cam->io == IO_READ)
1959 et61x251_release_buffers(cam);
1960
1961 err += et61x251_set_pix_format(cam, pix);
1962 err += et61x251_set_crop(cam, &rect);
1963 if (s->set_pix_format)
1964 err += s->set_pix_format(cam, pix);
1965 if (s->set_crop)
1966 err += s->set_crop(cam, &rect);
1967 err += et61x251_set_scale(cam, scale);
1968
1969 if (err) { /* atomic, no rollback in ioctl() */
1970 cam->state |= DEV_MISCONFIGURED;
1971 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1972 "use the camera, close and open /dev/video%d again.",
1973 cam->v4ldev->minor);
1974 return -EIO;
1975 }
1976
1977 memcpy(pfmt, pix, sizeof(*pix));
1978 memcpy(&(s->_rect), &rect, sizeof(rect));
1979
1980 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1981 nbuffers != et61x251_request_buffers(cam, nbuffers, cam->io)) {
1982 cam->state |= DEV_MISCONFIGURED;
1983 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1984 "use the camera, close and open /dev/video%d again.",
1985 cam->v4ldev->minor);
1986 return -ENOMEM;
1987 }
1988
1989 if (cam->io == IO_READ)
1990 et61x251_empty_framequeues(cam);
1991 else if (cam->module_param.force_munmap)
1992 et61x251_requeue_outqueue(cam);
1993
1994 cam->stream = stream;
1995
1996 return 0;
1997}
1998
1999
2000static int
2001et61x251_vidioc_g_jpegcomp(struct et61x251_device* cam, void __user * arg)
2002{
2003 if (copy_to_user(arg, &cam->compression,
2004 sizeof(cam->compression)))
2005 return -EFAULT;
2006
2007 return 0;
2008}
2009
2010
2011static int
2012et61x251_vidioc_s_jpegcomp(struct et61x251_device* cam, void __user * arg)
2013{
2014 struct v4l2_jpegcompression jc;
2015 const enum et61x251_stream_state stream = cam->stream;
2016 int err = 0;
2017
2018 if (copy_from_user(&jc, arg, sizeof(jc)))
2019 return -EFAULT;
2020
2021 if (jc.quality != 0 && jc.quality != 1)
2022 return -EINVAL;
2023
2024 if (cam->stream == STREAM_ON)
2025 if ((err = et61x251_stream_interrupt(cam)))
2026 return err;
2027
2028 err += et61x251_set_compression(cam, &jc);
2029 if (err) { /* atomic, no rollback in ioctl() */
2030 cam->state |= DEV_MISCONFIGURED;
2031 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2032 "problems. To use the camera, close and open "
2033 "/dev/video%d again.", cam->v4ldev->minor);
2034 return -EIO;
2035 }
2036
2037 cam->compression.quality = jc.quality;
2038
2039 cam->stream = stream;
2040
2041 return 0;
2042}
2043
2044
2045static int
2046et61x251_vidioc_reqbufs(struct et61x251_device* cam, void __user * arg)
2047{
2048 struct v4l2_requestbuffers rb;
2049 u32 i;
2050 int err;
2051
2052 if (copy_from_user(&rb, arg, sizeof(rb)))
2053 return -EFAULT;
2054
2055 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2056 rb.memory != V4L2_MEMORY_MMAP)
2057 return -EINVAL;
2058
2059 if (cam->io == IO_READ) {
2060 DBG(3, "Close and open the device again to choose the mmap "
2061 "I/O method");
2062 return -EINVAL;
2063 }
2064
2065 for (i = 0; i < cam->nbuffers; i++)
2066 if (cam->frame[i].vma_use_count) {
2067 DBG(3, "VIDIOC_REQBUFS failed. "
2068 "Previous buffers are still mapped.");
2069 return -EINVAL;
2070 }
2071
2072 if (cam->stream == STREAM_ON)
2073 if ((err = et61x251_stream_interrupt(cam)))
2074 return err;
2075
2076 et61x251_empty_framequeues(cam);
2077
2078 et61x251_release_buffers(cam);
2079 if (rb.count)
2080 rb.count = et61x251_request_buffers(cam, rb.count, IO_MMAP);
2081
2082 if (copy_to_user(arg, &rb, sizeof(rb))) {
2083 et61x251_release_buffers(cam);
2084 cam->io = IO_NONE;
2085 return -EFAULT;
2086 }
2087
2088 cam->io = rb.count ? IO_MMAP : IO_NONE;
2089
2090 return 0;
2091}
2092
2093
2094static int
2095et61x251_vidioc_querybuf(struct et61x251_device* cam, void __user * arg)
2096{
2097 struct v4l2_buffer b;
2098
2099 if (copy_from_user(&b, arg, sizeof(b)))
2100 return -EFAULT;
2101
2102 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2103 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2104 return -EINVAL;
2105
2106 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2107
2108 if (cam->frame[b.index].vma_use_count)
2109 b.flags |= V4L2_BUF_FLAG_MAPPED;
2110
2111 if (cam->frame[b.index].state == F_DONE)
2112 b.flags |= V4L2_BUF_FLAG_DONE;
2113 else if (cam->frame[b.index].state != F_UNUSED)
2114 b.flags |= V4L2_BUF_FLAG_QUEUED;
2115
2116 if (copy_to_user(arg, &b, sizeof(b)))
2117 return -EFAULT;
2118
2119 return 0;
2120}
2121
2122
2123static int
2124et61x251_vidioc_qbuf(struct et61x251_device* cam, void __user * arg)
2125{
2126 struct v4l2_buffer b;
2127 unsigned long lock_flags;
2128
2129 if (copy_from_user(&b, arg, sizeof(b)))
2130 return -EFAULT;
2131
2132 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2133 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2134 return -EINVAL;
2135
2136 if (cam->frame[b.index].state != F_UNUSED)
2137 return -EINVAL;
2138
2139 cam->frame[b.index].state = F_QUEUED;
2140
2141 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2142 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2143 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2144
2145 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2146
2147 return 0;
2148}
2149
2150
2151static int
2152et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2153 void __user * arg)
2154{
2155 struct v4l2_buffer b;
2156 struct et61x251_frame_t *f;
2157 unsigned long lock_flags;
2158 int err = 0;
2159
2160 if (copy_from_user(&b, arg, sizeof(b)))
2161 return -EFAULT;
2162
2163 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
2164 return -EINVAL;
2165
2166 if (list_empty(&cam->outqueue)) {
2167 if (cam->stream == STREAM_OFF)
2168 return -EINVAL;
2169 if (filp->f_flags & O_NONBLOCK)
2170 return -EAGAIN;
2171 err = wait_event_interruptible
2172 ( cam->wait_frame,
2173 (!list_empty(&cam->outqueue)) ||
2174 (cam->state & DEV_DISCONNECTED) ||
2175 (cam->state & DEV_MISCONFIGURED) );
2176 if (err)
2177 return err;
2178 if (cam->state & DEV_DISCONNECTED)
2179 return -ENODEV;
2180 if (cam->state & DEV_MISCONFIGURED)
2181 return -EIO;
2182 }
2183
2184 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2185 f = list_entry(cam->outqueue.next, struct et61x251_frame_t, frame);
2186 list_del(cam->outqueue.next);
2187 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2188
2189 f->state = F_UNUSED;
2190
2191 memcpy(&b, &f->buf, sizeof(b));
2192 if (f->vma_use_count)
2193 b.flags |= V4L2_BUF_FLAG_MAPPED;
2194
2195 if (copy_to_user(arg, &b, sizeof(b)))
2196 return -EFAULT;
2197
2198 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2199
2200 return 0;
2201}
2202
2203
2204static int
2205et61x251_vidioc_streamon(struct et61x251_device* cam, void __user * arg)
2206{
2207 int type;
2208
2209 if (copy_from_user(&type, arg, sizeof(type)))
2210 return -EFAULT;
2211
2212 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2213 return -EINVAL;
2214
2215 if (list_empty(&cam->inqueue))
2216 return -EINVAL;
2217
2218 cam->stream = STREAM_ON;
2219
2220 DBG(3, "Stream on");
2221
2222 return 0;
2223}
2224
2225
2226static int
2227et61x251_vidioc_streamoff(struct et61x251_device* cam, void __user * arg)
2228{
2229 int type, err;
2230
2231 if (copy_from_user(&type, arg, sizeof(type)))
2232 return -EFAULT;
2233
2234 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2235 return -EINVAL;
2236
2237 if (cam->stream == STREAM_ON)
2238 if ((err = et61x251_stream_interrupt(cam)))
2239 return err;
2240
2241 et61x251_empty_framequeues(cam);
2242
2243 DBG(3, "Stream off");
2244
2245 return 0;
2246}
2247
2248
2249static int
2250et61x251_vidioc_g_parm(struct et61x251_device* cam, void __user * arg)
2251{
2252 struct v4l2_streamparm sp;
2253
2254 if (copy_from_user(&sp, arg, sizeof(sp)))
2255 return -EFAULT;
2256
2257 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2258 return -EINVAL;
2259
2260 sp.parm.capture.extendedmode = 0;
2261 sp.parm.capture.readbuffers = cam->nreadbuffers;
2262
2263 if (copy_to_user(arg, &sp, sizeof(sp)))
2264 return -EFAULT;
2265
2266 return 0;
2267}
2268
2269
2270static int
2271et61x251_vidioc_s_parm(struct et61x251_device* cam, void __user * arg)
2272{
2273 struct v4l2_streamparm sp;
2274
2275 if (copy_from_user(&sp, arg, sizeof(sp)))
2276 return -EFAULT;
2277
2278 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2279 return -EINVAL;
2280
2281 sp.parm.capture.extendedmode = 0;
2282
2283 if (sp.parm.capture.readbuffers == 0)
2284 sp.parm.capture.readbuffers = cam->nreadbuffers;
2285
2286 if (sp.parm.capture.readbuffers > ET61X251_MAX_FRAMES)
2287 sp.parm.capture.readbuffers = ET61X251_MAX_FRAMES;
2288
2289 if (copy_to_user(arg, &sp, sizeof(sp)))
2290 return -EFAULT;
2291
2292 cam->nreadbuffers = sp.parm.capture.readbuffers;
2293
2294 return 0;
2295}
2296
2297
2298static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2299 unsigned int cmd, void __user * arg)
2300{
2301 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2302
2303 switch (cmd) {
2304
2305 case VIDIOC_QUERYCAP:
2306 return et61x251_vidioc_querycap(cam, arg);
2307
2308 case VIDIOC_ENUMINPUT:
2309 return et61x251_vidioc_enuminput(cam, arg);
2310
2311 case VIDIOC_G_INPUT:
2312 case VIDIOC_S_INPUT:
2313 return et61x251_vidioc_gs_input(cam, arg);
2314
2315 case VIDIOC_QUERYCTRL:
2316 return et61x251_vidioc_query_ctrl(cam, arg);
2317
2318 case VIDIOC_G_CTRL:
2319 return et61x251_vidioc_g_ctrl(cam, arg);
2320
2321 case VIDIOC_S_CTRL_OLD:
2322 case VIDIOC_S_CTRL:
2323 return et61x251_vidioc_s_ctrl(cam, arg);
2324
2325 case VIDIOC_CROPCAP_OLD:
2326 case VIDIOC_CROPCAP:
2327 return et61x251_vidioc_cropcap(cam, arg);
2328
2329 case VIDIOC_G_CROP:
2330 return et61x251_vidioc_g_crop(cam, arg);
2331
2332 case VIDIOC_S_CROP:
2333 return et61x251_vidioc_s_crop(cam, arg);
2334
2335 case VIDIOC_ENUM_FMT:
2336 return et61x251_vidioc_enum_fmt(cam, arg);
2337
2338 case VIDIOC_G_FMT:
2339 return et61x251_vidioc_g_fmt(cam, arg);
2340
2341 case VIDIOC_TRY_FMT:
2342 case VIDIOC_S_FMT:
2343 return et61x251_vidioc_try_s_fmt(cam, cmd, arg);
2344
2345 case VIDIOC_G_JPEGCOMP:
2346 return et61x251_vidioc_g_jpegcomp(cam, arg);
2347
2348 case VIDIOC_S_JPEGCOMP:
2349 return et61x251_vidioc_s_jpegcomp(cam, arg);
2350
2351 case VIDIOC_REQBUFS:
2352 return et61x251_vidioc_reqbufs(cam, arg);
2353
2354 case VIDIOC_QUERYBUF:
2355 return et61x251_vidioc_querybuf(cam, arg);
2356
2357 case VIDIOC_QBUF:
2358 return et61x251_vidioc_qbuf(cam, arg);
2359
2360 case VIDIOC_DQBUF:
2361 return et61x251_vidioc_dqbuf(cam, filp, arg);
2362
2363 case VIDIOC_STREAMON:
2364 return et61x251_vidioc_streamon(cam, arg);
2365
2366 case VIDIOC_STREAMOFF:
2367 return et61x251_vidioc_streamoff(cam, arg);
2368
2369 case VIDIOC_G_PARM:
2370 return et61x251_vidioc_g_parm(cam, arg);
2371
2372 case VIDIOC_S_PARM_OLD:
2373 case VIDIOC_S_PARM:
2374 return et61x251_vidioc_s_parm(cam, arg);
2375
2376 case VIDIOC_G_STD:
2377 case VIDIOC_S_STD:
2378 case VIDIOC_QUERYSTD:
2379 case VIDIOC_ENUMSTD:
2380 case VIDIOC_QUERYMENU:
2381 return -EINVAL;
2382
2383 default:
2384 return -EINVAL;
2385
2386 }
2387}
2388
2389
2390static int et61x251_ioctl(struct inode* inode, struct file* filp,
2391 unsigned int cmd, unsigned long arg)
2392{
2393 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2394 int err = 0;
2395
2396 if (down_interruptible(&cam->fileop_sem))
2397 return -ERESTARTSYS;
2398
2399 if (cam->state & DEV_DISCONNECTED) {
2400 DBG(1, "Device not present");
2401 up(&cam->fileop_sem);
2402 return -ENODEV;
2403 }
2404
2405 if (cam->state & DEV_MISCONFIGURED) {
2406 DBG(1, "The camera is misconfigured. Close and open it "
2407 "again.");
2408 up(&cam->fileop_sem);
2409 return -EIO;
2410 }
2411
2412 V4LDBG(3, "et61x251", cmd);
2413
2414 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2415
2416 up(&cam->fileop_sem);
2417
2418 return err;
2419}
2420
2421
2422static struct file_operations et61x251_fops = {
2423 .owner = THIS_MODULE,
2424 .open = et61x251_open,
2425 .release = et61x251_release,
2426 .ioctl = et61x251_ioctl,
2427 .read = et61x251_read,
2428 .poll = et61x251_poll,
2429 .mmap = et61x251_mmap,
2430 .llseek = no_llseek,
2431};
2432
2433/*****************************************************************************/
2434
2435/* It exists a single interface only. We do not need to validate anything. */
2436static int
2437et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2438{
2439 struct usb_device *udev = interface_to_usbdev(intf);
2440 struct et61x251_device* cam;
2441 static unsigned int dev_nr = 0;
2442 unsigned int i;
2443 int err = 0;
2444
2445 if (!(cam = kzalloc(sizeof(struct et61x251_device), GFP_KERNEL)))
2446 return -ENOMEM;
2447
2448 cam->usbdev = udev;
2449
2450 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2451 DBG(1, "kmalloc() failed");
2452 err = -ENOMEM;
2453 goto fail;
2454 }
2455
2456 if (!(cam->v4ldev = video_device_alloc())) {
2457 DBG(1, "video_device_alloc() failed");
2458 err = -ENOMEM;
2459 goto fail;
2460 }
2461
2462 init_MUTEX(&cam->dev_sem);
2463
2464 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2465 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
2466
2467 for (i = 0; et61x251_sensor_table[i]; i++) {
2468 err = et61x251_sensor_table[i](cam);
2469 if (!err)
2470 break;
2471 }
2472
2473 if (!err && cam->sensor)
2474 DBG(2, "%s image sensor detected", cam->sensor->name);
2475 else {
2476 DBG(1, "No supported image sensor detected");
2477 err = -ENODEV;
2478 goto fail;
2479 }
2480
2481 if (et61x251_init(cam)) {
2482 DBG(1, "Initialization failed. I will retry on open().");
2483 cam->state |= DEV_MISCONFIGURED;
2484 }
2485
2486 strcpy(cam->v4ldev->name, "ET61X[12]51 PC Camera");
2487 cam->v4ldev->owner = THIS_MODULE;
2488 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2489 cam->v4ldev->hardware = 0;
2490 cam->v4ldev->fops = &et61x251_fops;
2491 cam->v4ldev->minor = video_nr[dev_nr];
2492 cam->v4ldev->release = video_device_release;
2493 video_set_drvdata(cam->v4ldev, cam);
2494
2495 down(&cam->dev_sem);
2496
2497 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2498 video_nr[dev_nr]);
2499 if (err) {
2500 DBG(1, "V4L2 device registration failed");
2501 if (err == -ENFILE && video_nr[dev_nr] == -1)
2502 DBG(1, "Free /dev/videoX node not found");
2503 video_nr[dev_nr] = -1;
2504 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2505 up(&cam->dev_sem);
2506 goto fail;
2507 }
2508
2509 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2510
2511 cam->module_param.force_munmap = force_munmap[dev_nr];
2512
2513 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2514
2515#ifdef CONFIG_VIDEO_ADV_DEBUG
2516 et61x251_create_sysfs(cam);
2517 DBG(2, "Optional device control through 'sysfs' interface ready");
2518#endif
2519
2520 usb_set_intfdata(intf, cam);
2521
2522 up(&cam->dev_sem);
2523
2524 return 0;
2525
2526fail:
2527 if (cam) {
2528 kfree(cam->control_buffer);
2529 if (cam->v4ldev)
2530 video_device_release(cam->v4ldev);
2531 kfree(cam);
2532 }
2533 return err;
2534}
2535
2536
2537static void et61x251_usb_disconnect(struct usb_interface* intf)
2538{
2539 struct et61x251_device* cam = usb_get_intfdata(intf);
2540
2541 if (!cam)
2542 return;
2543
2544 down_write(&et61x251_disconnect);
2545
2546 down(&cam->dev_sem);
2547
2548 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2549
2550 wake_up_interruptible_all(&cam->open);
2551
2552 if (cam->users) {
2553 DBG(2, "Device /dev/video%d is open! Deregistration and "
2554 "memory deallocation are deferred on close.",
2555 cam->v4ldev->minor);
2556 cam->state |= DEV_MISCONFIGURED;
2557 et61x251_stop_transfer(cam);
2558 cam->state |= DEV_DISCONNECTED;
2559 wake_up_interruptible(&cam->wait_frame);
2560 wake_up_interruptible(&cam->wait_stream);
2561 } else {
2562 cam->state |= DEV_DISCONNECTED;
2563 et61x251_release_resources(cam);
2564 }
2565
2566 up(&cam->dev_sem);
2567
2568 if (!cam->users)
2569 kfree(cam);
2570
2571 up_write(&et61x251_disconnect);
2572}
2573
2574
2575static struct usb_driver et61x251_usb_driver = {
2576 .name = "et61x251",
2577 .id_table = et61x251_id_table,
2578 .probe = et61x251_usb_probe,
2579 .disconnect = et61x251_usb_disconnect,
2580};
2581
2582/*****************************************************************************/
2583
2584static int __init et61x251_module_init(void)
2585{
2586 int err = 0;
2587
2588 KDBG(2, ET61X251_MODULE_NAME " v" ET61X251_MODULE_VERSION);
2589 KDBG(3, ET61X251_MODULE_AUTHOR);
2590
2591 if ((err = usb_register(&et61x251_usb_driver)))
2592 KDBG(1, "usb_register() failed");
2593
2594 return err;
2595}
2596
2597
2598static void __exit et61x251_module_exit(void)
2599{
2600 usb_deregister(&et61x251_usb_driver);
2601}
2602
2603
2604module_init(et61x251_module_init);
2605module_exit(et61x251_module_exit);
diff --git a/drivers/usb/media/et61x251_sensor.h b/drivers/usb/media/et61x251_sensor.h
new file mode 100644
index 000000000000..b9df91062fc0
--- /dev/null
+++ b/drivers/usb/media/et61x251_sensor.h
@@ -0,0 +1,115 @@
1/***************************************************************************
2 * API for image sensors connected to ET61X[12]51 PC Camera Controllers *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _ET61X251_SENSOR_H_
22#define _ET61X251_SENSOR_H_
23
24#include <linux/usb.h>
25#include <linux/videodev.h>
26#include <linux/device.h>
27#include <linux/stddef.h>
28#include <linux/errno.h>
29#include <asm/types.h>
30
31struct et61x251_device;
32struct et61x251_sensor;
33
34/*****************************************************************************/
35
36extern int et61x251_probe_tas5130d1b(struct et61x251_device* cam);
37
38#define ET61X251_SENSOR_TABLE \
39/* Weak detections must go at the end of the list */ \
40static int (*et61x251_sensor_table[])(struct et61x251_device*) = { \
41 &et61x251_probe_tas5130d1b, \
42 NULL, \
43};
44
45extern void
46et61x251_attach_sensor(struct et61x251_device* cam,
47 struct et61x251_sensor* sensor);
48
49/*****************************************************************************/
50
51extern int et61x251_write_reg(struct et61x251_device*, u8 value, u16 index);
52extern int et61x251_read_reg(struct et61x251_device*, u16 index);
53extern int et61x251_i2c_write(struct et61x251_device*, u8 address, u8 value);
54extern int et61x251_i2c_read(struct et61x251_device*, u8 address);
55extern int et61x251_i2c_try_write(struct et61x251_device*,
56 struct et61x251_sensor*, u8 address,
57 u8 value);
58extern int et61x251_i2c_try_read(struct et61x251_device*,
59 struct et61x251_sensor*, u8 address);
60extern int et61x251_i2c_raw_write(struct et61x251_device*, u8 n, u8 data1,
61 u8 data2, u8 data3, u8 data4, u8 data5,
62 u8 data6, u8 data7, u8 data8, u8 address);
63
64/*****************************************************************************/
65
66enum et61x251_i2c_sysfs_ops {
67 ET61X251_I2C_READ = 0x01,
68 ET61X251_I2C_WRITE = 0x02,
69};
70
71enum et61x251_i2c_interface {
72 ET61X251_I2C_2WIRES,
73 ET61X251_I2C_3WIRES,
74};
75
76/* Repeat start condition when RSTA is high */
77enum et61x251_i2c_rsta {
78 ET61X251_I2C_RSTA_STOP = 0x00, /* stop then start */
79 ET61X251_I2C_RSTA_REPEAT = 0x01, /* repeat start */
80};
81
82#define ET61X251_MAX_CTRLS V4L2_CID_LASTP1-V4L2_CID_BASE+10
83
84struct et61x251_sensor {
85 char name[32];
86
87 enum et61x251_i2c_sysfs_ops sysfs_ops;
88
89 enum et61x251_i2c_interface interface;
90 u8 i2c_slave_id;
91 enum et61x251_i2c_rsta rsta;
92 struct v4l2_rect active_pixel; /* left and top define FVSX and FVSY */
93
94 struct v4l2_queryctrl qctrl[ET61X251_MAX_CTRLS];
95 struct v4l2_cropcap cropcap;
96 struct v4l2_pix_format pix_format;
97
98 int (*init)(struct et61x251_device* cam);
99 int (*get_ctrl)(struct et61x251_device* cam,
100 struct v4l2_control* ctrl);
101 int (*set_ctrl)(struct et61x251_device* cam,
102 const struct v4l2_control* ctrl);
103 int (*set_crop)(struct et61x251_device* cam,
104 const struct v4l2_rect* rect);
105 int (*set_pix_format)(struct et61x251_device* cam,
106 const struct v4l2_pix_format* pix);
107
108 const struct usb_device* usbdev;
109
110 /* Private */
111 struct v4l2_queryctrl _qctrl[ET61X251_MAX_CTRLS];
112 struct v4l2_rect _rect;
113};
114
115#endif /* _ET61X251_SENSOR_H_ */
diff --git a/drivers/usb/media/et61x251_tas5130d1b.c b/drivers/usb/media/et61x251_tas5130d1b.c
new file mode 100644
index 000000000000..65f1ae9cf2b3
--- /dev/null
+++ b/drivers/usb/media/et61x251_tas5130d1b.c
@@ -0,0 +1,137 @@
1/***************************************************************************
2 * Plug-in for TAS5130D1B image sensor connected to the ET61X[12]51 *
3 * PC Camera Controllers *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include "et61x251_sensor.h"
23
24
25static int tas5130d1b_init(struct et61x251_device* cam)
26{
27 int err = 0;
28
29 err += et61x251_write_reg(cam, 0x14, 0x01);
30 err += et61x251_write_reg(cam, 0x1b, 0x02);
31 err += et61x251_write_reg(cam, 0x02, 0x12);
32 err += et61x251_write_reg(cam, 0x0e, 0x60);
33 err += et61x251_write_reg(cam, 0x80, 0x61);
34 err += et61x251_write_reg(cam, 0xf0, 0x62);
35 err += et61x251_write_reg(cam, 0x03, 0x63);
36 err += et61x251_write_reg(cam, 0x14, 0x64);
37 err += et61x251_write_reg(cam, 0xf4, 0x65);
38 err += et61x251_write_reg(cam, 0x01, 0x66);
39 err += et61x251_write_reg(cam, 0x05, 0x67);
40 err += et61x251_write_reg(cam, 0x8f, 0x68);
41 err += et61x251_write_reg(cam, 0x0f, 0x8d);
42 err += et61x251_write_reg(cam, 0x08, 0x8e);
43
44 return err;
45}
46
47
48static int tas5130d1b_set_ctrl(struct et61x251_device* cam,
49 const struct v4l2_control* ctrl)
50{
51 int err = 0;
52
53 switch (ctrl->id) {
54 case V4L2_CID_GAIN:
55 err += et61x251_i2c_raw_write(cam, 2, 0x20,
56 0xf6-ctrl->value, 0, 0, 0,
57 0, 0, 0, 0);
58 break;
59 case V4L2_CID_EXPOSURE:
60 err += et61x251_i2c_raw_write(cam, 2, 0x40,
61 0x47-ctrl->value, 0, 0, 0,
62 0, 0, 0, 0);
63 break;
64 default:
65 return -EINVAL;
66 }
67
68 return err ? -EIO : 0;
69}
70
71
72static struct et61x251_sensor tas5130d1b = {
73 .name = "TAS5130D1B",
74 .interface = ET61X251_I2C_3WIRES,
75 .rsta = ET61X251_I2C_RSTA_STOP,
76 .active_pixel = {
77 .left = 106,
78 .top = 13,
79 },
80 .init = &tas5130d1b_init,
81 .qctrl = {
82 {
83 .id = V4L2_CID_GAIN,
84 .type = V4L2_CTRL_TYPE_INTEGER,
85 .name = "global gain",
86 .minimum = 0x00,
87 .maximum = 0xf6,
88 .step = 0x02,
89 .default_value = 0x0d,
90 .flags = 0,
91 },
92 {
93 .id = V4L2_CID_EXPOSURE,
94 .type = V4L2_CTRL_TYPE_INTEGER,
95 .name = "exposure",
96 .minimum = 0x00,
97 .maximum = 0x47,
98 .step = 0x01,
99 .default_value = 0x23,
100 .flags = 0,
101 },
102 },
103 .set_ctrl = &tas5130d1b_set_ctrl,
104 .cropcap = {
105 .bounds = {
106 .left = 0,
107 .top = 0,
108 .width = 640,
109 .height = 480,
110 },
111 .defrect = {
112 .left = 0,
113 .top = 0,
114 .width = 640,
115 .height = 480,
116 },
117 },
118 .pix_format = {
119 .width = 640,
120 .height = 480,
121 .pixelformat = V4L2_PIX_FMT_SBGGR8,
122 .priv = 8,
123 },
124};
125
126
127int et61x251_probe_tas5130d1b(struct et61x251_device* cam)
128{
129 /* This sensor has no identifiers, so let's attach it anyway */
130 et61x251_attach_sensor(cam, &tas5130d1b);
131
132 /* Sensor detection is based on USB pid/vid */
133 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6251)
134 return -ENODEV;
135
136 return 0;
137}
diff --git a/drivers/usb/media/ov511.c b/drivers/usb/media/ov511.c
index 8af665bbe330..51e9cc06f7e3 100644
--- a/drivers/usb/media/ov511.c
+++ b/drivers/usb/media/ov511.c
@@ -204,22 +204,10 @@ MODULE_LICENSE("GPL");
204 204
205static struct usb_driver ov511_driver; 205static struct usb_driver ov511_driver;
206 206
207static struct ov51x_decomp_ops *ov511_decomp_ops;
208static struct ov51x_decomp_ops *ov511_mmx_decomp_ops;
209static struct ov51x_decomp_ops *ov518_decomp_ops;
210static struct ov51x_decomp_ops *ov518_mmx_decomp_ops;
211
212/* Number of times to retry a failed I2C transaction. Increase this if you 207/* Number of times to retry a failed I2C transaction. Increase this if you
213 * are getting "Failed to read sensor ID..." */ 208 * are getting "Failed to read sensor ID..." */
214static const int i2c_detect_tries = 5; 209static const int i2c_detect_tries = 5;
215 210
216/* MMX support is present in kernel and CPU. Checked upon decomp module load. */
217#if defined(__i386__) || defined(__x86_64__)
218#define ov51x_mmx_available (cpu_has_mmx)
219#else
220#define ov51x_mmx_available (0)
221#endif
222
223static struct usb_device_id device_table [] = { 211static struct usb_device_id device_table [] = {
224 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) }, 212 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
225 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) }, 213 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
@@ -3012,93 +3000,18 @@ yuv420raw_to_yuv420p(struct ov511_frame *frame,
3012 * 3000 *
3013 **********************************************************************/ 3001 **********************************************************************/
3014 3002
3015/* Chooses a decompression module, locks it, and sets ov->decomp_ops
3016 * accordingly. Returns -ENXIO if decompressor is not available, otherwise
3017 * returns 0 if no other error.
3018 */
3019static int 3003static int
3020request_decompressor(struct usb_ov511 *ov) 3004request_decompressor(struct usb_ov511 *ov)
3021{ 3005{
3022 if (!ov) 3006 if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
3023 return -ENODEV; 3007 err("No decompressor available");
3024
3025 if (ov->decomp_ops) {
3026 err("ERROR: Decompressor already requested!");
3027 return -EINVAL;
3028 }
3029
3030 lock_kernel();
3031
3032 /* Try to get MMX, and fall back on no-MMX if necessary */
3033 if (ov->bclass == BCL_OV511) {
3034 if (ov511_mmx_decomp_ops) {
3035 PDEBUG(3, "Using OV511 MMX decompressor");
3036 ov->decomp_ops = ov511_mmx_decomp_ops;
3037 } else if (ov511_decomp_ops) {
3038 PDEBUG(3, "Using OV511 decompressor");
3039 ov->decomp_ops = ov511_decomp_ops;
3040 } else {
3041 err("No decompressor available");
3042 }
3043 } else if (ov->bclass == BCL_OV518) {
3044 if (ov518_mmx_decomp_ops) {
3045 PDEBUG(3, "Using OV518 MMX decompressor");
3046 ov->decomp_ops = ov518_mmx_decomp_ops;
3047 } else if (ov518_decomp_ops) {
3048 PDEBUG(3, "Using OV518 decompressor");
3049 ov->decomp_ops = ov518_decomp_ops;
3050 } else {
3051 err("No decompressor available");
3052 }
3053 } else { 3008 } else {
3054 err("Unknown bridge"); 3009 err("Unknown bridge");
3055 } 3010 }
3056 3011
3057 if (!ov->decomp_ops)
3058 goto nosys;
3059
3060 if (!ov->decomp_ops->owner) {
3061 ov->decomp_ops = NULL;
3062 goto nosys;
3063 }
3064
3065 if (!try_module_get(ov->decomp_ops->owner))
3066 goto nosys;
3067
3068 unlock_kernel();
3069 return 0;
3070
3071 nosys:
3072 unlock_kernel();
3073 return -ENOSYS; 3012 return -ENOSYS;
3074} 3013}
3075 3014
3076/* Unlocks decompression module and nulls ov->decomp_ops. Safe to call even
3077 * if ov->decomp_ops is NULL.
3078 */
3079static void
3080release_decompressor(struct usb_ov511 *ov)
3081{
3082 int released = 0; /* Did we actually do anything? */
3083
3084 if (!ov)
3085 return;
3086
3087 lock_kernel();
3088
3089 if (ov->decomp_ops) {
3090 module_put(ov->decomp_ops->owner);
3091 released = 1;
3092 }
3093
3094 ov->decomp_ops = NULL;
3095
3096 unlock_kernel();
3097
3098 if (released)
3099 PDEBUG(3, "Decompressor released");
3100}
3101
3102static void 3015static void
3103decompress(struct usb_ov511 *ov, struct ov511_frame *frame, 3016decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
3104 unsigned char *pIn0, unsigned char *pOut0) 3017 unsigned char *pIn0, unsigned char *pOut0)
@@ -3107,31 +3020,6 @@ decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
3107 if (request_decompressor(ov)) 3020 if (request_decompressor(ov))
3108 return; 3021 return;
3109 3022
3110 PDEBUG(4, "Decompressing %d bytes", frame->bytes_recvd);
3111
3112 if (frame->format == VIDEO_PALETTE_GREY
3113 && ov->decomp_ops->decomp_400) {
3114 int ret = ov->decomp_ops->decomp_400(
3115 pIn0,
3116 pOut0,
3117 frame->compbuf,
3118 frame->rawwidth,
3119 frame->rawheight,
3120 frame->bytes_recvd);
3121 PDEBUG(4, "DEBUG: decomp_400 returned %d", ret);
3122 } else if (frame->format != VIDEO_PALETTE_GREY
3123 && ov->decomp_ops->decomp_420) {
3124 int ret = ov->decomp_ops->decomp_420(
3125 pIn0,
3126 pOut0,
3127 frame->compbuf,
3128 frame->rawwidth,
3129 frame->rawheight,
3130 frame->bytes_recvd);
3131 PDEBUG(4, "DEBUG: decomp_420 returned %d", ret);
3132 } else {
3133 err("Decompressor does not support this format");
3134 }
3135} 3023}
3136 3024
3137/********************************************************************** 3025/**********************************************************************
@@ -4087,8 +3975,6 @@ ov51x_v4l1_close(struct inode *inode, struct file *file)
4087 ov->user--; 3975 ov->user--;
4088 ov51x_stop_isoc(ov); 3976 ov51x_stop_isoc(ov);
4089 3977
4090 release_decompressor(ov);
4091
4092 if (ov->led_policy == LED_AUTO) 3978 if (ov->led_policy == LED_AUTO)
4093 ov51x_led_control(ov, 0); 3979 ov51x_led_control(ov, 0);
4094 3980
@@ -6021,82 +5907,6 @@ static struct usb_driver ov511_driver = {
6021 * 5907 *
6022 ***************************************************************************/ 5908 ***************************************************************************/
6023 5909
6024/* Returns 0 for success */
6025int
6026ov511_register_decomp_module(int ver, struct ov51x_decomp_ops *ops, int ov518,
6027 int mmx)
6028{
6029 if (ver != DECOMP_INTERFACE_VER) {
6030 err("Decompression module has incompatible");
6031 err("interface version %d", ver);
6032 err("Interface version %d is required", DECOMP_INTERFACE_VER);
6033 return -EINVAL;
6034 }
6035
6036 if (!ops)
6037 return -EFAULT;
6038
6039 if (mmx && !ov51x_mmx_available) {
6040 err("MMX not available on this system or kernel");
6041 return -EINVAL;
6042 }
6043
6044 lock_kernel();
6045
6046 if (ov518) {
6047 if (mmx) {
6048 if (ov518_mmx_decomp_ops)
6049 goto err_in_use;
6050 else
6051 ov518_mmx_decomp_ops = ops;
6052 } else {
6053 if (ov518_decomp_ops)
6054 goto err_in_use;
6055 else
6056 ov518_decomp_ops = ops;
6057 }
6058 } else {
6059 if (mmx) {
6060 if (ov511_mmx_decomp_ops)
6061 goto err_in_use;
6062 else
6063 ov511_mmx_decomp_ops = ops;
6064 } else {
6065 if (ov511_decomp_ops)
6066 goto err_in_use;
6067 else
6068 ov511_decomp_ops = ops;
6069 }
6070 }
6071
6072 unlock_kernel();
6073 return 0;
6074
6075err_in_use:
6076 unlock_kernel();
6077 return -EBUSY;
6078}
6079
6080void
6081ov511_deregister_decomp_module(int ov518, int mmx)
6082{
6083 lock_kernel();
6084
6085 if (ov518) {
6086 if (mmx)
6087 ov518_mmx_decomp_ops = NULL;
6088 else
6089 ov518_decomp_ops = NULL;
6090 } else {
6091 if (mmx)
6092 ov511_mmx_decomp_ops = NULL;
6093 else
6094 ov511_decomp_ops = NULL;
6095 }
6096
6097 unlock_kernel();
6098}
6099
6100static int __init 5910static int __init
6101usb_ov511_init(void) 5911usb_ov511_init(void)
6102{ 5912{
@@ -6123,5 +5933,3 @@ usb_ov511_exit(void)
6123module_init(usb_ov511_init); 5933module_init(usb_ov511_init);
6124module_exit(usb_ov511_exit); 5934module_exit(usb_ov511_exit);
6125 5935
6126EXPORT_SYMBOL(ov511_register_decomp_module);
6127EXPORT_SYMBOL(ov511_deregister_decomp_module);
diff --git a/drivers/usb/media/pwc/pwc-ctrl.c b/drivers/usb/media/pwc/pwc-ctrl.c
index 359c4b2df735..3ebb6e9cdf92 100644
--- a/drivers/usb/media/pwc/pwc-ctrl.c
+++ b/drivers/usb/media/pwc/pwc-ctrl.c
@@ -1152,45 +1152,6 @@ int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
1152 /* End of Add-Ons */ 1152 /* End of Add-Ons */
1153 /* ************************************************* */ 1153 /* ************************************************* */
1154 1154
1155/* Linux 2.5.something and 2.6 pass direct pointers to arguments of
1156 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
1157 and copy_to_user() calls. With these macros we circumvent this,
1158 and let me maintain only one source file. The functionality is
1159 exactly the same otherwise.
1160 */
1161
1162#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1163
1164/* define local variable for arg */
1165#define ARG_DEF(ARG_type, ARG_name)\
1166 ARG_type *ARG_name = arg;
1167/* copy arg to local variable */
1168#define ARG_IN(ARG_name) /* nothing */
1169/* argument itself (referenced) */
1170#define ARGR(ARG_name) (*ARG_name)
1171/* argument address */
1172#define ARGA(ARG_name) ARG_name
1173/* copy local variable to arg */
1174#define ARG_OUT(ARG_name) /* nothing */
1175
1176#else
1177
1178#define ARG_DEF(ARG_type, ARG_name)\
1179 ARG_type ARG_name;
1180#define ARG_IN(ARG_name)\
1181 if (copy_from_user(&ARG_name, arg, sizeof(ARG_name))) {\
1182 ret = -EFAULT;\
1183 break;\
1184 }
1185#define ARGR(ARG_name) ARG_name
1186#define ARGA(ARG_name) &ARG_name
1187#define ARG_OUT(ARG_name)\
1188 if (copy_to_user(arg, &ARG_name, sizeof(ARG_name))) {\
1189 ret = -EFAULT;\
1190 break;\
1191 }
1192
1193#endif
1194 1155
1195int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg) 1156int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1196{ 1157{
@@ -1220,243 +1181,206 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1220 1181
1221 case VIDIOCPWCSCQUAL: 1182 case VIDIOCPWCSCQUAL:
1222 { 1183 {
1223 ARG_DEF(int, qual) 1184 int *qual = arg;
1224 1185
1225 ARG_IN(qual) 1186 if (*qual < 0 || *qual > 3)
1226 if (ARGR(qual) < 0 || ARGR(qual) > 3)
1227 ret = -EINVAL; 1187 ret = -EINVAL;
1228 else 1188 else
1229 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot); 1189 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, *qual, pdev->vsnapshot);
1230 if (ret >= 0) 1190 if (ret >= 0)
1231 pdev->vcompression = ARGR(qual); 1191 pdev->vcompression = *qual;
1232 break; 1192 break;
1233 } 1193 }
1234 1194
1235 case VIDIOCPWCGCQUAL: 1195 case VIDIOCPWCGCQUAL:
1236 { 1196 {
1237 ARG_DEF(int, qual) 1197 int *qual = arg;
1238 1198 *qual = pdev->vcompression;
1239 ARGR(qual) = pdev->vcompression;
1240 ARG_OUT(qual)
1241 break; 1199 break;
1242 } 1200 }
1243 1201
1244 case VIDIOCPWCPROBE: 1202 case VIDIOCPWCPROBE:
1245 { 1203 {
1246 ARG_DEF(struct pwc_probe, probe) 1204 struct pwc_probe *probe = arg;
1247 1205 strcpy(probe->name, pdev->vdev->name);
1248 strcpy(ARGR(probe).name, pdev->vdev->name); 1206 probe->type = pdev->type;
1249 ARGR(probe).type = pdev->type;
1250 ARG_OUT(probe)
1251 break; 1207 break;
1252 } 1208 }
1253 1209
1254 case VIDIOCPWCGSERIAL: 1210 case VIDIOCPWCGSERIAL:
1255 { 1211 {
1256 ARG_DEF(struct pwc_serial, serial) 1212 struct pwc_serial *serial = arg;
1257 1213 strcpy(serial->serial, pdev->serial);
1258 strcpy(ARGR(serial).serial, pdev->serial);
1259 ARG_OUT(serial)
1260 break; 1214 break;
1261 } 1215 }
1262 1216
1263 case VIDIOCPWCSAGC: 1217 case VIDIOCPWCSAGC:
1264 { 1218 {
1265 ARG_DEF(int, agc) 1219 int *agc = arg;
1266 1220 if (pwc_set_agc(pdev, *agc < 0 ? 1 : 0, *agc))
1267 ARG_IN(agc)
1268 if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
1269 ret = -EINVAL; 1221 ret = -EINVAL;
1270 break; 1222 break;
1271 } 1223 }
1272 1224
1273 case VIDIOCPWCGAGC: 1225 case VIDIOCPWCGAGC:
1274 { 1226 {
1275 ARG_DEF(int, agc) 1227 int *agc = arg;
1276 1228
1277 if (pwc_get_agc(pdev, ARGA(agc))) 1229 if (pwc_get_agc(pdev, agc))
1278 ret = -EINVAL; 1230 ret = -EINVAL;
1279 ARG_OUT(agc)
1280 break; 1231 break;
1281 } 1232 }
1282 1233
1283 case VIDIOCPWCSSHUTTER: 1234 case VIDIOCPWCSSHUTTER:
1284 { 1235 {
1285 ARG_DEF(int, shutter_speed) 1236 int *shutter_speed = arg;
1286 1237 ret = pwc_set_shutter_speed(pdev, *shutter_speed < 0 ? 1 : 0, *shutter_speed);
1287 ARG_IN(shutter_speed)
1288 ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
1289 break; 1238 break;
1290 } 1239 }
1291 1240
1292 case VIDIOCPWCSAWB: 1241 case VIDIOCPWCSAWB:
1293 { 1242 {
1294 ARG_DEF(struct pwc_whitebalance, wb) 1243 struct pwc_whitebalance *wb = arg;
1295 1244
1296 ARG_IN(wb) 1245 ret = pwc_set_awb(pdev, wb->mode);
1297 ret = pwc_set_awb(pdev, ARGR(wb).mode); 1246 if (ret >= 0 && wb->mode == PWC_WB_MANUAL) {
1298 if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) { 1247 pwc_set_red_gain(pdev, wb->manual_red);
1299 pwc_set_red_gain(pdev, ARGR(wb).manual_red); 1248 pwc_set_blue_gain(pdev, wb->manual_blue);
1300 pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
1301 } 1249 }
1302 break; 1250 break;
1303 } 1251 }
1304 1252
1305 case VIDIOCPWCGAWB: 1253 case VIDIOCPWCGAWB:
1306 { 1254 {
1307 ARG_DEF(struct pwc_whitebalance, wb) 1255 struct pwc_whitebalance *wb = arg;
1308 1256
1309 memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance)); 1257 memset(wb, 0, sizeof(struct pwc_whitebalance));
1310 ARGR(wb).mode = pwc_get_awb(pdev); 1258 wb->mode = pwc_get_awb(pdev);
1311 if (ARGR(wb).mode < 0) 1259 if (wb->mode < 0)
1312 ret = -EINVAL; 1260 ret = -EINVAL;
1313 else { 1261 else {
1314 if (ARGR(wb).mode == PWC_WB_MANUAL) { 1262 if (wb->mode == PWC_WB_MANUAL) {
1315 ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red); 1263 ret = pwc_get_red_gain(pdev, &wb->manual_red);
1316 if (ret < 0) 1264 if (ret < 0)
1317 break; 1265 break;
1318 ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue); 1266 ret = pwc_get_blue_gain(pdev, &wb->manual_blue);
1319 if (ret < 0) 1267 if (ret < 0)
1320 break; 1268 break;
1321 } 1269 }
1322 if (ARGR(wb).mode == PWC_WB_AUTO) { 1270 if (wb->mode == PWC_WB_AUTO) {
1323 ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red); 1271 ret = pwc_read_red_gain(pdev, &wb->read_red);
1324 if (ret < 0) 1272 if (ret < 0)
1325 break; 1273 break;
1326 ret =pwc_read_blue_gain(pdev, &ARGR(wb).read_blue); 1274 ret = pwc_read_blue_gain(pdev, &wb->read_blue);
1327 if (ret < 0) 1275 if (ret < 0)
1328 break; 1276 break;
1329 } 1277 }
1330 } 1278 }
1331 ARG_OUT(wb)
1332 break; 1279 break;
1333 } 1280 }
1334 1281
1335 case VIDIOCPWCSAWBSPEED: 1282 case VIDIOCPWCSAWBSPEED:
1336 { 1283 {
1337 ARG_DEF(struct pwc_wb_speed, wbs) 1284 struct pwc_wb_speed *wbs = arg;
1338 1285
1339 if (ARGR(wbs).control_speed > 0) { 1286 if (wbs->control_speed > 0) {
1340 ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed); 1287 ret = pwc_set_wb_speed(pdev, wbs->control_speed);
1341 } 1288 }
1342 if (ARGR(wbs).control_delay > 0) { 1289 if (wbs->control_delay > 0) {
1343 ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay); 1290 ret = pwc_set_wb_delay(pdev, wbs->control_delay);
1344 } 1291 }
1345 break; 1292 break;
1346 } 1293 }
1347 1294
1348 case VIDIOCPWCGAWBSPEED: 1295 case VIDIOCPWCGAWBSPEED:
1349 { 1296 {
1350 ARG_DEF(struct pwc_wb_speed, wbs) 1297 struct pwc_wb_speed *wbs = arg;
1351 1298
1352 ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed); 1299 ret = pwc_get_wb_speed(pdev, &wbs->control_speed);
1353 if (ret < 0) 1300 if (ret < 0)
1354 break; 1301 break;
1355 ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay); 1302 ret = pwc_get_wb_delay(pdev, &wbs->control_delay);
1356 if (ret < 0) 1303 if (ret < 0)
1357 break; 1304 break;
1358 ARG_OUT(wbs)
1359 break; 1305 break;
1360 } 1306 }
1361 1307
1362 case VIDIOCPWCSLED: 1308 case VIDIOCPWCSLED:
1363 { 1309 {
1364 ARG_DEF(struct pwc_leds, leds) 1310 struct pwc_leds *leds = arg;
1365 1311 ret = pwc_set_leds(pdev, leds->led_on, leds->led_off);
1366 ARG_IN(leds)
1367 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
1368 break; 1312 break;
1369 } 1313 }
1370 1314
1371 1315
1372 case VIDIOCPWCGLED: 1316 case VIDIOCPWCGLED:
1373 { 1317 {
1374 ARG_DEF(struct pwc_leds, leds) 1318 struct pwc_leds *leds = arg;
1375 1319 ret = pwc_get_leds(pdev, &leds->led_on, &leds->led_off);
1376 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
1377 ARG_OUT(leds)
1378 break; 1320 break;
1379 } 1321 }
1380 1322
1381 case VIDIOCPWCSCONTOUR: 1323 case VIDIOCPWCSCONTOUR:
1382 { 1324 {
1383 ARG_DEF(int, contour) 1325 int *contour = arg;
1384 1326 ret = pwc_set_contour(pdev, *contour);
1385 ARG_IN(contour)
1386 ret = pwc_set_contour(pdev, ARGR(contour));
1387 break; 1327 break;
1388 } 1328 }
1389 1329
1390 case VIDIOCPWCGCONTOUR: 1330 case VIDIOCPWCGCONTOUR:
1391 { 1331 {
1392 ARG_DEF(int, contour) 1332 int *contour = arg;
1393 1333 ret = pwc_get_contour(pdev, contour);
1394 ret = pwc_get_contour(pdev, ARGA(contour));
1395 ARG_OUT(contour)
1396 break; 1334 break;
1397 } 1335 }
1398 1336
1399 case VIDIOCPWCSBACKLIGHT: 1337 case VIDIOCPWCSBACKLIGHT:
1400 { 1338 {
1401 ARG_DEF(int, backlight) 1339 int *backlight = arg;
1402 1340 ret = pwc_set_backlight(pdev, *backlight);
1403 ARG_IN(backlight)
1404 ret = pwc_set_backlight(pdev, ARGR(backlight));
1405 break; 1341 break;
1406 } 1342 }
1407 1343
1408 case VIDIOCPWCGBACKLIGHT: 1344 case VIDIOCPWCGBACKLIGHT:
1409 { 1345 {
1410 ARG_DEF(int, backlight) 1346 int *backlight = arg;
1411 1347 ret = pwc_get_backlight(pdev, backlight);
1412 ret = pwc_get_backlight(pdev, ARGA(backlight));
1413 ARG_OUT(backlight)
1414 break; 1348 break;
1415 } 1349 }
1416 1350
1417 case VIDIOCPWCSFLICKER: 1351 case VIDIOCPWCSFLICKER:
1418 { 1352 {
1419 ARG_DEF(int, flicker) 1353 int *flicker = arg;
1420 1354 ret = pwc_set_flicker(pdev, *flicker);
1421 ARG_IN(flicker)
1422 ret = pwc_set_flicker(pdev, ARGR(flicker));
1423 break; 1355 break;
1424 } 1356 }
1425 1357
1426 case VIDIOCPWCGFLICKER: 1358 case VIDIOCPWCGFLICKER:
1427 { 1359 {
1428 ARG_DEF(int, flicker) 1360 int *flicker = arg;
1429 1361 ret = pwc_get_flicker(pdev, flicker);
1430 ret = pwc_get_flicker(pdev, ARGA(flicker));
1431 ARG_OUT(flicker)
1432 break; 1362 break;
1433 } 1363 }
1434 1364
1435 case VIDIOCPWCSDYNNOISE: 1365 case VIDIOCPWCSDYNNOISE:
1436 { 1366 {
1437 ARG_DEF(int, dynnoise) 1367 int *dynnoise = arg;
1438 1368 ret = pwc_set_dynamic_noise(pdev, *dynnoise);
1439 ARG_IN(dynnoise)
1440 ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
1441 break; 1369 break;
1442 } 1370 }
1443 1371
1444 case VIDIOCPWCGDYNNOISE: 1372 case VIDIOCPWCGDYNNOISE:
1445 { 1373 {
1446 ARG_DEF(int, dynnoise) 1374 int *dynnoise = arg;
1447 1375 ret = pwc_get_dynamic_noise(pdev, dynnoise);
1448 ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
1449 ARG_OUT(dynnoise);
1450 break; 1376 break;
1451 } 1377 }
1452 1378
1453 case VIDIOCPWCGREALSIZE: 1379 case VIDIOCPWCGREALSIZE:
1454 { 1380 {
1455 ARG_DEF(struct pwc_imagesize, size) 1381 struct pwc_imagesize *size = arg;
1456 1382 size->width = pdev->image.x;
1457 ARGR(size).width = pdev->image.x; 1383 size->height = pdev->image.y;
1458 ARGR(size).height = pdev->image.y;
1459 ARG_OUT(size)
1460 break; 1384 break;
1461 } 1385 }
1462 1386
@@ -1464,10 +1388,9 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1464 { 1388 {
1465 if (pdev->features & FEATURE_MOTOR_PANTILT) 1389 if (pdev->features & FEATURE_MOTOR_PANTILT)
1466 { 1390 {
1467 ARG_DEF(int, flags) 1391 int *flags = arg;
1468 1392
1469 ARG_IN(flags) 1393 ret = pwc_mpt_reset(pdev, *flags);
1470 ret = pwc_mpt_reset(pdev, ARGR(flags));
1471 if (ret >= 0) 1394 if (ret >= 0)
1472 { 1395 {
1473 pdev->pan_angle = 0; 1396 pdev->pan_angle = 0;
@@ -1485,10 +1408,8 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1485 { 1408 {
1486 if (pdev->features & FEATURE_MOTOR_PANTILT) 1409 if (pdev->features & FEATURE_MOTOR_PANTILT)
1487 { 1410 {
1488 ARG_DEF(struct pwc_mpt_range, range) 1411 struct pwc_mpt_range *range = arg;
1489 1412 *range = pdev->angle_range;
1490 ARGR(range) = pdev->angle_range;
1491 ARG_OUT(range)
1492 } 1413 }
1493 else 1414 else
1494 { 1415 {
@@ -1503,21 +1424,19 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1503 1424
1504 if (pdev->features & FEATURE_MOTOR_PANTILT) 1425 if (pdev->features & FEATURE_MOTOR_PANTILT)
1505 { 1426 {
1506 ARG_DEF(struct pwc_mpt_angles, angles) 1427 struct pwc_mpt_angles *angles = arg;
1507
1508 ARG_IN(angles)
1509 /* The camera can only set relative angles, so 1428 /* The camera can only set relative angles, so
1510 do some calculations when getting an absolute angle . 1429 do some calculations when getting an absolute angle .
1511 */ 1430 */
1512 if (ARGR(angles).absolute) 1431 if (angles->absolute)
1513 { 1432 {
1514 new_pan = ARGR(angles).pan; 1433 new_pan = angles->pan;
1515 new_tilt = ARGR(angles).tilt; 1434 new_tilt = angles->tilt;
1516 } 1435 }
1517 else 1436 else
1518 { 1437 {
1519 new_pan = pdev->pan_angle + ARGR(angles).pan; 1438 new_pan = pdev->pan_angle + angles->pan;
1520 new_tilt = pdev->tilt_angle + ARGR(angles).tilt; 1439 new_tilt = pdev->tilt_angle + angles->tilt;
1521 } 1440 }
1522 /* check absolute ranges */ 1441 /* check absolute ranges */
1523 if (new_pan < pdev->angle_range.pan_min || 1442 if (new_pan < pdev->angle_range.pan_min ||
@@ -1560,12 +1479,11 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1560 1479
1561 if (pdev->features & FEATURE_MOTOR_PANTILT) 1480 if (pdev->features & FEATURE_MOTOR_PANTILT)
1562 { 1481 {
1563 ARG_DEF(struct pwc_mpt_angles, angles) 1482 struct pwc_mpt_angles *angles = arg;
1564 1483
1565 ARGR(angles).absolute = 1; 1484 angles->absolute = 1;
1566 ARGR(angles).pan = pdev->pan_angle; 1485 angles->pan = pdev->pan_angle;
1567 ARGR(angles).tilt = pdev->tilt_angle; 1486 angles->tilt = pdev->tilt_angle;
1568 ARG_OUT(angles)
1569 } 1487 }
1570 else 1488 else
1571 { 1489 {
@@ -1578,10 +1496,8 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1578 { 1496 {
1579 if (pdev->features & FEATURE_MOTOR_PANTILT) 1497 if (pdev->features & FEATURE_MOTOR_PANTILT)
1580 { 1498 {
1581 ARG_DEF(struct pwc_mpt_status, status) 1499 struct pwc_mpt_status *status = arg;
1582 1500 ret = pwc_mpt_get_status(pdev, status);
1583 ret = pwc_mpt_get_status(pdev, ARGA(status));
1584 ARG_OUT(status)
1585 } 1501 }
1586 else 1502 else
1587 { 1503 {
@@ -1592,24 +1508,22 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1592 1508
1593 case VIDIOCPWCGVIDCMD: 1509 case VIDIOCPWCGVIDCMD:
1594 { 1510 {
1595 ARG_DEF(struct pwc_video_command, cmd); 1511 struct pwc_video_command *cmd = arg;
1596 1512
1597 ARGR(cmd).type = pdev->type; 1513 cmd->type = pdev->type;
1598 ARGR(cmd).release = pdev->release; 1514 cmd->release = pdev->release;
1599 ARGR(cmd).command_len = pdev->cmd_len; 1515 cmd->command_len = pdev->cmd_len;
1600 memcpy(&ARGR(cmd).command_buf, pdev->cmd_buf, pdev->cmd_len); 1516 memcpy(&cmd->command_buf, pdev->cmd_buf, pdev->cmd_len);
1601 ARGR(cmd).bandlength = pdev->vbandlength; 1517 cmd->bandlength = pdev->vbandlength;
1602 ARGR(cmd).frame_size = pdev->frame_size; 1518 cmd->frame_size = pdev->frame_size;
1603 ARG_OUT(cmd)
1604 break; 1519 break;
1605 } 1520 }
1606 /* 1521 /*
1607 case VIDIOCPWCGVIDTABLE: 1522 case VIDIOCPWCGVIDTABLE:
1608 { 1523 {
1609 ARG_DEF(struct pwc_table_init_buffer, table); 1524 struct pwc_table_init_buffer *table = arg;
1610 ARGR(table).len = pdev->cmd_len; 1525 table->len = pdev->cmd_len;
1611 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size); 1526 memcpy(&table->buffer, pdev->decompress_data, pdev->decompressor->table_size);
1612 ARG_OUT(table)
1613 break; 1527 break;
1614 } 1528 }
1615 */ 1529 */
diff --git a/drivers/usb/media/sn9c102.h b/drivers/usb/media/sn9c102.h
index e5cea0e2eb57..17d60c1eea7e 100644
--- a/drivers/usb/media/sn9c102.h
+++ b/drivers/usb/media/sn9c102.h
@@ -1,7 +1,7 @@
1/*************************************************************************** 1/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers * 2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * * 3 * *
4 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * * 5 * *
6 * This program is free software; you can redistribute it and/or modify * 6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by * 7 * it under the terms of the GNU General Public License as published by *
@@ -23,7 +23,8 @@
23 23
24#include <linux/version.h> 24#include <linux/version.h>
25#include <linux/usb.h> 25#include <linux/usb.h>
26#include <linux/videodev.h> 26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
27#include <linux/device.h> 28#include <linux/device.h>
28#include <linux/list.h> 29#include <linux/list.h>
29#include <linux/spinlock.h> 30#include <linux/spinlock.h>
@@ -52,13 +53,6 @@
52 53
53/*****************************************************************************/ 54/*****************************************************************************/
54 55
55#define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
56#define SN9C102_MODULE_AUTHOR "(C) 2004-2005 Luca Risolia"
57#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
58#define SN9C102_MODULE_LICENSE "GPL"
59#define SN9C102_MODULE_VERSION "1:1.24a"
60#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 24)
61
62enum sn9c102_bridge { 56enum sn9c102_bridge {
63 BRIDGE_SN9C101 = 0x01, 57 BRIDGE_SN9C101 = 0x01,
64 BRIDGE_SN9C102 = 0x02, 58 BRIDGE_SN9C102 = 0x02,
@@ -102,12 +96,13 @@ enum sn9c102_stream_state {
102 STREAM_ON, 96 STREAM_ON,
103}; 97};
104 98
99typedef char sn9c103_sof_header_t[18];
105typedef char sn9c102_sof_header_t[12]; 100typedef char sn9c102_sof_header_t[12];
106typedef char sn9c102_eof_header_t[4]; 101typedef char sn9c102_eof_header_t[4];
107 102
108struct sn9c102_sysfs_attr { 103struct sn9c102_sysfs_attr {
109 u8 reg, i2c_reg; 104 u8 reg, i2c_reg;
110 sn9c102_sof_header_t frame_header; 105 sn9c103_sof_header_t frame_header;
111}; 106};
112 107
113struct sn9c102_module_param { 108struct sn9c102_module_param {
@@ -118,8 +113,6 @@ static DECLARE_MUTEX(sn9c102_sysfs_lock);
118static DECLARE_RWSEM(sn9c102_disconnect); 113static DECLARE_RWSEM(sn9c102_disconnect);
119 114
120struct sn9c102_device { 115struct sn9c102_device {
121 struct device dev;
122
123 struct video_device* v4ldev; 116 struct video_device* v4ldev;
124 117
125 enum sn9c102_bridge bridge; 118 enum sn9c102_bridge bridge;
@@ -140,8 +133,8 @@ struct sn9c102_device {
140 struct v4l2_jpegcompression compression; 133 struct v4l2_jpegcompression compression;
141 134
142 struct sn9c102_sysfs_attr sysfs; 135 struct sn9c102_sysfs_attr sysfs;
143 sn9c102_sof_header_t sof_header; 136 sn9c103_sof_header_t sof_header;
144 u16 reg[32]; 137 u16 reg[63];
145 138
146 struct sn9c102_module_param module_param; 139 struct sn9c102_module_param module_param;
147 140
@@ -160,7 +153,6 @@ sn9c102_attach_sensor(struct sn9c102_device* cam,
160 struct sn9c102_sensor* sensor) 153 struct sn9c102_sensor* sensor)
161{ 154{
162 cam->sensor = sensor; 155 cam->sensor = sensor;
163 cam->sensor->dev = &cam->dev;
164 cam->sensor->usbdev = cam->usbdev; 156 cam->sensor->usbdev = cam->usbdev;
165} 157}
166 158
@@ -170,19 +162,24 @@ sn9c102_attach_sensor(struct sn9c102_device* cam,
170#undef KDBG 162#undef KDBG
171#ifdef SN9C102_DEBUG 163#ifdef SN9C102_DEBUG
172# define DBG(level, fmt, args...) \ 164# define DBG(level, fmt, args...) \
173{ \ 165do { \
174 if (debug >= (level)) { \ 166 if (debug >= (level)) { \
175 if ((level) == 1) \ 167 if ((level) == 1) \
176 dev_err(&cam->dev, fmt "\n", ## args); \ 168 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
177 else if ((level) == 2) \ 169 else if ((level) == 2) \
178 dev_info(&cam->dev, fmt "\n", ## args); \ 170 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
179 else if ((level) >= 3) \ 171 else if ((level) >= 3) \
180 dev_info(&cam->dev, "[%s:%d] " fmt "\n", \ 172 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
181 __FUNCTION__, __LINE__ , ## args); \ 173 __FUNCTION__, __LINE__ , ## args); \
182 } \ 174 } \
183} 175} while (0)
176# define V4LDBG(level, name, cmd) \
177do { \
178 if (debug >= (level)) \
179 v4l_print_ioctl(name, cmd); \
180} while (0)
184# define KDBG(level, fmt, args...) \ 181# define KDBG(level, fmt, args...) \
185{ \ 182do { \
186 if (debug >= (level)) { \ 183 if (debug >= (level)) { \
187 if ((level) == 1 || (level) == 2) \ 184 if ((level) == 1 || (level) == 2) \
188 pr_info("sn9c102: " fmt "\n", ## args); \ 185 pr_info("sn9c102: " fmt "\n", ## args); \
@@ -190,17 +187,18 @@ sn9c102_attach_sensor(struct sn9c102_device* cam,
190 pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \ 187 pr_debug("sn9c102: [%s:%d] " fmt "\n", __FUNCTION__, \
191 __LINE__ , ## args); \ 188 __LINE__ , ## args); \
192 } \ 189 } \
193} 190} while (0)
194#else 191#else
195# define KDBG(level, fmt, args...) do {;} while(0); 192# define DBG(level, fmt, args...) do {;} while(0)
196# define DBG(level, fmt, args...) do {;} while(0); 193# define V4LDBG(level, name, cmd) do {;} while(0)
194# define KDBG(level, fmt, args...) do {;} while(0)
197#endif 195#endif
198 196
199#undef PDBG 197#undef PDBG
200#define PDBG(fmt, args...) \ 198#define PDBG(fmt, args...) \
201dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args); 199dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args)
202 200
203#undef PDBGG 201#undef PDBGG
204#define PDBGG(fmt, args...) do {;} while(0); /* placeholder */ 202#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
205 203
206#endif /* _SN9C102_H_ */ 204#endif /* _SN9C102_H_ */
diff --git a/drivers/usb/media/sn9c102_core.c b/drivers/usb/media/sn9c102_core.c
index 8d1a1c357d5a..c81397e4714b 100644
--- a/drivers/usb/media/sn9c102_core.c
+++ b/drivers/usb/media/sn9c102_core.c
@@ -1,7 +1,7 @@
1/*************************************************************************** 1/***************************************************************************
2 * V4L2 driver for SN9C10x PC Camera Controllers * 2 * V4L2 driver for SN9C10x PC Camera Controllers *
3 * * 3 * *
4 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * * 5 * *
6 * This program is free software; you can redistribute it and/or modify * 6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by * 7 * it under the terms of the GNU General Public License as published by *
@@ -45,6 +45,15 @@
45 45
46/*****************************************************************************/ 46/*****************************************************************************/
47 47
48#define SN9C102_MODULE_NAME "V4L2 driver for SN9C10x PC Camera Controllers"
49#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
50#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
51#define SN9C102_MODULE_LICENSE "GPL"
52#define SN9C102_MODULE_VERSION "1:1.26"
53#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 26)
54
55/*****************************************************************************/
56
48MODULE_DEVICE_TABLE(usb, sn9c102_id_table); 57MODULE_DEVICE_TABLE(usb, sn9c102_id_table);
49 58
50MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL); 59MODULE_AUTHOR(SN9C102_MODULE_AUTHOR " " SN9C102_AUTHOR_EMAIL);
@@ -70,10 +79,10 @@ static short force_munmap[] = {[0 ... SN9C102_MAX_DEVICES-1] =
70 SN9C102_FORCE_MUNMAP}; 79 SN9C102_FORCE_MUNMAP};
71module_param_array(force_munmap, bool, NULL, 0444); 80module_param_array(force_munmap, bool, NULL, 0444);
72MODULE_PARM_DESC(force_munmap, 81MODULE_PARM_DESC(force_munmap,
73 "\n<0|1[,...]> Force the application to unmap previously " 82 "\n<0|1[,...]> Force the application to unmap previously"
74 "\nmapped buffer memory before calling any VIDIOC_S_CROP or " 83 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
75 "\nVIDIOC_S_FMT ioctl's. Not all the applications support " 84 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
76 "\nthis feature. This parameter is specific for each " 85 "\nthis feature. This parameter is specific for each"
77 "\ndetected camera." 86 "\ndetected camera."
78 "\n 0 = do not force memory unmapping" 87 "\n 0 = do not force memory unmapping"
79 "\n 1 = force memory unmapping (save memory)" 88 "\n 1 = force memory unmapping (save memory)"
@@ -102,6 +111,9 @@ static sn9c102_sof_header_t sn9c102_sof_header[] = {
102 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01}, 111 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x01},
103}; 112};
104 113
114static sn9c103_sof_header_t sn9c103_sof_header[] = {
115 {0xff, 0xff, 0x00, 0xc4, 0xc4, 0x96, 0x20},
116};
105 117
106static sn9c102_eof_header_t sn9c102_eof_header[] = { 118static sn9c102_eof_header_t sn9c102_eof_header[] = {
107 {0x00, 0x00, 0x00, 0x00}, 119 {0x00, 0x00, 0x00, 0x00},
@@ -112,50 +124,6 @@ static sn9c102_eof_header_t sn9c102_eof_header[] = {
112 124
113/*****************************************************************************/ 125/*****************************************************************************/
114 126
115static void* rvmalloc(size_t size)
116{
117 void* mem;
118 unsigned long adr;
119
120 size = PAGE_ALIGN(size);
121
122 mem = vmalloc_32((unsigned long)size);
123 if (!mem)
124 return NULL;
125
126 memset(mem, 0, size);
127
128 adr = (unsigned long)mem;
129 while (size > 0) {
130 SetPageReserved(vmalloc_to_page((void *)adr));
131 adr += PAGE_SIZE;
132 size -= PAGE_SIZE;
133 }
134
135 return mem;
136}
137
138
139static void rvfree(void* mem, size_t size)
140{
141 unsigned long adr;
142
143 if (!mem)
144 return;
145
146 size = PAGE_ALIGN(size);
147
148 adr = (unsigned long)mem;
149 while (size > 0) {
150 ClearPageReserved(vmalloc_to_page((void *)adr));
151 adr += PAGE_SIZE;
152 size -= PAGE_SIZE;
153 }
154
155 vfree(mem);
156}
157
158
159static u32 127static u32
160sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, 128sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
161 enum sn9c102_io_method io) 129 enum sn9c102_io_method io)
@@ -174,7 +142,7 @@ sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
174 142
175 cam->nbuffers = count; 143 cam->nbuffers = count;
176 while (cam->nbuffers > 0) { 144 while (cam->nbuffers > 0) {
177 if ((buff = rvmalloc(cam->nbuffers * PAGE_ALIGN(imagesize)))) 145 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
178 break; 146 break;
179 cam->nbuffers--; 147 cam->nbuffers--;
180 } 148 }
@@ -198,10 +166,10 @@ sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
198static void sn9c102_release_buffers(struct sn9c102_device* cam) 166static void sn9c102_release_buffers(struct sn9c102_device* cam)
199{ 167{
200 if (cam->nbuffers) { 168 if (cam->nbuffers) {
201 rvfree(cam->frame[0].bufmem, 169 vfree(cam->frame[0].bufmem);
202 cam->nbuffers * PAGE_ALIGN(cam->frame[0].buf.length));
203 cam->nbuffers = 0; 170 cam->nbuffers = 0;
204 } 171 }
172 cam->frame_current = NULL;
205} 173}
206 174
207 175
@@ -219,6 +187,19 @@ static void sn9c102_empty_framequeues(struct sn9c102_device* cam)
219} 187}
220 188
221 189
190static void sn9c102_requeue_outqueue(struct sn9c102_device* cam)
191{
192 struct sn9c102_frame_t *i;
193
194 list_for_each_entry(i, &cam->outqueue, frame) {
195 i->state = F_QUEUED;
196 list_add(&i->frame, &cam->inqueue);
197 }
198
199 INIT_LIST_HEAD(&cam->outqueue);
200}
201
202
222static void sn9c102_queue_unusedframes(struct sn9c102_device* cam) 203static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
223{ 204{
224 unsigned long lock_flags; 205 unsigned long lock_flags;
@@ -235,19 +216,46 @@ static void sn9c102_queue_unusedframes(struct sn9c102_device* cam)
235 216
236/*****************************************************************************/ 217/*****************************************************************************/
237 218
219int sn9c102_write_regs(struct sn9c102_device* cam, u8* buff, u16 index)
220{
221 struct usb_device* udev = cam->usbdev;
222 int i, res;
223
224 if (index + sizeof(buff) >= ARRAY_SIZE(cam->reg))
225 return -1;
226
227 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
228 index, 0, buff, sizeof(buff),
229 SN9C102_CTRL_TIMEOUT*sizeof(buff));
230 if (res < 0) {
231 DBG(3, "Failed to write registers (index 0x%02X, error %d)",
232 index, res);
233 return -1;
234 }
235
236 for (i = 0; i < sizeof(buff); i++)
237 cam->reg[index+i] = buff[i];
238
239 return 0;
240}
241
242
238int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index) 243int sn9c102_write_reg(struct sn9c102_device* cam, u8 value, u16 index)
239{ 244{
240 struct usb_device* udev = cam->usbdev; 245 struct usb_device* udev = cam->usbdev;
241 u8* buff = cam->control_buffer; 246 u8* buff = cam->control_buffer;
242 int res; 247 int res;
243 248
249 if (index >= ARRAY_SIZE(cam->reg))
250 return -1;
251
244 *buff = value; 252 *buff = value;
245 253
246 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41, 254 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x08, 0x41,
247 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); 255 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
248 if (res < 0) { 256 if (res < 0) {
249 DBG(3, "Failed to write a register (value 0x%02X, index " 257 DBG(3, "Failed to write a register (value 0x%02X, index "
250 "0x%02X, error %d)", value, index, res) 258 "0x%02X, error %d)", value, index, res);
251 return -1; 259 return -1;
252 } 260 }
253 261
@@ -268,7 +276,7 @@ static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
268 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT); 276 index, 0, buff, 1, SN9C102_CTRL_TIMEOUT);
269 if (res < 0) 277 if (res < 0)
270 DBG(3, "Failed to read a register (index 0x%02X, error %d)", 278 DBG(3, "Failed to read a register (index 0x%02X, error %d)",
271 index, res) 279 index, res);
272 280
273 return (res >= 0) ? (int)(*buff) : -1; 281 return (res >= 0) ? (int)(*buff) : -1;
274} 282}
@@ -276,8 +284,8 @@ static int sn9c102_read_reg(struct sn9c102_device* cam, u16 index)
276 284
277int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index) 285int sn9c102_pread_reg(struct sn9c102_device* cam, u16 index)
278{ 286{
279 if (index > 0x1f) 287 if (index >= ARRAY_SIZE(cam->reg))
280 return -EINVAL; 288 return -1;
281 289
282 return cam->reg[index]; 290 return cam->reg[index];
283} 291}
@@ -367,10 +375,10 @@ sn9c102_i2c_try_raw_read(struct sn9c102_device* cam,
367 err += sn9c102_i2c_detect_read_error(cam, sensor); 375 err += sn9c102_i2c_detect_read_error(cam, sensor);
368 376
369 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1, 377 PDBGG("I2C read: address 0x%02X, first read byte: 0x%02X", data1,
370 data[4]) 378 data[4]);
371 379
372 if (err) { 380 if (err) {
373 DBG(3, "I2C read failed for %s image sensor", sensor->name) 381 DBG(3, "I2C read failed for %s image sensor", sensor->name);
374 return -1; 382 return -1;
375 } 383 }
376 384
@@ -410,11 +418,11 @@ sn9c102_i2c_try_raw_write(struct sn9c102_device* cam,
410 err += sn9c102_i2c_detect_write_error(cam, sensor); 418 err += sn9c102_i2c_detect_write_error(cam, sensor);
411 419
412 if (err) 420 if (err)
413 DBG(3, "I2C write failed for %s image sensor", sensor->name) 421 DBG(3, "I2C write failed for %s image sensor", sensor->name);
414 422
415 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, " 423 PDBGG("I2C raw write: %u bytes, data0 = 0x%02X, data1 = 0x%02X, "
416 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X", 424 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X",
417 n, data0, data1, data2, data3, data4, data5) 425 n, data0, data1, data2, data3, data4, data5);
418 426
419 return err ? -1 : 0; 427 return err ? -1 : 0;
420} 428}
@@ -461,13 +469,27 @@ int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
461static void* 469static void*
462sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len) 470sn9c102_find_sof_header(struct sn9c102_device* cam, void* mem, size_t len)
463{ 471{
464 size_t soflen = sizeof(sn9c102_sof_header_t), i; 472 size_t soflen = 0, i;
465 u8 j, n = sizeof(sn9c102_sof_header) / soflen; 473 u8 j, n = 0;
466 474
467 for (i = 0; (len >= soflen) && (i <= len - soflen); i++) 475 switch (cam->bridge) {
476 case BRIDGE_SN9C101:
477 case BRIDGE_SN9C102:
478 soflen = sizeof(sn9c102_sof_header_t);
479 n = sizeof(sn9c102_sof_header) / soflen;
480 break;
481 case BRIDGE_SN9C103:
482 soflen = sizeof(sn9c103_sof_header_t);
483 n = sizeof(sn9c103_sof_header) / soflen;
484 }
485
486 for (i = 0; (len >= soflen) && (i <= len - soflen); i++)
468 for (j = 0; j < n; j++) 487 for (j = 0; j < n; j++)
469 /* It's enough to compare 7 bytes */ 488 /* The invariable part of the header is 6 bytes long */
470 if (!memcmp(mem + i, sn9c102_sof_header[j], 7)) { 489 if ((cam->bridge != BRIDGE_SN9C103 &&
490 !memcmp(mem + i, sn9c102_sof_header[j], 6)) ||
491 (cam->bridge == BRIDGE_SN9C103 &&
492 !memcmp(mem + i, sn9c103_sof_header[j], 6))) {
471 memcpy(cam->sof_header, mem + i, soflen); 493 memcpy(cam->sof_header, mem + i, soflen);
472 /* Skip the header */ 494 /* Skip the header */
473 return mem + i + soflen; 495 return mem + i + soflen;
@@ -499,8 +521,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
499{ 521{
500 struct sn9c102_device* cam = urb->context; 522 struct sn9c102_device* cam = urb->context;
501 struct sn9c102_frame_t** f; 523 struct sn9c102_frame_t** f;
502 size_t imagesize; 524 size_t imagesize, soflen;
503 unsigned long lock_flags;
504 u8 i; 525 u8 i;
505 int err = 0; 526 int err = 0;
506 527
@@ -513,7 +534,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
513 cam->stream = STREAM_OFF; 534 cam->stream = STREAM_OFF;
514 if ((*f)) 535 if ((*f))
515 (*f)->state = F_QUEUED; 536 (*f)->state = F_QUEUED;
516 DBG(3, "Stream interrupted") 537 DBG(3, "Stream interrupted");
517 wake_up_interruptible(&cam->wait_stream); 538 wake_up_interruptible(&cam->wait_stream);
518 } 539 }
519 540
@@ -536,6 +557,10 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
536 cam->sensor->pix_format.height * 557 cam->sensor->pix_format.height *
537 cam->sensor->pix_format.priv) / 8; 558 cam->sensor->pix_format.priv) / 8;
538 559
560 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
561 sizeof(sn9c103_sof_header_t) :
562 sizeof(sn9c102_sof_header_t);
563
539 for (i = 0; i < urb->number_of_packets; i++) { 564 for (i = 0; i < urb->number_of_packets; i++) {
540 unsigned int img, len, status; 565 unsigned int img, len, status;
541 void *pos, *sof, *eof; 566 void *pos, *sof, *eof;
@@ -545,19 +570,12 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
545 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer; 570 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
546 571
547 if (status) { 572 if (status) {
548 DBG(3, "Error in isochronous frame") 573 DBG(3, "Error in isochronous frame");
549 (*f)->state = F_ERROR; 574 (*f)->state = F_ERROR;
550 continue; 575 continue;
551 } 576 }
552 577
553 PDBGG("Isochrnous frame: length %u, #%u i", len, i) 578 PDBGG("Isochrnous frame: length %u, #%u i", len, i);
554
555 /*
556 NOTE: It is probably correct to assume that SOF and EOF
557 headers do not occur between two consecutive packets,
558 but who knows..Whatever is the truth, this assumption
559 doesn't introduce bugs.
560 */
561 579
562redo: 580redo:
563 sof = sn9c102_find_sof_header(cam, pos, len); 581 sof = sn9c102_find_sof_header(cam, pos, len);
@@ -575,10 +593,10 @@ end_of_frame:
575 imagesize; 593 imagesize;
576 img = imagesize - (*f)->buf.bytesused; 594 img = imagesize - (*f)->buf.bytesused;
577 DBG(3, "Expected EOF not found: " 595 DBG(3, "Expected EOF not found: "
578 "video frame cut") 596 "video frame cut");
579 if (eof) 597 if (eof)
580 DBG(3, "Exceeded limit: +%u " 598 DBG(3, "Exceeded limit: +%u "
581 "bytes", (unsigned)(b)) 599 "bytes", (unsigned)(b));
582 } 600 }
583 601
584 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos, 602 memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,
@@ -595,8 +613,7 @@ end_of_frame:
595 u32 b = (*f)->buf.bytesused; 613 u32 b = (*f)->buf.bytesused;
596 (*f)->state = F_DONE; 614 (*f)->state = F_DONE;
597 (*f)->buf.sequence= ++cam->frame_count; 615 (*f)->buf.sequence= ++cam->frame_count;
598 spin_lock_irqsave(&cam->queue_lock, 616 spin_lock(&cam->queue_lock);
599 lock_flags);
600 list_move_tail(&(*f)->frame, 617 list_move_tail(&(*f)->frame,
601 &cam->outqueue); 618 &cam->outqueue);
602 if (!list_empty(&cam->inqueue)) 619 if (!list_empty(&cam->inqueue))
@@ -606,13 +623,11 @@ end_of_frame:
606 frame ); 623 frame );
607 else 624 else
608 (*f) = NULL; 625 (*f) = NULL;
609 spin_unlock_irqrestore(&cam->queue_lock 626 spin_unlock(&cam->queue_lock);
610 , lock_flags);
611 memcpy(cam->sysfs.frame_header, 627 memcpy(cam->sysfs.frame_header,
612 cam->sof_header, 628 cam->sof_header, soflen);
613 sizeof(sn9c102_sof_header_t)); 629 DBG(3, "Video frame captured: %lu "
614 DBG(3, "Video frame captured: " 630 "bytes", (unsigned long)(b));
615 "%lu bytes", (unsigned long)(b))
616 631
617 if (!(*f)) 632 if (!(*f))
618 goto resubmit_urb; 633 goto resubmit_urb;
@@ -621,18 +636,19 @@ end_of_frame:
621 (*f)->state = F_ERROR; 636 (*f)->state = F_ERROR;
622 DBG(3, "Not expected EOF after %lu " 637 DBG(3, "Not expected EOF after %lu "
623 "bytes of image data", 638 "bytes of image data",
624 (unsigned long)((*f)->buf.bytesused)) 639 (unsigned long)
640 ((*f)->buf.bytesused));
625 } 641 }
626 642
627 if (sof) /* (1) */ 643 if (sof) /* (1) */
628 goto start_of_frame; 644 goto start_of_frame;
629 645
630 } else if (eof) { 646 } else if (eof) {
631 DBG(3, "EOF without SOF") 647 DBG(3, "EOF without SOF");
632 continue; 648 continue;
633 649
634 } else { 650 } else {
635 PDBGG("Ignoring pointless isochronous frame") 651 PDBGG("Ignoring pointless isochronous frame");
636 continue; 652 continue;
637 } 653 }
638 654
@@ -642,7 +658,7 @@ start_of_frame:
642 (*f)->buf.bytesused = 0; 658 (*f)->buf.bytesused = 0;
643 len -= (sof - pos); 659 len -= (sof - pos);
644 pos = sof; 660 pos = sof;
645 DBG(3, "SOF detected: new video frame") 661 DBG(3, "SOF detected: new video frame");
646 if (len) 662 if (len)
647 goto redo; 663 goto redo;
648 664
@@ -653,12 +669,13 @@ start_of_frame:
653 else { 669 else {
654 if (cam->sensor->pix_format.pixelformat == 670 if (cam->sensor->pix_format.pixelformat ==
655 V4L2_PIX_FMT_SN9C10X) { 671 V4L2_PIX_FMT_SN9C10X) {
656 eof = sof-sizeof(sn9c102_sof_header_t); 672 eof = sof - soflen;
657 goto end_of_frame; 673 goto end_of_frame;
658 } else { 674 } else {
659 DBG(3, "SOF before expected EOF after " 675 DBG(3, "SOF before expected EOF after "
660 "%lu bytes of image data", 676 "%lu bytes of image data",
661 (unsigned long)((*f)->buf.bytesused)) 677 (unsigned long)
678 ((*f)->buf.bytesused));
662 goto start_of_frame; 679 goto start_of_frame;
663 } 680 }
664 } 681 }
@@ -670,7 +687,7 @@ resubmit_urb:
670 err = usb_submit_urb(urb, GFP_ATOMIC); 687 err = usb_submit_urb(urb, GFP_ATOMIC);
671 if (err < 0 && err != -EPERM) { 688 if (err < 0 && err != -EPERM) {
672 cam->state |= DEV_MISCONFIGURED; 689 cam->state |= DEV_MISCONFIGURED;
673 DBG(1, "usb_submit_urb() failed") 690 DBG(1, "usb_submit_urb() failed");
674 } 691 }
675 692
676 wake_up_interruptible(&cam->wait_frame); 693 wake_up_interruptible(&cam->wait_frame);
@@ -681,18 +698,22 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
681{ 698{
682 struct usb_device *udev = cam->usbdev; 699 struct usb_device *udev = cam->usbdev;
683 struct urb* urb; 700 struct urb* urb;
684 const unsigned int wMaxPacketSize[] = {0, 128, 256, 384, 512, 701 const unsigned int sn9c102_wMaxPacketSize[] = {0, 128, 256, 384, 512,
685 680, 800, 900, 1023}; 702 680, 800, 900, 1023};
686 const unsigned int psz = wMaxPacketSize[SN9C102_ALTERNATE_SETTING]; 703 const unsigned int sn9c103_wMaxPacketSize[] = {0, 128, 256, 384, 512,
704 680, 800, 900, 1003};
705 const unsigned int psz = (cam->bridge == BRIDGE_SN9C103) ?
706 sn9c103_wMaxPacketSize[SN9C102_ALTERNATE_SETTING] :
707 sn9c102_wMaxPacketSize[SN9C102_ALTERNATE_SETTING];
687 s8 i, j; 708 s8 i, j;
688 int err = 0; 709 int err = 0;
689 710
690 for (i = 0; i < SN9C102_URBS; i++) { 711 for (i = 0; i < SN9C102_URBS; i++) {
691 cam->transfer_buffer[i] = kmalloc(SN9C102_ISO_PACKETS * psz, 712 cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,
692 GFP_KERNEL); 713 GFP_KERNEL);
693 if (!cam->transfer_buffer[i]) { 714 if (!cam->transfer_buffer[i]) {
694 err = -ENOMEM; 715 err = -ENOMEM;
695 DBG(1, "Not enough memory") 716 DBG(1, "Not enough memory");
696 goto free_buffers; 717 goto free_buffers;
697 } 718 }
698 } 719 }
@@ -702,7 +723,7 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
702 cam->urb[i] = urb; 723 cam->urb[i] = urb;
703 if (!urb) { 724 if (!urb) {
704 err = -ENOMEM; 725 err = -ENOMEM;
705 DBG(1, "usb_alloc_urb() failed") 726 DBG(1, "usb_alloc_urb() failed");
706 goto free_urbs; 727 goto free_urbs;
707 } 728 }
708 urb->dev = udev; 729 urb->dev = udev;
@@ -725,14 +746,14 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
725 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01); 746 err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);
726 if (err) { 747 if (err) {
727 err = -EIO; 748 err = -EIO;
728 DBG(1, "I/O hardware error") 749 DBG(1, "I/O hardware error");
729 goto free_urbs; 750 goto free_urbs;
730 } 751 }
731 } 752 }
732 753
733 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING); 754 err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);
734 if (err) { 755 if (err) {
735 DBG(1, "usb_set_interface() failed") 756 DBG(1, "usb_set_interface() failed");
736 goto free_urbs; 757 goto free_urbs;
737 } 758 }
738 759
@@ -743,7 +764,7 @@ static int sn9c102_start_transfer(struct sn9c102_device* cam)
743 if (err) { 764 if (err) {
744 for (j = i-1; j >= 0; j--) 765 for (j = i-1; j >= 0; j--)
745 usb_kill_urb(cam->urb[j]); 766 usb_kill_urb(cam->urb[j]);
746 DBG(1, "usb_submit_urb() failed, error %d", err) 767 DBG(1, "usb_submit_urb() failed, error %d", err);
747 goto free_urbs; 768 goto free_urbs;
748 } 769 }
749 } 770 }
@@ -779,7 +800,7 @@ static int sn9c102_stop_transfer(struct sn9c102_device* cam)
779 800
780 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */ 801 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
781 if (err) 802 if (err)
782 DBG(3, "usb_set_interface() failed") 803 DBG(3, "usb_set_interface() failed");
783 804
784 return err; 805 return err;
785} 806}
@@ -799,7 +820,7 @@ static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
799 else if (err) { 820 else if (err) {
800 cam->state |= DEV_MISCONFIGURED; 821 cam->state |= DEV_MISCONFIGURED;
801 DBG(1, "The camera is misconfigured. To use it, close and " 822 DBG(1, "The camera is misconfigured. To use it, close and "
802 "open /dev/video%d again.", cam->v4ldev->minor) 823 "open /dev/video%d again.", cam->v4ldev->minor);
803 return err; 824 return err;
804 } 825 }
805 826
@@ -808,6 +829,7 @@ static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
808 829
809/*****************************************************************************/ 830/*****************************************************************************/
810 831
832#ifdef CONFIG_VIDEO_ADV_DEBUG
811static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count) 833static u8 sn9c102_strtou8(const char* buff, size_t len, ssize_t* count)
812{ 834{
813 char str[5]; 835 char str[5];
@@ -885,8 +907,8 @@ sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
885 907
886 cam->sysfs.reg = index; 908 cam->sysfs.reg = index;
887 909
888 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg) 910 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
889 DBG(3, "Written bytes: %zd", count) 911 DBG(3, "Written bytes: %zd", count);
890 912
891 up(&sn9c102_sysfs_lock); 913 up(&sn9c102_sysfs_lock);
892 914
@@ -916,7 +938,7 @@ static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
916 938
917 count = sprintf(buf, "%d\n", val); 939 count = sprintf(buf, "%d\n", val);
918 940
919 DBG(3, "Read bytes: %zd", count) 941 DBG(3, "Read bytes: %zd", count);
920 942
921 up(&sn9c102_sysfs_lock); 943 up(&sn9c102_sysfs_lock);
922 944
@@ -954,8 +976,8 @@ sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
954 } 976 }
955 977
956 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X", 978 DBG(2, "Written SN9C10X reg. 0x%02X, val. 0x%02X",
957 cam->sysfs.reg, value) 979 cam->sysfs.reg, value);
958 DBG(3, "Written bytes: %zd", count) 980 DBG(3, "Written bytes: %zd", count);
959 981
960 up(&sn9c102_sysfs_lock); 982 up(&sn9c102_sysfs_lock);
961 983
@@ -979,7 +1001,7 @@ static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
979 1001
980 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg); 1002 count = sprintf(buf, "%u\n", cam->sysfs.i2c_reg);
981 1003
982 DBG(3, "Read bytes: %zd", count) 1004 DBG(3, "Read bytes: %zd", count);
983 1005
984 up(&sn9c102_sysfs_lock); 1006 up(&sn9c102_sysfs_lock);
985 1007
@@ -1011,8 +1033,8 @@ sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1011 1033
1012 cam->sysfs.i2c_reg = index; 1034 cam->sysfs.i2c_reg = index;
1013 1035
1014 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg) 1036 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1015 DBG(3, "Written bytes: %zd", count) 1037 DBG(3, "Written bytes: %zd", count);
1016 1038
1017 up(&sn9c102_sysfs_lock); 1039 up(&sn9c102_sysfs_lock);
1018 1040
@@ -1047,7 +1069,7 @@ static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1047 1069
1048 count = sprintf(buf, "%d\n", val); 1070 count = sprintf(buf, "%d\n", val);
1049 1071
1050 DBG(3, "Read bytes: %zd", count) 1072 DBG(3, "Read bytes: %zd", count);
1051 1073
1052 up(&sn9c102_sysfs_lock); 1074 up(&sn9c102_sysfs_lock);
1053 1075
@@ -1090,8 +1112,8 @@ sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1090 } 1112 }
1091 1113
1092 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X", 1114 DBG(2, "Written sensor reg. 0x%02X, val. 0x%02X",
1093 cam->sysfs.i2c_reg, value) 1115 cam->sysfs.i2c_reg, value);
1094 DBG(3, "Written bytes: %zd", count) 1116 DBG(3, "Written bytes: %zd", count);
1095 1117
1096 up(&sn9c102_sysfs_lock); 1118 up(&sn9c102_sysfs_lock);
1097 1119
@@ -1193,7 +1215,7 @@ static ssize_t sn9c102_show_frame_header(struct class_device* cd, char* buf)
1193 count = sizeof(cam->sysfs.frame_header); 1215 count = sizeof(cam->sysfs.frame_header);
1194 memcpy(buf, cam->sysfs.frame_header, count); 1216 memcpy(buf, cam->sysfs.frame_header, count);
1195 1217
1196 DBG(3, "Frame header, read bytes: %zd", count) 1218 DBG(3, "Frame header, read bytes: %zd", count);
1197 1219
1198 return count; 1220 return count;
1199} 1221}
@@ -1227,11 +1249,12 @@ static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1227 video_device_create_file(v4ldev, &class_device_attr_blue); 1249 video_device_create_file(v4ldev, &class_device_attr_blue);
1228 video_device_create_file(v4ldev, &class_device_attr_red); 1250 video_device_create_file(v4ldev, &class_device_attr_red);
1229 } 1251 }
1230 if (cam->sensor->sysfs_ops) { 1252 if (cam->sensor && cam->sensor->sysfs_ops) {
1231 video_device_create_file(v4ldev, &class_device_attr_i2c_reg); 1253 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1232 video_device_create_file(v4ldev, &class_device_attr_i2c_val); 1254 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1233 } 1255 }
1234} 1256}
1257#endif /* CONFIG_VIDEO_ADV_DEBUG */
1235 1258
1236/*****************************************************************************/ 1259/*****************************************************************************/
1237 1260
@@ -1281,7 +1304,7 @@ static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1281 if (err) 1304 if (err)
1282 return -EIO; 1305 return -EIO;
1283 1306
1284 PDBGG("Scaling factor: %u", scale) 1307 PDBGG("Scaling factor: %u", scale);
1285 1308
1286 return 0; 1309 return 0;
1287} 1310}
@@ -1304,7 +1327,7 @@ static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1304 return -EIO; 1327 return -EIO;
1305 1328
1306 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size " 1329 PDBGG("h_start, v_start, h_size, v_size, ho_size, vo_size "
1307 "%u %u %u %u", h_start, v_start, h_size, v_size) 1330 "%u %u %u %u", h_start, v_start, h_size, v_size);
1308 1331
1309 return 0; 1332 return 0;
1310} 1333}
@@ -1336,7 +1359,7 @@ static int sn9c102_init(struct sn9c102_device* cam)
1336 if (s->init) { 1359 if (s->init) {
1337 err = s->init(cam); 1360 err = s->init(cam);
1338 if (err) { 1361 if (err) {
1339 DBG(3, "Sensor initialization failed") 1362 DBG(3, "Sensor initialization failed");
1340 return err; 1363 return err;
1341 } 1364 }
1342 } 1365 }
@@ -1353,13 +1376,13 @@ static int sn9c102_init(struct sn9c102_device* cam)
1353 1376
1354 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) 1377 if (s->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
1355 DBG(3, "Compressed video format is active, quality %d", 1378 DBG(3, "Compressed video format is active, quality %d",
1356 cam->compression.quality) 1379 cam->compression.quality);
1357 else 1380 else
1358 DBG(3, "Uncompressed video format is active") 1381 DBG(3, "Uncompressed video format is active");
1359 1382
1360 if (s->set_crop) 1383 if (s->set_crop)
1361 if ((err = s->set_crop(cam, rect))) { 1384 if ((err = s->set_crop(cam, rect))) {
1362 DBG(3, "set_crop() failed") 1385 DBG(3, "set_crop() failed");
1363 return err; 1386 return err;
1364 } 1387 }
1365 1388
@@ -1372,11 +1395,11 @@ static int sn9c102_init(struct sn9c102_device* cam)
1372 err = s->set_ctrl(cam, &ctrl); 1395 err = s->set_ctrl(cam, &ctrl);
1373 if (err) { 1396 if (err) {
1374 DBG(3, "Set %s control failed", 1397 DBG(3, "Set %s control failed",
1375 s->qctrl[i].name) 1398 s->qctrl[i].name);
1376 return err; 1399 return err;
1377 } 1400 }
1378 DBG(3, "Image sensor supports '%s' control", 1401 DBG(3, "Image sensor supports '%s' control",
1379 s->qctrl[i].name) 1402 s->qctrl[i].name);
1380 } 1403 }
1381 } 1404 }
1382 1405
@@ -1392,7 +1415,7 @@ static int sn9c102_init(struct sn9c102_device* cam)
1392 cam->state |= DEV_INITIALIZED; 1415 cam->state |= DEV_INITIALIZED;
1393 } 1416 }
1394 1417
1395 DBG(2, "Initialization succeeded") 1418 DBG(2, "Initialization succeeded");
1396 return 0; 1419 return 0;
1397} 1420}
1398 1421
@@ -1401,7 +1424,7 @@ static void sn9c102_release_resources(struct sn9c102_device* cam)
1401{ 1424{
1402 down(&sn9c102_sysfs_lock); 1425 down(&sn9c102_sysfs_lock);
1403 1426
1404 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor) 1427 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1405 video_set_drvdata(cam->v4ldev, NULL); 1428 video_set_drvdata(cam->v4ldev, NULL);
1406 video_unregister_device(cam->v4ldev); 1429 video_unregister_device(cam->v4ldev);
1407 1430
@@ -1432,7 +1455,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1432 } 1455 }
1433 1456
1434 if (cam->users) { 1457 if (cam->users) {
1435 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor) 1458 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
1436 if ((filp->f_flags & O_NONBLOCK) || 1459 if ((filp->f_flags & O_NONBLOCK) ||
1437 (filp->f_flags & O_NDELAY)) { 1460 (filp->f_flags & O_NDELAY)) {
1438 err = -EWOULDBLOCK; 1461 err = -EWOULDBLOCK;
@@ -1458,7 +1481,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1458 err = sn9c102_init(cam); 1481 err = sn9c102_init(cam);
1459 if (err) { 1482 if (err) {
1460 DBG(1, "Initialization failed again. " 1483 DBG(1, "Initialization failed again. "
1461 "I will retry on next open().") 1484 "I will retry on next open().");
1462 goto out; 1485 goto out;
1463 } 1486 }
1464 cam->state &= ~DEV_MISCONFIGURED; 1487 cam->state &= ~DEV_MISCONFIGURED;
@@ -1475,7 +1498,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1475 cam->frame_count = 0; 1498 cam->frame_count = 0;
1476 sn9c102_empty_framequeues(cam); 1499 sn9c102_empty_framequeues(cam);
1477 1500
1478 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor) 1501 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1479 1502
1480out: 1503out:
1481 up(&cam->dev_sem); 1504 up(&cam->dev_sem);
@@ -1504,7 +1527,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1504 cam->users--; 1527 cam->users--;
1505 wake_up_interruptible_nr(&cam->open, 1); 1528 wake_up_interruptible_nr(&cam->open, 1);
1506 1529
1507 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor) 1530 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1508 1531
1509 up(&cam->dev_sem); 1532 up(&cam->dev_sem);
1510 1533
@@ -1524,32 +1547,38 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1524 return -ERESTARTSYS; 1547 return -ERESTARTSYS;
1525 1548
1526 if (cam->state & DEV_DISCONNECTED) { 1549 if (cam->state & DEV_DISCONNECTED) {
1527 DBG(1, "Device not present") 1550 DBG(1, "Device not present");
1528 up(&cam->fileop_sem); 1551 up(&cam->fileop_sem);
1529 return -ENODEV; 1552 return -ENODEV;
1530 } 1553 }
1531 1554
1532 if (cam->state & DEV_MISCONFIGURED) { 1555 if (cam->state & DEV_MISCONFIGURED) {
1533 DBG(1, "The camera is misconfigured. Close and open it again.") 1556 DBG(1, "The camera is misconfigured. Close and open it "
1557 "again.");
1534 up(&cam->fileop_sem); 1558 up(&cam->fileop_sem);
1535 return -EIO; 1559 return -EIO;
1536 } 1560 }
1537 1561
1538 if (cam->io == IO_MMAP) { 1562 if (cam->io == IO_MMAP) {
1539 DBG(3, "Close and open the device again to choose " 1563 DBG(3, "Close and open the device again to choose "
1540 "the read method") 1564 "the read method");
1541 up(&cam->fileop_sem); 1565 up(&cam->fileop_sem);
1542 return -EINVAL; 1566 return -EINVAL;
1543 } 1567 }
1544 1568
1545 if (cam->io == IO_NONE) { 1569 if (cam->io == IO_NONE) {
1546 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { 1570 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1547 DBG(1, "read() failed, not enough memory") 1571 DBG(1, "read() failed, not enough memory");
1548 up(&cam->fileop_sem); 1572 up(&cam->fileop_sem);
1549 return -ENOMEM; 1573 return -ENOMEM;
1550 } 1574 }
1551 cam->io = IO_READ; 1575 cam->io = IO_READ;
1552 cam->stream = STREAM_ON; 1576 cam->stream = STREAM_ON;
1577 }
1578
1579 if (list_empty(&cam->inqueue)) {
1580 if (!list_empty(&cam->outqueue))
1581 sn9c102_empty_framequeues(cam);
1553 sn9c102_queue_unusedframes(cam); 1582 sn9c102_queue_unusedframes(cam);
1554 } 1583 }
1555 1584
@@ -1584,6 +1613,16 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1584 1613
1585 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame); 1614 f = list_entry(cam->outqueue.prev, struct sn9c102_frame_t, frame);
1586 1615
1616 if (count > f->buf.bytesused)
1617 count = f->buf.bytesused;
1618
1619 if (copy_to_user(buf, f->bufmem, count)) {
1620 err = -EFAULT;
1621 goto exit;
1622 }
1623 *f_pos += count;
1624
1625exit:
1587 spin_lock_irqsave(&cam->queue_lock, lock_flags); 1626 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1588 list_for_each_entry(i, &cam->outqueue, frame) 1627 list_for_each_entry(i, &cam->outqueue, frame)
1589 i->state = F_UNUSED; 1628 i->state = F_UNUSED;
@@ -1592,16 +1631,8 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1592 1631
1593 sn9c102_queue_unusedframes(cam); 1632 sn9c102_queue_unusedframes(cam);
1594 1633
1595 if (count > f->buf.bytesused) 1634 PDBGG("Frame #%lu, bytes read: %zu",
1596 count = f->buf.bytesused; 1635 (unsigned long)f->buf.index, count);
1597
1598 if (copy_to_user(buf, f->bufmem, count)) {
1599 up(&cam->fileop_sem);
1600 return -EFAULT;
1601 }
1602 *f_pos += count;
1603
1604 PDBGG("Frame #%lu, bytes read: %zu", (unsigned long)f->buf.index,count)
1605 1636
1606 up(&cam->fileop_sem); 1637 up(&cam->fileop_sem);
1607 1638
@@ -1612,33 +1643,42 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1612static unsigned int sn9c102_poll(struct file *filp, poll_table *wait) 1643static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1613{ 1644{
1614 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1645 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1646 struct sn9c102_frame_t* f;
1647 unsigned long lock_flags;
1615 unsigned int mask = 0; 1648 unsigned int mask = 0;
1616 1649
1617 if (down_interruptible(&cam->fileop_sem)) 1650 if (down_interruptible(&cam->fileop_sem))
1618 return POLLERR; 1651 return POLLERR;
1619 1652
1620 if (cam->state & DEV_DISCONNECTED) { 1653 if (cam->state & DEV_DISCONNECTED) {
1621 DBG(1, "Device not present") 1654 DBG(1, "Device not present");
1622 goto error; 1655 goto error;
1623 } 1656 }
1624 1657
1625 if (cam->state & DEV_MISCONFIGURED) { 1658 if (cam->state & DEV_MISCONFIGURED) {
1626 DBG(1, "The camera is misconfigured. Close and open it again.") 1659 DBG(1, "The camera is misconfigured. Close and open it "
1660 "again.");
1627 goto error; 1661 goto error;
1628 } 1662 }
1629 1663
1630 if (cam->io == IO_NONE) { 1664 if (cam->io == IO_NONE) {
1631 if (!sn9c102_request_buffers(cam, cam->nreadbuffers, 1665 if (!sn9c102_request_buffers(cam, cam->nreadbuffers,
1632 IO_READ)) { 1666 IO_READ)) {
1633 DBG(1, "poll() failed, not enough memory") 1667 DBG(1, "poll() failed, not enough memory");
1634 goto error; 1668 goto error;
1635 } 1669 }
1636 cam->io = IO_READ; 1670 cam->io = IO_READ;
1637 cam->stream = STREAM_ON; 1671 cam->stream = STREAM_ON;
1638 } 1672 }
1639 1673
1640 if (cam->io == IO_READ) 1674 if (cam->io == IO_READ) {
1675 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1676 list_for_each_entry(f, &cam->outqueue, frame)
1677 f->state = F_UNUSED;
1678 INIT_LIST_HEAD(&cam->outqueue);
1679 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1641 sn9c102_queue_unusedframes(cam); 1680 sn9c102_queue_unusedframes(cam);
1681 }
1642 1682
1643 poll_wait(filp, &cam->wait_frame, wait); 1683 poll_wait(filp, &cam->wait_frame, wait);
1644 1684
@@ -1680,22 +1720,22 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1680{ 1720{
1681 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1721 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1682 unsigned long size = vma->vm_end - vma->vm_start, 1722 unsigned long size = vma->vm_end - vma->vm_start,
1683 start = vma->vm_start, 1723 start = vma->vm_start;
1684 pos, 1724 void *pos;
1685 page;
1686 u32 i; 1725 u32 i;
1687 1726
1688 if (down_interruptible(&cam->fileop_sem)) 1727 if (down_interruptible(&cam->fileop_sem))
1689 return -ERESTARTSYS; 1728 return -ERESTARTSYS;
1690 1729
1691 if (cam->state & DEV_DISCONNECTED) { 1730 if (cam->state & DEV_DISCONNECTED) {
1692 DBG(1, "Device not present") 1731 DBG(1, "Device not present");
1693 up(&cam->fileop_sem); 1732 up(&cam->fileop_sem);
1694 return -ENODEV; 1733 return -ENODEV;
1695 } 1734 }
1696 1735
1697 if (cam->state & DEV_MISCONFIGURED) { 1736 if (cam->state & DEV_MISCONFIGURED) {
1698 DBG(1, "The camera is misconfigured. Close and open it again.") 1737 DBG(1, "The camera is misconfigured. Close and open it "
1738 "again.");
1699 up(&cam->fileop_sem); 1739 up(&cam->fileop_sem);
1700 return -EIO; 1740 return -EIO;
1701 } 1741 }
@@ -1715,15 +1755,12 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1715 return -EINVAL; 1755 return -EINVAL;
1716 } 1756 }
1717 1757
1718 /* VM_IO is eventually going to replace PageReserved altogether */
1719 vma->vm_flags |= VM_IO; 1758 vma->vm_flags |= VM_IO;
1720 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ 1759 vma->vm_flags |= VM_RESERVED;
1721 1760
1722 pos = (unsigned long)cam->frame[i].bufmem; 1761 pos = cam->frame[i].bufmem;
1723 while (size > 0) { /* size is page-aligned */ 1762 while (size > 0) { /* size is page-aligned */
1724 page = vmalloc_to_pfn((void *)pos); 1763 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1725 if (remap_pfn_range(vma, start, page, PAGE_SIZE,
1726 vma->vm_page_prot)) {
1727 up(&cam->fileop_sem); 1764 up(&cam->fileop_sem);
1728 return -EAGAIN; 1765 return -EAGAIN;
1729 } 1766 }
@@ -1742,738 +1779,861 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1742 return 0; 1779 return 0;
1743} 1780}
1744 1781
1782/*****************************************************************************/
1745 1783
1746static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp, 1784static int
1747 unsigned int cmd, void __user * arg) 1785sn9c102_vidioc_querycap(struct sn9c102_device* cam, void __user * arg)
1748{ 1786{
1749 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1787 struct v4l2_capability cap = {
1788 .driver = "sn9c102",
1789 .version = SN9C102_MODULE_VERSION_CODE,
1790 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1791 V4L2_CAP_STREAMING,
1792 };
1793
1794 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1795 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1796 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1797 sizeof(cap.bus_info));
1798
1799 if (copy_to_user(arg, &cap, sizeof(cap)))
1800 return -EFAULT;
1750 1801
1751 switch (cmd) { 1802 return 0;
1803}
1752 1804
1753 case VIDIOC_QUERYCAP:
1754 {
1755 struct v4l2_capability cap = {
1756 .driver = "sn9c102",
1757 .version = SN9C102_MODULE_VERSION_CODE,
1758 .capabilities = V4L2_CAP_VIDEO_CAPTURE |
1759 V4L2_CAP_READWRITE |
1760 V4L2_CAP_STREAMING,
1761 };
1762
1763 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1764 if (usb_make_path(cam->usbdev, cap.bus_info,
1765 sizeof(cap.bus_info)) < 0)
1766 strlcpy(cap.bus_info, cam->dev.bus_id,
1767 sizeof(cap.bus_info));
1768
1769 if (copy_to_user(arg, &cap, sizeof(cap)))
1770 return -EFAULT;
1771 1805
1772 return 0; 1806static int
1773 } 1807sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1808{
1809 struct v4l2_input i;
1774 1810
1775 case VIDIOC_ENUMINPUT: 1811 if (copy_from_user(&i, arg, sizeof(i)))
1776 { 1812 return -EFAULT;
1777 struct v4l2_input i;
1778 1813
1779 if (copy_from_user(&i, arg, sizeof(i))) 1814 if (i.index)
1780 return -EFAULT; 1815 return -EINVAL;
1781 1816
1782 if (i.index) 1817 memset(&i, 0, sizeof(i));
1783 return -EINVAL; 1818 strcpy(i.name, "Camera");
1784 1819
1785 memset(&i, 0, sizeof(i)); 1820 if (copy_to_user(arg, &i, sizeof(i)))
1786 strcpy(i.name, "USB"); 1821 return -EFAULT;
1787 1822
1788 if (copy_to_user(arg, &i, sizeof(i))) 1823 return 0;
1789 return -EFAULT; 1824}
1790 1825
1791 return 0;
1792 }
1793 1826
1794 case VIDIOC_G_INPUT: 1827static int
1795 case VIDIOC_S_INPUT: 1828sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1796 { 1829{
1797 int index; 1830 int index;
1798 1831
1799 if (copy_from_user(&index, arg, sizeof(index))) 1832 if (copy_from_user(&index, arg, sizeof(index)))
1800 return -EFAULT; 1833 return -EFAULT;
1801 1834
1802 if (index != 0) 1835 if (index != 0)
1803 return -EINVAL; 1836 return -EINVAL;
1804 1837
1805 return 0; 1838 return 0;
1806 } 1839}
1807 1840
1808 case VIDIOC_QUERYCTRL:
1809 {
1810 struct sn9c102_sensor* s = cam->sensor;
1811 struct v4l2_queryctrl qc;
1812 u8 i;
1813 1841
1814 if (copy_from_user(&qc, arg, sizeof(qc))) 1842static int
1815 return -EFAULT; 1843sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1844{
1845 struct sn9c102_sensor* s = cam->sensor;
1846 struct v4l2_queryctrl qc;
1847 u8 i;
1816 1848
1849 if (copy_from_user(&qc, arg, sizeof(qc)))
1850 return -EFAULT;
1851
1852 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1853 if (qc.id && qc.id == s->qctrl[i].id) {
1854 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1855 if (copy_to_user(arg, &qc, sizeof(qc)))
1856 return -EFAULT;
1857 return 0;
1858 }
1859
1860 return -EINVAL;
1861}
1862
1863
1864static int
1865sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1866{
1867 struct sn9c102_sensor* s = cam->sensor;
1868 struct v4l2_control ctrl;
1869 int err = 0;
1870 u8 i;
1871
1872 if (!s->get_ctrl && !s->set_ctrl)
1873 return -EINVAL;
1874
1875 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1876 return -EFAULT;
1877
1878 if (!s->get_ctrl) {
1817 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1879 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1818 if (qc.id && qc.id == s->qctrl[i].id) { 1880 if (ctrl.id && ctrl.id == s->qctrl[i].id) {
1819 memcpy(&qc, &(s->qctrl[i]), sizeof(qc)); 1881 ctrl.value = s->_qctrl[i].default_value;
1820 if (copy_to_user(arg, &qc, sizeof(qc))) 1882 goto exit;
1821 return -EFAULT;
1822 return 0;
1823 } 1883 }
1824
1825 return -EINVAL; 1884 return -EINVAL;
1826 } 1885 } else
1886 err = s->get_ctrl(cam, &ctrl);
1827 1887
1828 case VIDIOC_G_CTRL: 1888exit:
1829 { 1889 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1830 struct sn9c102_sensor* s = cam->sensor; 1890 return -EFAULT;
1831 struct v4l2_control ctrl;
1832 int err = 0;
1833 1891
1834 if (!s->get_ctrl) 1892 return err;
1835 return -EINVAL; 1893}
1836 1894
1837 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1838 return -EFAULT;
1839 1895
1840 err = s->get_ctrl(cam, &ctrl); 1896static int
1897sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1898{
1899 struct sn9c102_sensor* s = cam->sensor;
1900 struct v4l2_control ctrl;
1901 u8 i;
1902 int err = 0;
1841 1903
1842 if (copy_to_user(arg, &ctrl, sizeof(ctrl))) 1904 if (!s->set_ctrl)
1843 return -EFAULT; 1905 return -EINVAL;
1906
1907 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1908 return -EFAULT;
1844 1909
1910 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1911 if (ctrl.id == s->qctrl[i].id) {
1912 if (ctrl.value < s->qctrl[i].minimum ||
1913 ctrl.value > s->qctrl[i].maximum)
1914 return -ERANGE;
1915 ctrl.value -= ctrl.value % s->qctrl[i].step;
1916 break;
1917 }
1918
1919 if ((err = s->set_ctrl(cam, &ctrl)))
1845 return err; 1920 return err;
1846 }
1847 1921
1848 case VIDIOC_S_CTRL_OLD: 1922 s->_qctrl[i].default_value = ctrl.value;
1849 case VIDIOC_S_CTRL:
1850 {
1851 struct sn9c102_sensor* s = cam->sensor;
1852 struct v4l2_control ctrl;
1853 u8 i;
1854 int err = 0;
1855 1923
1856 if (!s->set_ctrl) 1924 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu",
1857 return -EINVAL; 1925 (unsigned long)ctrl.id, (unsigned long)ctrl.value);
1858 1926
1859 if (copy_from_user(&ctrl, arg, sizeof(ctrl))) 1927 return 0;
1860 return -EFAULT; 1928}
1861 1929
1862 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1930
1863 if (ctrl.id == s->qctrl[i].id) { 1931static int
1864 if (ctrl.value < s->qctrl[i].minimum || 1932sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1865 ctrl.value > s->qctrl[i].maximum) 1933{
1866 return -ERANGE; 1934 struct v4l2_cropcap* cc = &(cam->sensor->cropcap);
1867 ctrl.value -= ctrl.value % s->qctrl[i].step; 1935
1868 break; 1936 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 cc->pixelaspect.numerator = 1;
1938 cc->pixelaspect.denominator = 1;
1939
1940 if (copy_to_user(arg, cc, sizeof(*cc)))
1941 return -EFAULT;
1942
1943 return 0;
1944}
1945
1946
1947static int
1948sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1949{
1950 struct sn9c102_sensor* s = cam->sensor;
1951 struct v4l2_crop crop = {
1952 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1953 };
1954
1955 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1956
1957 if (copy_to_user(arg, &crop, sizeof(crop)))
1958 return -EFAULT;
1959
1960 return 0;
1961}
1962
1963
1964static int
1965sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1966{
1967 struct sn9c102_sensor* s = cam->sensor;
1968 struct v4l2_crop crop;
1969 struct v4l2_rect* rect;
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1971 struct v4l2_pix_format* pix_format = &(s->pix_format);
1972 u8 scale;
1973 const enum sn9c102_stream_state stream = cam->stream;
1974 const u32 nbuffers = cam->nbuffers;
1975 u32 i;
1976 int err = 0;
1977
1978 if (copy_from_user(&crop, arg, sizeof(crop)))
1979 return -EFAULT;
1980
1981 rect = &(crop.c);
1982
1983 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1984 return -EINVAL;
1985
1986 if (cam->module_param.force_munmap)
1987 for (i = 0; i < cam->nbuffers; i++)
1988 if (cam->frame[i].vma_use_count) {
1989 DBG(3, "VIDIOC_S_CROP failed. "
1990 "Unmap the buffers first.");
1991 return -EINVAL;
1869 } 1992 }
1870 1993
1871 if ((err = s->set_ctrl(cam, &ctrl))) 1994 /* Preserve R,G or B origin */
1995 rect->left = (s->_rect.left & 1L) ? rect->left | 1L : rect->left & ~1L;
1996 rect->top = (s->_rect.top & 1L) ? rect->top | 1L : rect->top & ~1L;
1997
1998 if (rect->width < 16)
1999 rect->width = 16;
2000 if (rect->height < 16)
2001 rect->height = 16;
2002 if (rect->width > bounds->width)
2003 rect->width = bounds->width;
2004 if (rect->height > bounds->height)
2005 rect->height = bounds->height;
2006 if (rect->left < bounds->left)
2007 rect->left = bounds->left;
2008 if (rect->top < bounds->top)
2009 rect->top = bounds->top;
2010 if (rect->left + rect->width > bounds->left + bounds->width)
2011 rect->left = bounds->left+bounds->width - rect->width;
2012 if (rect->top + rect->height > bounds->top + bounds->height)
2013 rect->top = bounds->top+bounds->height - rect->height;
2014
2015 rect->width &= ~15L;
2016 rect->height &= ~15L;
2017
2018 if (SN9C102_PRESERVE_IMGSCALE) {
2019 /* Calculate the actual scaling factor */
2020 u32 a, b;
2021 a = rect->width * rect->height;
2022 b = pix_format->width * pix_format->height;
2023 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2024 } else
2025 scale = 1;
2026
2027 if (cam->stream == STREAM_ON)
2028 if ((err = sn9c102_stream_interrupt(cam)))
1872 return err; 2029 return err;
1873 2030
1874 s->_qctrl[i].default_value = ctrl.value; 2031 if (copy_to_user(arg, &crop, sizeof(crop))) {
2032 cam->stream = stream;
2033 return -EFAULT;
2034 }
2035
2036 if (cam->module_param.force_munmap || cam->io == IO_READ)
2037 sn9c102_release_buffers(cam);
1875 2038
1876 PDBGG("VIDIOC_S_CTRL: id %lu, value %lu", 2039 err = sn9c102_set_crop(cam, rect);
1877 (unsigned long)ctrl.id, (unsigned long)ctrl.value) 2040 if (s->set_crop)
2041 err += s->set_crop(cam, rect);
2042 err += sn9c102_set_scale(cam, scale);
1878 2043
1879 return 0; 2044 if (err) { /* atomic, no rollback in ioctl() */
2045 cam->state |= DEV_MISCONFIGURED;
2046 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
2047 "use the camera, close and open /dev/video%d again.",
2048 cam->v4ldev->minor);
2049 return -EIO;
1880 } 2050 }
1881 2051
1882 case VIDIOC_CROPCAP: 2052 s->pix_format.width = rect->width/scale;
1883 { 2053 s->pix_format.height = rect->height/scale;
1884 struct v4l2_cropcap* cc = &(cam->sensor->cropcap); 2054 memcpy(&(s->_rect), rect, sizeof(*rect));
1885 2055
1886 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2056 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1887 cc->pixelaspect.numerator = 1; 2057 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
1888 cc->pixelaspect.denominator = 1; 2058 cam->state |= DEV_MISCONFIGURED;
2059 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
2060 "use the camera, close and open /dev/video%d again.",
2061 cam->v4ldev->minor);
2062 return -ENOMEM;
2063 }
1889 2064
1890 if (copy_to_user(arg, cc, sizeof(*cc))) 2065 if (cam->io == IO_READ)
1891 return -EFAULT; 2066 sn9c102_empty_framequeues(cam);
2067 else if (cam->module_param.force_munmap)
2068 sn9c102_requeue_outqueue(cam);
1892 2069
1893 return 0; 2070 cam->stream = stream;
1894 }
1895 2071
1896 case VIDIOC_G_CROP: 2072 return 0;
1897 { 2073}
1898 struct sn9c102_sensor* s = cam->sensor;
1899 struct v4l2_crop crop = {
1900 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1901 };
1902 2074
1903 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1904 2075
1905 if (copy_to_user(arg, &crop, sizeof(crop))) 2076static int
1906 return -EFAULT; 2077sn9c102_vidioc_enum_fmt(struct sn9c102_device* cam, void __user * arg)
2078{
2079 struct v4l2_fmtdesc fmtd;
1907 2080
1908 return 0; 2081 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1909 } 2082 return -EFAULT;
1910 2083
1911 case VIDIOC_S_CROP: 2084 if (fmtd.index == 0) {
1912 { 2085 strcpy(fmtd.description, "bayer rgb");
1913 struct sn9c102_sensor* s = cam->sensor; 2086 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
1914 struct v4l2_crop crop; 2087 } else if (fmtd.index == 1) {
1915 struct v4l2_rect* rect; 2088 strcpy(fmtd.description, "compressed");
1916 struct v4l2_rect* bounds = &(s->cropcap.bounds); 2089 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
1917 struct v4l2_pix_format* pix_format = &(s->pix_format); 2090 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1918 u8 scale; 2091 } else
1919 const enum sn9c102_stream_state stream = cam->stream; 2092 return -EINVAL;
1920 const u32 nbuffers = cam->nbuffers;
1921 u32 i;
1922 int err = 0;
1923
1924 if (copy_from_user(&crop, arg, sizeof(crop)))
1925 return -EFAULT;
1926 2093
1927 rect = &(crop.c); 2094 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2095 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1928 2096
1929 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2097 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1930 return -EINVAL; 2098 return -EFAULT;
1931 2099
1932 if (cam->module_param.force_munmap) 2100 return 0;
1933 for (i = 0; i < cam->nbuffers; i++) 2101}
1934 if (cam->frame[i].vma_use_count) {
1935 DBG(3, "VIDIOC_S_CROP failed. "
1936 "Unmap the buffers first.")
1937 return -EINVAL;
1938 }
1939 2102
1940 /* Preserve R,G or B origin */
1941 rect->left = (s->_rect.left & 1L) ?
1942 rect->left | 1L : rect->left & ~1L;
1943 rect->top = (s->_rect.top & 1L) ?
1944 rect->top | 1L : rect->top & ~1L;
1945
1946 if (rect->width < 16)
1947 rect->width = 16;
1948 if (rect->height < 16)
1949 rect->height = 16;
1950 if (rect->width > bounds->width)
1951 rect->width = bounds->width;
1952 if (rect->height > bounds->height)
1953 rect->height = bounds->height;
1954 if (rect->left < bounds->left)
1955 rect->left = bounds->left;
1956 if (rect->top < bounds->top)
1957 rect->top = bounds->top;
1958 if (rect->left + rect->width > bounds->left + bounds->width)
1959 rect->left = bounds->left+bounds->width - rect->width;
1960 if (rect->top + rect->height > bounds->top + bounds->height)
1961 rect->top = bounds->top+bounds->height - rect->height;
1962
1963 rect->width &= ~15L;
1964 rect->height &= ~15L;
1965
1966 if (SN9C102_PRESERVE_IMGSCALE) {
1967 /* Calculate the actual scaling factor */
1968 u32 a, b;
1969 a = rect->width * rect->height;
1970 b = pix_format->width * pix_format->height;
1971 scale = b ? (u8)((a / b) < 4 ? 1 :
1972 ((a / b) < 16 ? 2 : 4)) : 1;
1973 } else
1974 scale = 1;
1975
1976 if (cam->stream == STREAM_ON)
1977 if ((err = sn9c102_stream_interrupt(cam)))
1978 return err;
1979
1980 if (copy_to_user(arg, &crop, sizeof(crop))) {
1981 cam->stream = stream;
1982 return -EFAULT;
1983 }
1984 2103
1985 if (cam->module_param.force_munmap || cam->io == IO_READ) 2104static int
1986 sn9c102_release_buffers(cam); 2105sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2106{
2107 struct v4l2_format format;
2108 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
1987 2109
1988 err = sn9c102_set_crop(cam, rect); 2110 if (copy_from_user(&format, arg, sizeof(format)))
1989 if (s->set_crop) 2111 return -EFAULT;
1990 err += s->set_crop(cam, rect);
1991 err += sn9c102_set_scale(cam, scale);
1992 2112
1993 if (err) { /* atomic, no rollback in ioctl() */ 2113 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1994 cam->state |= DEV_MISCONFIGURED; 2114 return -EINVAL;
1995 DBG(1, "VIDIOC_S_CROP failed because of hardware "
1996 "problems. To use the camera, close and open "
1997 "/dev/video%d again.", cam->v4ldev->minor)
1998 return -EIO;
1999 }
2000 2115
2001 s->pix_format.width = rect->width/scale; 2116 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X)
2002 s->pix_format.height = rect->height/scale; 2117 ? 0 : (pfmt->width * pfmt->priv) / 8;
2003 memcpy(&(s->_rect), rect, sizeof(*rect)); 2118 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2004 2119 pfmt->field = V4L2_FIELD_NONE;
2005 if ((cam->module_param.force_munmap || cam->io == IO_READ) && 2120 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2006 nbuffers != sn9c102_request_buffers(cam, nbuffers,
2007 cam->io)) {
2008 cam->state |= DEV_MISCONFIGURED;
2009 DBG(1, "VIDIOC_S_CROP failed because of not enough "
2010 "memory. To use the camera, close and open "
2011 "/dev/video%d again.", cam->v4ldev->minor)
2012 return -ENOMEM;
2013 }
2014 2121
2015 cam->stream = stream; 2122 if (copy_to_user(arg, &format, sizeof(format)))
2123 return -EFAULT;
2016 2124
2017 return 0; 2125 return 0;
2018 } 2126}
2019 2127
2020 case VIDIOC_ENUM_FMT:
2021 {
2022 struct v4l2_fmtdesc fmtd;
2023 2128
2024 if (copy_from_user(&fmtd, arg, sizeof(fmtd))) 2129static int
2025 return -EFAULT; 2130sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2131 void __user * arg)
2132{
2133 struct sn9c102_sensor* s = cam->sensor;
2134 struct v4l2_format format;
2135 struct v4l2_pix_format* pix;
2136 struct v4l2_pix_format* pfmt = &(s->pix_format);
2137 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2138 struct v4l2_rect rect;
2139 u8 scale;
2140 const enum sn9c102_stream_state stream = cam->stream;
2141 const u32 nbuffers = cam->nbuffers;
2142 u32 i;
2143 int err = 0;
2026 2144
2027 if (fmtd.index == 0) { 2145 if (copy_from_user(&format, arg, sizeof(format)))
2028 strcpy(fmtd.description, "bayer rgb"); 2146 return -EFAULT;
2029 fmtd.pixelformat = V4L2_PIX_FMT_SBGGR8;
2030 } else if (fmtd.index == 1) {
2031 strcpy(fmtd.description, "compressed");
2032 fmtd.pixelformat = V4L2_PIX_FMT_SN9C10X;
2033 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
2034 } else
2035 return -EINVAL;
2036 2147
2037 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2148 pix = &(format.fmt.pix);
2038 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
2039 2149
2040 if (copy_to_user(arg, &fmtd, sizeof(fmtd))) 2150 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2041 return -EFAULT; 2151 return -EINVAL;
2042 2152
2043 return 0; 2153 memcpy(&rect, &(s->_rect), sizeof(rect));
2154
2155 { /* calculate the actual scaling factor */
2156 u32 a, b;
2157 a = rect.width * rect.height;
2158 b = pix->width * pix->height;
2159 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2044 } 2160 }
2045 2161
2046 case VIDIOC_G_FMT: 2162 rect.width = scale * pix->width;
2047 { 2163 rect.height = scale * pix->height;
2048 struct v4l2_format format;
2049 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format);
2050 2164
2051 if (copy_from_user(&format, arg, sizeof(format))) 2165 if (rect.width < 16)
2052 return -EFAULT; 2166 rect.width = 16;
2167 if (rect.height < 16)
2168 rect.height = 16;
2169 if (rect.width > bounds->left + bounds->width - rect.left)
2170 rect.width = bounds->left + bounds->width - rect.left;
2171 if (rect.height > bounds->top + bounds->height - rect.top)
2172 rect.height = bounds->top + bounds->height - rect.top;
2053 2173
2054 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2174 rect.width &= ~15L;
2055 return -EINVAL; 2175 rect.height &= ~15L;
2176
2177 { /* adjust the scaling factor */
2178 u32 a, b;
2179 a = rect.width * rect.height;
2180 b = pix->width * pix->height;
2181 scale = b ? (u8)((a / b) < 4 ? 1 : ((a / b) < 16 ? 2 : 4)) : 1;
2182 }
2056 2183
2057 pfmt->bytesperline = (pfmt->pixelformat==V4L2_PIX_FMT_SN9C10X) 2184 pix->width = rect.width / scale;
2058 ? 0 : (pfmt->width * pfmt->priv) / 8; 2185 pix->height = rect.height / scale;
2059 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
2060 pfmt->field = V4L2_FIELD_NONE;
2061 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
2062 2186
2187 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2188 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2189 pix->pixelformat = pfmt->pixelformat;
2190 pix->priv = pfmt->priv; /* bpp */
2191 pix->colorspace = pfmt->colorspace;
2192 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2193 ? 0 : (pix->width * pix->priv) / 8;
2194 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2195 pix->field = V4L2_FIELD_NONE;
2196
2197 if (cmd == VIDIOC_TRY_FMT) {
2063 if (copy_to_user(arg, &format, sizeof(format))) 2198 if (copy_to_user(arg, &format, sizeof(format)))
2064 return -EFAULT; 2199 return -EFAULT;
2065
2066 return 0; 2200 return 0;
2067 } 2201 }
2068 2202
2069 case VIDIOC_TRY_FMT: 2203 if (cam->module_param.force_munmap)
2070 case VIDIOC_S_FMT: 2204 for (i = 0; i < cam->nbuffers; i++)
2071 { 2205 if (cam->frame[i].vma_use_count) {
2072 struct sn9c102_sensor* s = cam->sensor; 2206 DBG(3, "VIDIOC_S_FMT failed. Unmap the "
2073 struct v4l2_format format; 2207 "buffers first.");
2074 struct v4l2_pix_format* pix; 2208 return -EINVAL;
2075 struct v4l2_pix_format* pfmt = &(s->pix_format); 2209 }
2076 struct v4l2_rect* bounds = &(s->cropcap.bounds);
2077 struct v4l2_rect rect;
2078 u8 scale;
2079 const enum sn9c102_stream_state stream = cam->stream;
2080 const u32 nbuffers = cam->nbuffers;
2081 u32 i;
2082 int err = 0;
2083
2084 if (copy_from_user(&format, arg, sizeof(format)))
2085 return -EFAULT;
2086 2210
2087 pix = &(format.fmt.pix); 2211 if (cam->stream == STREAM_ON)
2212 if ((err = sn9c102_stream_interrupt(cam)))
2213 return err;
2088 2214
2089 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2215 if (copy_to_user(arg, &format, sizeof(format))) {
2090 return -EINVAL; 2216 cam->stream = stream;
2217 return -EFAULT;
2218 }
2091 2219
2092 memcpy(&rect, &(s->_rect), sizeof(rect)); 2220 if (cam->module_param.force_munmap || cam->io == IO_READ)
2221 sn9c102_release_buffers(cam);
2093 2222
2094 { /* calculate the actual scaling factor */ 2223 err += sn9c102_set_pix_format(cam, pix);
2095 u32 a, b; 2224 err += sn9c102_set_crop(cam, &rect);
2096 a = rect.width * rect.height; 2225 if (s->set_pix_format)
2097 b = pix->width * pix->height; 2226 err += s->set_pix_format(cam, pix);
2098 scale = b ? (u8)((a / b) < 4 ? 1 : 2227 if (s->set_crop)
2099 ((a / b) < 16 ? 2 : 4)) : 1; 2228 err += s->set_crop(cam, &rect);
2100 } 2229 err += sn9c102_set_scale(cam, scale);
2101 2230
2102 rect.width = scale * pix->width; 2231 if (err) { /* atomic, no rollback in ioctl() */
2103 rect.height = scale * pix->height; 2232 cam->state |= DEV_MISCONFIGURED;
2104 2233 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
2105 if (rect.width < 16) 2234 "use the camera, close and open /dev/video%d again.",
2106 rect.width = 16; 2235 cam->v4ldev->minor);
2107 if (rect.height < 16) 2236 return -EIO;
2108 rect.height = 16; 2237 }
2109 if (rect.width > bounds->left + bounds->width - rect.left)
2110 rect.width = bounds->left + bounds->width - rect.left;
2111 if (rect.height > bounds->top + bounds->height - rect.top)
2112 rect.height = bounds->top + bounds->height - rect.top;
2113
2114 rect.width &= ~15L;
2115 rect.height &= ~15L;
2116
2117 { /* adjust the scaling factor */
2118 u32 a, b;
2119 a = rect.width * rect.height;
2120 b = pix->width * pix->height;
2121 scale = b ? (u8)((a / b) < 4 ? 1 :
2122 ((a / b) < 16 ? 2 : 4)) : 1;
2123 }
2124 2238
2125 pix->width = rect.width / scale; 2239 memcpy(pfmt, pix, sizeof(*pix));
2126 pix->height = rect.height / scale; 2240 memcpy(&(s->_rect), &rect, sizeof(rect));
2127
2128 if (pix->pixelformat != V4L2_PIX_FMT_SN9C10X &&
2129 pix->pixelformat != V4L2_PIX_FMT_SBGGR8)
2130 pix->pixelformat = pfmt->pixelformat;
2131 pix->priv = pfmt->priv; /* bpp */
2132 pix->colorspace = pfmt->colorspace;
2133 pix->bytesperline = (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
2134 ? 0 : (pix->width * pix->priv) / 8;
2135 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
2136 pix->field = V4L2_FIELD_NONE;
2137
2138 if (cmd == VIDIOC_TRY_FMT) {
2139 if (copy_to_user(arg, &format, sizeof(format)))
2140 return -EFAULT;
2141 return 0;
2142 }
2143 2241
2144 if (cam->module_param.force_munmap) 2242 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
2145 for (i = 0; i < cam->nbuffers; i++) 2243 nbuffers != sn9c102_request_buffers(cam, nbuffers, cam->io)) {
2146 if (cam->frame[i].vma_use_count) { 2244 cam->state |= DEV_MISCONFIGURED;
2147 DBG(3, "VIDIOC_S_FMT failed. " 2245 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
2148 "Unmap the buffers first.") 2246 "use the camera, close and open /dev/video%d again.",
2149 return -EINVAL; 2247 cam->v4ldev->minor);
2150 } 2248 return -ENOMEM;
2249 }
2151 2250
2152 if (cam->stream == STREAM_ON) 2251 if (cam->io == IO_READ)
2153 if ((err = sn9c102_stream_interrupt(cam))) 2252 sn9c102_empty_framequeues(cam);
2154 return err; 2253 else if (cam->module_param.force_munmap)
2254 sn9c102_requeue_outqueue(cam);
2155 2255
2156 if (copy_to_user(arg, &format, sizeof(format))) { 2256 cam->stream = stream;
2157 cam->stream = stream;
2158 return -EFAULT;
2159 }
2160 2257
2161 if (cam->module_param.force_munmap || cam->io == IO_READ) 2258 return 0;
2162 sn9c102_release_buffers(cam); 2259}
2163
2164 err += sn9c102_set_pix_format(cam, pix);
2165 err += sn9c102_set_crop(cam, &rect);
2166 if (s->set_pix_format)
2167 err += s->set_pix_format(cam, pix);
2168 if (s->set_crop)
2169 err += s->set_crop(cam, &rect);
2170 err += sn9c102_set_scale(cam, scale);
2171
2172 if (err) { /* atomic, no rollback in ioctl() */
2173 cam->state |= DEV_MISCONFIGURED;
2174 DBG(1, "VIDIOC_S_FMT failed because of hardware "
2175 "problems. To use the camera, close and open "
2176 "/dev/video%d again.", cam->v4ldev->minor)
2177 return -EIO;
2178 }
2179 2260
2180 memcpy(pfmt, pix, sizeof(*pix));
2181 memcpy(&(s->_rect), &rect, sizeof(rect));
2182 2261
2183 if ((cam->module_param.force_munmap || cam->io == IO_READ) && 2262static int
2184 nbuffers != sn9c102_request_buffers(cam, nbuffers, 2263sn9c102_vidioc_g_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2185 cam->io)) { 2264{
2186 cam->state |= DEV_MISCONFIGURED; 2265 if (copy_to_user(arg, &cam->compression,
2187 DBG(1, "VIDIOC_S_FMT failed because of not enough " 2266 sizeof(cam->compression)))
2188 "memory. To use the camera, close and open " 2267 return -EFAULT;
2189 "/dev/video%d again.", cam->v4ldev->minor)
2190 return -ENOMEM;
2191 }
2192 2268
2193 cam->stream = stream; 2269 return 0;
2270}
2194 2271
2195 return 0;
2196 }
2197 2272
2198 case VIDIOC_G_JPEGCOMP: 2273static int
2199 { 2274sn9c102_vidioc_s_jpegcomp(struct sn9c102_device* cam, void __user * arg)
2200 if (copy_to_user(arg, &cam->compression, 2275{
2201 sizeof(cam->compression))) 2276 struct v4l2_jpegcompression jc;
2202 return -EFAULT; 2277 const enum sn9c102_stream_state stream = cam->stream;
2278 int err = 0;
2203 2279
2204 return 0; 2280 if (copy_from_user(&jc, arg, sizeof(jc)))
2205 } 2281 return -EFAULT;
2206 2282
2207 case VIDIOC_S_JPEGCOMP: 2283 if (jc.quality != 0 && jc.quality != 1)
2208 { 2284 return -EINVAL;
2209 struct v4l2_jpegcompression jc;
2210 const enum sn9c102_stream_state stream = cam->stream;
2211 int err = 0;
2212 2285
2213 if (copy_from_user(&jc, arg, sizeof(jc))) 2286 if (cam->stream == STREAM_ON)
2214 return -EFAULT; 2287 if ((err = sn9c102_stream_interrupt(cam)))
2288 return err;
2215 2289
2216 if (jc.quality != 0 && jc.quality != 1) 2290 err += sn9c102_set_compression(cam, &jc);
2217 return -EINVAL; 2291 if (err) { /* atomic, no rollback in ioctl() */
2292 cam->state |= DEV_MISCONFIGURED;
2293 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2294 "problems. To use the camera, close and open "
2295 "/dev/video%d again.", cam->v4ldev->minor);
2296 return -EIO;
2297 }
2218 2298
2219 if (cam->stream == STREAM_ON) 2299 cam->compression.quality = jc.quality;
2220 if ((err = sn9c102_stream_interrupt(cam)))
2221 return err;
2222 2300
2223 err += sn9c102_set_compression(cam, &jc); 2301 cam->stream = stream;
2224 if (err) { /* atomic, no rollback in ioctl() */
2225 cam->state |= DEV_MISCONFIGURED;
2226 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
2227 "problems. To use the camera, close and open "
2228 "/dev/video%d again.", cam->v4ldev->minor)
2229 return -EIO;
2230 }
2231 2302
2232 cam->compression.quality = jc.quality; 2303 return 0;
2304}
2233 2305
2234 cam->stream = stream;
2235 2306
2236 return 0; 2307static int
2237 } 2308sn9c102_vidioc_reqbufs(struct sn9c102_device* cam, void __user * arg)
2309{
2310 struct v4l2_requestbuffers rb;
2311 u32 i;
2312 int err;
2238 2313
2239 case VIDIOC_REQBUFS: 2314 if (copy_from_user(&rb, arg, sizeof(rb)))
2240 { 2315 return -EFAULT;
2241 struct v4l2_requestbuffers rb;
2242 u32 i;
2243 int err;
2244 2316
2245 if (copy_from_user(&rb, arg, sizeof(rb))) 2317 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2246 return -EFAULT; 2318 rb.memory != V4L2_MEMORY_MMAP)
2319 return -EINVAL;
2247 2320
2248 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 2321 if (cam->io == IO_READ) {
2249 rb.memory != V4L2_MEMORY_MMAP) 2322 DBG(3, "Close and open the device again to choose the mmap "
2250 return -EINVAL; 2323 "I/O method");
2324 return -EINVAL;
2325 }
2251 2326
2252 if (cam->io == IO_READ) { 2327 for (i = 0; i < cam->nbuffers; i++)
2253 DBG(3, "Close and open the device again to choose " 2328 if (cam->frame[i].vma_use_count) {
2254 "the mmap I/O method") 2329 DBG(3, "VIDIOC_REQBUFS failed. Previous buffers are "
2330 "still mapped.");
2255 return -EINVAL; 2331 return -EINVAL;
2256 } 2332 }
2257 2333
2258 for (i = 0; i < cam->nbuffers; i++) 2334 if (cam->stream == STREAM_ON)
2259 if (cam->frame[i].vma_use_count) { 2335 if ((err = sn9c102_stream_interrupt(cam)))
2260 DBG(3, "VIDIOC_REQBUFS failed. " 2336 return err;
2261 "Previous buffers are still mapped.")
2262 return -EINVAL;
2263 }
2264 2337
2265 if (cam->stream == STREAM_ON) 2338 sn9c102_empty_framequeues(cam);
2266 if ((err = sn9c102_stream_interrupt(cam)))
2267 return err;
2268 2339
2269 sn9c102_empty_framequeues(cam); 2340 sn9c102_release_buffers(cam);
2341 if (rb.count)
2342 rb.count = sn9c102_request_buffers(cam, rb.count, IO_MMAP);
2270 2343
2344 if (copy_to_user(arg, &rb, sizeof(rb))) {
2271 sn9c102_release_buffers(cam); 2345 sn9c102_release_buffers(cam);
2272 if (rb.count) 2346 cam->io = IO_NONE;
2273 rb.count = sn9c102_request_buffers(cam, rb.count, 2347 return -EFAULT;
2274 IO_MMAP); 2348 }
2275 2349
2276 if (copy_to_user(arg, &rb, sizeof(rb))) { 2350 cam->io = rb.count ? IO_MMAP : IO_NONE;
2277 sn9c102_release_buffers(cam);
2278 cam->io = IO_NONE;
2279 return -EFAULT;
2280 }
2281 2351
2282 cam->io = rb.count ? IO_MMAP : IO_NONE; 2352 return 0;
2353}
2283 2354
2284 return 0;
2285 }
2286 2355
2287 case VIDIOC_QUERYBUF: 2356static int
2288 { 2357sn9c102_vidioc_querybuf(struct sn9c102_device* cam, void __user * arg)
2289 struct v4l2_buffer b; 2358{
2359 struct v4l2_buffer b;
2290 2360
2291 if (copy_from_user(&b, arg, sizeof(b))) 2361 if (copy_from_user(&b, arg, sizeof(b)))
2292 return -EFAULT; 2362 return -EFAULT;
2293 2363
2294 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 2364 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2295 b.index >= cam->nbuffers || cam->io != IO_MMAP) 2365 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2296 return -EINVAL; 2366 return -EINVAL;
2297 2367
2298 memcpy(&b, &cam->frame[b.index].buf, sizeof(b)); 2368 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
2299 2369
2300 if (cam->frame[b.index].vma_use_count) 2370 if (cam->frame[b.index].vma_use_count)
2301 b.flags |= V4L2_BUF_FLAG_MAPPED; 2371 b.flags |= V4L2_BUF_FLAG_MAPPED;
2302 2372
2303 if (cam->frame[b.index].state == F_DONE) 2373 if (cam->frame[b.index].state == F_DONE)
2304 b.flags |= V4L2_BUF_FLAG_DONE; 2374 b.flags |= V4L2_BUF_FLAG_DONE;
2305 else if (cam->frame[b.index].state != F_UNUSED) 2375 else if (cam->frame[b.index].state != F_UNUSED)
2306 b.flags |= V4L2_BUF_FLAG_QUEUED; 2376 b.flags |= V4L2_BUF_FLAG_QUEUED;
2307 2377
2308 if (copy_to_user(arg, &b, sizeof(b))) 2378 if (copy_to_user(arg, &b, sizeof(b)))
2309 return -EFAULT; 2379 return -EFAULT;
2310 2380
2311 return 0; 2381 return 0;
2312 } 2382}
2313 2383
2314 case VIDIOC_QBUF:
2315 {
2316 struct v4l2_buffer b;
2317 unsigned long lock_flags;
2318 2384
2319 if (copy_from_user(&b, arg, sizeof(b))) 2385static int
2320 return -EFAULT; 2386sn9c102_vidioc_qbuf(struct sn9c102_device* cam, void __user * arg)
2387{
2388 struct v4l2_buffer b;
2389 unsigned long lock_flags;
2321 2390
2322 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 2391 if (copy_from_user(&b, arg, sizeof(b)))
2323 b.index >= cam->nbuffers || cam->io != IO_MMAP) 2392 return -EFAULT;
2324 return -EINVAL;
2325 2393
2326 if (cam->frame[b.index].state != F_UNUSED) 2394 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2327 return -EINVAL; 2395 b.index >= cam->nbuffers || cam->io != IO_MMAP)
2396 return -EINVAL;
2328 2397
2329 cam->frame[b.index].state = F_QUEUED; 2398 if (cam->frame[b.index].state != F_UNUSED)
2399 return -EINVAL;
2330 2400
2331 spin_lock_irqsave(&cam->queue_lock, lock_flags); 2401 cam->frame[b.index].state = F_QUEUED;
2332 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2333 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2334 2402
2335 PDBGG("Frame #%lu queued", (unsigned long)b.index) 2403 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2404 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
2405 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2336 2406
2337 return 0; 2407 PDBGG("Frame #%lu queued", (unsigned long)b.index);
2338 }
2339 2408
2340 case VIDIOC_DQBUF: 2409 return 0;
2341 { 2410}
2342 struct v4l2_buffer b;
2343 struct sn9c102_frame_t *f;
2344 unsigned long lock_flags;
2345 int err = 0;
2346 2411
2347 if (copy_from_user(&b, arg, sizeof(b)))
2348 return -EFAULT;
2349 2412
2350 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP) 2413static int
2414sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2415 void __user * arg)
2416{
2417 struct v4l2_buffer b;
2418 struct sn9c102_frame_t *f;
2419 unsigned long lock_flags;
2420 int err = 0;
2421
2422 if (copy_from_user(&b, arg, sizeof(b)))
2423 return -EFAULT;
2424
2425 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2426 return -EINVAL;
2427
2428 if (list_empty(&cam->outqueue)) {
2429 if (cam->stream == STREAM_OFF)
2351 return -EINVAL; 2430 return -EINVAL;
2431 if (filp->f_flags & O_NONBLOCK)
2432 return -EAGAIN;
2433 err = wait_event_interruptible
2434 ( cam->wait_frame,
2435 (!list_empty(&cam->outqueue)) ||
2436 (cam->state & DEV_DISCONNECTED) ||
2437 (cam->state & DEV_MISCONFIGURED) );
2438 if (err)
2439 return err;
2440 if (cam->state & DEV_DISCONNECTED)
2441 return -ENODEV;
2442 if (cam->state & DEV_MISCONFIGURED)
2443 return -EIO;
2444 }
2352 2445
2353 if (list_empty(&cam->outqueue)) { 2446 spin_lock_irqsave(&cam->queue_lock, lock_flags);
2354 if (cam->stream == STREAM_OFF) 2447 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t, frame);
2355 return -EINVAL; 2448 list_del(cam->outqueue.next);
2356 if (filp->f_flags & O_NONBLOCK) 2449 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2357 return -EAGAIN;
2358 err = wait_event_interruptible
2359 ( cam->wait_frame,
2360 (!list_empty(&cam->outqueue)) ||
2361 (cam->state & DEV_DISCONNECTED) ||
2362 (cam->state & DEV_MISCONFIGURED) );
2363 if (err)
2364 return err;
2365 if (cam->state & DEV_DISCONNECTED)
2366 return -ENODEV;
2367 if (cam->state & DEV_MISCONFIGURED)
2368 return -EIO;
2369 }
2370 2450
2371 spin_lock_irqsave(&cam->queue_lock, lock_flags); 2451 f->state = F_UNUSED;
2372 f = list_entry(cam->outqueue.next, struct sn9c102_frame_t,
2373 frame);
2374 list_del(cam->outqueue.next);
2375 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
2376 2452
2377 f->state = F_UNUSED; 2453 memcpy(&b, &f->buf, sizeof(b));
2454 if (f->vma_use_count)
2455 b.flags |= V4L2_BUF_FLAG_MAPPED;
2378 2456
2379 memcpy(&b, &f->buf, sizeof(b)); 2457 if (copy_to_user(arg, &b, sizeof(b)))
2380 if (f->vma_use_count) 2458 return -EFAULT;
2381 b.flags |= V4L2_BUF_FLAG_MAPPED;
2382 2459
2383 if (copy_to_user(arg, &b, sizeof(b))) 2460 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
2384 return -EFAULT;
2385 2461
2386 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index) 2462 return 0;
2463}
2387 2464
2388 return 0;
2389 }
2390 2465
2391 case VIDIOC_STREAMON: 2466static int
2392 { 2467sn9c102_vidioc_streamon(struct sn9c102_device* cam, void __user * arg)
2393 int type; 2468{
2469 int type;
2394 2470
2395 if (copy_from_user(&type, arg, sizeof(type))) 2471 if (copy_from_user(&type, arg, sizeof(type)))
2396 return -EFAULT; 2472 return -EFAULT;
2397 2473
2398 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) 2474 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2399 return -EINVAL; 2475 return -EINVAL;
2400 2476
2401 if (list_empty(&cam->inqueue)) 2477 if (list_empty(&cam->inqueue))
2402 return -EINVAL; 2478 return -EINVAL;
2403 2479
2404 cam->stream = STREAM_ON; 2480 cam->stream = STREAM_ON;
2405 2481
2406 DBG(3, "Stream on") 2482 DBG(3, "Stream on");
2407 2483
2408 return 0; 2484 return 0;
2409 } 2485}
2410 2486
2411 case VIDIOC_STREAMOFF:
2412 {
2413 int type, err;
2414 2487
2415 if (copy_from_user(&type, arg, sizeof(type))) 2488static int
2416 return -EFAULT; 2489sn9c102_vidioc_streamoff(struct sn9c102_device* cam, void __user * arg)
2490{
2491 int type, err;
2417 2492
2418 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP) 2493 if (copy_from_user(&type, arg, sizeof(type)))
2419 return -EINVAL; 2494 return -EFAULT;
2420 2495
2421 if (cam->stream == STREAM_ON) 2496 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
2422 if ((err = sn9c102_stream_interrupt(cam))) 2497 return -EINVAL;
2423 return err;
2424 2498
2425 sn9c102_empty_framequeues(cam); 2499 if (cam->stream == STREAM_ON)
2500 if ((err = sn9c102_stream_interrupt(cam)))
2501 return err;
2426 2502
2427 DBG(3, "Stream off") 2503 sn9c102_empty_framequeues(cam);
2428 2504
2429 return 0; 2505 DBG(3, "Stream off");
2430 }
2431 2506
2432 case VIDIOC_G_PARM: 2507 return 0;
2433 { 2508}
2434 struct v4l2_streamparm sp;
2435 2509
2436 if (copy_from_user(&sp, arg, sizeof(sp)))
2437 return -EFAULT;
2438 2510
2439 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 2511static int
2440 return -EINVAL; 2512sn9c102_vidioc_g_parm(struct sn9c102_device* cam, void __user * arg)
2513{
2514 struct v4l2_streamparm sp;
2515
2516 if (copy_from_user(&sp, arg, sizeof(sp)))
2517 return -EFAULT;
2518
2519 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2520 return -EINVAL;
2521
2522 sp.parm.capture.extendedmode = 0;
2523 sp.parm.capture.readbuffers = cam->nreadbuffers;
2524
2525 if (copy_to_user(arg, &sp, sizeof(sp)))
2526 return -EFAULT;
2527
2528 return 0;
2529}
2530
2531
2532static int
2533sn9c102_vidioc_s_parm(struct sn9c102_device* cam, void __user * arg)
2534{
2535 struct v4l2_streamparm sp;
2536
2537 if (copy_from_user(&sp, arg, sizeof(sp)))
2538 return -EFAULT;
2539
2540 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2541 return -EINVAL;
2542
2543 sp.parm.capture.extendedmode = 0;
2441 2544
2442 sp.parm.capture.extendedmode = 0; 2545 if (sp.parm.capture.readbuffers == 0)
2443 sp.parm.capture.readbuffers = cam->nreadbuffers; 2546 sp.parm.capture.readbuffers = cam->nreadbuffers;
2444 2547
2445 if (copy_to_user(arg, &sp, sizeof(sp))) 2548 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES)
2446 return -EFAULT; 2549 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES;
2447 2550
2448 return 0; 2551 if (copy_to_user(arg, &sp, sizeof(sp)))
2449 } 2552 return -EFAULT;
2450 2553
2451 case VIDIOC_S_PARM_OLD: 2554 cam->nreadbuffers = sp.parm.capture.readbuffers;
2452 case VIDIOC_S_PARM:
2453 {
2454 struct v4l2_streamparm sp;
2455 2555
2456 if (copy_from_user(&sp, arg, sizeof(sp))) 2556 return 0;
2457 return -EFAULT; 2557}
2458 2558
2459 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2460 return -EINVAL;
2461 2559
2462 sp.parm.capture.extendedmode = 0; 2560static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2561 unsigned int cmd, void __user * arg)
2562{
2563 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2564
2565 switch (cmd) {
2566
2567 case VIDIOC_QUERYCAP:
2568 return sn9c102_vidioc_querycap(cam, arg);
2463 2569
2464 if (sp.parm.capture.readbuffers == 0) 2570 case VIDIOC_ENUMINPUT:
2465 sp.parm.capture.readbuffers = cam->nreadbuffers; 2571 return sn9c102_vidioc_enuminput(cam, arg);
2466 2572
2467 if (sp.parm.capture.readbuffers > SN9C102_MAX_FRAMES) 2573 case VIDIOC_G_INPUT:
2468 sp.parm.capture.readbuffers = SN9C102_MAX_FRAMES; 2574 case VIDIOC_S_INPUT:
2575 return sn9c102_vidioc_gs_input(cam, arg);
2469 2576
2470 if (copy_to_user(arg, &sp, sizeof(sp))) 2577 case VIDIOC_QUERYCTRL:
2471 return -EFAULT; 2578 return sn9c102_vidioc_query_ctrl(cam, arg);
2472 2579
2473 cam->nreadbuffers = sp.parm.capture.readbuffers; 2580 case VIDIOC_G_CTRL:
2581 return sn9c102_vidioc_g_ctrl(cam, arg);
2474 2582
2475 return 0; 2583 case VIDIOC_S_CTRL_OLD:
2476 } 2584 case VIDIOC_S_CTRL:
2585 return sn9c102_vidioc_s_ctrl(cam, arg);
2586
2587 case VIDIOC_CROPCAP_OLD:
2588 case VIDIOC_CROPCAP:
2589 return sn9c102_vidioc_cropcap(cam, arg);
2590
2591 case VIDIOC_G_CROP:
2592 return sn9c102_vidioc_g_crop(cam, arg);
2593
2594 case VIDIOC_S_CROP:
2595 return sn9c102_vidioc_s_crop(cam, arg);
2596
2597 case VIDIOC_ENUM_FMT:
2598 return sn9c102_vidioc_enum_fmt(cam, arg);
2599
2600 case VIDIOC_G_FMT:
2601 return sn9c102_vidioc_g_fmt(cam, arg);
2602
2603 case VIDIOC_TRY_FMT:
2604 case VIDIOC_S_FMT:
2605 return sn9c102_vidioc_try_s_fmt(cam, cmd, arg);
2606
2607 case VIDIOC_G_JPEGCOMP:
2608 return sn9c102_vidioc_g_jpegcomp(cam, arg);
2609
2610 case VIDIOC_S_JPEGCOMP:
2611 return sn9c102_vidioc_s_jpegcomp(cam, arg);
2612
2613 case VIDIOC_REQBUFS:
2614 return sn9c102_vidioc_reqbufs(cam, arg);
2615
2616 case VIDIOC_QUERYBUF:
2617 return sn9c102_vidioc_querybuf(cam, arg);
2618
2619 case VIDIOC_QBUF:
2620 return sn9c102_vidioc_qbuf(cam, arg);
2621
2622 case VIDIOC_DQBUF:
2623 return sn9c102_vidioc_dqbuf(cam, filp, arg);
2624
2625 case VIDIOC_STREAMON:
2626 return sn9c102_vidioc_streamon(cam, arg);
2627
2628 case VIDIOC_STREAMOFF:
2629 return sn9c102_vidioc_streamoff(cam, arg);
2630
2631 case VIDIOC_G_PARM:
2632 return sn9c102_vidioc_g_parm(cam, arg);
2633
2634 case VIDIOC_S_PARM_OLD:
2635 case VIDIOC_S_PARM:
2636 return sn9c102_vidioc_s_parm(cam, arg);
2477 2637
2478 case VIDIOC_G_STD: 2638 case VIDIOC_G_STD:
2479 case VIDIOC_S_STD: 2639 case VIDIOC_S_STD:
@@ -2499,17 +2659,20 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2499 return -ERESTARTSYS; 2659 return -ERESTARTSYS;
2500 2660
2501 if (cam->state & DEV_DISCONNECTED) { 2661 if (cam->state & DEV_DISCONNECTED) {
2502 DBG(1, "Device not present") 2662 DBG(1, "Device not present");
2503 up(&cam->fileop_sem); 2663 up(&cam->fileop_sem);
2504 return -ENODEV; 2664 return -ENODEV;
2505 } 2665 }
2506 2666
2507 if (cam->state & DEV_MISCONFIGURED) { 2667 if (cam->state & DEV_MISCONFIGURED) {
2508 DBG(1, "The camera is misconfigured. Close and open it again.") 2668 DBG(1, "The camera is misconfigured. Close and open it "
2669 "again.");
2509 up(&cam->fileop_sem); 2670 up(&cam->fileop_sem);
2510 return -EIO; 2671 return -EIO;
2511 } 2672 }
2512 2673
2674 V4LDBG(3, "sn9c102", cmd);
2675
2513 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); 2676 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2514 2677
2515 up(&cam->fileop_sem); 2678 up(&cam->fileop_sem);
@@ -2517,9 +2680,10 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2517 return err; 2680 return err;
2518} 2681}
2519 2682
2683/*****************************************************************************/
2520 2684
2521static struct file_operations sn9c102_fops = { 2685static struct file_operations sn9c102_fops = {
2522 .owner = THIS_MODULE, 2686 .owner = THIS_MODULE,
2523 .open = sn9c102_open, 2687 .open = sn9c102_open,
2524 .release = sn9c102_release, 2688 .release = sn9c102_release,
2525 .ioctl = sn9c102_ioctl, 2689 .ioctl = sn9c102_ioctl,
@@ -2538,36 +2702,22 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2538 struct usb_device *udev = interface_to_usbdev(intf); 2702 struct usb_device *udev = interface_to_usbdev(intf);
2539 struct sn9c102_device* cam; 2703 struct sn9c102_device* cam;
2540 static unsigned int dev_nr = 0; 2704 static unsigned int dev_nr = 0;
2541 unsigned int i, n; 2705 unsigned int i;
2542 int err = 0, r; 2706 int err = 0, r;
2543 2707
2544 n = ARRAY_SIZE(sn9c102_id_table); 2708 if (!(cam = kzalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2545 for (i = 0; i < n-1; i++)
2546 if (le16_to_cpu(udev->descriptor.idVendor) ==
2547 sn9c102_id_table[i].idVendor &&
2548 le16_to_cpu(udev->descriptor.idProduct) ==
2549 sn9c102_id_table[i].idProduct)
2550 break;
2551 if (i == n-1)
2552 return -ENODEV;
2553
2554 if (!(cam = kmalloc(sizeof(struct sn9c102_device), GFP_KERNEL)))
2555 return -ENOMEM; 2709 return -ENOMEM;
2556 memset(cam, 0, sizeof(*cam));
2557 2710
2558 cam->usbdev = udev; 2711 cam->usbdev = udev;
2559 2712
2560 memcpy(&cam->dev, &udev->dev, sizeof(struct device)); 2713 if (!(cam->control_buffer = kzalloc(8, GFP_KERNEL))) {
2561 2714 DBG(1, "kmalloc() failed");
2562 if (!(cam->control_buffer = kmalloc(8, GFP_KERNEL))) {
2563 DBG(1, "kmalloc() failed")
2564 err = -ENOMEM; 2715 err = -ENOMEM;
2565 goto fail; 2716 goto fail;
2566 } 2717 }
2567 memset(cam->control_buffer, 0, 8);
2568 2718
2569 if (!(cam->v4ldev = video_device_alloc())) { 2719 if (!(cam->v4ldev = video_device_alloc())) {
2570 DBG(1, "video_device_alloc() failed") 2720 DBG(1, "video_device_alloc() failed");
2571 err = -ENOMEM; 2721 err = -ENOMEM;
2572 goto fail; 2722 goto fail;
2573 } 2723 }
@@ -2577,25 +2727,22 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2577 r = sn9c102_read_reg(cam, 0x00); 2727 r = sn9c102_read_reg(cam, 0x00);
2578 if (r < 0 || r != 0x10) { 2728 if (r < 0 || r != 0x10) {
2579 DBG(1, "Sorry, this is not a SN9C10x based camera " 2729 DBG(1, "Sorry, this is not a SN9C10x based camera "
2580 "(vid/pid 0x%04X/0x%04X)", 2730 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2581 sn9c102_id_table[i].idVendor,sn9c102_id_table[i].idProduct)
2582 err = -ENODEV; 2731 err = -ENODEV;
2583 goto fail; 2732 goto fail;
2584 } 2733 }
2585 2734
2586 cam->bridge = (sn9c102_id_table[i].idProduct & 0xffc0) == 0x6080 ? 2735 cam->bridge = (id->idProduct & 0xffc0) == 0x6080 ?
2587 BRIDGE_SN9C103 : BRIDGE_SN9C102; 2736 BRIDGE_SN9C103 : BRIDGE_SN9C102;
2588 switch (cam->bridge) { 2737 switch (cam->bridge) {
2589 case BRIDGE_SN9C101: 2738 case BRIDGE_SN9C101:
2590 case BRIDGE_SN9C102: 2739 case BRIDGE_SN9C102:
2591 DBG(2, "SN9C10[12] PC Camera Controller detected " 2740 DBG(2, "SN9C10[12] PC Camera Controller detected "
2592 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor, 2741 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2593 sn9c102_id_table[i].idProduct)
2594 break; 2742 break;
2595 case BRIDGE_SN9C103: 2743 case BRIDGE_SN9C103:
2596 DBG(2, "SN9C103 PC Camera Controller detected " 2744 DBG(2, "SN9C103 PC Camera Controller detected "
2597 "(vid/pid 0x%04X/0x%04X)", sn9c102_id_table[i].idVendor, 2745 "(vid/pid 0x%04X/0x%04X)", id->idVendor, id->idProduct);
2598 sn9c102_id_table[i].idProduct)
2599 break; 2746 break;
2600 } 2747 }
2601 2748
@@ -2606,24 +2753,24 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2606 } 2753 }
2607 2754
2608 if (!err && cam->sensor) { 2755 if (!err && cam->sensor) {
2609 DBG(2, "%s image sensor detected", cam->sensor->name) 2756 DBG(2, "%s image sensor detected", cam->sensor->name);
2610 DBG(3, "Support for %s maintained by %s", 2757 DBG(3, "Support for %s maintained by %s",
2611 cam->sensor->name, cam->sensor->maintainer) 2758 cam->sensor->name, cam->sensor->maintainer);
2612 } else { 2759 } else {
2613 DBG(1, "No supported image sensor detected") 2760 DBG(1, "No supported image sensor detected");
2614 err = -ENODEV; 2761 err = -ENODEV;
2615 goto fail; 2762 goto fail;
2616 } 2763 }
2617 2764
2618 if (sn9c102_init(cam)) { 2765 if (sn9c102_init(cam)) {
2619 DBG(1, "Initialization failed. I will retry on open().") 2766 DBG(1, "Initialization failed. I will retry on open().");
2620 cam->state |= DEV_MISCONFIGURED; 2767 cam->state |= DEV_MISCONFIGURED;
2621 } 2768 }
2622 2769
2623 strcpy(cam->v4ldev->name, "SN9C10x PC Camera"); 2770 strcpy(cam->v4ldev->name, "SN9C10x PC Camera");
2624 cam->v4ldev->owner = THIS_MODULE; 2771 cam->v4ldev->owner = THIS_MODULE;
2625 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES; 2772 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
2626 cam->v4ldev->hardware = VID_HARDWARE_SN9C102; 2773 cam->v4ldev->hardware = 0;
2627 cam->v4ldev->fops = &sn9c102_fops; 2774 cam->v4ldev->fops = &sn9c102_fops;
2628 cam->v4ldev->minor = video_nr[dev_nr]; 2775 cam->v4ldev->minor = video_nr[dev_nr];
2629 cam->v4ldev->release = video_device_release; 2776 cam->v4ldev->release = video_device_release;
@@ -2634,23 +2781,25 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2634 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, 2781 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2635 video_nr[dev_nr]); 2782 video_nr[dev_nr]);
2636 if (err) { 2783 if (err) {
2637 DBG(1, "V4L2 device registration failed") 2784 DBG(1, "V4L2 device registration failed");
2638 if (err == -ENFILE && video_nr[dev_nr] == -1) 2785 if (err == -ENFILE && video_nr[dev_nr] == -1)
2639 DBG(1, "Free /dev/videoX node not found") 2786 DBG(1, "Free /dev/videoX node not found");
2640 video_nr[dev_nr] = -1; 2787 video_nr[dev_nr] = -1;
2641 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2788 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2642 up(&cam->dev_sem); 2789 up(&cam->dev_sem);
2643 goto fail; 2790 goto fail;
2644 } 2791 }
2645 2792
2646 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor) 2793 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2647 2794
2648 cam->module_param.force_munmap = force_munmap[dev_nr]; 2795 cam->module_param.force_munmap = force_munmap[dev_nr];
2649 2796
2650 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2797 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2651 2798
2799#ifdef CONFIG_VIDEO_ADV_DEBUG
2652 sn9c102_create_sysfs(cam); 2800 sn9c102_create_sysfs(cam);
2653 DBG(2, "Optional device control through 'sysfs' interface ready") 2801 DBG(2, "Optional device control through 'sysfs' interface ready");
2802#endif
2654 2803
2655 usb_set_intfdata(intf, cam); 2804 usb_set_intfdata(intf, cam);
2656 2805
@@ -2680,14 +2829,14 @@ static void sn9c102_usb_disconnect(struct usb_interface* intf)
2680 2829
2681 down(&cam->dev_sem); 2830 down(&cam->dev_sem);
2682 2831
2683 DBG(2, "Disconnecting %s...", cam->v4ldev->name) 2832 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2684 2833
2685 wake_up_interruptible_all(&cam->open); 2834 wake_up_interruptible_all(&cam->open);
2686 2835
2687 if (cam->users) { 2836 if (cam->users) {
2688 DBG(2, "Device /dev/video%d is open! Deregistration and " 2837 DBG(2, "Device /dev/video%d is open! Deregistration and "
2689 "memory deallocation are deferred on close.", 2838 "memory deallocation are deferred on close.",
2690 cam->v4ldev->minor) 2839 cam->v4ldev->minor);
2691 cam->state |= DEV_MISCONFIGURED; 2840 cam->state |= DEV_MISCONFIGURED;
2692 sn9c102_stop_transfer(cam); 2841 sn9c102_stop_transfer(cam);
2693 cam->state |= DEV_DISCONNECTED; 2842 cam->state |= DEV_DISCONNECTED;
@@ -2720,11 +2869,11 @@ static int __init sn9c102_module_init(void)
2720{ 2869{
2721 int err = 0; 2870 int err = 0;
2722 2871
2723 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION) 2872 KDBG(2, SN9C102_MODULE_NAME " v" SN9C102_MODULE_VERSION);
2724 KDBG(3, SN9C102_MODULE_AUTHOR) 2873 KDBG(3, SN9C102_MODULE_AUTHOR);
2725 2874
2726 if ((err = usb_register(&sn9c102_usb_driver))) 2875 if ((err = usb_register(&sn9c102_usb_driver)))
2727 KDBG(1, "usb_register() failed") 2876 KDBG(1, "usb_register() failed");
2728 2877
2729 return err; 2878 return err;
2730} 2879}
diff --git a/drivers/usb/media/sn9c102_hv7131d.c b/drivers/usb/media/sn9c102_hv7131d.c
index 18070d5333cf..46c12ec3ca62 100644
--- a/drivers/usb/media/sn9c102_hv7131d.c
+++ b/drivers/usb/media/sn9c102_hv7131d.c
@@ -2,7 +2,7 @@
2 * Plug-in for HV7131D image sensor connected to the SN9C10x PC Camera * 2 * Plug-in for HV7131D image sensor connected to the SN9C10x PC Camera *
3 * Controllers * 3 * Controllers *
4 * * 4 * *
5 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
diff --git a/drivers/usb/media/sn9c102_mi0343.c b/drivers/usb/media/sn9c102_mi0343.c
index 86676abf3547..d9aa7a61095d 100644
--- a/drivers/usb/media/sn9c102_mi0343.c
+++ b/drivers/usb/media/sn9c102_mi0343.c
@@ -2,7 +2,7 @@
2 * Plug-in for MI-0343 image sensor connected to the SN9C10x PC Camera * 2 * Plug-in for MI-0343 image sensor connected to the SN9C10x PC Camera *
3 * Controllers * 3 * Controllers *
4 * * 4 * *
5 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
diff --git a/drivers/usb/media/sn9c102_ov7630.c b/drivers/usb/media/sn9c102_ov7630.c
index d27c5aedeaf8..4a36519b5af4 100644
--- a/drivers/usb/media/sn9c102_ov7630.c
+++ b/drivers/usb/media/sn9c102_ov7630.c
@@ -2,7 +2,7 @@
2 * Plug-in for OV7630 image sensor connected to the SN9C10x PC Camera * 2 * Plug-in for OV7630 image sensor connected to the SN9C10x PC Camera *
3 * Controllers * 3 * Controllers *
4 * * 4 * *
5 * Copyright (C) 2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 5 * Copyright (C) 2005-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
@@ -375,8 +375,10 @@ int sn9c102_probe_ov7630(struct sn9c102_device* cam)
375 375
376 sn9c102_attach_sensor(cam, &ov7630); 376 sn9c102_attach_sensor(cam, &ov7630);
377 377
378 if (le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x608f && 378 if (le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602c &&
379 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602c) 379 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602d &&
380 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x608f &&
381 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x60b0)
380 return -ENODEV; 382 return -ENODEV;
381 383
382 err += sn9c102_write_reg(cam, 0x01, 0x01); 384 err += sn9c102_write_reg(cam, 0x01, 0x01);
diff --git a/drivers/usb/media/sn9c102_pas106b.c b/drivers/usb/media/sn9c102_pas106b.c
index 48e3ec39d4e2..b1dee78abe04 100644
--- a/drivers/usb/media/sn9c102_pas106b.c
+++ b/drivers/usb/media/sn9c102_pas106b.c
@@ -2,7 +2,7 @@
2 * Plug-in for PAS106B image sensor connected to the SN9C10x PC Camera * 2 * Plug-in for PAS106B image sensor connected to the SN9C10x PC Camera *
3 * Controllers * 3 * Controllers *
4 * * 4 * *
5 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
diff --git a/drivers/usb/media/sn9c102_sensor.h b/drivers/usb/media/sn9c102_sensor.h
index a45166c3488c..7d953b24f2f2 100644
--- a/drivers/usb/media/sn9c102_sensor.h
+++ b/drivers/usb/media/sn9c102_sensor.h
@@ -1,7 +1,7 @@
1/*************************************************************************** 1/***************************************************************************
2 * API for image sensors connected to the SN9C10x PC Camera Controllers * 2 * API for image sensors connected to the SN9C10x PC Camera Controllers *
3 * * 3 * *
4 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 4 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * * 5 * *
6 * This program is free software; you can redistribute it and/or modify * 6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by * 7 * it under the terms of the GNU General Public License as published by *
@@ -92,7 +92,18 @@ extern void
92sn9c102_attach_sensor(struct sn9c102_device* cam, 92sn9c102_attach_sensor(struct sn9c102_device* cam,
93 struct sn9c102_sensor* sensor); 93 struct sn9c102_sensor* sensor);
94 94
95/* Each SN9C10X camera has proper PID/VID identifiers. Add them here in case.*/ 95/*
96 Each SN9C10x camera has proper PID/VID identifiers.
97 SN9C103 supports multiple interfaces, but we only handle the video class
98 interface.
99*/
100#define SN9C102_USB_DEVICE(vend, prod, intclass) \
101 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
102 USB_DEVICE_ID_MATCH_INT_CLASS, \
103 .idVendor = (vend), \
104 .idProduct = (prod), \
105 .bInterfaceClass = (intclass)
106
96#define SN9C102_ID_TABLE \ 107#define SN9C102_ID_TABLE \
97static const struct usb_device_id sn9c102_id_table[] = { \ 108static const struct usb_device_id sn9c102_id_table[] = { \
98 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \ 109 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \
@@ -107,33 +118,34 @@ static const struct usb_device_id sn9c102_id_table[] = { \
107 { USB_DEVICE(0x0c45, 0x602b), }, /* MI-0343 */ \ 118 { USB_DEVICE(0x0c45, 0x602b), }, /* MI-0343 */ \
108 { USB_DEVICE(0x0c45, 0x602c), }, /* OV7630 */ \ 119 { USB_DEVICE(0x0c45, 0x602c), }, /* OV7630 */ \
109 { USB_DEVICE(0x0c45, 0x602d), }, \ 120 { USB_DEVICE(0x0c45, 0x602d), }, \
121 { USB_DEVICE(0x0c45, 0x602e), }, /* OV7630 */ \
110 { USB_DEVICE(0x0c45, 0x6030), }, /* MI03x */ \ 122 { USB_DEVICE(0x0c45, 0x6030), }, /* MI03x */ \
111 { USB_DEVICE(0x0c45, 0x6080), }, \ 123 { SN9C102_USB_DEVICE(0x0c45, 0x6080, 0xff), }, \
112 { USB_DEVICE(0x0c45, 0x6082), }, /* MI0343 and MI0360 */ \ 124 { SN9C102_USB_DEVICE(0x0c45, 0x6082, 0xff), }, /* MI0343 & MI0360 */ \
113 { USB_DEVICE(0x0c45, 0x6083), }, /* HV7131[D|E1] */ \ 125 { SN9C102_USB_DEVICE(0x0c45, 0x6083, 0xff), }, /* HV7131[D|E1] */ \
114 { USB_DEVICE(0x0c45, 0x6088), }, \ 126 { SN9C102_USB_DEVICE(0x0c45, 0x6088, 0xff), }, \
115 { USB_DEVICE(0x0c45, 0x608a), }, \ 127 { SN9C102_USB_DEVICE(0x0c45, 0x608a, 0xff), }, \
116 { USB_DEVICE(0x0c45, 0x608b), }, \ 128 { SN9C102_USB_DEVICE(0x0c45, 0x608b, 0xff), }, \
117 { USB_DEVICE(0x0c45, 0x608c), }, /* HV7131x */ \ 129 { SN9C102_USB_DEVICE(0x0c45, 0x608c, 0xff), }, /* HV7131x */ \
118 { USB_DEVICE(0x0c45, 0x608e), }, /* CIS-VF10 */ \ 130 { SN9C102_USB_DEVICE(0x0c45, 0x608e, 0xff), }, /* CIS-VF10 */ \
119 { USB_DEVICE(0x0c45, 0x608f), }, /* OV7630 */ \ 131 { SN9C102_USB_DEVICE(0x0c45, 0x608f, 0xff), }, /* OV7630 */ \
120 { USB_DEVICE(0x0c45, 0x60a0), }, \ 132 { SN9C102_USB_DEVICE(0x0c45, 0x60a0, 0xff), }, \
121 { USB_DEVICE(0x0c45, 0x60a2), }, \ 133 { SN9C102_USB_DEVICE(0x0c45, 0x60a2, 0xff), }, \
122 { USB_DEVICE(0x0c45, 0x60a3), }, \ 134 { SN9C102_USB_DEVICE(0x0c45, 0x60a3, 0xff), }, \
123 { USB_DEVICE(0x0c45, 0x60a8), }, /* PAS106B */ \ 135 { SN9C102_USB_DEVICE(0x0c45, 0x60a8, 0xff), }, /* PAS106B */ \
124 { USB_DEVICE(0x0c45, 0x60aa), }, /* TAS5130D1B */ \ 136 { SN9C102_USB_DEVICE(0x0c45, 0x60aa, 0xff), }, /* TAS5130D1B */ \
125 { USB_DEVICE(0x0c45, 0x60ab), }, /* TAS5110C1B */ \ 137 { SN9C102_USB_DEVICE(0x0c45, 0x60ab, 0xff), }, /* TAS5110C1B */ \
126 { USB_DEVICE(0x0c45, 0x60ac), }, \ 138 { SN9C102_USB_DEVICE(0x0c45, 0x60ac, 0xff), }, \
127 { USB_DEVICE(0x0c45, 0x60ae), }, \ 139 { SN9C102_USB_DEVICE(0x0c45, 0x60ae, 0xff), }, \
128 { USB_DEVICE(0x0c45, 0x60af), }, /* PAS202BCB */ \ 140 { SN9C102_USB_DEVICE(0x0c45, 0x60af, 0xff), }, /* PAS202BCB */ \
129 { USB_DEVICE(0x0c45, 0x60b0), }, \ 141 { SN9C102_USB_DEVICE(0x0c45, 0x60b0, 0xff), }, /* OV7630 (?) */ \
130 { USB_DEVICE(0x0c45, 0x60b2), }, \ 142 { SN9C102_USB_DEVICE(0x0c45, 0x60b2, 0xff), }, \
131 { USB_DEVICE(0x0c45, 0x60b3), }, \ 143 { SN9C102_USB_DEVICE(0x0c45, 0x60b3, 0xff), }, \
132 { USB_DEVICE(0x0c45, 0x60b8), }, \ 144 { SN9C102_USB_DEVICE(0x0c45, 0x60b8, 0xff), }, \
133 { USB_DEVICE(0x0c45, 0x60ba), }, \ 145 { SN9C102_USB_DEVICE(0x0c45, 0x60ba, 0xff), }, \
134 { USB_DEVICE(0x0c45, 0x60bb), }, \ 146 { SN9C102_USB_DEVICE(0x0c45, 0x60bb, 0xff), }, \
135 { USB_DEVICE(0x0c45, 0x60bc), }, \ 147 { SN9C102_USB_DEVICE(0x0c45, 0x60bc, 0xff), }, \
136 { USB_DEVICE(0x0c45, 0x60be), }, \ 148 { SN9C102_USB_DEVICE(0x0c45, 0x60be, 0xff), }, \
137 { } \ 149 { } \
138}; 150};
139 151
@@ -177,16 +189,18 @@ extern int sn9c102_i2c_write(struct sn9c102_device*, u8 address, u8 value);
177extern int sn9c102_i2c_read(struct sn9c102_device*, u8 address); 189extern int sn9c102_i2c_read(struct sn9c102_device*, u8 address);
178 190
179/* I/O on registers in the bridge. Could be used by the sensor methods too */ 191/* I/O on registers in the bridge. Could be used by the sensor methods too */
192extern int sn9c102_write_regs(struct sn9c102_device*, u8* buff, u16 index);
180extern int sn9c102_write_reg(struct sn9c102_device*, u8 value, u16 index); 193extern int sn9c102_write_reg(struct sn9c102_device*, u8 value, u16 index);
181extern int sn9c102_pread_reg(struct sn9c102_device*, u16 index); 194extern int sn9c102_pread_reg(struct sn9c102_device*, u16 index);
182 195
183/* 196/*
184 NOTE: there are no exported debugging functions. To uniform the output you 197 NOTE: there are no exported debugging functions. To uniform the output you
185 must use the dev_info()/dev_warn()/dev_err() macros defined in device.h, 198 must use the dev_info()/dev_warn()/dev_err() macros defined in device.h,
186 already included here, the argument being the struct device 'dev' of the 199 already included here, the argument being the struct device '&usbdev->dev'
187 sensor structure. Do NOT use these macros before the sensor is attached or 200 of the sensor structure. Do NOT use these macros before the sensor is
188 the kernel will crash! However, you should not need to notify the user about 201 attached or the kernel will crash! However, you should not need to notify
189 common errors or other messages, since this is done by the master module. 202 the user about common errors or other messages, since this is done by the
203 master module.
190*/ 204*/
191 205
192/*****************************************************************************/ 206/*****************************************************************************/
@@ -345,13 +359,6 @@ struct sn9c102_sensor {
345 error code without rolling back. 359 error code without rolling back.
346 */ 360 */
347 361
348 const struct device* dev;
349 /*
350 This is the argument for dev_err(), dev_info() and dev_warn(). It
351 is used for debugging purposes. You must not access the struct
352 before the sensor is attached.
353 */
354
355 const struct usb_device* usbdev; 362 const struct usb_device* usbdev;
356 /* 363 /*
357 Points to the usb_device struct after the sensor is attached. 364 Points to the usb_device struct after the sensor is attached.
diff --git a/drivers/usb/media/sn9c102_tas5110c1b.c b/drivers/usb/media/sn9c102_tas5110c1b.c
index 8775999b5aff..32ddf236cafe 100644
--- a/drivers/usb/media/sn9c102_tas5110c1b.c
+++ b/drivers/usb/media/sn9c102_tas5110c1b.c
@@ -2,7 +2,7 @@
2 * Plug-in for TAS5110C1B image sensor connected to the SN9C10x PC Camera * 2 * Plug-in for TAS5110C1B image sensor connected to the SN9C10x PC Camera *
3 * Controllers * 3 * Controllers *
4 * * 4 * *
5 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
diff --git a/drivers/usb/media/sn9c102_tas5130d1b.c b/drivers/usb/media/sn9c102_tas5130d1b.c
index 927eafdd8c73..a0728f0ae00c 100644
--- a/drivers/usb/media/sn9c102_tas5130d1b.c
+++ b/drivers/usb/media/sn9c102_tas5130d1b.c
@@ -2,7 +2,7 @@
2 * Plug-in for TAS5130D1B image sensor connected to the SN9C10x PC Camera * 2 * Plug-in for TAS5130D1B image sensor connected to the SN9C10x PC Camera *
3 * Controllers * 3 * Controllers *
4 * * 4 * *
5 * Copyright (C) 2004-2005 by Luca Risolia <luca.risolia@studio.unibo.it> * 5 * Copyright (C) 2004-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
diff --git a/drivers/usb/media/w9968cf.c b/drivers/usb/media/w9968cf.c
index bff9434c8e55..9937fc64c8bf 100644
--- a/drivers/usb/media/w9968cf.c
+++ b/drivers/usb/media/w9968cf.c
@@ -62,7 +62,6 @@ MODULE_LICENSE(W9968CF_MODULE_LICENSE);
62MODULE_SUPPORTED_DEVICE("Video"); 62MODULE_SUPPORTED_DEVICE("Video");
63 63
64static int ovmod_load = W9968CF_OVMOD_LOAD; 64static int ovmod_load = W9968CF_OVMOD_LOAD;
65static int vppmod_load = W9968CF_VPPMOD_LOAD;
66static unsigned short simcams = W9968CF_SIMCAMS; 65static unsigned short simcams = W9968CF_SIMCAMS;
67static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/ 66static short video_nr[]={[0 ... W9968CF_MAX_DEVICES-1] = -1}; /*-1=first free*/
68static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] = 67static unsigned int packet_size[] = {[0 ... W9968CF_MAX_DEVICES-1] =
@@ -107,7 +106,6 @@ static unsigned int param_nv[24]; /* number of values per parameter */
107 106
108#ifdef CONFIG_KMOD 107#ifdef CONFIG_KMOD
109module_param(ovmod_load, bool, 0644); 108module_param(ovmod_load, bool, 0644);
110module_param(vppmod_load, bool, 0444);
111#endif 109#endif
112module_param(simcams, ushort, 0644); 110module_param(simcams, ushort, 0644);
113module_param_array(video_nr, short, &param_nv[0], 0444); 111module_param_array(video_nr, short, &param_nv[0], 0444);
@@ -150,18 +148,6 @@ MODULE_PARM_DESC(ovmod_load,
150 "\ninto memory." 148 "\ninto memory."
151 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"." 149 "\nDefault value is "__MODULE_STRING(W9968CF_OVMOD_LOAD)"."
152 "\n"); 150 "\n");
153MODULE_PARM_DESC(vppmod_load,
154 "\n<0|1> Automatic 'w9968cf-vpp' module loading."
155 "\n0 disabled, 1 enabled."
156 "\nIf enabled, every time an application attempts to open a"
157 "\ncamera, 'insmod' searches for the video post-processing"
158 "\nmodule in the system and loads it automatically (if"
159 "\npresent). The optional 'w9968cf-vpp' module adds extra"
160 "\n image manipulation functions to the 'w9968cf' module,like"
161 "\nsoftware up-scaling,colour conversions and video decoding"
162 "\nfor very high frame rates."
163 "\nDefault value is "__MODULE_STRING(W9968CF_VPPMOD_LOAD)"."
164 "\n");
165#endif 151#endif
166MODULE_PARM_DESC(simcams, 152MODULE_PARM_DESC(simcams,
167 "\n<n> Number of cameras allowed to stream simultaneously." 153 "\n<n> Number of cameras allowed to stream simultaneously."
@@ -492,10 +478,6 @@ static void w9968cf_push_frame(struct w9968cf_device*, u8 f_num);
492static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**); 478static void w9968cf_pop_frame(struct w9968cf_device*,struct w9968cf_frame_t**);
493static void w9968cf_release_resources(struct w9968cf_device*); 479static void w9968cf_release_resources(struct w9968cf_device*);
494 480
495/* Intermodule communication */
496static int w9968cf_vppmod_detect(struct w9968cf_device*);
497static void w9968cf_vppmod_release(struct w9968cf_device*);
498
499 481
500 482
501/**************************************************************************** 483/****************************************************************************
@@ -2737,9 +2719,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2737 cam->streaming = 0; 2719 cam->streaming = 0;
2738 cam->misconfigured = 0; 2720 cam->misconfigured = 0;
2739 2721
2740 if (!w9968cf_vpp) 2722 w9968cf_adjust_configuration(cam);
2741 if ((err = w9968cf_vppmod_detect(cam)))
2742 goto out;
2743 2723
2744 if ((err = w9968cf_allocate_memory(cam))) 2724 if ((err = w9968cf_allocate_memory(cam)))
2745 goto deallocate_memory; 2725 goto deallocate_memory;
@@ -2766,7 +2746,6 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2766 2746
2767deallocate_memory: 2747deallocate_memory:
2768 w9968cf_deallocate_memory(cam); 2748 w9968cf_deallocate_memory(cam);
2769out:
2770 DBG(2, "Failed to open the video device") 2749 DBG(2, "Failed to open the video device")
2771 up(&cam->dev_sem); 2750 up(&cam->dev_sem);
2772 up_read(&w9968cf_disconnect); 2751 up_read(&w9968cf_disconnect);
@@ -2784,8 +2763,6 @@ static int w9968cf_release(struct inode* inode, struct file* filp)
2784 2763
2785 w9968cf_stop_transfer(cam); 2764 w9968cf_stop_transfer(cam);
2786 2765
2787 w9968cf_vppmod_release(cam);
2788
2789 if (cam->disconnected) { 2766 if (cam->disconnected) {
2790 w9968cf_release_resources(cam); 2767 w9968cf_release_resources(cam);
2791 up(&cam->dev_sem); 2768 up(&cam->dev_sem);
@@ -3681,106 +3658,6 @@ static struct usb_driver w9968cf_usb_driver = {
3681 * Module init, exit and intermodule communication * 3658 * Module init, exit and intermodule communication *
3682 ****************************************************************************/ 3659 ****************************************************************************/
3683 3660
3684static int w9968cf_vppmod_detect(struct w9968cf_device* cam)
3685{
3686 if (!w9968cf_vpp)
3687 if (vppmod_load)
3688 request_module("w9968cf-vpp");
3689
3690 down(&w9968cf_vppmod_lock);
3691
3692 if (!w9968cf_vpp) {
3693 DBG(4, "Video post-processing module not detected")
3694 w9968cf_adjust_configuration(cam);
3695 goto out;
3696 }
3697
3698 if (!try_module_get(w9968cf_vpp->owner)) {
3699 DBG(1, "Couldn't increment the reference count of "
3700 "the video post-processing module")
3701 up(&w9968cf_vppmod_lock);
3702 return -ENOSYS;
3703 }
3704
3705 w9968cf_vpp->busy++;
3706
3707 DBG(5, "Video post-processing module detected")
3708
3709out:
3710 up(&w9968cf_vppmod_lock);
3711 return 0;
3712}
3713
3714
3715static void w9968cf_vppmod_release(struct w9968cf_device* cam)
3716{
3717 down(&w9968cf_vppmod_lock);
3718
3719 if (w9968cf_vpp && w9968cf_vpp->busy) {
3720 module_put(w9968cf_vpp->owner);
3721 w9968cf_vpp->busy--;
3722 wake_up(&w9968cf_vppmod_wait);
3723 DBG(5, "Video post-processing module released")
3724 }
3725
3726 up(&w9968cf_vppmod_lock);
3727}
3728
3729
3730int w9968cf_vppmod_register(struct w9968cf_vpp_t* vpp)
3731{
3732 down(&w9968cf_vppmod_lock);
3733
3734 if (w9968cf_vpp) {
3735 KDBG(1, "Video post-processing module already registered")
3736 up(&w9968cf_vppmod_lock);
3737 return -EINVAL;
3738 }
3739
3740 w9968cf_vpp = vpp;
3741 w9968cf_vpp->busy = 0;
3742
3743 KDBG(2, "Video post-processing module registered")
3744 up(&w9968cf_vppmod_lock);
3745 return 0;
3746}
3747
3748
3749int w9968cf_vppmod_deregister(struct w9968cf_vpp_t* vpp)
3750{
3751 down(&w9968cf_vppmod_lock);
3752
3753 if (!w9968cf_vpp) {
3754 up(&w9968cf_vppmod_lock);
3755 return -EINVAL;
3756 }
3757
3758 if (w9968cf_vpp != vpp) {
3759 KDBG(1, "Only the owner can unregister the video "
3760 "post-processing module")
3761 up(&w9968cf_vppmod_lock);
3762 return -EINVAL;
3763 }
3764
3765 if (w9968cf_vpp->busy) {
3766 KDBG(2, "Video post-processing module busy. Wait for it to be "
3767 "released...")
3768 up(&w9968cf_vppmod_lock);
3769 wait_event(w9968cf_vppmod_wait, !w9968cf_vpp->busy);
3770 w9968cf_vpp = NULL;
3771 goto out;
3772 }
3773
3774 w9968cf_vpp = NULL;
3775
3776 up(&w9968cf_vppmod_lock);
3777
3778out:
3779 KDBG(2, "Video post-processing module unregistered")
3780 return 0;
3781}
3782
3783
3784static int __init w9968cf_module_init(void) 3661static int __init w9968cf_module_init(void)
3785{ 3662{
3786 int err; 3663 int err;
@@ -3810,6 +3687,3 @@ static void __exit w9968cf_module_exit(void)
3810module_init(w9968cf_module_init); 3687module_init(w9968cf_module_init);
3811module_exit(w9968cf_module_exit); 3688module_exit(w9968cf_module_exit);
3812 3689
3813
3814EXPORT_SYMBOL(w9968cf_vppmod_register);
3815EXPORT_SYMBOL(w9968cf_vppmod_deregister);
diff --git a/drivers/usb/media/w9968cf.h b/drivers/usb/media/w9968cf.h
index 8acbfe205bc7..47a6ff794171 100644
--- a/drivers/usb/media/w9968cf.h
+++ b/drivers/usb/media/w9968cf.h
@@ -195,7 +195,6 @@ enum w9968cf_vpp_flag {
195}; 195};
196 196
197static struct w9968cf_vpp_t* w9968cf_vpp; 197static struct w9968cf_vpp_t* w9968cf_vpp;
198static DECLARE_MUTEX(w9968cf_vppmod_lock);
199static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait); 198static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
200 199
201static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */ 200static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
diff --git a/drivers/usb/media/w9968cf_vpp.h b/drivers/usb/media/w9968cf_vpp.h
index 3f5317dc4c29..f3b91b782671 100644
--- a/drivers/usb/media/w9968cf_vpp.h
+++ b/drivers/usb/media/w9968cf_vpp.h
@@ -37,7 +37,4 @@ struct w9968cf_vpp_t {
37 u8 busy; /* read-only flag: module is/is not in use */ 37 u8 busy; /* read-only flag: module is/is not in use */
38}; 38};
39 39
40extern int w9968cf_vppmod_register(struct w9968cf_vpp_t*);
41extern int w9968cf_vppmod_deregister(struct w9968cf_vpp_t*);
42
43#endif /* _W9968CF_VPP_H_ */ 40#endif /* _W9968CF_VPP_H_ */
diff --git a/drivers/usb/misc/auerswald.c b/drivers/usb/misc/auerswald.c
index 449b2501acf3..ad2f4cccd388 100644
--- a/drivers/usb/misc/auerswald.c
+++ b/drivers/usb/misc/auerswald.c
@@ -2093,6 +2093,8 @@ static void auerswald_disconnect (struct usb_interface *intf)
2093static struct usb_device_id auerswald_ids [] = { 2093static struct usb_device_id auerswald_ids [] = {
2094 { USB_DEVICE (ID_AUERSWALD, 0x00C0) }, /* COMpact 2104 USB */ 2094 { USB_DEVICE (ID_AUERSWALD, 0x00C0) }, /* COMpact 2104 USB */
2095 { USB_DEVICE (ID_AUERSWALD, 0x00DB) }, /* COMpact 4410/2206 USB */ 2095 { USB_DEVICE (ID_AUERSWALD, 0x00DB) }, /* COMpact 4410/2206 USB */
2096 { USB_DEVICE (ID_AUERSWALD, 0x00DC) }, /* COMpact 4406 DSL */
2097 { USB_DEVICE (ID_AUERSWALD, 0x00DD) }, /* COMpact 2204 USB */
2096 { USB_DEVICE (ID_AUERSWALD, 0x00F1) }, /* Comfort 2000 System Telephone */ 2098 { USB_DEVICE (ID_AUERSWALD, 0x00F1) }, /* Comfort 2000 System Telephone */
2097 { USB_DEVICE (ID_AUERSWALD, 0x00F2) }, /* Comfort 1200 System Telephone */ 2099 { USB_DEVICE (ID_AUERSWALD, 0x00F2) }, /* Comfort 1200 System Telephone */
2098 { } /* Terminating entry */ 2100 { } /* Terminating entry */
diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c
index 981d8a5fbfd9..331d4ae949ed 100644
--- a/drivers/usb/misc/ldusb.c
+++ b/drivers/usb/misc/ldusb.c
@@ -593,7 +593,7 @@ static struct file_operations ld_usb_fops = {
593 593
594/* 594/*
595 * usb class driver info in order to get a minor number from the usb core, 595 * usb class driver info in order to get a minor number from the usb core,
596 * and to have the device registered with devfs and the driver core 596 * and to have the device registered with the driver core
597 */ 597 */
598static struct usb_class_driver ld_usb_class = { 598static struct usb_class_driver ld_usb_class = {
599 .name = "ldusb%d", 599 .name = "ldusb%d",
diff --git a/drivers/usb/net/asix.c b/drivers/usb/net/asix.c
index 541181695040..3094970615cb 100644
--- a/drivers/usb/net/asix.c
+++ b/drivers/usb/net/asix.c
@@ -916,6 +916,10 @@ static const struct usb_device_id products [] = {
916 // Linksys USB200M Rev 2 916 // Linksys USB200M Rev 2
917 USB_DEVICE (0x13b1, 0x0018), 917 USB_DEVICE (0x13b1, 0x0018),
918 .driver_info = (unsigned long) &ax88772_info, 918 .driver_info = (unsigned long) &ax88772_info,
919}, {
920 // 0Q0 cable ethernet
921 USB_DEVICE (0x1557, 0x7720),
922 .driver_info = (unsigned long) &ax88772_info,
919}, 923},
920 { }, // END 924 { }, // END
921}; 925};
diff --git a/drivers/usb/serial/cp2101.c b/drivers/usb/serial/cp2101.c
index da46b351e188..dc7a069503e0 100644
--- a/drivers/usb/serial/cp2101.c
+++ b/drivers/usb/serial/cp2101.c
@@ -32,7 +32,7 @@
32/* 32/*
33 * Version Information 33 * Version Information
34 */ 34 */
35#define DRIVER_VERSION "v0.05" 35#define DRIVER_VERSION "v0.06"
36#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver" 36#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
37 37
38/* 38/*
@@ -55,11 +55,15 @@ static int debug;
55 55
56static struct usb_device_id id_table [] = { 56static struct usb_device_id id_table [] = {
57 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */ 57 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
58 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
59 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
60 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
61 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */ 58 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
62 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */ 59 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
60 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
61 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
62 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
63 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
64 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
65 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
66 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
63 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */ 67 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
64 { } /* Terminating Entry */ 68 { } /* Terminating Entry */
65}; 69};
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 10bc1bf23b35..f2b4ca8692d8 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -476,10 +476,16 @@ static struct usb_device_id id_table_combined [] = {
476 { USB_DEVICE(EVOLUTION_VID, EVOLUTION_ER1_PID) }, 476 { USB_DEVICE(EVOLUTION_VID, EVOLUTION_ER1_PID) },
477 { USB_DEVICE(FTDI_VID, FTDI_ARTEMIS_PID) }, 477 { USB_DEVICE(FTDI_VID, FTDI_ARTEMIS_PID) },
478 { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16_PID) }, 478 { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16_PID) },
479 { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16C_PID) },
479 { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16HR_PID) }, 480 { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16HR_PID) },
481 { USB_DEVICE(FTDI_VID, FTDI_ATIK_ATK16HRC_PID) },
480 { USB_DEVICE(KOBIL_VID, KOBIL_CONV_B1_PID) }, 482 { USB_DEVICE(KOBIL_VID, KOBIL_CONV_B1_PID) },
481 { USB_DEVICE(KOBIL_VID, KOBIL_CONV_KAAN_PID) }, 483 { USB_DEVICE(KOBIL_VID, KOBIL_CONV_KAAN_PID) },
482 { USB_DEVICE(POSIFLEX_VID, POSIFLEX_PP7000_PID) }, 484 { USB_DEVICE(POSIFLEX_VID, POSIFLEX_PP7000_PID) },
485 { USB_DEVICE(FTDI_VID, FTDI_TTUSB_PID) },
486 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_777_PID) },
487 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_8900F_PID) },
488 { USB_DEVICE(FTDI_VID, FTDI_PCDJ_DAC2_PID) },
483 { }, /* Optional parameter entry */ 489 { }, /* Optional parameter entry */
484 { } /* Terminating entry */ 490 { } /* Terminating entry */
485}; 491};
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index 00d45f8600de..ca40f16370f1 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -31,9 +31,14 @@
31#define FTDI_NF_RIC_VID 0x0DCD /* Vendor Id */ 31#define FTDI_NF_RIC_VID 0x0DCD /* Vendor Id */
32#define FTDI_NF_RIC_PID 0x0001 /* Product Id */ 32#define FTDI_NF_RIC_PID 0x0001 /* Product Id */
33 33
34
34/* www.irtrans.de device */ 35/* www.irtrans.de device */
35#define FTDI_IRTRANS_PID 0xFC60 /* Product Id */ 36#define FTDI_IRTRANS_PID 0xFC60 /* Product Id */
36 37
38
39/* www.thoughttechnology.com/ TT-USB provide with procomp use ftdi_sio */
40#define FTDI_TTUSB_PID 0xFF20 /* Product Id */
41
37/* www.crystalfontz.com devices - thanx for providing free devices for evaluation ! */ 42/* www.crystalfontz.com devices - thanx for providing free devices for evaluation ! */
38/* they use the ftdi chipset for the USB interface and the vendor id is the same */ 43/* they use the ftdi chipset for the USB interface and the vendor id is the same */
39#define FTDI_XF_632_PID 0xFC08 /* 632: 16x2 Character Display */ 44#define FTDI_XF_632_PID 0xFC08 /* 632: 16x2 Character Display */
@@ -51,6 +56,12 @@
51#define FTDI_VNHCPCUSB_D_PID 0xfe38 /* Product Id */ 56#define FTDI_VNHCPCUSB_D_PID 0xfe38 /* Product Id */
52 57
53/* 58/*
59 * PCDJ use ftdi based dj-controllers. The following PID is for their DAC-2 device
60 * http://www.pcdjhardware.com/DAC2.asp (PID sent by Wouter Paesen)
61 * (the VID is the standard ftdi vid (FTDI_VID) */
62#define FTDI_PCDJ_DAC2_PID 0xFA88
63
64/*
54 * The following are the values for the Matrix Orbital LCD displays, 65 * The following are the values for the Matrix Orbital LCD displays,
55 * which are the FT232BM ( similar to the 8U232AM ) 66 * which are the FT232BM ( similar to the 8U232AM )
56 */ 67 */
@@ -215,8 +226,10 @@
215 * Definitions for ATIK Instruments astronomical USB based cameras 226 * Definitions for ATIK Instruments astronomical USB based cameras
216 * Check it at http://www.atik-instruments.com/ 227 * Check it at http://www.atik-instruments.com/
217 */ 228 */
218#define FTDI_ATIK_ATK16_PID 0xDF30 /* ATIK ATK-16 Camera */ 229#define FTDI_ATIK_ATK16_PID 0xDF30 /* ATIK ATK-16 Grayscale Camera */
219#define FTDI_ATIK_ATK16HR_PID 0xDF31 /* ATIK ATK-16HR Camera */ 230#define FTDI_ATIK_ATK16C_PID 0xDF32 /* ATIK ATK-16C Colour Camera */
231#define FTDI_ATIK_ATK16HR_PID 0xDF31 /* ATIK ATK-16HR Grayscale Camera */
232#define FTDI_ATIK_ATK16HRC_PID 0xDF33 /* ATIK ATK-16HRC Colour Camera */
220 233
221/* 234/*
222 * Protego product ids 235 * Protego product ids
@@ -365,6 +378,12 @@
365#define POSIFLEX_VID 0x0d3a /* Vendor ID */ 378#define POSIFLEX_VID 0x0d3a /* Vendor ID */
366#define POSIFLEX_PP7000_PID 0x0300 /* PP-7000II thermal printer */ 379#define POSIFLEX_PP7000_PID 0x0300 /* PP-7000II thermal printer */
367 380
381/*
382 * Westrex International devices submitted by Cory Lee
383 */
384#define FTDI_WESTREX_MODEL_777_PID 0xDC00 /* Model 777 */
385#define FTDI_WESTREX_MODEL_8900F_PID 0xDC01 /* Model 8900F */
386
368/* Commands */ 387/* Commands */
369#define FTDI_SIO_RESET 0 /* Reset the port */ 388#define FTDI_SIO_RESET 0 /* Reset the port */
370#define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */ 389#define FTDI_SIO_MODEM_CTRL 1 /* Set the modem control register */
diff --git a/drivers/usb/serial/pl2303.c b/drivers/usb/serial/pl2303.c
index 0eb883f44ada..e8e575e037c1 100644
--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -73,7 +73,9 @@ static struct usb_device_id id_table [] = {
73 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) }, 73 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
74 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) }, 74 { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
75 { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) }, 75 { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
76 { USB_DEVICE( NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID ) }, 76 { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID ) },
77 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID ) },
78 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
77 { } /* Terminating entry */ 79 { } /* Terminating entry */
78}; 80};
79 81
diff --git a/drivers/usb/serial/pl2303.h b/drivers/usb/serial/pl2303.h
index 21d434d81813..1807087a76e3 100644
--- a/drivers/usb/serial/pl2303.h
+++ b/drivers/usb/serial/pl2303.h
@@ -64,3 +64,10 @@
64/* Nokia CA-42 Cable */ 64/* Nokia CA-42 Cable */
65#define NOKIA_CA42_VENDOR_ID 0x078b 65#define NOKIA_CA42_VENDOR_ID 0x078b
66#define NOKIA_CA42_PRODUCT_ID 0x1234 66#define NOKIA_CA42_PRODUCT_ID 0x1234
67
68/* CA-42 CLONE Cable www.ca-42.com chipset: Prolific Technology Inc */
69#define CA_42_CA42_VENDOR_ID 0x10b5
70#define CA_42_CA42_PRODUCT_ID 0xac70
71
72#define SAGEM_VENDOR_ID 0x079b
73#define SAGEM_PRODUCT_ID 0x0027
diff --git a/drivers/usb/storage/initializers.c b/drivers/usb/storage/initializers.c
index 5b06f9240d05..ab173b30076e 100644
--- a/drivers/usb/storage/initializers.c
+++ b/drivers/usb/storage/initializers.c
@@ -45,6 +45,12 @@
45#include "debug.h" 45#include "debug.h"
46#include "transport.h" 46#include "transport.h"
47 47
48#define RIO_MSC 0x08
49#define RIOP_INIT "RIOP\x00\x01\x08"
50#define RIOP_INIT_LEN 7
51#define RIO_SEND_LEN 40
52#define RIO_RECV_LEN 0x200
53
48/* This places the Shuttle/SCM USB<->SCSI bridge devices in multi-target 54/* This places the Shuttle/SCM USB<->SCSI bridge devices in multi-target
49 * mode */ 55 * mode */
50int usb_stor_euscsi_init(struct us_data *us) 56int usb_stor_euscsi_init(struct us_data *us)
@@ -91,3 +97,70 @@ int usb_stor_ucr61s2b_init(struct us_data *us)
91 97
92 return (res ? -1 : 0); 98 return (res ? -1 : 0);
93} 99}
100
101/* Place the Rio Karma into mass storage mode.
102 *
103 * The initialization begins by sending 40 bytes starting
104 * RIOP\x00\x01\x08\x00, which the device will ack with a 512-byte
105 * packet with the high four bits set and everything else null.
106 *
107 * Next, we send RIOP\x80\x00\x08\x00. Each time, a 512 byte response
108 * must be read, but we must loop until byte 5 in the response is 0x08,
109 * indicating success. */
110int rio_karma_init(struct us_data *us)
111{
112 int result, partial;
113 char *recv;
114 unsigned long timeout;
115
116 // us->iobuf is big enough to hold cmd but not receive
117 if (!(recv = kmalloc(RIO_RECV_LEN, GFP_KERNEL)))
118 goto die_nomem;
119
120 US_DEBUGP("Initializing Karma...\n");
121
122 memset(us->iobuf, 0, RIO_SEND_LEN);
123 memcpy(us->iobuf, RIOP_INIT, RIOP_INIT_LEN);
124
125 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
126 us->iobuf, RIO_SEND_LEN, &partial);
127 if (result != USB_STOR_XFER_GOOD)
128 goto die;
129
130 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
131 recv, RIO_RECV_LEN, &partial);
132 if (result != USB_STOR_XFER_GOOD)
133 goto die;
134
135 us->iobuf[4] = 0x80;
136 us->iobuf[5] = 0;
137 timeout = jiffies + msecs_to_jiffies(3000);
138 for (;;) {
139 US_DEBUGP("Sending init command\n");
140 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
141 us->iobuf, RIO_SEND_LEN, &partial);
142 if (result != USB_STOR_XFER_GOOD)
143 goto die;
144
145 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
146 recv, RIO_RECV_LEN, &partial);
147 if (result != USB_STOR_XFER_GOOD)
148 goto die;
149
150 if (recv[5] == RIO_MSC)
151 break;
152 if (time_after(jiffies, timeout))
153 goto die;
154 msleep(10);
155 }
156 US_DEBUGP("Karma initialized.\n");
157 kfree(recv);
158 return 0;
159
160die:
161 kfree(recv);
162die_nomem:
163 US_DEBUGP("Could not initialize karma.\n");
164 return USB_STOR_TRANSPORT_FAILED;
165}
166
diff --git a/drivers/usb/storage/initializers.h b/drivers/usb/storage/initializers.h
index 4c1b2bd2e2e4..f9907a5cf129 100644
--- a/drivers/usb/storage/initializers.h
+++ b/drivers/usb/storage/initializers.h
@@ -48,3 +48,4 @@ int usb_stor_euscsi_init(struct us_data *us);
48/* This function is required to activate all four slots on the UCR-61S2B 48/* This function is required to activate all four slots on the UCR-61S2B
49 * flash reader */ 49 * flash reader */
50int usb_stor_ucr61s2b_init(struct us_data *us); 50int usb_stor_ucr61s2b_init(struct us_data *us);
51int rio_karma_init(struct us_data *us);
diff --git a/drivers/usb/storage/libusual.c b/drivers/usb/storage/libusual.c
index b28151d1b609..b1ec4a718547 100644
--- a/drivers/usb/storage/libusual.c
+++ b/drivers/usb/storage/libusual.c
@@ -116,7 +116,7 @@ EXPORT_SYMBOL_GPL(usb_usual_check_type);
116static int usu_probe(struct usb_interface *intf, 116static int usu_probe(struct usb_interface *intf,
117 const struct usb_device_id *id) 117 const struct usb_device_id *id)
118{ 118{
119 int type; 119 unsigned long type;
120 int rc; 120 int rc;
121 unsigned long flags; 121 unsigned long flags;
122 122
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index dc301e567cfc..ee958f986eb8 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -145,6 +145,11 @@ UNUSUAL_DEV( 0x0451, 0x5416, 0x0100, 0x0100,
145 US_SC_DEVICE, US_PR_BULK, NULL, 145 US_SC_DEVICE, US_PR_BULK, NULL,
146 US_FL_NEED_OVERRIDE ), 146 US_FL_NEED_OVERRIDE ),
147 147
148UNUSUAL_DEV( 0x045a, 0x5210, 0x0101, 0x0101,
149 "Rio",
150 "Rio Karma",
151 US_SC_SCSI, US_PR_BULK, rio_karma_init, 0),
152
148/* Patch submitted by Philipp Friedrich <philipp@void.at> */ 153/* Patch submitted by Philipp Friedrich <philipp@void.at> */
149UNUSUAL_DEV( 0x0482, 0x0100, 0x0100, 0x0100, 154UNUSUAL_DEV( 0x0482, 0x0100, 0x0100, 0x0100,
150 "Kyocera", 155 "Kyocera",
@@ -424,11 +429,11 @@ UNUSUAL_DEV( 0x054c, 0x0010, 0x0106, 0x0450,
424 US_FL_SINGLE_LUN | US_FL_NOT_LOCKABLE | US_FL_NO_WP_DETECT ), 429 US_FL_SINGLE_LUN | US_FL_NOT_LOCKABLE | US_FL_NO_WP_DETECT ),
425 430
426/* This entry is needed because the device reports Sub=ff */ 431/* This entry is needed because the device reports Sub=ff */
427UNUSUAL_DEV( 0x054c, 0x0010, 0x0500, 0x0500, 432UNUSUAL_DEV( 0x054c, 0x0010, 0x0500, 0x0600,
428 "Sony", 433 "Sony",
429 "DSC-T1", 434 "DSC-T1/T5",
430 US_SC_8070, US_PR_DEVICE, NULL, 435 US_SC_8070, US_PR_DEVICE, NULL,
431 US_FL_SINGLE_LUN ), 436 US_FL_SINGLE_LUN ),
432 437
433 438
434/* Reported by wim@geeks.nl */ 439/* Reported by wim@geeks.nl */
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c
index 5d02f16b7d0e..4de9fb56ebfc 100644
--- a/drivers/usb/usb-skeleton.c
+++ b/drivers/usb/usb-skeleton.c
@@ -234,7 +234,7 @@ static struct file_operations skel_fops = {
234 234
235/* 235/*
236 * usb class driver info in order to get a minor number from the usb core, 236 * usb class driver info in order to get a minor number from the usb core,
237 * and to have the device registered with devfs and the driver core 237 * and to have the device registered with the driver core
238 */ 238 */
239static struct usb_class_driver skel_class = { 239static struct usb_class_driver skel_class = {
240 .name = "skel%d", 240 .name = "skel%d",