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
);
78 struct crystalhd_ioctl_data
*chd_dec_alloc_iodata(struct crystalhd_adp
*adp
, bool isr
)
80 unsigned long flags
= 0;
81 struct crystalhd_ioctl_data
*temp
;
86 spin_lock_irqsave(&adp
->lock
, flags
);
88 temp
= adp
->idata_free_head
;
90 adp
->idata_free_head
= adp
->idata_free_head
->next
;
91 memset(temp
, 0, sizeof(*temp
));
94 spin_unlock_irqrestore(&adp
->lock
, flags
);
98 void chd_dec_free_iodata(struct crystalhd_adp
*adp
, struct crystalhd_ioctl_data
*iodata
,
101 unsigned long flags
= 0;
106 spin_lock_irqsave(&adp
->lock
, flags
);
107 iodata
->next
= adp
->idata_free_head
;
108 adp
->idata_free_head
= iodata
;
109 spin_unlock_irqrestore(&adp
->lock
, flags
);
112 static inline int crystalhd_user_data(unsigned long ud
, void *dr
, int size
, int set
)
117 BCMLOG_ERR("Invalid arg\n");
122 rc
= copy_to_user((void *)ud
, dr
, size
);
124 rc
= copy_from_user(dr
, (void *)ud
, size
);
127 BCMLOG_ERR("Invalid args for command\n");
134 static int chd_dec_fetch_cdata(struct crystalhd_adp
*adp
, struct crystalhd_ioctl_data
*io
,
135 uint32_t m_sz
, unsigned long ua
)
137 unsigned long ua_off
;
140 if (!adp
|| !io
|| !ua
|| !m_sz
) {
141 BCMLOG_ERR("Invalid Arg!!\n");
145 io
->add_cdata
= vmalloc(m_sz
);
146 if (!io
->add_cdata
) {
147 BCMLOG_ERR("kalloc fail for sz:%x\n", m_sz
);
151 io
->add_cdata_sz
= m_sz
;
152 ua_off
= ua
+ sizeof(io
->udata
);
153 rc
= crystalhd_user_data(ua_off
, io
->add_cdata
, io
->add_cdata_sz
, 0);
155 BCMLOG_ERR("failed to pull add_cdata sz:%x ua_off:%x\n",
156 io
->add_cdata_sz
, (unsigned int)ua_off
);
157 kfree(io
->add_cdata
);
158 io
->add_cdata
= NULL
;
165 static int chd_dec_release_cdata(struct crystalhd_adp
*adp
,
166 struct crystalhd_ioctl_data
*io
, unsigned long ua
)
168 unsigned long ua_off
;
171 if (!adp
|| !io
|| !ua
) {
172 BCMLOG_ERR("Invalid Arg!!\n");
176 if (io
->cmd
!= BCM_IOC_FW_DOWNLOAD
) {
177 ua_off
= ua
+ sizeof(io
->udata
);
178 rc
= crystalhd_user_data(ua_off
, io
->add_cdata
,
179 io
->add_cdata_sz
, 1);
181 BCMLOG_ERR("failed to push add_cdata sz:%x ua_off:%x\n",
182 io
->add_cdata_sz
, (unsigned int)ua_off
);
188 vfree(io
->add_cdata
);
189 io
->add_cdata
= NULL
;
195 static int chd_dec_proc_user_data(struct crystalhd_adp
*adp
,
196 struct crystalhd_ioctl_data
*io
,
197 unsigned long ua
, int set
)
202 if (!adp
|| !io
|| !ua
) {
203 BCMLOG_ERR("Invalid Arg!!\n");
207 rc
= crystalhd_user_data(ua
, &io
->udata
, sizeof(io
->udata
), set
);
209 BCMLOG_ERR("failed to %s iodata\n", (set
? "set" : "get"));
216 case BCM_IOC_FW_DOWNLOAD
:
217 m_sz
= io
->udata
.u
.devMem
.NumDwords
* 4;
219 rc
= chd_dec_release_cdata(adp
, io
, ua
);
221 rc
= chd_dec_fetch_cdata(adp
, io
, m_sz
, ua
);
230 static int chd_dec_api_cmd(struct crystalhd_adp
*adp
, unsigned long ua
,
231 uint32_t uid
, uint32_t cmd
, crystalhd_cmd_proc func
)
234 struct crystalhd_ioctl_data
*temp
;
235 enum BC_STATUS sts
= BC_STS_SUCCESS
;
237 temp
= chd_dec_alloc_iodata(adp
, 0);
239 BCMLOG_ERR("Failed to get iodata..\n");
246 rc
= chd_dec_proc_user_data(adp
, temp
, ua
, 0);
248 sts
= func(&adp
->cmds
, temp
);
249 if (sts
== BC_STS_PENDING
)
250 sts
= BC_STS_NOT_IMPL
;
251 temp
->udata
.RetSts
= sts
;
252 rc
= chd_dec_proc_user_data(adp
, temp
, ua
, 1);
256 chd_dec_free_iodata(adp
, temp
, 0);
264 static long chd_dec_ioctl(struct file
*fd
, unsigned int cmd
, unsigned long ua
)
266 struct crystalhd_adp
*adp
= chd_get_adp();
267 crystalhd_cmd_proc cproc
;
268 struct crystalhd_user
*uc
;
272 BCMLOG_ERR("Invalid adp\n");
276 uc
= fd
->private_data
;
278 BCMLOG_ERR("Failed to get uc\n");
282 mutex_lock(&chd_dec_mutex
);
283 cproc
= crystalhd_get_cmd_proc(&adp
->cmds
, cmd
, uc
);
285 BCMLOG_ERR("Unhandled command: %d\n", cmd
);
286 mutex_unlock(&chd_dec_mutex
);
290 ret
= chd_dec_api_cmd(adp
, ua
, uc
->uid
, cmd
, cproc
);
291 mutex_unlock(&chd_dec_mutex
);
295 static int chd_dec_open(struct inode
*in
, struct file
*fd
)
297 struct crystalhd_adp
*adp
= chd_get_adp();
299 enum BC_STATUS sts
= BC_STS_SUCCESS
;
300 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
;
331 BCMLOG_ERR("Invalid adp\n");
335 uc
= fd
->private_data
;
337 BCMLOG_ERR("Failed to get uc\n");
341 crystalhd_user_close(&adp
->cmds
, uc
);
348 static const struct file_operations chd_dec_fops
= {
349 .owner
= THIS_MODULE
,
350 .unlocked_ioctl
= chd_dec_ioctl
,
351 .open
= chd_dec_open
,
352 .release
= chd_dec_close
,
353 .llseek
= noop_llseek
,
356 static int __devinit
chd_dec_init_chdev(struct crystalhd_adp
*adp
)
358 struct crystalhd_ioctl_data
*temp
;
360 int rc
= -ENODEV
, i
= 0;
365 adp
->chd_dec_major
= register_chrdev(0, CRYSTALHD_API_NAME
,
367 if (adp
->chd_dec_major
< 0) {
368 BCMLOG_ERR("Failed to create config dev\n");
369 rc
= adp
->chd_dec_major
;
373 /* register crystalhd class */
374 crystalhd_class
= class_create(THIS_MODULE
, "crystalhd");
375 if (IS_ERR(crystalhd_class
)) {
376 rc
= PTR_ERR(crystalhd_class
);
377 BCMLOG_ERR("failed to create class\n");
378 goto class_create_fail
;
381 dev
= device_create(crystalhd_class
, NULL
, MKDEV(adp
->chd_dec_major
, 0),
385 BCMLOG_ERR("failed to create device\n");
386 goto device_create_fail
;
389 rc
= crystalhd_create_elem_pool(adp
, BC_LINK_ELEM_POOL_SZ
);
391 BCMLOG_ERR("failed to create device\n");
395 /* Allocate general purpose ioctl pool. */
396 for (i
= 0; i
< CHD_IODATA_POOL_SZ
; i
++) {
397 temp
= kzalloc(sizeof(struct crystalhd_ioctl_data
), GFP_KERNEL
);
399 BCMLOG_ERR("ioctl data pool kzalloc failed\n");
403 /* Add to global pool.. */
404 chd_dec_free_iodata(adp
, temp
, 0);
410 crystalhd_delete_elem_pool(adp
);
412 device_destroy(crystalhd_class
, MKDEV(adp
->chd_dec_major
, 0));
414 class_destroy(crystalhd_class
);
416 unregister_chrdev(adp
->chd_dec_major
, CRYSTALHD_API_NAME
);
421 static void __devexit
chd_dec_release_chdev(struct crystalhd_adp
*adp
)
423 struct crystalhd_ioctl_data
*temp
= NULL
;
427 if (adp
->chd_dec_major
> 0) {
428 /* unregister crystalhd class */
429 device_destroy(crystalhd_class
, MKDEV(adp
->chd_dec_major
, 0));
430 unregister_chrdev(adp
->chd_dec_major
, CRYSTALHD_API_NAME
);
431 BCMLOG(BCMLOG_INFO
, "released api device - %d\n",
433 class_destroy(crystalhd_class
);
435 adp
->chd_dec_major
= 0;
437 /* Clear iodata pool.. */
439 temp
= chd_dec_alloc_iodata(adp
, 0);
443 crystalhd_delete_elem_pool(adp
);
446 static int __devinit
chd_pci_reserve_mem(struct crystalhd_adp
*pinfo
)
449 unsigned long bar2
= pci_resource_start(pinfo
->pdev
, 2);
450 uint32_t mem_len
= pci_resource_len(pinfo
->pdev
, 2);
451 unsigned long bar0
= pci_resource_start(pinfo
->pdev
, 0);
452 uint32_t i2o_len
= pci_resource_len(pinfo
->pdev
, 0);
454 BCMLOG(BCMLOG_SSTEP
, "bar2:0x%lx-0x%08x bar0:0x%lx-0x%08x\n",
455 bar2
, mem_len
, bar0
, i2o_len
);
457 rc
= check_mem_region(bar2
, mem_len
);
459 BCMLOG_ERR("No valid mem region...\n");
463 pinfo
->addr
= ioremap_nocache(bar2
, mem_len
);
465 BCMLOG_ERR("Failed to remap mem region...\n");
469 pinfo
->pci_mem_start
= bar2
;
470 pinfo
->pci_mem_len
= mem_len
;
472 rc
= check_mem_region(bar0
, i2o_len
);
474 BCMLOG_ERR("No valid mem region...\n");
478 pinfo
->i2o_addr
= ioremap_nocache(bar0
, i2o_len
);
479 if (!pinfo
->i2o_addr
) {
480 BCMLOG_ERR("Failed to remap mem region...\n");
484 pinfo
->pci_i2o_start
= bar0
;
485 pinfo
->pci_i2o_len
= i2o_len
;
487 rc
= pci_request_regions(pinfo
->pdev
, pinfo
->name
);
489 BCMLOG_ERR("Region request failed: %d\n", rc
);
493 BCMLOG(BCMLOG_SSTEP
, "Mapped addr:0x%08lx i2o_addr:0x%08lx\n",
494 (unsigned long)pinfo
->addr
, (unsigned long)pinfo
->i2o_addr
);
499 static void __devexit
chd_pci_release_mem(struct crystalhd_adp
*pinfo
)
505 iounmap(pinfo
->addr
);
508 iounmap(pinfo
->i2o_addr
);
510 pci_release_regions(pinfo
->pdev
);
514 static void __devexit
chd_dec_pci_remove(struct pci_dev
*pdev
)
516 struct crystalhd_adp
*pinfo
;
517 enum BC_STATUS sts
= BC_STS_SUCCESS
;
519 pinfo
= pci_get_drvdata(pdev
);
521 BCMLOG_ERR("could not get adp\n");
525 sts
= crystalhd_delete_cmd_context(&pinfo
->cmds
);
526 if (sts
!= BC_STS_SUCCESS
)
527 BCMLOG_ERR("cmd delete :%d\n", sts
);
529 chd_dec_release_chdev(pinfo
);
531 chd_dec_disable_int(pinfo
);
533 chd_pci_release_mem(pinfo
);
534 pci_disable_device(pinfo
->pdev
);
540 static int __devinit
chd_dec_pci_probe(struct pci_dev
*pdev
,
541 const struct pci_device_id
*entry
)
543 struct crystalhd_adp
*pinfo
;
545 enum BC_STATUS sts
= BC_STS_SUCCESS
;
547 BCMLOG(BCMLOG_DBG
, "PCI_INFO: Vendor:0x%04x Device:0x%04x "
548 "s_vendor:0x%04x s_device: 0x%04x\n",
549 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
550 pdev
->subsystem_device
);
552 pinfo
= kzalloc(sizeof(struct crystalhd_adp
), GFP_KERNEL
);
554 BCMLOG_ERR("Failed to allocate memory\n");
560 rc
= pci_enable_device(pdev
);
562 BCMLOG_ERR("Failed to enable PCI device\n");
566 snprintf(pinfo
->name
, sizeof(pinfo
->name
), "crystalhd_pci_e:%d:%d:%d",
567 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
),
568 PCI_FUNC(pdev
->devfn
));
570 rc
= chd_pci_reserve_mem(pinfo
);
572 BCMLOG_ERR("Failed to setup memory regions.\n");
573 pci_disable_device(pdev
);
579 pinfo
->drv_data
= entry
->driver_data
;
581 /* Setup adapter level lock.. */
582 spin_lock_init(&pinfo
->lock
);
584 /* setup api stuff.. */
585 chd_dec_init_chdev(pinfo
);
586 rc
= chd_dec_enable_int(pinfo
);
588 BCMLOG_ERR("_enable_int err:%d\n", rc
);
589 pci_disable_device(pdev
);
594 /* Set dma mask... */
595 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
596 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
598 } else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
599 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
602 BCMLOG_ERR("Unabled to setup DMA %d\n", rc
);
603 pci_disable_device(pdev
);
608 sts
= crystalhd_setup_cmd_context(&pinfo
->cmds
, pinfo
);
609 if (sts
!= BC_STS_SUCCESS
) {
610 BCMLOG_ERR("cmd setup :%d\n", sts
);
611 pci_disable_device(pdev
);
616 pci_set_master(pdev
);
618 pci_set_drvdata(pdev
, pinfo
);
630 int chd_dec_pci_suspend(struct pci_dev
*pdev
, pm_message_t state
)
632 struct crystalhd_adp
*adp
;
633 struct crystalhd_ioctl_data
*temp
;
634 enum BC_STATUS sts
= BC_STS_SUCCESS
;
636 adp
= pci_get_drvdata(pdev
);
638 BCMLOG_ERR("could not get adp\n");
642 temp
= chd_dec_alloc_iodata(adp
, false);
644 BCMLOG_ERR("could not get ioctl data\n");
648 sts
= crystalhd_suspend(&adp
->cmds
, temp
);
649 if (sts
!= BC_STS_SUCCESS
) {
650 BCMLOG_ERR("BCM70012 Suspend %d\n", sts
);
654 chd_dec_free_iodata(adp
, temp
, false);
655 chd_dec_disable_int(adp
);
656 pci_save_state(pdev
);
658 /* Disable IO/bus master/irq router */
659 pci_disable_device(pdev
);
660 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
664 int chd_dec_pci_resume(struct pci_dev
*pdev
)
666 struct crystalhd_adp
*adp
;
667 enum BC_STATUS sts
= BC_STS_SUCCESS
;
670 adp
= pci_get_drvdata(pdev
);
672 BCMLOG_ERR("could not get adp\n");
676 pci_set_power_state(pdev
, PCI_D0
);
677 pci_restore_state(pdev
);
679 /* device's irq possibly is changed, driver should take care */
680 if (pci_enable_device(pdev
)) {
681 BCMLOG_ERR("Failed to enable PCI device\n");
685 pci_set_master(pdev
);
687 rc
= chd_dec_enable_int(adp
);
689 BCMLOG_ERR("_enable_int err:%d\n", rc
);
690 pci_disable_device(pdev
);
694 sts
= crystalhd_resume(&adp
->cmds
);
695 if (sts
!= BC_STS_SUCCESS
) {
696 BCMLOG_ERR("BCM70012 Resume %d\n", sts
);
697 pci_disable_device(pdev
);
705 static DEFINE_PCI_DEVICE_TABLE(chd_dec_pci_id_table
) = {
706 { PCI_VDEVICE(BROADCOM
, 0x1612), 8 },
709 MODULE_DEVICE_TABLE(pci
, chd_dec_pci_id_table
);
711 static struct pci_driver bc_chd_70012_driver
= {
712 .name
= "Broadcom 70012 Decoder",
713 .probe
= chd_dec_pci_probe
,
714 .remove
= __devexit_p(chd_dec_pci_remove
),
715 .id_table
= chd_dec_pci_id_table
,
717 .suspend
= chd_dec_pci_suspend
,
718 .resume
= chd_dec_pci_resume
722 void chd_set_log_level(struct crystalhd_adp
*adp
, char *arg
)
724 if ((!arg
) || (strlen(arg
) < 3))
725 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
;
726 else if (!strncmp(arg
, "sstep", 5))
727 g_linklog_level
= BCMLOG_INFO
| BCMLOG_DATA
| BCMLOG_DBG
|
728 BCMLOG_SSTEP
| BCMLOG_ERROR
;
729 else if (!strncmp(arg
, "info", 4))
730 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
| BCMLOG_INFO
;
731 else if (!strncmp(arg
, "debug", 5))
732 g_linklog_level
= BCMLOG_ERROR
| BCMLOG_DATA
| BCMLOG_INFO
|
734 else if (!strncmp(arg
, "pball", 5))
735 g_linklog_level
= 0xFFFFFFFF & ~(BCMLOG_SPINLOCK
);
736 else if (!strncmp(arg
, "silent", 6))
742 struct crystalhd_adp
*chd_get_adp(void)
747 static int __init
chd_dec_module_init(void)
751 chd_set_log_level(NULL
, "debug");
752 BCMLOG(BCMLOG_DATA
, "Loading crystalhd %d.%d.%d\n",
753 crystalhd_kmod_major
, crystalhd_kmod_minor
, crystalhd_kmod_rev
);
755 rc
= pci_register_driver(&bc_chd_70012_driver
);
758 BCMLOG_ERR("Could not find any devices. err:%d\n", rc
);
762 module_init(chd_dec_module_init
);
764 static void __exit
chd_dec_module_cleanup(void)
766 BCMLOG(BCMLOG_DATA
, "unloading crystalhd %d.%d.%d\n",
767 crystalhd_kmod_major
, crystalhd_kmod_minor
, crystalhd_kmod_rev
);
769 pci_unregister_driver(&bc_chd_70012_driver
);
771 module_exit(chd_dec_module_cleanup
);
773 MODULE_AUTHOR("Naren Sankar <nsankar@broadcom.com>");
774 MODULE_AUTHOR("Prasad Bolisetty <prasadb@broadcom.com>");
775 MODULE_DESCRIPTION(CRYSTAL_HD_NAME
);
776 MODULE_LICENSE("GPL");
777 MODULE_ALIAS("bcm70012");