1 /***************************************************************************
3 Copyright (c) 2005-2009, Broadcom Corporation.
5 This driver is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2 of the License.
9 This driver is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this driver. If not, see <http://www.gnu.org/licenses/>.
16 ***************************************************************************/
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
21 #include "crystalhd_lnx.h"
23 static DEFINE_MUTEX(chd_dec_mutex
);
24 static struct class *crystalhd_class
;
26 static struct crystalhd_adp
*g_adp_info
;
28 static irqreturn_t
chd_dec_isr(int irq
, void *arg
)
30 struct crystalhd_adp
*adp
= (struct crystalhd_adp
*) arg
;
33 rc
= crystalhd_cmd_interrupt(&adp
->cmds
);
35 return IRQ_RETVAL(rc
);
38 static int chd_dec_enable_int(struct crystalhd_adp
*adp
)
42 if (!adp
|| !adp
->pdev
) {
43 BCMLOG_ERR("Invalid arg!!\n");
47 if (adp
->pdev
->msi_enabled
)
50 adp
->msi
= pci_enable_msi(adp
->pdev
);
52 rc
= request_irq(adp
->pdev
->irq
, chd_dec_isr
, IRQF_SHARED
,
53 adp
->name
, (void *)adp
);
55 BCMLOG_ERR("Interrupt request failed..\n");
56 pci_disable_msi(adp
->pdev
);
62 static int chd_dec_disable_int(struct crystalhd_adp
*adp
)
64 if (!adp
|| !adp
->pdev
) {
65 BCMLOG_ERR("Invalid arg!!\n");
69 free_irq(adp
->pdev
->irq
, adp
);
72 pci_disable_msi(adp
->pdev
);
77 struct crystalhd_ioctl_data
*chd_dec_alloc_iodata(struct crystalhd_adp
*adp
, bool isr
)
79 unsigned long flags
= 0;
80 struct crystalhd_ioctl_data
*temp
;
85 spin_lock_irqsave(&adp
->lock
, flags
);
87 temp
= adp
->idata_free_head
;
89 adp
->idata_free_head
= adp
->idata_free_head
->next
;
90 memset(temp
, 0, sizeof(*temp
));
93 spin_unlock_irqrestore(&adp
->lock
, flags
);
97 void chd_dec_free_iodata(struct crystalhd_adp
*adp
, struct crystalhd_ioctl_data
*iodata
,
100 unsigned long flags
= 0;
105 spin_lock_irqsave(&adp
->lock
, flags
);
106 iodata
->next
= adp
->idata_free_head
;
107 adp
->idata_free_head
= iodata
;
108 spin_unlock_irqrestore(&adp
->lock
, flags
);
111 static inline int crystalhd_user_data(unsigned long ud
, void *dr
, int size
, int set
)
116 BCMLOG_ERR("Invalid arg\n");
121 rc
= copy_to_user((void *)ud
, dr
, size
);
123 rc
= copy_from_user(dr
, (void *)ud
, size
);
126 BCMLOG_ERR("Invalid args for command\n");
133 static int chd_dec_fetch_cdata(struct crystalhd_adp
*adp
, struct crystalhd_ioctl_data
*io
,
134 uint32_t m_sz
, unsigned long ua
)
136 unsigned long ua_off
;
139 if (!adp
|| !io
|| !ua
|| !m_sz
) {
140 BCMLOG_ERR("Invalid Arg!!\n");
144 io
->add_cdata
= vmalloc(m_sz
);
145 if (!io
->add_cdata
) {
146 BCMLOG_ERR("kalloc fail for sz:%x\n", m_sz
);
150 io
->add_cdata_sz
= m_sz
;
151 ua_off
= ua
+ sizeof(io
->udata
);
152 rc
= crystalhd_user_data(ua_off
, io
->add_cdata
, io
->add_cdata_sz
, 0);
154 BCMLOG_ERR("failed to pull add_cdata sz:%x ua_off:%x\n",
155 io
->add_cdata_sz
, (unsigned int)ua_off
);
156 kfree(io
->add_cdata
);
157 io
->add_cdata
= NULL
;
164 static int chd_dec_release_cdata(struct crystalhd_adp
*adp
,
165 struct crystalhd_ioctl_data
*io
, unsigned long ua
)
167 unsigned long ua_off
;
170 if (!adp
|| !io
|| !ua
) {
171 BCMLOG_ERR("Invalid Arg!!\n");
175 if (io
->cmd
!= BCM_IOC_FW_DOWNLOAD
) {
176 ua_off
= ua
+ sizeof(io
->udata
);
177 rc
= crystalhd_user_data(ua_off
, io
->add_cdata
,
178 io
->add_cdata_sz
, 1);
180 BCMLOG_ERR("failed to push add_cdata sz:%x ua_off:%x\n",
181 io
->add_cdata_sz
, (unsigned int)ua_off
);
187 vfree(io
->add_cdata
);
188 io
->add_cdata
= NULL
;
194 static int chd_dec_proc_user_data(struct crystalhd_adp
*adp
,
195 struct crystalhd_ioctl_data
*io
,
196 unsigned long ua
, int set
)
201 if (!adp
|| !io
|| !ua
) {
202 BCMLOG_ERR("Invalid Arg!!\n");
206 rc
= crystalhd_user_data(ua
, &io
->udata
, sizeof(io
->udata
), set
);
208 BCMLOG_ERR("failed to %s iodata\n", (set
? "set" : "get"));
215 case BCM_IOC_FW_DOWNLOAD
:
216 m_sz
= io
->udata
.u
.devMem
.NumDwords
* 4;
218 rc
= chd_dec_release_cdata(adp
, io
, ua
);
220 rc
= chd_dec_fetch_cdata(adp
, io
, m_sz
, ua
);
229 static int chd_dec_api_cmd(struct crystalhd_adp
*adp
, unsigned long ua
,
230 uint32_t uid
, uint32_t cmd
, crystalhd_cmd_proc func
)
233 struct crystalhd_ioctl_data
*temp
;
234 enum BC_STATUS sts
= BC_STS_SUCCESS
;
236 temp
= chd_dec_alloc_iodata(adp
, 0);
238 BCMLOG_ERR("Failed to get iodata..\n");
245 rc
= chd_dec_proc_user_data(adp
, temp
, ua
, 0);
247 sts
= func(&adp
->cmds
, temp
);
248 if (sts
== BC_STS_PENDING
)
249 sts
= BC_STS_NOT_IMPL
;
250 temp
->udata
.RetSts
= sts
;
251 rc
= chd_dec_proc_user_data(adp
, temp
, ua
, 1);
255 chd_dec_free_iodata(adp
, temp
, 0);
263 static long chd_dec_ioctl(struct file
*fd
, unsigned int cmd
, unsigned long ua
)
265 struct crystalhd_adp
*adp
= chd_get_adp();
266 crystalhd_cmd_proc cproc
;
267 struct crystalhd_user
*uc
;
271 BCMLOG_ERR("Invalid adp\n");
275 uc
= fd
->private_data
;
277 BCMLOG_ERR("Failed to get uc\n");
281 mutex_lock(&chd_dec_mutex
);
282 cproc
= crystalhd_get_cmd_proc(&adp
->cmds
, cmd
, uc
);
284 BCMLOG_ERR("Unhandled command: %d\n", cmd
);
285 mutex_unlock(&chd_dec_mutex
);
289 ret
= chd_dec_api_cmd(adp
, ua
, uc
->uid
, cmd
, cproc
);
290 mutex_unlock(&chd_dec_mutex
);
294 static int chd_dec_open(struct inode
*in
, struct file
*fd
)
296 struct crystalhd_adp
*adp
= chd_get_adp();
298 enum BC_STATUS sts
= BC_STS_SUCCESS
;
299 struct crystalhd_user
*uc
= NULL
;
303 BCMLOG_ERR("Invalid adp\n");
307 if (adp
->cfg_users
>= BC_LINK_MAX_OPENS
) {
308 BCMLOG(BCMLOG_INFO
, "Already in use.%d\n", adp
->cfg_users
);
312 sts
= crystalhd_user_open(&adp
->cmds
, &uc
);
313 if (sts
!= BC_STS_SUCCESS
) {
314 BCMLOG_ERR("cmd_user_open - %d\n", sts
);
320 fd
->private_data
= uc
;
325 static int chd_dec_close(struct inode
*in
, struct file
*fd
)
327 struct crystalhd_adp
*adp
= chd_get_adp();
328 struct crystalhd_user
*uc
;
332 BCMLOG_ERR("Invalid adp\n");
336 uc
= fd
->private_data
;
338 BCMLOG_ERR("Failed to get uc\n");
342 crystalhd_user_close(&adp
->cmds
, uc
);
349 static const struct file_operations chd_dec_fops
= {
350 .owner
= THIS_MODULE
,
351 .unlocked_ioctl
= chd_dec_ioctl
,
352 .open
= chd_dec_open
,
353 .release
= chd_dec_close
,
354 .llseek
= noop_llseek
,
357 static int __devinit
chd_dec_init_chdev(struct crystalhd_adp
*adp
)
359 struct crystalhd_ioctl_data
*temp
;
361 int rc
= -ENODEV
, i
= 0;
366 adp
->chd_dec_major
= register_chrdev(0, CRYSTALHD_API_NAME
,
368 if (adp
->chd_dec_major
< 0) {
369 BCMLOG_ERR("Failed to create config dev\n");
370 rc
= adp
->chd_dec_major
;
374 /* register crystalhd class */
375 crystalhd_class
= class_create(THIS_MODULE
, "crystalhd");
376 if (IS_ERR(crystalhd_class
)) {
377 BCMLOG_ERR("failed to create class\n");
381 dev
= device_create(crystalhd_class
, NULL
, MKDEV(adp
->chd_dec_major
, 0),
384 BCMLOG_ERR("failed to create device\n");
385 goto device_create_fail
;
388 rc
= crystalhd_create_elem_pool(adp
, BC_LINK_ELEM_POOL_SZ
);
390 BCMLOG_ERR("failed to create device\n");
394 /* Allocate general purpose ioctl pool. */
395 for (i
= 0; i
< CHD_IODATA_POOL_SZ
; i
++) {
396 temp
= kzalloc(sizeof(struct crystalhd_ioctl_data
), GFP_KERNEL
);
398 BCMLOG_ERR("ioctl data pool kzalloc failed\n");
402 /* Add to global pool.. */
403 chd_dec_free_iodata(adp
, temp
, 0);
409 crystalhd_delete_elem_pool(adp
);
411 device_destroy(crystalhd_class
, MKDEV(adp
->chd_dec_major
, 0));
413 class_destroy(crystalhd_class
);
418 static void __devexit
chd_dec_release_chdev(struct crystalhd_adp
*adp
)
420 struct crystalhd_ioctl_data
*temp
= NULL
;
424 if (adp
->chd_dec_major
> 0) {
425 /* unregister crystalhd class */
426 device_destroy(crystalhd_class
, MKDEV(adp
->chd_dec_major
, 0));
427 unregister_chrdev(adp
->chd_dec_major
, CRYSTALHD_API_NAME
);
428 BCMLOG(BCMLOG_INFO
, "released api device - %d\n",
430 class_destroy(crystalhd_class
);
432 adp
->chd_dec_major
= 0;
434 /* Clear iodata pool.. */
436 temp
= chd_dec_alloc_iodata(adp
, 0);
440 crystalhd_delete_elem_pool(adp
);
443 static int __devinit
chd_pci_reserve_mem(struct crystalhd_adp
*pinfo
)
446 unsigned long bar2
= pci_resource_start(pinfo
->pdev
, 2);
447 uint32_t mem_len
= pci_resource_len(pinfo
->pdev
, 2);
448 unsigned long bar0
= pci_resource_start(pinfo
->pdev
, 0);
449 uint32_t i2o_len
= pci_resource_len(pinfo
->pdev
, 0);
451 BCMLOG(BCMLOG_SSTEP
, "bar2:0x%lx-0x%08x bar0:0x%lx-0x%08x\n",
452 bar2
, mem_len
, bar0
, i2o_len
);
454 rc
= check_mem_region(bar2
, mem_len
);
456 BCMLOG_ERR("No valid mem region...\n");
460 pinfo
->addr
= ioremap_nocache(bar2
, mem_len
);
462 BCMLOG_ERR("Failed to remap mem region...\n");
466 pinfo
->pci_mem_start
= bar2
;
467 pinfo
->pci_mem_len
= mem_len
;
469 rc
= check_mem_region(bar0
, i2o_len
);
471 BCMLOG_ERR("No valid mem region...\n");
475 pinfo
->i2o_addr
= ioremap_nocache(bar0
, i2o_len
);
476 if (!pinfo
->i2o_addr
) {
477 BCMLOG_ERR("Failed to remap mem region...\n");
481 pinfo
->pci_i2o_start
= bar0
;
482 pinfo
->pci_i2o_len
= i2o_len
;
484 rc
= pci_request_regions(pinfo
->pdev
, pinfo
->name
);
486 BCMLOG_ERR("Region request failed: %d\n", rc
);
490 BCMLOG(BCMLOG_SSTEP
, "Mapped addr:0x%08lx i2o_addr:0x%08lx\n",
491 (unsigned long)pinfo
->addr
, (unsigned long)pinfo
->i2o_addr
);
496 static void __devexit
chd_pci_release_mem(struct crystalhd_adp
*pinfo
)
502 iounmap(pinfo
->addr
);
505 iounmap(pinfo
->i2o_addr
);
507 pci_release_regions(pinfo
->pdev
);
511 static void __devexit
chd_dec_pci_remove(struct pci_dev
*pdev
)
513 struct crystalhd_adp
*pinfo
;
514 enum BC_STATUS sts
= BC_STS_SUCCESS
;
518 pinfo
= pci_get_drvdata(pdev
);
520 BCMLOG_ERR("could not get adp\n");
524 sts
= crystalhd_delete_cmd_context(&pinfo
->cmds
);
525 if (sts
!= BC_STS_SUCCESS
)
526 BCMLOG_ERR("cmd delete :%d\n", sts
);
528 chd_dec_release_chdev(pinfo
);
530 chd_dec_disable_int(pinfo
);
532 chd_pci_release_mem(pinfo
);
533 pci_disable_device(pinfo
->pdev
);
539 static int __devinit
chd_dec_pci_probe(struct pci_dev
*pdev
,
540 const struct pci_device_id
*entry
)
542 struct crystalhd_adp
*pinfo
;
544 enum BC_STATUS sts
= BC_STS_SUCCESS
;
546 BCMLOG(BCMLOG_DBG
, "PCI_INFO: Vendor:0x%04x Device:0x%04x "
547 "s_vendor:0x%04x s_device: 0x%04x\n",
548 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
549 pdev
->subsystem_device
);
551 pinfo
= kzalloc(sizeof(struct crystalhd_adp
), GFP_KERNEL
);
553 BCMLOG_ERR("Failed to allocate memory\n");
559 rc
= pci_enable_device(pdev
);
561 BCMLOG_ERR("Failed to enable PCI device\n");
565 snprintf(pinfo
->name
, sizeof(pinfo
->name
), "crystalhd_pci_e:%d:%d:%d",
566 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
),
567 PCI_FUNC(pdev
->devfn
));
569 rc
= chd_pci_reserve_mem(pinfo
);
571 BCMLOG_ERR("Failed to setup memory regions.\n");
572 pci_disable_device(pdev
);
578 pinfo
->drv_data
= entry
->driver_data
;
580 /* Setup adapter level lock.. */
581 spin_lock_init(&pinfo
->lock
);
583 /* setup api stuff.. */
584 chd_dec_init_chdev(pinfo
);
585 rc
= chd_dec_enable_int(pinfo
);
587 BCMLOG_ERR("_enable_int err:%d\n", rc
);
588 pci_disable_device(pdev
);
593 /* Set dma mask... */
594 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
595 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
597 } else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
598 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
601 BCMLOG_ERR("Unabled to setup DMA %d\n", rc
);
602 pci_disable_device(pdev
);
607 sts
= crystalhd_setup_cmd_context(&pinfo
->cmds
, pinfo
);
608 if (sts
!= BC_STS_SUCCESS
) {
609 BCMLOG_ERR("cmd setup :%d\n", sts
);
610 pci_disable_device(pdev
);
615 pci_set_master(pdev
);
617 pci_set_drvdata(pdev
, pinfo
);
629 int chd_dec_pci_suspend(struct pci_dev
*pdev
, pm_message_t state
)
631 struct crystalhd_adp
*adp
;
632 struct crystalhd_ioctl_data
*temp
;
633 enum BC_STATUS sts
= BC_STS_SUCCESS
;
635 adp
= pci_get_drvdata(pdev
);
637 BCMLOG_ERR("could not get adp\n");
641 temp
= chd_dec_alloc_iodata(adp
, false);
643 BCMLOG_ERR("could not get ioctl data\n");
647 sts
= crystalhd_suspend(&adp
->cmds
, temp
);
648 if (sts
!= BC_STS_SUCCESS
) {
649 BCMLOG_ERR("BCM70012 Suspend %d\n", sts
);
653 chd_dec_free_iodata(adp
, temp
, false);
654 chd_dec_disable_int(adp
);
655 pci_save_state(pdev
);
657 /* Disable IO/bus master/irq router */
658 pci_disable_device(pdev
);
659 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
663 int chd_dec_pci_resume(struct pci_dev
*pdev
)
665 struct crystalhd_adp
*adp
;
666 enum BC_STATUS sts
= BC_STS_SUCCESS
;
669 adp
= pci_get_drvdata(pdev
);
671 BCMLOG_ERR("could not get adp\n");
675 pci_set_power_state(pdev
, PCI_D0
);
676 pci_restore_state(pdev
);
678 /* device's irq possibly is changed, driver should take care */
679 if (pci_enable_device(pdev
)) {
680 BCMLOG_ERR("Failed to enable PCI device\n");
684 pci_set_master(pdev
);
686 rc
= chd_dec_enable_int(adp
);
688 BCMLOG_ERR("_enable_int err:%d\n", rc
);
689 pci_disable_device(pdev
);
693 sts
= crystalhd_resume(&adp
->cmds
);
694 if (sts
!= BC_STS_SUCCESS
) {
695 BCMLOG_ERR("BCM70012 Resume %d\n", sts
);
696 pci_disable_device(pdev
);
704 static DEFINE_PCI_DEVICE_TABLE(chd_dec_pci_id_table
) = {
705 { PCI_VDEVICE(BROADCOM
, 0x1612), 8 },
708 MODULE_DEVICE_TABLE(pci
, chd_dec_pci_id_table
);
710 static struct pci_driver bc_chd_70012_driver
= {
711 .name
= "Broadcom 70012 Decoder",
712 .probe
= chd_dec_pci_probe
,
713 .remove
= __devexit_p(chd_dec_pci_remove
),
714 .id_table
= chd_dec_pci_id_table
,
716 .suspend
= chd_dec_pci_suspend
,
717 .resume
= chd_dec_pci_resume
721 void chd_set_log_level(struct crystalhd_adp
*adp
, char *arg
)
723 if ((!arg
) || (strlen(arg
) < 3))
724 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
;
725 else if (!strncmp(arg
, "sstep", 5))
726 g_linklog_level
= BCMLOG_INFO
| BCMLOG_DATA
| BCMLOG_DBG
|
727 BCMLOG_SSTEP
| BCMLOG_ERROR
;
728 else if (!strncmp(arg
, "info", 4))
729 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
| BCMLOG_INFO
;
730 else if (!strncmp(arg
, "debug", 5))
731 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
| BCMLOG_INFO
|
733 else if (!strncmp(arg
, "pball", 5))
734 g_linklog_level
= 0xFFFFFFFF & ~(BCMLOG_SPINLOCK
);
735 else if (!strncmp(arg
, "silent", 6))
741 struct crystalhd_adp
*chd_get_adp(void)
746 static int __init
chd_dec_module_init(void)
750 chd_set_log_level(NULL
, "debug");
751 BCMLOG(BCMLOG_DATA
, "Loading crystalhd %d.%d.%d\n",
752 crystalhd_kmod_major
, crystalhd_kmod_minor
, crystalhd_kmod_rev
);
754 rc
= pci_register_driver(&bc_chd_70012_driver
);
757 BCMLOG_ERR("Could not find any devices. err:%d\n", rc
);
761 module_init(chd_dec_module_init
);
763 static void __exit
chd_dec_module_cleanup(void)
765 BCMLOG(BCMLOG_DATA
, "unloading crystalhd %d.%d.%d\n",
766 crystalhd_kmod_major
, crystalhd_kmod_minor
, crystalhd_kmod_rev
);
768 pci_unregister_driver(&bc_chd_70012_driver
);
770 module_exit(chd_dec_module_cleanup
);
772 MODULE_AUTHOR("Naren Sankar <nsankar@broadcom.com>");
773 MODULE_AUTHOR("Prasad Bolisetty <prasadb@broadcom.com>");
774 MODULE_DESCRIPTION(CRYSTAL_HD_NAME
);
775 MODULE_LICENSE("GPL");
776 MODULE_ALIAS("bcm70012");