PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / usb / em28xx / em28xx-core.c
blob898fb9bd88a279db1cec20f44dc952eb4a721254
1 /*
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>
35 #include "em28xx.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 {\
54 if (core_debug) \
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 {\
63 if (reg_debug) \
64 printk(KERN_INFO "%s %s :"fmt, \
65 dev->name, __func__ , ##arg); } while (0)
67 /* FIXME */
68 #define em28xx_isocdbg(fmt, arg...) do {\
69 if (core_debug) \
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,
78 char *buf, int len)
80 int ret;
81 int pipe = usb_rcvctrlpipe(dev->udev, 0);
83 if (dev->disconnected)
84 return -ENODEV;
86 if (len > URB_MAX_CTRL_SIZE)
87 return -EINVAL;
89 if (reg_debug) {
90 printk(KERN_DEBUG "(pipe 0x%08x): "
91 "IN: %02x %02x %02x %02x %02x %02x %02x %02x ",
92 pipe,
93 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
94 req, 0, 0,
95 reg & 0xff, reg >> 8,
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);
103 if (ret < 0) {
104 if (reg_debug)
105 printk(" failed!\n");
106 mutex_unlock(&dev->ctrl_urb_lock);
107 return usb_translate_errors(ret);
110 if (len)
111 memcpy(buf, dev->urb_buf, len);
113 mutex_unlock(&dev->ctrl_urb_lock);
115 if (reg_debug) {
116 int byte;
118 printk("<<<");
119 for (byte = 0; byte < len; byte++)
120 printk(" %02x", (unsigned char)buf[byte]);
121 printk("\n");
124 return ret;
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)
133 int ret;
134 u8 val;
136 ret = em28xx_read_reg_req_len(dev, req, reg, &val, 1);
137 if (ret < 0)
138 return ret;
140 return val;
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,
154 int len)
156 int ret;
157 int pipe = usb_sndctrlpipe(dev->udev, 0);
159 if (dev->disconnected)
160 return -ENODEV;
162 if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
163 return -EINVAL;
165 if (reg_debug) {
166 int byte;
168 printk(KERN_DEBUG "(pipe 0x%08x): "
169 "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
170 pipe,
171 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
172 req, 0, 0,
173 reg & 0xff, reg >> 8,
174 len & 0xff, len >> 8);
176 for (byte = 0; byte < len; byte++)
177 printk(" %02x", (unsigned char)buf[byte]);
178 printk("\n");
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);
188 if (ret < 0)
189 return usb_translate_errors(ret);
191 if (dev->wait_after_write)
192 msleep(dev->wait_after_write);
194 return ret;
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
213 * the actual value
215 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
216 u8 bitmask)
218 int oldval;
219 u8 newval;
221 oldval = em28xx_read_reg(dev, reg);
222 if (oldval < 0)
223 return oldval;
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)
237 int oldval;
238 u8 newval;
240 oldval = em28xx_read_reg(dev, reg);
241 if (oldval < 0)
242 return oldval;
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);
257 int ret;
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);
262 if (ret < 0)
263 return ret;
265 if (!(ret & 0x01))
266 return 0;
267 msleep(5);
270 em28xx_warn("AC97 command still being executed: not handled properly!\n");
271 return -EBUSY;
275 * em28xx_read_ac97()
276 * write a 16 bit value to the specified AC97 address (LSB first!)
278 int em28xx_read_ac97(struct em28xx *dev, u8 reg)
280 int ret;
281 u8 addr = (reg & 0x7f) | 0x80;
282 u16 val;
284 ret = em28xx_is_ac97_ready(dev);
285 if (ret < 0)
286 return ret;
288 ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
289 if (ret < 0)
290 return ret;
292 ret = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R40_AC97LSB,
293 (u8 *)&val, sizeof(val));
295 if (ret < 0)
296 return ret;
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)
307 int ret;
308 u8 addr = reg & 0x7f;
309 __le16 value;
311 value = cpu_to_le16(val);
313 ret = em28xx_is_ac97_ready(dev);
314 if (ret < 0)
315 return ret;
317 ret = em28xx_write_regs(dev, EM28XX_R40_AC97LSB, (u8 *) &value, 2);
318 if (ret < 0)
319 return ret;
321 ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
322 if (ret < 0)
323 return ret;
325 return 0;
327 EXPORT_SYMBOL_GPL(em28xx_write_ac97);
329 struct em28xx_vol_itable {
330 enum em28xx_amux mux;
331 u8 reg;
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)
346 int ret, i;
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);
359 else
360 ret = em28xx_write_ac97(dev, inputs[i].reg, 0x8000);
362 if (ret < 0)
363 em28xx_warn("couldn't setup AC97 register %d\n",
364 inputs[i].reg);
366 return 0;
369 static int em28xx_set_audio_source(struct em28xx *dev)
371 int ret;
372 u8 input;
374 if (dev->board.is_em2800) {
375 if (dev->ctl_ainput == EM28XX_AMUX_VIDEO)
376 input = EM2800_AUDIO_SRC_TUNER;
377 else
378 input = EM2800_AUDIO_SRC_LINE;
380 ret = em28xx_write_regs(dev, EM2800_R08_AUDIOSRC, &input, 1);
381 if (ret < 0)
382 return ret;
385 if (dev->board.has_msp34xx)
386 input = EM28XX_AUDIO_SRC_TUNER;
387 else {
388 switch (dev->ctl_ainput) {
389 case EM28XX_AMUX_VIDEO:
390 input = EM28XX_AUDIO_SRC_TUNER;
391 break;
392 default:
393 input = EM28XX_AUDIO_SRC_LINE;
394 break;
398 if (dev->board.mute_gpio && dev->mute)
399 em28xx_gpio_set(dev, dev->board.mute_gpio);
400 else
401 em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
403 ret = em28xx_write_reg_bits(dev, EM28XX_R0E_AUDIOSRC, input, 0xc0);
404 if (ret < 0)
405 return ret;
406 msleep(5);
408 switch (dev->audio_mode.ac97) {
409 case EM28XX_NO_AC97:
410 break;
411 default:
412 ret = set_ac97_input(dev);
415 return ret;
418 struct em28xx_vol_otable {
419 enum em28xx_aout mux;
420 u8 reg;
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)
433 int ret, i;
434 u8 xclk;
436 if (!dev->audio_mode.has_audio)
437 return 0;
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);
446 if (ret < 0)
447 em28xx_warn("couldn't setup AC97 register %d\n",
448 outputs[i].reg);
452 xclk = dev->board.xclk & 0x7f;
453 if (!dev->mute)
454 xclk |= EM28XX_XCLK_AUDIO_UNMUTE;
456 ret = em28xx_write_reg(dev, EM28XX_R0F_XCLK, xclk);
457 if (ret < 0)
458 return ret;
459 msleep(10);
461 /* Selects the proper audio input */
462 ret = em28xx_set_audio_source(dev);
464 /* Sets volume */
465 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
466 int vol;
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 */
476 if (dev->mute)
477 vol |= 0x8000;
479 /* Sets volume */
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,
483 vol);
484 if (ret < 0)
485 em28xx_warn("couldn't setup AC97 register %d\n",
486 outputs[i].reg);
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
493 channels */
494 sel |= (sel << 8);
496 em28xx_write_ac97(dev, AC97_REC_SEL, sel);
500 return ret;
502 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
504 int em28xx_audio_setup(struct em28xx *dev)
506 int vid1, vid2, feat, cfg;
507 u32 vid;
509 if (!dev->audio_mode.has_audio)
510 return 0;
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);
515 if (cfg < 0) {
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;
522 return 0;
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;
532 else
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;
538 goto init_audio;
541 dev->audio_mode.ac97 = EM28XX_AC97_OTHER;
543 vid1 = em28xx_read_ac97(dev, AC97_VENDOR_ID1);
544 if (vid1 < 0) {
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;
554 goto init_audio;
557 vid2 = em28xx_read_ac97(dev, AC97_VENDOR_ID2);
558 if (vid2 < 0)
559 goto init_audio;
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);
567 if (feat < 0)
568 goto init_audio;
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;
579 init_audio:
580 /* Reports detected AC97 processor */
581 switch (dev->audio_mode.ac97) {
582 case EM28XX_NO_AC97:
583 em28xx_info("No AC97 audio processor\n");
584 break;
585 case EM28XX_AC97_EM202:
586 em28xx_info("Empia 202 AC97 audio processor detected\n");
587 break;
588 case EM28XX_AC97_SIGMATEL:
589 em28xx_info("Sigmatel audio processor detected(stac 97%02x)\n",
590 dev->audio_mode.ac97_vendor_id & 0xff);
591 break;
592 case EM28XX_AC97_OTHER:
593 em28xx_warn("Unknown AC97 audio processor detected!\n");
594 break;
595 default:
596 break;
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) {
607 u8 k = 0;
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];
612 k++;
615 return NULL;
617 EXPORT_SYMBOL_GPL(em28xx_find_led);
619 int em28xx_capture_start(struct em28xx *dev, int start)
621 int rc;
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,
629 start ?
630 EM2874_TS1_CAPTURE_ENABLE : 0x00,
631 EM2874_TS1_CAPTURE_ENABLE);
632 } else {
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);
637 if (rc < 0)
638 return rc;
640 if (start) {
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);
650 else
651 rc = em28xx_write_reg(dev,
652 EM28XX_R12_VINENABLE, 0x37);
654 msleep(6);
655 } else {
656 /* disable video capture */
657 rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x27);
661 if (rc < 0)
662 return rc;
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);
668 if (led)
669 em28xx_write_reg_bits(dev, led->gpio_reg,
670 (!start ^ led->inverted) ?
671 ~led->gpio_mask : led->gpio_mask,
672 led->gpio_mask);
675 return rc;
678 int em28xx_gpio_set(struct em28xx *dev, struct em28xx_reg_seq *gpio)
680 int rc = 0;
682 if (!gpio)
683 return rc;
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);
689 else
690 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);
691 msleep(6);
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,
698 gpio->reg,
699 gpio->val,
700 gpio->mask);
701 if (rc < 0)
702 return rc;
704 if (gpio->sleep > 0)
705 msleep(gpio->sleep);
707 gpio++;
709 return rc;
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)
716 return 0;
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);
730 else
731 return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
733 EXPORT_SYMBOL_GPL(em28xx_set_mode);
735 /* ------------------------------------------------------------------
736 URB control
737 ------------------------------------------------------------------*/
740 * URB completion handler for isoc/bulk transfers
742 static void em28xx_irq_callback(struct urb *urb)
744 struct em28xx *dev = urb->context;
745 int i;
747 switch (urb->status) {
748 case 0: /* success */
749 case -ETIMEDOUT: /* NAK */
750 break;
751 case -ECONNRESET: /* kill */
752 case -ENOENT:
753 case -ESHUTDOWN:
754 return;
755 default: /* error */
756 em28xx_isocdbg("urb completition error %d.\n", urb->status);
757 break;
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;
771 urb->status = 0;
773 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
774 if (urb->status) {
775 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
776 urb->status);
781 * Stop and Deallocate URBs
783 void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
785 struct urb *urb;
786 struct em28xx_usb_bufs *usb_bufs;
787 int i;
789 em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
790 mode);
792 if (mode == EM28XX_DIGITAL_MODE)
793 usb_bufs = &dev->usb_ctl.digital_bufs;
794 else
795 usb_bufs = &dev->usb_ctl.analog_bufs;
797 for (i = 0; i < usb_bufs->num_bufs; i++) {
798 urb = usb_bufs->urb[i];
799 if (urb) {
800 if (!irqs_disabled())
801 usb_kill_urb(urb);
802 else
803 usb_unlink_urb(urb);
805 if (usb_bufs->transfer_buffer[i]) {
806 usb_free_coherent(dev->udev,
807 urb->transfer_buffer_length,
808 usb_bufs->transfer_buffer[i],
809 urb->transfer_dma);
811 usb_free_urb(urb);
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);
829 * Stop URBs
831 void em28xx_stop_urbs(struct em28xx *dev)
833 int i;
834 struct urb *urb;
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];
841 if (urb) {
842 if (!irqs_disabled())
843 usb_kill_urb(urb);
844 else
845 usb_unlink_urb(urb);
849 em28xx_capture_start(dev, 0);
851 EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
854 * Allocate 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;
860 int i;
861 int sb_size, pipe;
862 struct urb *urb;
863 int j, k;
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",
873 xfer_bulk > 0);
874 return -EINVAL;
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",
881 xfer_bulk > 0);
882 return -EINVAL;
884 usb_bufs = &dev->usb_ctl.analog_bufs;
885 } else {
886 em28xx_errdev("invalid mode selected\n");
887 return -EINVAL;
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");
898 return -ENOMEM;
901 usb_bufs->transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
902 GFP_KERNEL);
903 if (!usb_bufs->transfer_buffer) {
904 em28xx_errdev("cannot allocate memory for usb transfer\n");
905 kfree(usb_bufs->urb);
906 return -ENOMEM;
909 usb_bufs->max_pkt_size = max_pkt_size;
910 if (xfer_bulk)
911 usb_bufs->num_packets = 0;
912 else
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);
922 if (!urb) {
923 em28xx_err("cannot alloc usb_ctl.urb %i\n", i);
924 em28xx_uninit_usb_xfer(dev, mode);
925 return -ENOMEM;
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"
933 " buffer %i%s\n",
934 sb_size, i,
935 in_interrupt() ? " while in int" : "");
936 em28xx_uninit_usb_xfer(dev, mode);
937 return -ENOMEM;
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 :
945 dev->dvb_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;
950 } else { /* isoc */
951 pipe = usb_rcvisocpipe(dev->udev,
952 mode == EM28XX_ANALOG_MODE ?
953 dev->analog_ep_isoc :
954 dev->dvb_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;
960 k = 0;
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;
972 return 0;
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;
987 int i;
988 int rc;
989 int alloc;
991 em28xx_isocdbg("em28xx: called em28xx_init_usb_xfer in mode %d\n",
992 mode);
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 */
999 alloc = 0;
1000 } else {
1001 usb_bufs = &dev->usb_ctl.analog_bufs;
1002 alloc = 1;
1005 if (alloc) {
1006 rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1007 max_pkt_size, packet_multiplier);
1008 if (rc)
1009 return rc;
1012 if (xfer_bulk) {
1013 rc = usb_clear_halt(dev->udev, usb_bufs->urb[0]->pipe);
1014 if (rc < 0) {
1015 em28xx_err("failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1016 rc);
1017 em28xx_uninit_usb_xfer(dev, mode);
1018 return rc;
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);
1030 if (rc) {
1031 em28xx_err("submit of urb %i failed (error=%i)\n", i,
1032 rc);
1033 em28xx_uninit_usb_xfer(dev, mode);
1034 return rc;
1038 return 0;
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) {
1062 ops->init(dev);
1064 mutex_unlock(&em28xx_devlist_mutex);
1065 printk(KERN_INFO "em28xx: Registered (%s) extension\n", ops->name);
1066 return 0;
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) {
1076 ops->fini(dev);
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) {
1091 if (ops->init)
1092 ops->init(dev);
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) {
1103 if (ops->fini)
1104 ops->fini(dev);
1106 list_del(&dev->devlist);
1107 mutex_unlock(&em28xx_devlist_mutex);