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->chip_id == CHIP_ID_EM2870 ||
510 dev->chip_id == CHIP_ID_EM2874 ||
511 dev->chip_id == CHIP_ID_EM28174 ||
512 dev->chip_id == CHIP_ID_EM28178) {
513 /* Digital only device - don't load any alsa module */
514 dev->audio_mode.has_audio = false;
515 dev->has_audio_class = false;
516 dev->has_alsa_audio = false;
520 dev->audio_mode.has_audio = true;
522 /* See how this device is configured */
523 cfg = em28xx_read_reg(dev, EM28XX_R00_CHIPCFG);
524 em28xx_info("Config register raw data: 0x%02x\n", cfg);
526 /* Register read error? */
527 cfg = EM28XX_CHIPCFG_AC97; /* Be conservative */
528 } else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) == 0x00) {
529 /* The device doesn't have vendor audio at all */
530 dev->has_alsa_audio = false;
531 dev->audio_mode.has_audio = false;
533 } else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) != EM28XX_CHIPCFG_AC97) {
534 if (dev->chip_id < CHIP_ID_EM2860 &&
535 (cfg & EM28XX_CHIPCFG_AUDIOMASK) ==
536 EM2820_CHIPCFG_I2S_1_SAMPRATE)
537 dev->audio_mode.i2s_samplerates = 1;
538 else if (dev->chip_id >= CHIP_ID_EM2860 &&
539 (cfg & EM28XX_CHIPCFG_AUDIOMASK) ==
540 EM2860_CHIPCFG_I2S_5_SAMPRATES)
541 dev->audio_mode.i2s_samplerates = 5;
543 dev->audio_mode.i2s_samplerates = 3;
544 em28xx_info("I2S Audio (%d sample rate(s))\n",
545 dev->audio_mode.i2s_samplerates);
546 /* Skip the code that does AC97 vendor detection */
547 dev->audio_mode.ac97 = EM28XX_NO_AC97;
551 dev->audio_mode.ac97 = EM28XX_AC97_OTHER;
553 vid1 = em28xx_read_ac97(dev, AC97_VENDOR_ID1);
556 * Device likely doesn't support AC97
557 * Note: (some) em2800 devices without eeprom reports 0x91 on
558 * CHIPCFG register, even not having an AC97 chip
560 em28xx_warn("AC97 chip type couldn't be determined\n");
561 dev->audio_mode.ac97 = EM28XX_NO_AC97;
562 dev->has_alsa_audio = false;
563 dev->audio_mode.has_audio = false;
567 vid2 = em28xx_read_ac97(dev, AC97_VENDOR_ID2);
571 vid = vid1 << 16 | vid2;
573 dev->audio_mode.ac97_vendor_id = vid;
574 em28xx_warn("AC97 vendor ID = 0x%08x\n", vid);
576 feat = em28xx_read_ac97(dev, AC97_RESET);
580 dev->audio_mode.ac97_feat = feat;
581 em28xx_warn("AC97 features = 0x%04x\n", feat);
583 /* Try to identify what audio processor we have */
584 if (((vid == 0xffffffff) || (vid == 0x83847650)) && (feat == 0x6a90))
585 dev->audio_mode.ac97 = EM28XX_AC97_EM202;
586 else if ((vid >> 8) == 0x838476)
587 dev->audio_mode.ac97 = EM28XX_AC97_SIGMATEL;
590 /* Reports detected AC97 processor */
591 switch (dev->audio_mode.ac97) {
593 em28xx_info("No AC97 audio processor\n");
595 case EM28XX_AC97_EM202:
596 em28xx_info("Empia 202 AC97 audio processor detected\n");
598 case EM28XX_AC97_SIGMATEL:
599 em28xx_info("Sigmatel audio processor detected(stac 97%02x)\n",
600 dev->audio_mode.ac97_vendor_id & 0xff);
602 case EM28XX_AC97_OTHER:
603 em28xx_warn("Unknown AC97 audio processor detected!\n");
609 return em28xx_audio_analog_set(dev);
611 EXPORT_SYMBOL_GPL(em28xx_audio_setup);
613 const struct em28xx_led *em28xx_find_led(struct em28xx *dev,
614 enum em28xx_led_role role)
616 if (dev->board.leds) {
618 while (dev->board.leds[k].role >= 0 &&
619 dev->board.leds[k].role < EM28XX_NUM_LED_ROLES) {
620 if (dev->board.leds[k].role == role)
621 return &dev->board.leds[k];
627 EXPORT_SYMBOL_GPL(em28xx_find_led);
629 int em28xx_capture_start(struct em28xx *dev, int start)
633 if (dev->chip_id == CHIP_ID_EM2874 ||
634 dev->chip_id == CHIP_ID_EM2884 ||
635 dev->chip_id == CHIP_ID_EM28174 ||
636 dev->chip_id == CHIP_ID_EM28178) {
637 /* The Transport Stream Enable Register moved in em2874 */
638 rc = em28xx_write_reg_bits(dev, EM2874_R5F_TS_ENABLE,
640 EM2874_TS1_CAPTURE_ENABLE : 0x00,
641 EM2874_TS1_CAPTURE_ENABLE);
643 /* FIXME: which is the best order? */
644 /* video registers are sampled by VREF */
645 rc = em28xx_write_reg_bits(dev, EM28XX_R0C_USBSUSP,
646 start ? 0x10 : 0x00, 0x10);
651 if (dev->board.is_webcam)
652 rc = em28xx_write_reg(dev, 0x13, 0x0c);
654 /* Enable video capture */
655 rc = em28xx_write_reg(dev, 0x48, 0x00);
657 if (dev->mode == EM28XX_ANALOG_MODE)
658 rc = em28xx_write_reg(dev,
659 EM28XX_R12_VINENABLE, 0x67);
661 rc = em28xx_write_reg(dev,
662 EM28XX_R12_VINENABLE, 0x37);
666 /* disable video capture */
667 rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x27);
674 /* Switch (explicitly controlled) analog capturing LED on/off */
675 if (dev->mode == EM28XX_ANALOG_MODE) {
676 const struct em28xx_led *led;
677 led = em28xx_find_led(dev, EM28XX_LED_ANALOG_CAPTURING);
679 em28xx_write_reg_bits(dev, led->gpio_reg,
680 (!start ^ led->inverted) ?
681 ~led->gpio_mask : led->gpio_mask,
688 int em28xx_gpio_set(struct em28xx *dev, struct em28xx_reg_seq *gpio)
695 if (dev->mode != EM28XX_SUSPEND) {
696 em28xx_write_reg(dev, 0x48, 0x00);
697 if (dev->mode == EM28XX_ANALOG_MODE)
698 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);
700 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);
704 /* Send GPIO reset sequences specified at board entry */
705 while (gpio->sleep >= 0) {
706 if (gpio->reg >= 0) {
707 rc = em28xx_write_reg_bits(dev,
721 EXPORT_SYMBOL_GPL(em28xx_gpio_set);
723 int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode)
725 if (dev->mode == set_mode)
728 if (set_mode == EM28XX_SUSPEND) {
729 dev->mode = set_mode;
731 /* FIXME: add suspend support for ac97 */
733 return em28xx_gpio_set(dev, dev->board.suspend_gpio);
736 dev->mode = set_mode;
738 if (dev->mode == EM28XX_DIGITAL_MODE)
739 return em28xx_gpio_set(dev, dev->board.dvb_gpio);
741 return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
743 EXPORT_SYMBOL_GPL(em28xx_set_mode);
745 /* ------------------------------------------------------------------
747 ------------------------------------------------------------------*/
750 * URB completion handler for isoc/bulk transfers
752 static void em28xx_irq_callback(struct urb *urb)
754 struct em28xx *dev = urb->context;
757 switch (urb->status) {
758 case 0: /* success */
759 case -ETIMEDOUT: /* NAK */
761 case -ECONNRESET: /* kill */
766 em28xx_isocdbg("urb completition error %d.\n", urb->status);
770 /* Copy data from URB */
771 spin_lock(&dev->slock);
772 dev->usb_ctl.urb_data_copy(dev, urb);
773 spin_unlock(&dev->slock);
775 /* Reset urb buffers */
776 for (i = 0; i < urb->number_of_packets; i++) {
777 /* isoc only (bulk: number_of_packets = 0) */
778 urb->iso_frame_desc[i].status = 0;
779 urb->iso_frame_desc[i].actual_length = 0;
783 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
785 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
791 * Stop and Deallocate URBs
793 void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
796 struct em28xx_usb_bufs *usb_bufs;
799 em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
802 if (mode == EM28XX_DIGITAL_MODE)
803 usb_bufs = &dev->usb_ctl.digital_bufs;
805 usb_bufs = &dev->usb_ctl.analog_bufs;
807 for (i = 0; i < usb_bufs->num_bufs; i++) {
808 urb = usb_bufs->urb[i];
810 if (!irqs_disabled())
815 if (usb_bufs->transfer_buffer[i]) {
816 usb_free_coherent(dev->udev,
817 urb->transfer_buffer_length,
818 usb_bufs->transfer_buffer[i],
822 usb_bufs->urb[i] = NULL;
824 usb_bufs->transfer_buffer[i] = NULL;
827 kfree(usb_bufs->urb);
828 kfree(usb_bufs->transfer_buffer);
830 usb_bufs->urb = NULL;
831 usb_bufs->transfer_buffer = NULL;
832 usb_bufs->num_bufs = 0;
834 em28xx_capture_start(dev, 0);
836 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer);
841 void em28xx_stop_urbs(struct em28xx *dev)
845 struct em28xx_usb_bufs *isoc_bufs = &dev->usb_ctl.digital_bufs;
847 em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n");
849 for (i = 0; i < isoc_bufs->num_bufs; i++) {
850 urb = isoc_bufs->urb[i];
852 if (!irqs_disabled())
859 em28xx_capture_start(dev, 0);
861 EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
866 int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
867 int num_bufs, int max_pkt_size, int packet_multiplier)
869 struct em28xx_usb_bufs *usb_bufs;
875 em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode);
877 /* Check mode and if we have an endpoint for the selected
878 transfer type, select buffer */
879 if (mode == EM28XX_DIGITAL_MODE) {
880 if ((xfer_bulk && !dev->dvb_ep_bulk) ||
881 (!xfer_bulk && !dev->dvb_ep_isoc)) {
882 em28xx_errdev("no endpoint for DVB mode and transfer type %d\n",
886 usb_bufs = &dev->usb_ctl.digital_bufs;
887 } else if (mode == EM28XX_ANALOG_MODE) {
888 if ((xfer_bulk && !dev->analog_ep_bulk) ||
889 (!xfer_bulk && !dev->analog_ep_isoc)) {
890 em28xx_errdev("no endpoint for analog mode and transfer type %d\n",
894 usb_bufs = &dev->usb_ctl.analog_bufs;
896 em28xx_errdev("invalid mode selected\n");
900 /* De-allocates all pending stuff */
901 em28xx_uninit_usb_xfer(dev, mode);
903 usb_bufs->num_bufs = num_bufs;
905 usb_bufs->urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
906 if (!usb_bufs->urb) {
907 em28xx_errdev("cannot alloc memory for usb buffers\n");
911 usb_bufs->transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
913 if (!usb_bufs->transfer_buffer) {
914 em28xx_errdev("cannot allocate memory for usb transfer\n");
915 kfree(usb_bufs->urb);
919 usb_bufs->max_pkt_size = max_pkt_size;
921 usb_bufs->num_packets = 0;
923 usb_bufs->num_packets = packet_multiplier;
924 dev->usb_ctl.vid_buf = NULL;
925 dev->usb_ctl.vbi_buf = NULL;
927 sb_size = packet_multiplier * usb_bufs->max_pkt_size;
929 /* allocate urbs and transfer buffers */
930 for (i = 0; i < usb_bufs->num_bufs; i++) {
931 urb = usb_alloc_urb(usb_bufs->num_packets, GFP_KERNEL);
933 em28xx_err("cannot alloc usb_ctl.urb %i\n", i);
934 em28xx_uninit_usb_xfer(dev, mode);
937 usb_bufs->urb[i] = urb;
939 usb_bufs->transfer_buffer[i] = usb_alloc_coherent(dev->udev,
940 sb_size, GFP_KERNEL, &urb->transfer_dma);
941 if (!usb_bufs->transfer_buffer[i]) {
942 em28xx_err("unable to allocate %i bytes for transfer"
945 in_interrupt() ? " while in int" : "");
946 em28xx_uninit_usb_xfer(dev, mode);
949 memset(usb_bufs->transfer_buffer[i], 0, sb_size);
951 if (xfer_bulk) { /* bulk */
952 pipe = usb_rcvbulkpipe(dev->udev,
953 mode == EM28XX_ANALOG_MODE ?
954 dev->analog_ep_bulk :
956 usb_fill_bulk_urb(urb, dev->udev, pipe,
957 usb_bufs->transfer_buffer[i], sb_size,
958 em28xx_irq_callback, dev);
959 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
961 pipe = usb_rcvisocpipe(dev->udev,
962 mode == EM28XX_ANALOG_MODE ?
963 dev->analog_ep_isoc :
965 usb_fill_int_urb(urb, dev->udev, pipe,
966 usb_bufs->transfer_buffer[i], sb_size,
967 em28xx_irq_callback, dev, 1);
968 urb->transfer_flags = URB_ISO_ASAP |
969 URB_NO_TRANSFER_DMA_MAP;
971 for (j = 0; j < usb_bufs->num_packets; j++) {
972 urb->iso_frame_desc[j].offset = k;
973 urb->iso_frame_desc[j].length =
974 usb_bufs->max_pkt_size;
975 k += usb_bufs->max_pkt_size;
979 urb->number_of_packets = usb_bufs->num_packets;
984 EXPORT_SYMBOL_GPL(em28xx_alloc_urbs);
987 * Allocate URBs and start IRQ
989 int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
990 int xfer_bulk, int num_bufs, int max_pkt_size,
991 int packet_multiplier,
992 int (*urb_data_copy) (struct em28xx *dev, struct urb *urb))
994 struct em28xx_dmaqueue *dma_q = &dev->vidq;
995 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
996 struct em28xx_usb_bufs *usb_bufs;
1001 em28xx_isocdbg("em28xx: called em28xx_init_usb_xfer in mode %d\n",
1004 dev->usb_ctl.urb_data_copy = urb_data_copy;
1006 if (mode == EM28XX_DIGITAL_MODE) {
1007 usb_bufs = &dev->usb_ctl.digital_bufs;
1008 /* no need to free/alloc usb buffers in digital mode */
1011 usb_bufs = &dev->usb_ctl.analog_bufs;
1016 rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1017 max_pkt_size, packet_multiplier);
1023 rc = usb_clear_halt(dev->udev, usb_bufs->urb[0]->pipe);
1025 em28xx_err("failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1027 em28xx_uninit_usb_xfer(dev, mode);
1032 init_waitqueue_head(&dma_q->wq);
1033 init_waitqueue_head(&vbi_dma_q->wq);
1035 em28xx_capture_start(dev, 1);
1037 /* submit urbs and enables IRQ */
1038 for (i = 0; i < usb_bufs->num_bufs; i++) {
1039 rc = usb_submit_urb(usb_bufs->urb[i], GFP_ATOMIC);
1041 em28xx_err("submit of urb %i failed (error=%i)\n", i,
1043 em28xx_uninit_usb_xfer(dev, mode);
1050 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer);
1053 * Device control list
1056 static LIST_HEAD(em28xx_devlist);
1057 static DEFINE_MUTEX(em28xx_devlist_mutex);
1060 * Extension interface
1063 static LIST_HEAD(em28xx_extension_devlist);
1065 int em28xx_register_extension(struct em28xx_ops *ops)
1067 struct em28xx *dev = NULL;
1069 mutex_lock(&em28xx_devlist_mutex);
1070 list_add_tail(&ops->next, &em28xx_extension_devlist);
1071 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1074 mutex_unlock(&em28xx_devlist_mutex);
1075 printk(KERN_INFO "em28xx: Registered (%s) extension\n", ops->name);
1078 EXPORT_SYMBOL(em28xx_register_extension);
1080 void em28xx_unregister_extension(struct em28xx_ops *ops)
1082 struct em28xx *dev = NULL;
1084 mutex_lock(&em28xx_devlist_mutex);
1085 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1088 list_del(&ops->next);
1089 mutex_unlock(&em28xx_devlist_mutex);
1090 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1092 EXPORT_SYMBOL(em28xx_unregister_extension);
1094 void em28xx_init_extension(struct em28xx *dev)
1096 const struct em28xx_ops *ops = NULL;
1098 mutex_lock(&em28xx_devlist_mutex);
1099 list_add_tail(&dev->devlist, &em28xx_devlist);
1100 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1104 mutex_unlock(&em28xx_devlist_mutex);
1107 void em28xx_close_extension(struct em28xx *dev)
1109 const struct em28xx_ops *ops = NULL;
1111 mutex_lock(&em28xx_devlist_mutex);
1112 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1116 list_del(&dev->devlist);
1117 mutex_unlock(&em28xx_devlist_mutex);