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 "crystalhd.h"
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
24 static DEFINE_MUTEX(chd_dec_mutex
);
25 static struct class *crystalhd_class
;
27 static struct crystalhd_adp
*g_adp_info
;
29 static irqreturn_t
chd_dec_isr(int irq
, void *arg
)
31 struct crystalhd_adp
*adp
= (struct crystalhd_adp
*) arg
;
34 rc
= crystalhd_cmd_interrupt(&adp
->cmds
);
36 return IRQ_RETVAL(rc
);
39 static int chd_dec_enable_int(struct crystalhd_adp
*adp
)
43 if (!adp
|| !adp
->pdev
) {
44 BCMLOG_ERR("Invalid arg!!\n");
48 if (adp
->pdev
->msi_enabled
)
51 adp
->msi
= pci_enable_msi(adp
->pdev
);
53 rc
= request_irq(adp
->pdev
->irq
, chd_dec_isr
, IRQF_SHARED
,
54 adp
->name
, (void *)adp
);
56 BCMLOG_ERR("Interrupt request failed..\n");
57 pci_disable_msi(adp
->pdev
);
63 static int chd_dec_disable_int(struct crystalhd_adp
*adp
)
65 if (!adp
|| !adp
->pdev
) {
66 BCMLOG_ERR("Invalid arg!!\n");
70 free_irq(adp
->pdev
->irq
, adp
);
73 pci_disable_msi(adp
->pdev
);
79 crystalhd_ioctl_data
*chd_dec_alloc_iodata(struct crystalhd_adp
*adp
,
82 unsigned long flags
= 0;
83 struct crystalhd_ioctl_data
*temp
;
88 spin_lock_irqsave(&adp
->lock
, flags
);
90 temp
= adp
->idata_free_head
;
92 adp
->idata_free_head
= adp
->idata_free_head
->next
;
93 memset(temp
, 0, sizeof(*temp
));
96 spin_unlock_irqrestore(&adp
->lock
, flags
);
100 static void chd_dec_free_iodata(struct crystalhd_adp
*adp
,
101 struct crystalhd_ioctl_data
*iodata
, bool isr
)
103 unsigned long flags
= 0;
108 spin_lock_irqsave(&adp
->lock
, flags
);
109 iodata
->next
= adp
->idata_free_head
;
110 adp
->idata_free_head
= iodata
;
111 spin_unlock_irqrestore(&adp
->lock
, flags
);
114 static inline int crystalhd_user_data(unsigned long ud
, void *dr
,
120 BCMLOG_ERR("Invalid arg\n");
125 rc
= copy_to_user((void *)ud
, dr
, size
);
127 rc
= copy_from_user(dr
, (void *)ud
, size
);
130 BCMLOG_ERR("Invalid args for command\n");
137 static int chd_dec_fetch_cdata(struct crystalhd_adp
*adp
,
138 struct crystalhd_ioctl_data
*io
, uint32_t m_sz
, unsigned long ua
)
140 unsigned long ua_off
;
143 if (!adp
|| !io
|| !ua
|| !m_sz
) {
144 BCMLOG_ERR("Invalid Arg!!\n");
148 io
->add_cdata
= vmalloc(m_sz
);
149 if (!io
->add_cdata
) {
150 BCMLOG_ERR("kalloc fail for sz:%x\n", m_sz
);
154 io
->add_cdata_sz
= m_sz
;
155 ua_off
= ua
+ sizeof(io
->udata
);
156 rc
= crystalhd_user_data(ua_off
, io
->add_cdata
, io
->add_cdata_sz
, 0);
158 BCMLOG_ERR("failed to pull add_cdata sz:%x ua_off:%x\n",
159 io
->add_cdata_sz
, (unsigned int)ua_off
);
160 vfree(io
->add_cdata
);
161 io
->add_cdata
= NULL
;
168 static int chd_dec_release_cdata(struct crystalhd_adp
*adp
,
169 struct crystalhd_ioctl_data
*io
, unsigned long ua
)
171 unsigned long ua_off
;
174 if (!adp
|| !io
|| !ua
) {
175 BCMLOG_ERR("Invalid Arg!!\n");
179 if (io
->cmd
!= BCM_IOC_FW_DOWNLOAD
) {
180 ua_off
= ua
+ sizeof(io
->udata
);
181 rc
= crystalhd_user_data(ua_off
, io
->add_cdata
,
182 io
->add_cdata_sz
, 1);
185 "failed to push add_cdata sz:%x ua_off:%x\n",
186 io
->add_cdata_sz
, (unsigned int)ua_off
);
192 vfree(io
->add_cdata
);
193 io
->add_cdata
= NULL
;
199 static int chd_dec_proc_user_data(struct crystalhd_adp
*adp
,
200 struct crystalhd_ioctl_data
*io
,
201 unsigned long ua
, int set
)
206 if (!adp
|| !io
|| !ua
) {
207 BCMLOG_ERR("Invalid Arg!!\n");
211 rc
= crystalhd_user_data(ua
, &io
->udata
, sizeof(io
->udata
), set
);
213 BCMLOG_ERR("failed to %s iodata\n", (set
? "set" : "get"));
220 case BCM_IOC_FW_DOWNLOAD
:
221 m_sz
= io
->udata
.u
.devMem
.NumDwords
* 4;
223 rc
= chd_dec_release_cdata(adp
, io
, ua
);
225 rc
= chd_dec_fetch_cdata(adp
, io
, m_sz
, ua
);
234 static int chd_dec_api_cmd(struct crystalhd_adp
*adp
, unsigned long ua
,
235 uint32_t uid
, uint32_t cmd
, crystalhd_cmd_proc func
)
238 struct crystalhd_ioctl_data
*temp
;
239 enum BC_STATUS sts
= BC_STS_SUCCESS
;
241 temp
= chd_dec_alloc_iodata(adp
, 0);
243 BCMLOG_ERR("Failed to get iodata..\n");
250 rc
= chd_dec_proc_user_data(adp
, temp
, ua
, 0);
252 sts
= func(&adp
->cmds
, temp
);
253 if (sts
== BC_STS_PENDING
)
254 sts
= BC_STS_NOT_IMPL
;
255 temp
->udata
.RetSts
= sts
;
256 rc
= chd_dec_proc_user_data(adp
, temp
, ua
, 1);
259 chd_dec_free_iodata(adp
, temp
, 0);
265 static long chd_dec_ioctl(struct file
*fd
, unsigned int cmd
, unsigned long ua
)
267 struct crystalhd_adp
*adp
= chd_get_adp();
268 crystalhd_cmd_proc cproc
;
269 struct crystalhd_user
*uc
;
273 BCMLOG_ERR("Invalid adp\n");
277 uc
= fd
->private_data
;
279 BCMLOG_ERR("Failed to get uc\n");
283 mutex_lock(&chd_dec_mutex
);
284 cproc
= crystalhd_get_cmd_proc(&adp
->cmds
, cmd
, uc
);
286 BCMLOG_ERR("Unhandled command: %d\n", cmd
);
287 mutex_unlock(&chd_dec_mutex
);
291 ret
= chd_dec_api_cmd(adp
, ua
, uc
->uid
, cmd
, cproc
);
292 mutex_unlock(&chd_dec_mutex
);
296 static int chd_dec_open(struct inode
*in
, struct file
*fd
)
298 struct crystalhd_adp
*adp
= chd_get_adp();
300 enum BC_STATUS sts
= BC_STS_SUCCESS
;
301 struct crystalhd_user
*uc
= NULL
;
304 BCMLOG_ERR("Invalid adp\n");
308 if (adp
->cfg_users
>= BC_LINK_MAX_OPENS
) {
309 BCMLOG(BCMLOG_INFO
, "Already in use.%d\n", adp
->cfg_users
);
313 sts
= crystalhd_user_open(&adp
->cmds
, &uc
);
314 if (sts
!= BC_STS_SUCCESS
) {
315 BCMLOG_ERR("cmd_user_open - %d\n", sts
);
321 fd
->private_data
= uc
;
326 static int chd_dec_close(struct inode
*in
, struct file
*fd
)
328 struct crystalhd_adp
*adp
= chd_get_adp();
329 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 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 rc
= PTR_ERR(crystalhd_class
);
378 BCMLOG_ERR("failed to create class\n");
379 goto class_create_fail
;
382 dev
= device_create(crystalhd_class
, NULL
,
383 MKDEV(adp
->chd_dec_major
, 0), NULL
, "crystalhd");
386 BCMLOG_ERR("failed to create device\n");
387 goto device_create_fail
;
390 rc
= crystalhd_create_elem_pool(adp
, BC_LINK_ELEM_POOL_SZ
);
392 BCMLOG_ERR("failed to create device\n");
396 /* Allocate general purpose ioctl pool. */
397 for (i
= 0; i
< CHD_IODATA_POOL_SZ
; i
++) {
398 temp
= kzalloc(sizeof(struct crystalhd_ioctl_data
),
401 BCMLOG_ERR("ioctl data pool kzalloc failed\n");
405 /* Add to global pool.. */
406 chd_dec_free_iodata(adp
, temp
, 0);
412 crystalhd_delete_elem_pool(adp
);
414 device_destroy(crystalhd_class
, MKDEV(adp
->chd_dec_major
, 0));
416 class_destroy(crystalhd_class
);
418 unregister_chrdev(adp
->chd_dec_major
, CRYSTALHD_API_NAME
);
423 static void chd_dec_release_chdev(struct crystalhd_adp
*adp
)
425 struct crystalhd_ioctl_data
*temp
= NULL
;
429 if (adp
->chd_dec_major
> 0) {
430 /* unregister crystalhd class */
431 device_destroy(crystalhd_class
, MKDEV(adp
->chd_dec_major
, 0));
432 unregister_chrdev(adp
->chd_dec_major
, CRYSTALHD_API_NAME
);
433 BCMLOG(BCMLOG_INFO
, "released api device - %d\n",
435 class_destroy(crystalhd_class
);
437 adp
->chd_dec_major
= 0;
439 /* Clear iodata pool.. */
441 temp
= chd_dec_alloc_iodata(adp
, 0);
445 crystalhd_delete_elem_pool(adp
);
448 static int chd_pci_reserve_mem(struct crystalhd_adp
*pinfo
)
451 unsigned long bar2
= pci_resource_start(pinfo
->pdev
, 2);
452 uint32_t mem_len
= pci_resource_len(pinfo
->pdev
, 2);
453 unsigned long bar0
= pci_resource_start(pinfo
->pdev
, 0);
454 uint32_t i2o_len
= pci_resource_len(pinfo
->pdev
, 0);
456 BCMLOG(BCMLOG_SSTEP
, "bar2:0x%lx-0x%08x bar0:0x%lx-0x%08x\n",
457 bar2
, mem_len
, bar0
, i2o_len
);
459 rc
= check_mem_region(bar2
, mem_len
);
461 BCMLOG_ERR("No valid mem region...\n");
465 pinfo
->addr
= ioremap_nocache(bar2
, mem_len
);
467 BCMLOG_ERR("Failed to remap mem region...\n");
471 pinfo
->pci_mem_start
= bar2
;
472 pinfo
->pci_mem_len
= mem_len
;
474 rc
= check_mem_region(bar0
, i2o_len
);
476 BCMLOG_ERR("No valid mem region...\n");
480 pinfo
->i2o_addr
= ioremap_nocache(bar0
, i2o_len
);
481 if (!pinfo
->i2o_addr
) {
482 BCMLOG_ERR("Failed to remap mem region...\n");
486 pinfo
->pci_i2o_start
= bar0
;
487 pinfo
->pci_i2o_len
= i2o_len
;
489 rc
= pci_request_regions(pinfo
->pdev
, pinfo
->name
);
491 BCMLOG_ERR("Region request failed: %d\n", rc
);
495 BCMLOG(BCMLOG_SSTEP
, "Mapped addr:0x%08lx i2o_addr:0x%08lx\n",
496 (unsigned long)pinfo
->addr
, (unsigned long)pinfo
->i2o_addr
);
501 static void chd_pci_release_mem(struct crystalhd_adp
*pinfo
)
507 iounmap(pinfo
->addr
);
510 iounmap(pinfo
->i2o_addr
);
512 pci_release_regions(pinfo
->pdev
);
516 static void chd_dec_pci_remove(struct pci_dev
*pdev
)
518 struct crystalhd_adp
*pinfo
;
519 enum BC_STATUS sts
= BC_STS_SUCCESS
;
521 pinfo
= pci_get_drvdata(pdev
);
523 BCMLOG_ERR("could not get adp\n");
527 sts
= crystalhd_delete_cmd_context(&pinfo
->cmds
);
528 if (sts
!= BC_STS_SUCCESS
)
529 BCMLOG_ERR("cmd delete :%d\n", sts
);
531 chd_dec_release_chdev(pinfo
);
533 chd_dec_disable_int(pinfo
);
535 chd_pci_release_mem(pinfo
);
536 pci_disable_device(pinfo
->pdev
);
542 static int chd_dec_pci_probe(struct pci_dev
*pdev
,
543 const struct pci_device_id
*entry
)
545 struct crystalhd_adp
*pinfo
;
547 enum BC_STATUS sts
= BC_STS_SUCCESS
;
549 BCMLOG(BCMLOG_DBG
, "PCI_INFO: Vendor:0x%04x Device:0x%04x s_vendor:0x%04x s_device: 0x%04x\n",
550 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
551 pdev
->subsystem_device
);
553 pinfo
= kzalloc(sizeof(struct crystalhd_adp
), GFP_KERNEL
);
555 BCMLOG_ERR("Failed to allocate memory\n");
561 rc
= pci_enable_device(pdev
);
563 BCMLOG_ERR("Failed to enable PCI device\n");
567 snprintf(pinfo
->name
, sizeof(pinfo
->name
), "crystalhd_pci_e:%d:%d:%d",
568 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
),
569 PCI_FUNC(pdev
->devfn
));
571 rc
= chd_pci_reserve_mem(pinfo
);
573 BCMLOG_ERR("Failed to setup memory regions.\n");
574 pci_disable_device(pdev
);
580 pinfo
->drv_data
= entry
->driver_data
;
582 /* Setup adapter level lock.. */
583 spin_lock_init(&pinfo
->lock
);
585 /* setup api stuff.. */
586 chd_dec_init_chdev(pinfo
);
587 rc
= chd_dec_enable_int(pinfo
);
589 BCMLOG_ERR("_enable_int err:%d\n", rc
);
590 pci_disable_device(pdev
);
595 /* Set dma mask... */
596 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
597 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
599 } else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
600 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
603 BCMLOG_ERR("Unabled to setup DMA %d\n", rc
);
604 pci_disable_device(pdev
);
609 sts
= crystalhd_setup_cmd_context(&pinfo
->cmds
, pinfo
);
610 if (sts
!= BC_STS_SUCCESS
) {
611 BCMLOG_ERR("cmd setup :%d\n", sts
);
612 pci_disable_device(pdev
);
617 pci_set_master(pdev
);
619 pci_set_drvdata(pdev
, pinfo
);
631 static int chd_dec_pci_suspend(struct pci_dev
*pdev
, pm_message_t state
)
633 struct crystalhd_adp
*adp
;
634 struct crystalhd_ioctl_data
*temp
;
635 enum BC_STATUS sts
= BC_STS_SUCCESS
;
637 adp
= pci_get_drvdata(pdev
);
639 BCMLOG_ERR("could not get adp\n");
643 temp
= chd_dec_alloc_iodata(adp
, false);
645 BCMLOG_ERR("could not get ioctl data\n");
649 sts
= crystalhd_suspend(&adp
->cmds
, temp
);
650 if (sts
!= BC_STS_SUCCESS
) {
651 BCMLOG_ERR("BCM70012 Suspend %d\n", sts
);
655 chd_dec_free_iodata(adp
, temp
, false);
656 chd_dec_disable_int(adp
);
657 pci_save_state(pdev
);
659 /* Disable IO/bus master/irq router */
660 pci_disable_device(pdev
);
661 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
665 static int chd_dec_pci_resume(struct pci_dev
*pdev
)
667 struct crystalhd_adp
*adp
;
668 enum BC_STATUS sts
= BC_STS_SUCCESS
;
671 adp
= pci_get_drvdata(pdev
);
673 BCMLOG_ERR("could not get adp\n");
677 pci_set_power_state(pdev
, PCI_D0
);
678 pci_restore_state(pdev
);
680 /* device's irq possibly is changed, driver should take care */
681 if (pci_enable_device(pdev
)) {
682 BCMLOG_ERR("Failed to enable PCI device\n");
686 pci_set_master(pdev
);
688 rc
= chd_dec_enable_int(adp
);
690 BCMLOG_ERR("_enable_int err:%d\n", rc
);
691 pci_disable_device(pdev
);
695 sts
= crystalhd_resume(&adp
->cmds
);
696 if (sts
!= BC_STS_SUCCESS
) {
697 BCMLOG_ERR("BCM70012 Resume %d\n", sts
);
698 pci_disable_device(pdev
);
706 static const struct pci_device_id chd_dec_pci_id_table
[] = {
707 { PCI_VDEVICE(BROADCOM
, 0x1612), 8 },
710 MODULE_DEVICE_TABLE(pci
, chd_dec_pci_id_table
);
712 static struct pci_driver bc_chd_70012_driver
= {
713 .name
= "Broadcom 70012 Decoder",
714 .probe
= chd_dec_pci_probe
,
715 .remove
= chd_dec_pci_remove
,
716 .id_table
= chd_dec_pci_id_table
,
718 .suspend
= chd_dec_pci_suspend
,
719 .resume
= chd_dec_pci_resume
723 void chd_set_log_level(struct crystalhd_adp
*adp
, char *arg
)
725 if ((!arg
) || (strlen(arg
) < 3))
726 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
;
727 else if (!strncmp(arg
, "sstep", 5))
728 g_linklog_level
= BCMLOG_INFO
| BCMLOG_DATA
| BCMLOG_DBG
|
729 BCMLOG_SSTEP
| BCMLOG_ERROR
;
730 else if (!strncmp(arg
, "info", 4))
731 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
| BCMLOG_INFO
;
732 else if (!strncmp(arg
, "debug", 5))
733 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
| BCMLOG_INFO
|
735 else if (!strncmp(arg
, "pball", 5))
736 g_linklog_level
= 0xFFFFFFFF & ~(BCMLOG_SPINLOCK
);
737 else if (!strncmp(arg
, "silent", 6))
743 struct crystalhd_adp
*chd_get_adp(void)
748 static int __init
chd_dec_module_init(void)
752 chd_set_log_level(NULL
, "debug");
753 BCMLOG(BCMLOG_DATA
, "Loading crystalhd %d.%d.%d\n",
754 crystalhd_kmod_major
, crystalhd_kmod_minor
, crystalhd_kmod_rev
);
756 rc
= pci_register_driver(&bc_chd_70012_driver
);
759 BCMLOG_ERR("Could not find any devices. err:%d\n", rc
);
763 module_init(chd_dec_module_init
);
765 static void __exit
chd_dec_module_cleanup(void)
767 BCMLOG(BCMLOG_DATA
, "unloading crystalhd %d.%d.%d\n",
768 crystalhd_kmod_major
, crystalhd_kmod_minor
, crystalhd_kmod_rev
);
770 pci_unregister_driver(&bc_chd_70012_driver
);
772 module_exit(chd_dec_module_cleanup
);
774 MODULE_AUTHOR("Naren Sankar <nsankar@broadcom.com>");
775 MODULE_AUTHOR("Prasad Bolisetty <prasadb@broadcom.com>");
776 MODULE_DESCRIPTION(CRYSTAL_HD_NAME
);
777 MODULE_LICENSE("GPL");
778 MODULE_ALIAS("bcm70012");