1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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>
34
35#include "em28xx.h"
36
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>"
41
42MODULE_AUTHOR(DRIVER_AUTHOR);
43MODULE_DESCRIPTION(DRIVER_DESC);
44MODULE_LICENSE("GPL");
45MODULE_VERSION(EM28XX_VERSION);
46
47
48
49static unsigned int core_debug;
50module_param(core_debug, int, 0644);
51MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
52
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)
57
58static unsigned int reg_debug;
59module_param(reg_debug, int, 0644);
60MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
61
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)
66
67
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)
72
73
74
75
76
77int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
78 char *buf, int len)
79{
80 int ret;
81 int pipe = usb_rcvctrlpipe(dev->udev, 0);
82
83 if (dev->disconnected)
84 return -ENODEV;
85
86 if (len > URB_MAX_CTRL_SIZE)
87 return -EINVAL;
88
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);
97 }
98
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);
108 }
109
110 if (len)
111 memcpy(buf, dev->urb_buf, len);
112
113 mutex_unlock(&dev->ctrl_urb_lock);
114
115 if (reg_debug) {
116 int byte;
117
118 printk("<<<");
119 for (byte = 0; byte < len; byte++)
120 printk(" %02x", (unsigned char)buf[byte]);
121 printk("\n");
122 }
123
124 return ret;
125}
126
127
128
129
130
131int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
132{
133 int ret;
134 u8 val;
135
136 ret = em28xx_read_reg_req_len(dev, req, reg, &val, 1);
137 if (ret < 0)
138 return ret;
139
140 return val;
141}
142
143int em28xx_read_reg(struct em28xx *dev, u16 reg)
144{
145 return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
146}
147EXPORT_SYMBOL_GPL(em28xx_read_reg);
148
149
150
151
152
153int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
154 int len)
155{
156 int ret;
157 int pipe = usb_sndctrlpipe(dev->udev, 0);
158
159 if (dev->disconnected)
160 return -ENODEV;
161
162 if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
163 return -EINVAL;
164
165 if (reg_debug) {
166 int byte;
167
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);
175
176 for (byte = 0; byte < len; byte++)
177 printk(" %02x", (unsigned char)buf[byte]);
178 printk("\n");
179 }
180
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);
187
188 if (ret < 0)
189 return usb_translate_errors(ret);
190
191 if (dev->wait_after_write)
192 msleep(dev->wait_after_write);
193
194 return ret;
195}
196
197int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
198{
199 return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
200}
201EXPORT_SYMBOL_GPL(em28xx_write_regs);
202
203
204int em28xx_write_reg(struct em28xx *dev, u16 reg, u8 val)
205{
206 return em28xx_write_regs(dev, reg, &val, 1);
207}
208EXPORT_SYMBOL_GPL(em28xx_write_reg);
209
210
211
212
213
214
215int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
216 u8 bitmask)
217{
218 int oldval;
219 u8 newval;
220
221 oldval = em28xx_read_reg(dev, reg);
222 if (oldval < 0)
223 return oldval;
224
225 newval = (((u8)oldval) & ~bitmask) | (val & bitmask);
226
227 return em28xx_write_regs(dev, reg, &newval, 1);
228}
229EXPORT_SYMBOL_GPL(em28xx_write_reg_bits);
230
231
232
233
234
235int em28xx_toggle_reg_bits(struct em28xx *dev, u16 reg, u8 bitmask)
236{
237 int oldval;
238 u8 newval;
239
240 oldval = em28xx_read_reg(dev, reg);
241 if (oldval < 0)
242 return oldval;
243
244 newval = (~oldval & bitmask) | (oldval & ~bitmask);
245
246 return em28xx_write_reg(dev, reg, newval);
247}
248EXPORT_SYMBOL_GPL(em28xx_toggle_reg_bits);
249
250
251
252
253
254static int em28xx_is_ac97_ready(struct em28xx *dev)
255{
256 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_AC97_XFER_TIMEOUT);
257 int ret;
258
259
260 while (time_is_after_jiffies(timeout)) {
261 ret = em28xx_read_reg(dev, EM28XX_R43_AC97BUSY);
262 if (ret < 0)
263 return ret;
264
265 if (!(ret & 0x01))
266 return 0;
267 msleep(5);
268 }
269
270 em28xx_warn("AC97 command still being executed: not handled properly!\n");
271 return -EBUSY;
272}
273
274
275
276
277
278int em28xx_read_ac97(struct em28xx *dev, u8 reg)
279{
280 int ret;
281 u8 addr = (reg & 0x7f) | 0x80;
282 __le16 val;
283
284 ret = em28xx_is_ac97_ready(dev);
285 if (ret < 0)
286 return ret;
287
288 ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
289 if (ret < 0)
290 return ret;
291
292 ret = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R40_AC97LSB,
293 (u8 *)&val, sizeof(val));
294
295 if (ret < 0)
296 return ret;
297 return le16_to_cpu(val);
298}
299EXPORT_SYMBOL_GPL(em28xx_read_ac97);
300
301
302
303
304
305int em28xx_write_ac97(struct em28xx *dev, u8 reg, u16 val)
306{
307 int ret;
308 u8 addr = reg & 0x7f;
309 __le16 value;
310
311 value = cpu_to_le16(val);
312
313 ret = em28xx_is_ac97_ready(dev);
314 if (ret < 0)
315 return ret;
316
317 ret = em28xx_write_regs(dev, EM28XX_R40_AC97LSB, (u8 *)&value, 2);
318 if (ret < 0)
319 return ret;
320
321 ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
322 if (ret < 0)
323 return ret;
324
325 return 0;
326}
327EXPORT_SYMBOL_GPL(em28xx_write_ac97);
328
329struct em28xx_vol_itable {
330 enum em28xx_amux mux;
331 u8 reg;
332};
333
334static 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 },
342};
343
344static int set_ac97_input(struct em28xx *dev)
345{
346 int ret, i;
347 enum em28xx_amux amux = dev->ctl_ainput;
348
349
350
351
352 if (amux == EM28XX_AMUX_VIDEO2)
353 amux = EM28XX_AMUX_VIDEO;
354
355
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);
361
362 if (ret < 0)
363 em28xx_warn("couldn't setup AC97 register %d\n",
364 inputs[i].reg);
365 }
366 return 0;
367}
368
369static int em28xx_set_audio_source(struct em28xx *dev)
370{
371 int ret;
372 u8 input;
373
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;
379
380 ret = em28xx_write_regs(dev, EM2800_R08_AUDIOSRC, &input, 1);
381 if (ret < 0)
382 return ret;
383 }
384
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;
395 }
396 }
397
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);
402
403 ret = em28xx_write_reg_bits(dev, EM28XX_R0E_AUDIOSRC, input, 0xc0);
404 if (ret < 0)
405 return ret;
406 msleep(5);
407
408 switch (dev->audio_mode.ac97) {
409 case EM28XX_NO_AC97:
410 break;
411 default:
412 ret = set_ac97_input(dev);
413 }
414
415 return ret;
416}
417
418struct em28xx_vol_otable {
419 enum em28xx_aout mux;
420 u8 reg;
421};
422
423static 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 },
429};
430
431int em28xx_audio_analog_set(struct em28xx *dev)
432{
433 int ret, i;
434 u8 xclk;
435
436 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE)
437 return 0;
438
439
440
441
442 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
443
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);
449 }
450 }
451
452 xclk = dev->board.xclk & 0x7f;
453 if (!dev->mute)
454 xclk |= EM28XX_XCLK_AUDIO_UNMUTE;
455
456 ret = em28xx_write_reg(dev, EM28XX_R0F_XCLK, xclk);
457 if (ret < 0)
458 return ret;
459 msleep(10);
460
461
462 ret = em28xx_set_audio_source(dev);
463
464
465 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
466 int vol;
467
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);
471
472
473 vol = (0x1f - dev->volume) | ((0x1f - dev->volume) << 8);
474
475
476 if (dev->mute)
477 vol |= 0x8000;
478
479
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);
487 }
488
489 if (dev->ctl_aoutput & EM28XX_AOUT_PCM_IN) {
490 int sel = ac97_return_record_select(dev->ctl_aoutput);
491
492
493
494 sel |= (sel << 8);
495
496 em28xx_write_ac97(dev, AC97_REC_SEL, sel);
497 }
498 }
499
500 return ret;
501}
502EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
503
504int em28xx_audio_setup(struct em28xx *dev)
505{
506 int vid1, vid2, feat, cfg;
507 u32 vid = 0;
508 u8 i2s_samplerates;
509
510 if (dev->chip_id == CHIP_ID_EM2870 ||
511 dev->chip_id == CHIP_ID_EM2874 ||
512 dev->chip_id == CHIP_ID_EM28174 ||
513 dev->chip_id == CHIP_ID_EM28178) {
514
515 dev->int_audio_type = EM28XX_INT_AUDIO_NONE;
516 dev->usb_audio_type = EM28XX_USB_AUDIO_NONE;
517 return 0;
518 }
519
520
521 cfg = em28xx_read_reg(dev, EM28XX_R00_CHIPCFG);
522 em28xx_info("Config register raw data: 0x%02x\n", cfg);
523 if (cfg < 0) {
524
525 dev->int_audio_type = EM28XX_INT_AUDIO_AC97;
526 } else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) == 0x00) {
527
528 dev->int_audio_type = EM28XX_INT_AUDIO_NONE;
529 dev->usb_audio_type = EM28XX_USB_AUDIO_NONE;
530 return 0;
531 } else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) != EM28XX_CHIPCFG_AC97) {
532 dev->int_audio_type = EM28XX_INT_AUDIO_I2S;
533 if (dev->chip_id < CHIP_ID_EM2860 &&
534 (cfg & EM28XX_CHIPCFG_AUDIOMASK) ==
535 EM2820_CHIPCFG_I2S_1_SAMPRATE)
536 i2s_samplerates = 1;
537 else if (dev->chip_id >= CHIP_ID_EM2860 &&
538 (cfg & EM28XX_CHIPCFG_AUDIOMASK) ==
539 EM2860_CHIPCFG_I2S_5_SAMPRATES)
540 i2s_samplerates = 5;
541 else
542 i2s_samplerates = 3;
543 em28xx_info("I2S Audio (%d sample rate(s))\n",
544 i2s_samplerates);
545
546 dev->audio_mode.ac97 = EM28XX_NO_AC97;
547 goto init_audio;
548 } else {
549 dev->int_audio_type = EM28XX_INT_AUDIO_AC97;
550 }
551
552 dev->audio_mode.ac97 = EM28XX_AC97_OTHER;
553
554 vid1 = em28xx_read_ac97(dev, AC97_VENDOR_ID1);
555 if (vid1 < 0) {
556
557
558
559
560
561 em28xx_warn("AC97 chip type couldn't be determined\n");
562 dev->audio_mode.ac97 = EM28XX_NO_AC97;
563 if (dev->usb_audio_type == EM28XX_USB_AUDIO_VENDOR)
564 dev->usb_audio_type = EM28XX_USB_AUDIO_NONE;
565 dev->int_audio_type = EM28XX_INT_AUDIO_NONE;
566 goto init_audio;
567 }
568
569 vid2 = em28xx_read_ac97(dev, AC97_VENDOR_ID2);
570 if (vid2 < 0)
571 goto init_audio;
572
573 vid = vid1 << 16 | vid2;
574 em28xx_warn("AC97 vendor ID = 0x%08x\n", vid);
575
576 feat = em28xx_read_ac97(dev, AC97_RESET);
577 if (feat < 0)
578 goto init_audio;
579
580 em28xx_warn("AC97 features = 0x%04x\n", feat);
581
582
583 if (((vid == 0xffffffff) || (vid == 0x83847650)) && (feat == 0x6a90))
584 dev->audio_mode.ac97 = EM28XX_AC97_EM202;
585 else if ((vid >> 8) == 0x838476)
586 dev->audio_mode.ac97 = EM28XX_AC97_SIGMATEL;
587
588init_audio:
589
590 switch (dev->audio_mode.ac97) {
591 case EM28XX_NO_AC97:
592 em28xx_info("No AC97 audio processor\n");
593 break;
594 case EM28XX_AC97_EM202:
595 em28xx_info("Empia 202 AC97 audio processor detected\n");
596 break;
597 case EM28XX_AC97_SIGMATEL:
598 em28xx_info("Sigmatel audio processor detected (stac 97%02x)\n",
599 vid & 0xff);
600 break;
601 case EM28XX_AC97_OTHER:
602 em28xx_warn("Unknown AC97 audio processor detected!\n");
603 break;
604 default:
605 break;
606 }
607
608 return em28xx_audio_analog_set(dev);
609}
610EXPORT_SYMBOL_GPL(em28xx_audio_setup);
611
612const struct em28xx_led *em28xx_find_led(struct em28xx *dev,
613 enum em28xx_led_role role)
614{
615 if (dev->board.leds) {
616 u8 k = 0;
617
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];
622 k++;
623 }
624 }
625 return NULL;
626}
627EXPORT_SYMBOL_GPL(em28xx_find_led);
628
629int em28xx_capture_start(struct em28xx *dev, int start)
630{
631 int rc;
632 const struct em28xx_led *led = NULL;
633
634 if (dev->chip_id == CHIP_ID_EM2874 ||
635 dev->chip_id == CHIP_ID_EM2884 ||
636 dev->chip_id == CHIP_ID_EM28174 ||
637 dev->chip_id == CHIP_ID_EM28178) {
638
639 rc = em28xx_write_reg_bits(dev, EM2874_R5F_TS_ENABLE,
640 start ?
641 EM2874_TS1_CAPTURE_ENABLE : 0x00,
642 EM2874_TS1_CAPTURE_ENABLE);
643 } else {
644
645
646 rc = em28xx_write_reg_bits(dev, EM28XX_R0C_USBSUSP,
647 start ? 0x10 : 0x00, 0x10);
648 if (rc < 0)
649 return rc;
650
651 if (start) {
652 if (dev->board.is_webcam)
653 rc = em28xx_write_reg(dev, 0x13, 0x0c);
654
655
656 rc = em28xx_write_reg(dev, 0x48, 0x00);
657 if (rc < 0)
658 return rc;
659
660 if (dev->mode == EM28XX_ANALOG_MODE)
661 rc = em28xx_write_reg(dev,
662 EM28XX_R12_VINENABLE,
663 0x67);
664 else
665 rc = em28xx_write_reg(dev,
666 EM28XX_R12_VINENABLE,
667 0x37);
668 if (rc < 0)
669 return rc;
670
671 msleep(6);
672 } else {
673
674 rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x27);
675 }
676 }
677
678 if (dev->mode == EM28XX_ANALOG_MODE)
679 led = em28xx_find_led(dev, EM28XX_LED_ANALOG_CAPTURING);
680 else
681 led = em28xx_find_led(dev, EM28XX_LED_DIGITAL_CAPTURING);
682
683 if (led)
684 em28xx_write_reg_bits(dev, led->gpio_reg,
685 (!start ^ led->inverted) ?
686 ~led->gpio_mask : led->gpio_mask,
687 led->gpio_mask);
688
689 return rc;
690}
691
692int em28xx_gpio_set(struct em28xx *dev, struct em28xx_reg_seq *gpio)
693{
694 int rc = 0;
695
696 if (!gpio)
697 return rc;
698
699 if (dev->mode != EM28XX_SUSPEND) {
700 em28xx_write_reg(dev, 0x48, 0x00);
701 if (dev->mode == EM28XX_ANALOG_MODE)
702 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);
703 else
704 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);
705 msleep(6);
706 }
707
708
709 while (gpio->sleep >= 0) {
710 if (gpio->reg >= 0) {
711 rc = em28xx_write_reg_bits(dev,
712 gpio->reg,
713 gpio->val,
714 gpio->mask);
715 if (rc < 0)
716 return rc;
717 }
718 if (gpio->sleep > 0)
719 msleep(gpio->sleep);
720
721 gpio++;
722 }
723 return rc;
724}
725EXPORT_SYMBOL_GPL(em28xx_gpio_set);
726
727int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode)
728{
729 if (dev->mode == set_mode)
730 return 0;
731
732 if (set_mode == EM28XX_SUSPEND) {
733 dev->mode = set_mode;
734
735
736
737 return em28xx_gpio_set(dev, dev->board.suspend_gpio);
738 }
739
740 dev->mode = set_mode;
741
742 if (dev->mode == EM28XX_DIGITAL_MODE)
743 return em28xx_gpio_set(dev, dev->board.dvb_gpio);
744 else
745 return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
746}
747EXPORT_SYMBOL_GPL(em28xx_set_mode);
748
749
750
751
752
753
754
755
756static void em28xx_irq_callback(struct urb *urb)
757{
758 struct em28xx *dev = urb->context;
759 int i;
760
761 switch (urb->status) {
762 case 0:
763 case -ETIMEDOUT:
764 break;
765 case -ECONNRESET:
766 case -ENOENT:
767 case -ESHUTDOWN:
768 return;
769 default:
770 em28xx_isocdbg("urb completition error %d.\n", urb->status);
771 break;
772 }
773
774
775 spin_lock(&dev->slock);
776 dev->usb_ctl.urb_data_copy(dev, urb);
777 spin_unlock(&dev->slock);
778
779
780 for (i = 0; i < urb->number_of_packets; i++) {
781
782 urb->iso_frame_desc[i].status = 0;
783 urb->iso_frame_desc[i].actual_length = 0;
784 }
785 urb->status = 0;
786
787 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
788 if (urb->status) {
789 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
790 urb->status);
791 }
792}
793
794
795
796
797void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
798{
799 struct urb *urb;
800 struct em28xx_usb_bufs *usb_bufs;
801 int i;
802
803 em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
804 mode);
805
806 if (mode == EM28XX_DIGITAL_MODE)
807 usb_bufs = &dev->usb_ctl.digital_bufs;
808 else
809 usb_bufs = &dev->usb_ctl.analog_bufs;
810
811 for (i = 0; i < usb_bufs->num_bufs; i++) {
812 urb = usb_bufs->urb[i];
813 if (urb) {
814 if (!irqs_disabled())
815 usb_kill_urb(urb);
816 else
817 usb_unlink_urb(urb);
818
819 if (usb_bufs->transfer_buffer[i]) {
820 usb_free_coherent(dev->udev,
821 urb->transfer_buffer_length,
822 usb_bufs->transfer_buffer[i],
823 urb->transfer_dma);
824 }
825 usb_free_urb(urb);
826 usb_bufs->urb[i] = NULL;
827 }
828 usb_bufs->transfer_buffer[i] = NULL;
829 }
830
831 kfree(usb_bufs->urb);
832 kfree(usb_bufs->transfer_buffer);
833
834 usb_bufs->urb = NULL;
835 usb_bufs->transfer_buffer = NULL;
836 usb_bufs->num_bufs = 0;
837
838 em28xx_capture_start(dev, 0);
839}
840EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer);
841
842
843
844
845void em28xx_stop_urbs(struct em28xx *dev)
846{
847 int i;
848 struct urb *urb;
849 struct em28xx_usb_bufs *isoc_bufs = &dev->usb_ctl.digital_bufs;
850
851 em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n");
852
853 for (i = 0; i < isoc_bufs->num_bufs; i++) {
854 urb = isoc_bufs->urb[i];
855 if (urb) {
856 if (!irqs_disabled())
857 usb_kill_urb(urb);
858 else
859 usb_unlink_urb(urb);
860 }
861 }
862
863 em28xx_capture_start(dev, 0);
864}
865EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
866
867
868
869
870int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
871 int num_bufs, int max_pkt_size, int packet_multiplier)
872{
873 struct em28xx_usb_bufs *usb_bufs;
874 int i;
875 int sb_size, pipe;
876 struct urb *urb;
877 int j, k;
878
879 em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode);
880
881
882
883 if (mode == EM28XX_DIGITAL_MODE) {
884 if ((xfer_bulk && !dev->dvb_ep_bulk) ||
885 (!xfer_bulk && !dev->dvb_ep_isoc)) {
886 em28xx_errdev("no endpoint for DVB mode and transfer type %d\n",
887 xfer_bulk > 0);
888 return -EINVAL;
889 }
890 usb_bufs = &dev->usb_ctl.digital_bufs;
891 } else if (mode == EM28XX_ANALOG_MODE) {
892 if ((xfer_bulk && !dev->analog_ep_bulk) ||
893 (!xfer_bulk && !dev->analog_ep_isoc)) {
894 em28xx_errdev("no endpoint for analog mode and transfer type %d\n",
895 xfer_bulk > 0);
896 return -EINVAL;
897 }
898 usb_bufs = &dev->usb_ctl.analog_bufs;
899 } else {
900 em28xx_errdev("invalid mode selected\n");
901 return -EINVAL;
902 }
903
904
905 em28xx_uninit_usb_xfer(dev, mode);
906
907 usb_bufs->num_bufs = num_bufs;
908
909 usb_bufs->urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
910 if (!usb_bufs->urb) {
911 em28xx_errdev("cannot alloc memory for usb buffers\n");
912 return -ENOMEM;
913 }
914
915 usb_bufs->transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
916 GFP_KERNEL);
917 if (!usb_bufs->transfer_buffer) {
918 em28xx_errdev("cannot allocate memory for usb transfer\n");
919 kfree(usb_bufs->urb);
920 return -ENOMEM;
921 }
922
923 usb_bufs->max_pkt_size = max_pkt_size;
924 if (xfer_bulk)
925 usb_bufs->num_packets = 0;
926 else
927 usb_bufs->num_packets = packet_multiplier;
928 dev->usb_ctl.vid_buf = NULL;
929 dev->usb_ctl.vbi_buf = NULL;
930
931 sb_size = packet_multiplier * usb_bufs->max_pkt_size;
932
933
934 for (i = 0; i < usb_bufs->num_bufs; i++) {
935 urb = usb_alloc_urb(usb_bufs->num_packets, GFP_KERNEL);
936 if (!urb) {
937 em28xx_err("cannot alloc usb_ctl.urb %i\n", i);
938 em28xx_uninit_usb_xfer(dev, mode);
939 return -ENOMEM;
940 }
941 usb_bufs->urb[i] = urb;
942
943 usb_bufs->transfer_buffer[i] = usb_alloc_coherent(dev->udev,
944 sb_size, GFP_KERNEL, &urb->transfer_dma);
945 if (!usb_bufs->transfer_buffer[i]) {
946 em28xx_err("unable to allocate %i bytes for transfer"
947 " buffer %i%s\n",
948 sb_size, i,
949 in_interrupt() ? " while in int" : "");
950 em28xx_uninit_usb_xfer(dev, mode);
951 return -ENOMEM;
952 }
953 memset(usb_bufs->transfer_buffer[i], 0, sb_size);
954
955 if (xfer_bulk) {
956 pipe = usb_rcvbulkpipe(dev->udev,
957 mode == EM28XX_ANALOG_MODE ?
958 dev->analog_ep_bulk :
959 dev->dvb_ep_bulk);
960 usb_fill_bulk_urb(urb, dev->udev, pipe,
961 usb_bufs->transfer_buffer[i], sb_size,
962 em28xx_irq_callback, dev);
963 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
964 } else {
965 pipe = usb_rcvisocpipe(dev->udev,
966 mode == EM28XX_ANALOG_MODE ?
967 dev->analog_ep_isoc :
968 dev->dvb_ep_isoc);
969 usb_fill_int_urb(urb, dev->udev, pipe,
970 usb_bufs->transfer_buffer[i], sb_size,
971 em28xx_irq_callback, dev, 1);
972 urb->transfer_flags = URB_ISO_ASAP |
973 URB_NO_TRANSFER_DMA_MAP;
974 k = 0;
975 for (j = 0; j < usb_bufs->num_packets; j++) {
976 urb->iso_frame_desc[j].offset = k;
977 urb->iso_frame_desc[j].length =
978 usb_bufs->max_pkt_size;
979 k += usb_bufs->max_pkt_size;
980 }
981 }
982
983 urb->number_of_packets = usb_bufs->num_packets;
984 }
985
986 return 0;
987}
988EXPORT_SYMBOL_GPL(em28xx_alloc_urbs);
989
990
991
992
993int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
994 int xfer_bulk, int num_bufs, int max_pkt_size,
995 int packet_multiplier,
996 int (*urb_data_copy)(struct em28xx *dev, struct urb *urb))
997{
998 struct em28xx_dmaqueue *dma_q = &dev->vidq;
999 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
1000 struct em28xx_usb_bufs *usb_bufs;
1001 int i;
1002 int rc;
1003 int alloc;
1004
1005 em28xx_isocdbg("em28xx: called em28xx_init_usb_xfer in mode %d\n",
1006 mode);
1007
1008 dev->usb_ctl.urb_data_copy = urb_data_copy;
1009
1010 if (mode == EM28XX_DIGITAL_MODE) {
1011 usb_bufs = &dev->usb_ctl.digital_bufs;
1012
1013 alloc = 0;
1014 } else {
1015 usb_bufs = &dev->usb_ctl.analog_bufs;
1016 alloc = 1;
1017 }
1018
1019 if (alloc) {
1020 rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1021 max_pkt_size, packet_multiplier);
1022 if (rc)
1023 return rc;
1024 }
1025
1026 if (xfer_bulk) {
1027 rc = usb_clear_halt(dev->udev, usb_bufs->urb[0]->pipe);
1028 if (rc < 0) {
1029 em28xx_err("failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1030 rc);
1031 em28xx_uninit_usb_xfer(dev, mode);
1032 return rc;
1033 }
1034 }
1035
1036 init_waitqueue_head(&dma_q->wq);
1037 init_waitqueue_head(&vbi_dma_q->wq);
1038
1039 em28xx_capture_start(dev, 1);
1040
1041
1042 for (i = 0; i < usb_bufs->num_bufs; i++) {
1043 rc = usb_submit_urb(usb_bufs->urb[i], GFP_ATOMIC);
1044 if (rc) {
1045 em28xx_err("submit of urb %i failed (error=%i)\n", i,
1046 rc);
1047 em28xx_uninit_usb_xfer(dev, mode);
1048 return rc;
1049 }
1050 }
1051
1052 return 0;
1053}
1054EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer);
1055
1056
1057
1058
1059
1060static LIST_HEAD(em28xx_devlist);
1061static DEFINE_MUTEX(em28xx_devlist_mutex);
1062
1063
1064
1065
1066
1067static LIST_HEAD(em28xx_extension_devlist);
1068
1069int em28xx_register_extension(struct em28xx_ops *ops)
1070{
1071 struct em28xx *dev = NULL;
1072
1073 mutex_lock(&em28xx_devlist_mutex);
1074 list_add_tail(&ops->next, &em28xx_extension_devlist);
1075 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1076 ops->init(dev);
1077 }
1078 mutex_unlock(&em28xx_devlist_mutex);
1079 printk(KERN_INFO "em28xx: Registered (%s) extension\n", ops->name);
1080 return 0;
1081}
1082EXPORT_SYMBOL(em28xx_register_extension);
1083
1084void em28xx_unregister_extension(struct em28xx_ops *ops)
1085{
1086 struct em28xx *dev = NULL;
1087
1088 mutex_lock(&em28xx_devlist_mutex);
1089 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1090 ops->fini(dev);
1091 }
1092 list_del(&ops->next);
1093 mutex_unlock(&em28xx_devlist_mutex);
1094 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1095}
1096EXPORT_SYMBOL(em28xx_unregister_extension);
1097
1098void em28xx_init_extension(struct em28xx *dev)
1099{
1100 const struct em28xx_ops *ops = NULL;
1101
1102 mutex_lock(&em28xx_devlist_mutex);
1103 list_add_tail(&dev->devlist, &em28xx_devlist);
1104 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1105 if (ops->init)
1106 ops->init(dev);
1107 }
1108 mutex_unlock(&em28xx_devlist_mutex);
1109}
1110
1111void em28xx_close_extension(struct em28xx *dev)
1112{
1113 const struct em28xx_ops *ops = NULL;
1114
1115 mutex_lock(&em28xx_devlist_mutex);
1116 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1117 if (ops->fini)
1118 ops->fini(dev);
1119 }
1120 list_del(&dev->devlist);
1121 mutex_unlock(&em28xx_devlist_mutex);
1122}
1123
1124int em28xx_suspend_extension(struct em28xx *dev)
1125{
1126 const struct em28xx_ops *ops = NULL;
1127
1128 em28xx_info("Suspending extensions\n");
1129 mutex_lock(&em28xx_devlist_mutex);
1130 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1131 if (ops->suspend)
1132 ops->suspend(dev);
1133 }
1134 mutex_unlock(&em28xx_devlist_mutex);
1135 return 0;
1136}
1137
1138int em28xx_resume_extension(struct em28xx *dev)
1139{
1140 const struct em28xx_ops *ops = NULL;
1141
1142 em28xx_info("Resuming extensions\n");
1143 mutex_lock(&em28xx_devlist_mutex);
1144 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1145 if (ops->resume)
1146 ops->resume(dev);
1147 }
1148 mutex_unlock(&em28xx_devlist_mutex);
1149 return 0;
1150}
1151