2 em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@infradead.org>
7 Sascha Sommer <saschasommer@freenet.de>
8 Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/init.h>
26 #include <linux/jiffies.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/usb.h>
31 #include <linux/vmalloc.h>
32 #include <sound/ac97_codec.h>
33 #include <media/v4l2-common.h>
37 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
38 "Markus Rechberger <mrechberger@gmail.com>, " \
39 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
40 "Sascha Sommer <saschasommer@freenet.de>"
42 MODULE_AUTHOR(DRIVER_AUTHOR
);
43 MODULE_DESCRIPTION(DRIVER_DESC
);
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(EM28XX_VERSION
);
47 /* #define ENABLE_DEBUG_ISOC_FRAMES */
49 static unsigned int core_debug
;
50 module_param(core_debug
, int, 0644);
51 MODULE_PARM_DESC(core_debug
, "enable debug messages [core]");
53 #define em28xx_coredbg(fmt, arg...) do {\
55 printk(KERN_INFO "%s %s :"fmt, \
56 dev->name, __func__ , ##arg); } while (0)
58 static unsigned int reg_debug
;
59 module_param(reg_debug
, int, 0644);
60 MODULE_PARM_DESC(reg_debug
, "enable debug messages [URB reg]");
62 #define em28xx_regdbg(fmt, arg...) do {\
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); } while (0)
68 #define em28xx_isocdbg(fmt, arg...) do {\
70 printk(KERN_INFO "%s %s :"fmt, \
71 dev->name, __func__ , ##arg); } while (0)
74 * em28xx_read_reg_req()
75 * reads data from the usb device specifying bRequest
77 int em28xx_read_reg_req_len(struct em28xx
*dev
, u8 req
, u16 reg
,
81 int pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
83 if (dev
->disconnected
)
86 if (len
> URB_MAX_CTRL_SIZE
)
90 printk(KERN_DEBUG
"(pipe 0x%08x): "
91 "IN: %02x %02x %02x %02x %02x %02x %02x %02x ",
93 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
96 len
& 0xff, len
>> 8);
99 mutex_lock(&dev
->ctrl_urb_lock
);
100 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
101 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
102 0x0000, reg
, dev
->urb_buf
, len
, HZ
);
105 printk(" failed!\n");
106 mutex_unlock(&dev
->ctrl_urb_lock
);
107 return usb_translate_errors(ret
);
111 memcpy(buf
, dev
->urb_buf
, len
);
113 mutex_unlock(&dev
->ctrl_urb_lock
);
119 for (byte
= 0; byte
< len
; byte
++)
120 printk(" %02x", (unsigned char)buf
[byte
]);
128 * em28xx_read_reg_req()
129 * reads data from the usb device specifying bRequest
131 int em28xx_read_reg_req(struct em28xx
*dev
, u8 req
, u16 reg
)
136 ret
= em28xx_read_reg_req_len(dev
, req
, reg
, &val
, 1);
143 int em28xx_read_reg(struct em28xx
*dev
, u16 reg
)
145 return em28xx_read_reg_req(dev
, USB_REQ_GET_STATUS
, reg
);
147 EXPORT_SYMBOL_GPL(em28xx_read_reg
);
150 * em28xx_write_regs_req()
151 * sends data to the usb device, specifying bRequest
153 int em28xx_write_regs_req(struct em28xx
*dev
, u8 req
, u16 reg
, char *buf
,
157 int pipe
= usb_sndctrlpipe(dev
->udev
, 0);
159 if (dev
->disconnected
)
162 if ((len
< 1) || (len
> URB_MAX_CTRL_SIZE
))
168 printk(KERN_DEBUG
"(pipe 0x%08x): "
169 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
171 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
173 reg
& 0xff, reg
>> 8,
174 len
& 0xff, len
>> 8);
176 for (byte
= 0; byte
< len
; byte
++)
177 printk(" %02x", (unsigned char)buf
[byte
]);
181 mutex_lock(&dev
->ctrl_urb_lock
);
182 memcpy(dev
->urb_buf
, buf
, len
);
183 ret
= usb_control_msg(dev
->udev
, pipe
, req
,
184 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
185 0x0000, reg
, dev
->urb_buf
, len
, HZ
);
186 mutex_unlock(&dev
->ctrl_urb_lock
);
189 return usb_translate_errors(ret
);
191 if (dev
->wait_after_write
)
192 msleep(dev
->wait_after_write
);
197 int em28xx_write_regs(struct em28xx
*dev
, u16 reg
, char *buf
, int len
)
199 return em28xx_write_regs_req(dev
, USB_REQ_GET_STATUS
, reg
, buf
, len
);
201 EXPORT_SYMBOL_GPL(em28xx_write_regs
);
203 /* Write a single register */
204 int em28xx_write_reg(struct em28xx
*dev
, u16 reg
, u8 val
)
206 return em28xx_write_regs(dev
, reg
, &val
, 1);
208 EXPORT_SYMBOL_GPL(em28xx_write_reg
);
211 * em28xx_write_reg_bits()
212 * sets only some bits (specified by bitmask) of a register, by first reading
215 int em28xx_write_reg_bits(struct em28xx
*dev
, u16 reg
, u8 val
,
221 oldval
= em28xx_read_reg(dev
, reg
);
225 newval
= (((u8
) oldval
) & ~bitmask
) | (val
& bitmask
);
227 return em28xx_write_regs(dev
, reg
, &newval
, 1);
229 EXPORT_SYMBOL_GPL(em28xx_write_reg_bits
);
232 * em28xx_toggle_reg_bits()
233 * toggles/inverts the bits (specified by bitmask) of a register
235 int em28xx_toggle_reg_bits(struct em28xx
*dev
, u16 reg
, u8 bitmask
)
240 oldval
= em28xx_read_reg(dev
, reg
);
244 newval
= (~oldval
& bitmask
) | (oldval
& ~bitmask
);
246 return em28xx_write_reg(dev
, reg
, newval
);
248 EXPORT_SYMBOL_GPL(em28xx_toggle_reg_bits
);
251 * em28xx_is_ac97_ready()
252 * Checks if ac97 is ready
254 static int em28xx_is_ac97_ready(struct em28xx
*dev
)
256 unsigned long timeout
= jiffies
+ msecs_to_jiffies(EM28XX_AC97_XFER_TIMEOUT
);
259 /* Wait up to 50 ms for AC97 command to complete */
260 while (time_is_after_jiffies(timeout
)) {
261 ret
= em28xx_read_reg(dev
, EM28XX_R43_AC97BUSY
);
270 em28xx_warn("AC97 command still being executed: not handled properly!\n");
276 * write a 16 bit value to the specified AC97 address (LSB first!)
278 int em28xx_read_ac97(struct em28xx
*dev
, u8 reg
)
281 u8 addr
= (reg
& 0x7f) | 0x80;
284 ret
= em28xx_is_ac97_ready(dev
);
288 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
292 ret
= dev
->em28xx_read_reg_req_len(dev
, 0, EM28XX_R40_AC97LSB
,
293 (u8
*)&val
, sizeof(val
));
297 return le16_to_cpu(val
);
299 EXPORT_SYMBOL_GPL(em28xx_read_ac97
);
302 * em28xx_write_ac97()
303 * write a 16 bit value to the specified AC97 address (LSB first!)
305 int em28xx_write_ac97(struct em28xx
*dev
, u8 reg
, u16 val
)
308 u8 addr
= reg
& 0x7f;
311 value
= cpu_to_le16(val
);
313 ret
= em28xx_is_ac97_ready(dev
);
317 ret
= em28xx_write_regs(dev
, EM28XX_R40_AC97LSB
, (u8
*) &value
, 2);
321 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
327 EXPORT_SYMBOL_GPL(em28xx_write_ac97
);
329 struct em28xx_vol_itable
{
330 enum em28xx_amux mux
;
334 static struct em28xx_vol_itable inputs
[] = {
335 { EM28XX_AMUX_VIDEO
, AC97_VIDEO
},
336 { EM28XX_AMUX_LINE_IN
, AC97_LINE
},
337 { EM28XX_AMUX_PHONE
, AC97_PHONE
},
338 { EM28XX_AMUX_MIC
, AC97_MIC
},
339 { EM28XX_AMUX_CD
, AC97_CD
},
340 { EM28XX_AMUX_AUX
, AC97_AUX
},
341 { EM28XX_AMUX_PCM_OUT
, AC97_PCM
},
344 static int set_ac97_input(struct em28xx
*dev
)
347 enum em28xx_amux amux
= dev
->ctl_ainput
;
349 /* EM28XX_AMUX_VIDEO2 is a special case used to indicate that
350 em28xx should point to LINE IN, while AC97 should use VIDEO
352 if (amux
== EM28XX_AMUX_VIDEO2
)
353 amux
= EM28XX_AMUX_VIDEO
;
355 /* Mute all entres but the one that were selected */
356 for (i
= 0; i
< ARRAY_SIZE(inputs
); i
++) {
357 if (amux
== inputs
[i
].mux
)
358 ret
= em28xx_write_ac97(dev
, inputs
[i
].reg
, 0x0808);
360 ret
= em28xx_write_ac97(dev
, inputs
[i
].reg
, 0x8000);
363 em28xx_warn("couldn't setup AC97 register %d\n",
369 static int em28xx_set_audio_source(struct em28xx
*dev
)
374 if (dev
->board
.is_em2800
) {
375 if (dev
->ctl_ainput
== EM28XX_AMUX_VIDEO
)
376 input
= EM2800_AUDIO_SRC_TUNER
;
378 input
= EM2800_AUDIO_SRC_LINE
;
380 ret
= em28xx_write_regs(dev
, EM2800_R08_AUDIOSRC
, &input
, 1);
385 if (dev
->board
.has_msp34xx
)
386 input
= EM28XX_AUDIO_SRC_TUNER
;
388 switch (dev
->ctl_ainput
) {
389 case EM28XX_AMUX_VIDEO
:
390 input
= EM28XX_AUDIO_SRC_TUNER
;
393 input
= EM28XX_AUDIO_SRC_LINE
;
398 if (dev
->board
.mute_gpio
&& dev
->mute
)
399 em28xx_gpio_set(dev
, dev
->board
.mute_gpio
);
401 em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
403 ret
= em28xx_write_reg_bits(dev
, EM28XX_R0E_AUDIOSRC
, input
, 0xc0);
408 switch (dev
->audio_mode
.ac97
) {
412 ret
= set_ac97_input(dev
);
418 struct em28xx_vol_otable
{
419 enum em28xx_aout mux
;
423 static const struct em28xx_vol_otable outputs
[] = {
424 { EM28XX_AOUT_MASTER
, AC97_MASTER
},
425 { EM28XX_AOUT_LINE
, AC97_HEADPHONE
},
426 { EM28XX_AOUT_MONO
, AC97_MASTER_MONO
},
427 { EM28XX_AOUT_LFE
, AC97_CENTER_LFE_MASTER
},
428 { EM28XX_AOUT_SURR
, AC97_SURROUND_MASTER
},
431 int em28xx_audio_analog_set(struct em28xx
*dev
)
436 if (!dev
->audio_mode
.has_audio
)
439 /* It is assumed that all devices use master volume for output.
440 It would be possible to use also line output.
442 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
443 /* Mute all outputs */
444 for (i
= 0; i
< ARRAY_SIZE(outputs
); i
++) {
445 ret
= em28xx_write_ac97(dev
, outputs
[i
].reg
, 0x8000);
447 em28xx_warn("couldn't setup AC97 register %d\n",
452 xclk
= dev
->board
.xclk
& 0x7f;
454 xclk
|= EM28XX_XCLK_AUDIO_UNMUTE
;
456 ret
= em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, xclk
);
461 /* Selects the proper audio input */
462 ret
= em28xx_set_audio_source(dev
);
465 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
468 em28xx_write_ac97(dev
, AC97_POWERDOWN
, 0x4200);
469 em28xx_write_ac97(dev
, AC97_EXTENDED_STATUS
, 0x0031);
470 em28xx_write_ac97(dev
, AC97_PCM_LR_ADC_RATE
, 0xbb80);
472 /* LSB: left channel - both channels with the same level */
473 vol
= (0x1f - dev
->volume
) | ((0x1f - dev
->volume
) << 8);
475 /* Mute device, if needed */
480 for (i
= 0; i
< ARRAY_SIZE(outputs
); i
++) {
481 if (dev
->ctl_aoutput
& outputs
[i
].mux
)
482 ret
= em28xx_write_ac97(dev
, outputs
[i
].reg
,
485 em28xx_warn("couldn't setup AC97 register %d\n",
489 if (dev
->ctl_aoutput
& EM28XX_AOUT_PCM_IN
) {
490 int sel
= ac97_return_record_select(dev
->ctl_aoutput
);
492 /* Use the same input for both left and right
496 em28xx_write_ac97(dev
, AC97_REC_SEL
, sel
);
502 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set
);
504 int em28xx_audio_setup(struct em28xx
*dev
)
506 int vid1
, vid2
, feat
, cfg
;
509 if (!dev
->audio_mode
.has_audio
)
512 /* See how this device is configured */
513 cfg
= em28xx_read_reg(dev
, EM28XX_R00_CHIPCFG
);
514 em28xx_info("Config register raw data: 0x%02x\n", cfg
);
516 /* Register read error? */
517 cfg
= EM28XX_CHIPCFG_AC97
; /* Be conservative */
518 } else if ((cfg
& EM28XX_CHIPCFG_AUDIOMASK
) == 0x00) {
519 /* The device doesn't have vendor audio at all */
520 dev
->has_alsa_audio
= false;
521 dev
->audio_mode
.has_audio
= false;
523 } else if ((cfg
& EM28XX_CHIPCFG_AUDIOMASK
) != EM28XX_CHIPCFG_AC97
) {
524 if (dev
->chip_id
< CHIP_ID_EM2860
&&
525 (cfg
& EM28XX_CHIPCFG_AUDIOMASK
) ==
526 EM2820_CHIPCFG_I2S_1_SAMPRATE
)
527 dev
->audio_mode
.i2s_samplerates
= 1;
528 else if (dev
->chip_id
>= CHIP_ID_EM2860
&&
529 (cfg
& EM28XX_CHIPCFG_AUDIOMASK
) ==
530 EM2860_CHIPCFG_I2S_5_SAMPRATES
)
531 dev
->audio_mode
.i2s_samplerates
= 5;
533 dev
->audio_mode
.i2s_samplerates
= 3;
534 em28xx_info("I2S Audio (%d sample rate(s))\n",
535 dev
->audio_mode
.i2s_samplerates
);
536 /* Skip the code that does AC97 vendor detection */
537 dev
->audio_mode
.ac97
= EM28XX_NO_AC97
;
541 dev
->audio_mode
.ac97
= EM28XX_AC97_OTHER
;
543 vid1
= em28xx_read_ac97(dev
, AC97_VENDOR_ID1
);
546 * Device likely doesn't support AC97
547 * Note: (some) em2800 devices without eeprom reports 0x91 on
548 * CHIPCFG register, even not having an AC97 chip
550 em28xx_warn("AC97 chip type couldn't be determined\n");
551 dev
->audio_mode
.ac97
= EM28XX_NO_AC97
;
552 dev
->has_alsa_audio
= false;
553 dev
->audio_mode
.has_audio
= false;
557 vid2
= em28xx_read_ac97(dev
, AC97_VENDOR_ID2
);
561 vid
= vid1
<< 16 | vid2
;
563 dev
->audio_mode
.ac97_vendor_id
= vid
;
564 em28xx_warn("AC97 vendor ID = 0x%08x\n", vid
);
566 feat
= em28xx_read_ac97(dev
, AC97_RESET
);
570 dev
->audio_mode
.ac97_feat
= feat
;
571 em28xx_warn("AC97 features = 0x%04x\n", feat
);
573 /* Try to identify what audio processor we have */
574 if (((vid
== 0xffffffff) || (vid
== 0x83847650)) && (feat
== 0x6a90))
575 dev
->audio_mode
.ac97
= EM28XX_AC97_EM202
;
576 else if ((vid
>> 8) == 0x838476)
577 dev
->audio_mode
.ac97
= EM28XX_AC97_SIGMATEL
;
580 /* Reports detected AC97 processor */
581 switch (dev
->audio_mode
.ac97
) {
583 em28xx_info("No AC97 audio processor\n");
585 case EM28XX_AC97_EM202
:
586 em28xx_info("Empia 202 AC97 audio processor detected\n");
588 case EM28XX_AC97_SIGMATEL
:
589 em28xx_info("Sigmatel audio processor detected(stac 97%02x)\n",
590 dev
->audio_mode
.ac97_vendor_id
& 0xff);
592 case EM28XX_AC97_OTHER
:
593 em28xx_warn("Unknown AC97 audio processor detected!\n");
599 return em28xx_audio_analog_set(dev
);
601 EXPORT_SYMBOL_GPL(em28xx_audio_setup
);
603 const struct em28xx_led
*em28xx_find_led(struct em28xx
*dev
,
604 enum em28xx_led_role role
)
606 if (dev
->board
.leds
) {
608 while (dev
->board
.leds
[k
].role
>= 0 &&
609 dev
->board
.leds
[k
].role
< EM28XX_NUM_LED_ROLES
) {
610 if (dev
->board
.leds
[k
].role
== role
)
611 return &dev
->board
.leds
[k
];
617 EXPORT_SYMBOL_GPL(em28xx_find_led
);
619 int em28xx_capture_start(struct em28xx
*dev
, int start
)
623 if (dev
->chip_id
== CHIP_ID_EM2874
||
624 dev
->chip_id
== CHIP_ID_EM2884
||
625 dev
->chip_id
== CHIP_ID_EM28174
||
626 dev
->chip_id
== CHIP_ID_EM28178
) {
627 /* The Transport Stream Enable Register moved in em2874 */
628 rc
= em28xx_write_reg_bits(dev
, EM2874_R5F_TS_ENABLE
,
630 EM2874_TS1_CAPTURE_ENABLE
: 0x00,
631 EM2874_TS1_CAPTURE_ENABLE
);
633 /* FIXME: which is the best order? */
634 /* video registers are sampled by VREF */
635 rc
= em28xx_write_reg_bits(dev
, EM28XX_R0C_USBSUSP
,
636 start
? 0x10 : 0x00, 0x10);
641 if (dev
->board
.is_webcam
)
642 rc
= em28xx_write_reg(dev
, 0x13, 0x0c);
644 /* Enable video capture */
645 rc
= em28xx_write_reg(dev
, 0x48, 0x00);
647 if (dev
->mode
== EM28XX_ANALOG_MODE
)
648 rc
= em28xx_write_reg(dev
,
649 EM28XX_R12_VINENABLE
, 0x67);
651 rc
= em28xx_write_reg(dev
,
652 EM28XX_R12_VINENABLE
, 0x37);
656 /* disable video capture */
657 rc
= em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x27);
664 /* Switch (explicitly controlled) analog capturing LED on/off */
665 if (dev
->mode
== EM28XX_ANALOG_MODE
) {
666 const struct em28xx_led
*led
;
667 led
= em28xx_find_led(dev
, EM28XX_LED_ANALOG_CAPTURING
);
669 em28xx_write_reg_bits(dev
, led
->gpio_reg
,
670 (!start
^ led
->inverted
) ?
671 ~led
->gpio_mask
: led
->gpio_mask
,
678 int em28xx_gpio_set(struct em28xx
*dev
, struct em28xx_reg_seq
*gpio
)
685 if (dev
->mode
!= EM28XX_SUSPEND
) {
686 em28xx_write_reg(dev
, 0x48, 0x00);
687 if (dev
->mode
== EM28XX_ANALOG_MODE
)
688 em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x67);
690 em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x37);
694 /* Send GPIO reset sequences specified at board entry */
695 while (gpio
->sleep
>= 0) {
696 if (gpio
->reg
>= 0) {
697 rc
= em28xx_write_reg_bits(dev
,
711 EXPORT_SYMBOL_GPL(em28xx_gpio_set
);
713 int em28xx_set_mode(struct em28xx
*dev
, enum em28xx_mode set_mode
)
715 if (dev
->mode
== set_mode
)
718 if (set_mode
== EM28XX_SUSPEND
) {
719 dev
->mode
= set_mode
;
721 /* FIXME: add suspend support for ac97 */
723 return em28xx_gpio_set(dev
, dev
->board
.suspend_gpio
);
726 dev
->mode
= set_mode
;
728 if (dev
->mode
== EM28XX_DIGITAL_MODE
)
729 return em28xx_gpio_set(dev
, dev
->board
.dvb_gpio
);
731 return em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
733 EXPORT_SYMBOL_GPL(em28xx_set_mode
);
735 /* ------------------------------------------------------------------
737 ------------------------------------------------------------------*/
740 * URB completion handler for isoc/bulk transfers
742 static void em28xx_irq_callback(struct urb
*urb
)
744 struct em28xx
*dev
= urb
->context
;
747 switch (urb
->status
) {
748 case 0: /* success */
749 case -ETIMEDOUT
: /* NAK */
751 case -ECONNRESET
: /* kill */
756 em28xx_isocdbg("urb completition error %d.\n", urb
->status
);
760 /* Copy data from URB */
761 spin_lock(&dev
->slock
);
762 dev
->usb_ctl
.urb_data_copy(dev
, urb
);
763 spin_unlock(&dev
->slock
);
765 /* Reset urb buffers */
766 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
767 /* isoc only (bulk: number_of_packets = 0) */
768 urb
->iso_frame_desc
[i
].status
= 0;
769 urb
->iso_frame_desc
[i
].actual_length
= 0;
773 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
775 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
781 * Stop and Deallocate URBs
783 void em28xx_uninit_usb_xfer(struct em28xx
*dev
, enum em28xx_mode mode
)
786 struct em28xx_usb_bufs
*usb_bufs
;
789 em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
792 if (mode
== EM28XX_DIGITAL_MODE
)
793 usb_bufs
= &dev
->usb_ctl
.digital_bufs
;
795 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
797 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
798 urb
= usb_bufs
->urb
[i
];
800 if (!irqs_disabled())
805 if (usb_bufs
->transfer_buffer
[i
]) {
806 usb_free_coherent(dev
->udev
,
807 urb
->transfer_buffer_length
,
808 usb_bufs
->transfer_buffer
[i
],
812 usb_bufs
->urb
[i
] = NULL
;
814 usb_bufs
->transfer_buffer
[i
] = NULL
;
817 kfree(usb_bufs
->urb
);
818 kfree(usb_bufs
->transfer_buffer
);
820 usb_bufs
->urb
= NULL
;
821 usb_bufs
->transfer_buffer
= NULL
;
822 usb_bufs
->num_bufs
= 0;
824 em28xx_capture_start(dev
, 0);
826 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer
);
831 void em28xx_stop_urbs(struct em28xx
*dev
)
835 struct em28xx_usb_bufs
*isoc_bufs
= &dev
->usb_ctl
.digital_bufs
;
837 em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n");
839 for (i
= 0; i
< isoc_bufs
->num_bufs
; i
++) {
840 urb
= isoc_bufs
->urb
[i
];
842 if (!irqs_disabled())
849 em28xx_capture_start(dev
, 0);
851 EXPORT_SYMBOL_GPL(em28xx_stop_urbs
);
856 int em28xx_alloc_urbs(struct em28xx
*dev
, enum em28xx_mode mode
, int xfer_bulk
,
857 int num_bufs
, int max_pkt_size
, int packet_multiplier
)
859 struct em28xx_usb_bufs
*usb_bufs
;
865 em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode
);
867 /* Check mode and if we have an endpoint for the selected
868 transfer type, select buffer */
869 if (mode
== EM28XX_DIGITAL_MODE
) {
870 if ((xfer_bulk
&& !dev
->dvb_ep_bulk
) ||
871 (!xfer_bulk
&& !dev
->dvb_ep_isoc
)) {
872 em28xx_errdev("no endpoint for DVB mode and transfer type %d\n",
876 usb_bufs
= &dev
->usb_ctl
.digital_bufs
;
877 } else if (mode
== EM28XX_ANALOG_MODE
) {
878 if ((xfer_bulk
&& !dev
->analog_ep_bulk
) ||
879 (!xfer_bulk
&& !dev
->analog_ep_isoc
)) {
880 em28xx_errdev("no endpoint for analog mode and transfer type %d\n",
884 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
886 em28xx_errdev("invalid mode selected\n");
890 /* De-allocates all pending stuff */
891 em28xx_uninit_usb_xfer(dev
, mode
);
893 usb_bufs
->num_bufs
= num_bufs
;
895 usb_bufs
->urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
896 if (!usb_bufs
->urb
) {
897 em28xx_errdev("cannot alloc memory for usb buffers\n");
901 usb_bufs
->transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
903 if (!usb_bufs
->transfer_buffer
) {
904 em28xx_errdev("cannot allocate memory for usb transfer\n");
905 kfree(usb_bufs
->urb
);
909 usb_bufs
->max_pkt_size
= max_pkt_size
;
911 usb_bufs
->num_packets
= 0;
913 usb_bufs
->num_packets
= packet_multiplier
;
914 dev
->usb_ctl
.vid_buf
= NULL
;
915 dev
->usb_ctl
.vbi_buf
= NULL
;
917 sb_size
= packet_multiplier
* usb_bufs
->max_pkt_size
;
919 /* allocate urbs and transfer buffers */
920 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
921 urb
= usb_alloc_urb(usb_bufs
->num_packets
, GFP_KERNEL
);
923 em28xx_err("cannot alloc usb_ctl.urb %i\n", i
);
924 em28xx_uninit_usb_xfer(dev
, mode
);
927 usb_bufs
->urb
[i
] = urb
;
929 usb_bufs
->transfer_buffer
[i
] = usb_alloc_coherent(dev
->udev
,
930 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
931 if (!usb_bufs
->transfer_buffer
[i
]) {
932 em28xx_err("unable to allocate %i bytes for transfer"
935 in_interrupt() ? " while in int" : "");
936 em28xx_uninit_usb_xfer(dev
, mode
);
939 memset(usb_bufs
->transfer_buffer
[i
], 0, sb_size
);
941 if (xfer_bulk
) { /* bulk */
942 pipe
= usb_rcvbulkpipe(dev
->udev
,
943 mode
== EM28XX_ANALOG_MODE
?
944 dev
->analog_ep_bulk
:
946 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
947 usb_bufs
->transfer_buffer
[i
], sb_size
,
948 em28xx_irq_callback
, dev
);
949 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
951 pipe
= usb_rcvisocpipe(dev
->udev
,
952 mode
== EM28XX_ANALOG_MODE
?
953 dev
->analog_ep_isoc
:
955 usb_fill_int_urb(urb
, dev
->udev
, pipe
,
956 usb_bufs
->transfer_buffer
[i
], sb_size
,
957 em28xx_irq_callback
, dev
, 1);
958 urb
->transfer_flags
= URB_ISO_ASAP
|
959 URB_NO_TRANSFER_DMA_MAP
;
961 for (j
= 0; j
< usb_bufs
->num_packets
; j
++) {
962 urb
->iso_frame_desc
[j
].offset
= k
;
963 urb
->iso_frame_desc
[j
].length
=
964 usb_bufs
->max_pkt_size
;
965 k
+= usb_bufs
->max_pkt_size
;
969 urb
->number_of_packets
= usb_bufs
->num_packets
;
974 EXPORT_SYMBOL_GPL(em28xx_alloc_urbs
);
977 * Allocate URBs and start IRQ
979 int em28xx_init_usb_xfer(struct em28xx
*dev
, enum em28xx_mode mode
,
980 int xfer_bulk
, int num_bufs
, int max_pkt_size
,
981 int packet_multiplier
,
982 int (*urb_data_copy
) (struct em28xx
*dev
, struct urb
*urb
))
984 struct em28xx_dmaqueue
*dma_q
= &dev
->vidq
;
985 struct em28xx_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
986 struct em28xx_usb_bufs
*usb_bufs
;
991 em28xx_isocdbg("em28xx: called em28xx_init_usb_xfer in mode %d\n",
994 dev
->usb_ctl
.urb_data_copy
= urb_data_copy
;
996 if (mode
== EM28XX_DIGITAL_MODE
) {
997 usb_bufs
= &dev
->usb_ctl
.digital_bufs
;
998 /* no need to free/alloc usb buffers in digital mode */
1001 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
1006 rc
= em28xx_alloc_urbs(dev
, mode
, xfer_bulk
, num_bufs
,
1007 max_pkt_size
, packet_multiplier
);
1013 rc
= usb_clear_halt(dev
->udev
, usb_bufs
->urb
[0]->pipe
);
1015 em28xx_err("failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1017 em28xx_uninit_usb_xfer(dev
, mode
);
1022 init_waitqueue_head(&dma_q
->wq
);
1023 init_waitqueue_head(&vbi_dma_q
->wq
);
1025 em28xx_capture_start(dev
, 1);
1027 /* submit urbs and enables IRQ */
1028 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
1029 rc
= usb_submit_urb(usb_bufs
->urb
[i
], GFP_ATOMIC
);
1031 em28xx_err("submit of urb %i failed (error=%i)\n", i
,
1033 em28xx_uninit_usb_xfer(dev
, mode
);
1040 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer
);
1043 * Device control list
1046 static LIST_HEAD(em28xx_devlist
);
1047 static DEFINE_MUTEX(em28xx_devlist_mutex
);
1050 * Extension interface
1053 static LIST_HEAD(em28xx_extension_devlist
);
1055 int em28xx_register_extension(struct em28xx_ops
*ops
)
1057 struct em28xx
*dev
= NULL
;
1059 mutex_lock(&em28xx_devlist_mutex
);
1060 list_add_tail(&ops
->next
, &em28xx_extension_devlist
);
1061 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1064 mutex_unlock(&em28xx_devlist_mutex
);
1065 printk(KERN_INFO
"em28xx: Registered (%s) extension\n", ops
->name
);
1068 EXPORT_SYMBOL(em28xx_register_extension
);
1070 void em28xx_unregister_extension(struct em28xx_ops
*ops
)
1072 struct em28xx
*dev
= NULL
;
1074 mutex_lock(&em28xx_devlist_mutex
);
1075 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1078 list_del(&ops
->next
);
1079 mutex_unlock(&em28xx_devlist_mutex
);
1080 printk(KERN_INFO
"Em28xx: Removed (%s) extension\n", ops
->name
);
1082 EXPORT_SYMBOL(em28xx_unregister_extension
);
1084 void em28xx_init_extension(struct em28xx
*dev
)
1086 const struct em28xx_ops
*ops
= NULL
;
1088 mutex_lock(&em28xx_devlist_mutex
);
1089 list_add_tail(&dev
->devlist
, &em28xx_devlist
);
1090 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1094 mutex_unlock(&em28xx_devlist_mutex
);
1097 void em28xx_close_extension(struct em28xx
*dev
)
1099 const struct em28xx_ops
*ops
= NULL
;
1101 mutex_lock(&em28xx_devlist_mutex
);
1102 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1106 list_del(&dev
->devlist
);
1107 mutex_unlock(&em28xx_devlist_mutex
);