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 = 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(void __user *ud, void *dr,
120 BCMLOG_ERR("Invalid arg\n");
125 rc = copy_to_user(ud, dr, size);
127 rc = copy_from_user(dr, 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,
141 unsigned long ua_off;
144 if (!adp || !io || !ua || !m_sz) {
145 BCMLOG_ERR("Invalid Arg!!\n");
149 io->add_cdata = vmalloc(m_sz);
150 if (!io->add_cdata) {
151 BCMLOG_ERR("kalloc fail for sz:%x\n", m_sz);
155 io->add_cdata_sz = m_sz;
156 ua_off = ua + sizeof(io->udata);
157 rc = crystalhd_user_data((void __user *)ua_off, io->add_cdata,
158 io->add_cdata_sz, 0);
160 BCMLOG_ERR("failed to pull add_cdata sz:%x ua_off:%x\n",
161 io->add_cdata_sz, (unsigned int)ua_off);
162 vfree(io->add_cdata);
163 io->add_cdata = NULL;
170 static int chd_dec_release_cdata(struct crystalhd_adp *adp,
171 struct crystalhd_ioctl_data *io,
174 unsigned long ua_off;
177 if (!adp || !io || !ua) {
178 BCMLOG_ERR("Invalid Arg!!\n");
182 if (io->cmd != BCM_IOC_FW_DOWNLOAD) {
183 ua_off = ua + sizeof(io->udata);
184 rc = crystalhd_user_data((void __user *)ua_off, io->add_cdata,
185 io->add_cdata_sz, 1);
188 "failed to push add_cdata sz:%x ua_off:%x\n",
189 io->add_cdata_sz, (unsigned int)ua_off);
195 vfree(io->add_cdata);
196 io->add_cdata = NULL;
202 static int chd_dec_proc_user_data(struct crystalhd_adp *adp,
203 struct crystalhd_ioctl_data *io,
204 unsigned long ua, int set)
209 if (!adp || !io || !ua) {
210 BCMLOG_ERR("Invalid Arg!!\n");
214 rc = crystalhd_user_data((void __user *)ua, &io->udata,
215 sizeof(io->udata), set);
217 BCMLOG_ERR("failed to %s iodata\n", (set ? "set" : "get"));
224 case BCM_IOC_FW_DOWNLOAD:
225 m_sz = io->udata.u.devMem.NumDwords * 4;
227 rc = chd_dec_release_cdata(adp, io, ua);
229 rc = chd_dec_fetch_cdata(adp, io, m_sz, ua);
238 static int chd_dec_api_cmd(struct crystalhd_adp *adp, unsigned long ua,
239 uint32_t uid, uint32_t cmd, crystalhd_cmd_proc func)
242 struct crystalhd_ioctl_data *temp;
243 enum BC_STATUS sts = BC_STS_SUCCESS;
245 temp = chd_dec_alloc_iodata(adp, 0);
247 BCMLOG_ERR("Failed to get iodata..\n");
254 rc = chd_dec_proc_user_data(adp, temp, ua, 0);
256 sts = func(&adp->cmds, temp);
257 if (sts == BC_STS_PENDING)
258 sts = BC_STS_NOT_IMPL;
259 temp->udata.RetSts = sts;
260 rc = chd_dec_proc_user_data(adp, temp, ua, 1);
263 chd_dec_free_iodata(adp, temp, 0);
269 static long chd_dec_ioctl(struct file *fd, unsigned int cmd, unsigned long ua)
271 struct crystalhd_adp *adp = chd_get_adp();
272 crystalhd_cmd_proc cproc;
273 struct crystalhd_user *uc;
277 BCMLOG_ERR("Invalid adp\n");
281 uc = fd->private_data;
283 BCMLOG_ERR("Failed to get uc\n");
287 mutex_lock(&chd_dec_mutex);
288 cproc = crystalhd_get_cmd_proc(&adp->cmds, cmd, uc);
290 BCMLOG_ERR("Unhandled command: %d\n", cmd);
291 mutex_unlock(&chd_dec_mutex);
295 ret = chd_dec_api_cmd(adp, ua, uc->uid, cmd, cproc);
296 mutex_unlock(&chd_dec_mutex);
300 static int chd_dec_open(struct inode *in, struct file *fd)
302 struct crystalhd_adp *adp = chd_get_adp();
304 enum BC_STATUS sts = BC_STS_SUCCESS;
305 struct crystalhd_user *uc = NULL;
308 BCMLOG_ERR("Invalid adp\n");
312 if (adp->cfg_users >= BC_LINK_MAX_OPENS) {
313 BCMLOG(BCMLOG_INFO, "Already in use.%d\n", adp->cfg_users);
317 sts = crystalhd_user_open(&adp->cmds, &uc);
318 if (sts != BC_STS_SUCCESS) {
319 BCMLOG_ERR("cmd_user_open - %d\n", sts);
325 fd->private_data = uc;
330 static int chd_dec_close(struct inode *in, struct file *fd)
332 struct crystalhd_adp *adp = chd_get_adp();
333 struct crystalhd_user *uc;
336 BCMLOG_ERR("Invalid adp\n");
340 uc = fd->private_data;
342 BCMLOG_ERR("Failed to get uc\n");
346 crystalhd_user_close(&adp->cmds, uc);
353 static const struct file_operations chd_dec_fops = {
354 .owner = THIS_MODULE,
355 .unlocked_ioctl = chd_dec_ioctl,
356 .open = chd_dec_open,
357 .release = chd_dec_close,
358 .llseek = noop_llseek,
361 static int chd_dec_init_chdev(struct crystalhd_adp *adp)
363 struct crystalhd_ioctl_data *temp;
365 int rc = -ENODEV, i = 0;
370 adp->chd_dec_major = register_chrdev(0, CRYSTALHD_API_NAME,
372 if (adp->chd_dec_major < 0) {
373 BCMLOG_ERR("Failed to create config dev\n");
374 rc = adp->chd_dec_major;
378 /* register crystalhd class */
379 crystalhd_class = class_create(THIS_MODULE, "crystalhd");
380 if (IS_ERR(crystalhd_class)) {
381 rc = PTR_ERR(crystalhd_class);
382 BCMLOG_ERR("failed to create class\n");
383 goto class_create_fail;
386 dev = device_create(crystalhd_class, NULL,
387 MKDEV(adp->chd_dec_major, 0), NULL, "crystalhd");
390 BCMLOG_ERR("failed to create device\n");
391 goto device_create_fail;
394 rc = crystalhd_create_elem_pool(adp, BC_LINK_ELEM_POOL_SZ);
396 BCMLOG_ERR("failed to create device\n");
400 /* Allocate general purpose ioctl pool. */
401 for (i = 0; i < CHD_IODATA_POOL_SZ; i++) {
402 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
404 BCMLOG_ERR("ioctl data pool kzalloc failed\n");
408 /* Add to global pool.. */
409 chd_dec_free_iodata(adp, temp, 0);
415 crystalhd_delete_elem_pool(adp);
417 device_destroy(crystalhd_class, MKDEV(adp->chd_dec_major, 0));
419 class_destroy(crystalhd_class);
421 unregister_chrdev(adp->chd_dec_major, CRYSTALHD_API_NAME);
426 static void chd_dec_release_chdev(struct crystalhd_adp *adp)
428 struct crystalhd_ioctl_data *temp = NULL;
432 if (adp->chd_dec_major > 0) {
433 /* unregister crystalhd class */
434 device_destroy(crystalhd_class, MKDEV(adp->chd_dec_major, 0));
435 unregister_chrdev(adp->chd_dec_major, CRYSTALHD_API_NAME);
436 BCMLOG(BCMLOG_INFO, "released api device - %d\n",
438 class_destroy(crystalhd_class);
440 adp->chd_dec_major = 0;
442 /* Clear iodata pool.. */
444 temp = chd_dec_alloc_iodata(adp, 0);
448 crystalhd_delete_elem_pool(adp);
451 static int chd_pci_reserve_mem(struct crystalhd_adp *pinfo)
454 unsigned long bar2 = pci_resource_start(pinfo->pdev, 2);
455 uint32_t mem_len = pci_resource_len(pinfo->pdev, 2);
456 unsigned long bar0 = pci_resource_start(pinfo->pdev, 0);
457 uint32_t i2o_len = pci_resource_len(pinfo->pdev, 0);
459 BCMLOG(BCMLOG_SSTEP, "bar2:0x%lx-0x%08x bar0:0x%lx-0x%08x\n",
460 bar2, mem_len, bar0, i2o_len);
462 rc = check_mem_region(bar2, mem_len);
464 BCMLOG_ERR("No valid mem region...\n");
468 pinfo->addr = ioremap_nocache(bar2, mem_len);
470 BCMLOG_ERR("Failed to remap mem region...\n");
474 pinfo->pci_mem_start = bar2;
475 pinfo->pci_mem_len = mem_len;
477 rc = check_mem_region(bar0, i2o_len);
479 BCMLOG_ERR("No valid mem region...\n");
483 pinfo->i2o_addr = ioremap_nocache(bar0, i2o_len);
484 if (!pinfo->i2o_addr) {
485 BCMLOG_ERR("Failed to remap mem region...\n");
489 pinfo->pci_i2o_start = bar0;
490 pinfo->pci_i2o_len = i2o_len;
492 rc = pci_request_regions(pinfo->pdev, pinfo->name);
494 BCMLOG_ERR("Region request failed: %d\n", rc);
498 BCMLOG(BCMLOG_SSTEP, "Mapped addr:0x%08lx i2o_addr:0x%08lx\n",
499 (unsigned long)pinfo->addr, (unsigned long)pinfo->i2o_addr);
504 static void chd_pci_release_mem(struct crystalhd_adp *pinfo)
510 iounmap(pinfo->addr);
513 iounmap(pinfo->i2o_addr);
515 pci_release_regions(pinfo->pdev);
519 static void chd_dec_pci_remove(struct pci_dev *pdev)
521 struct crystalhd_adp *pinfo;
522 enum BC_STATUS sts = BC_STS_SUCCESS;
524 pinfo = pci_get_drvdata(pdev);
526 BCMLOG_ERR("could not get adp\n");
530 sts = crystalhd_delete_cmd_context(&pinfo->cmds);
531 if (sts != BC_STS_SUCCESS)
532 BCMLOG_ERR("cmd delete :%d\n", sts);
534 chd_dec_release_chdev(pinfo);
536 chd_dec_disable_int(pinfo);
538 chd_pci_release_mem(pinfo);
539 pci_disable_device(pinfo->pdev);
545 static int chd_dec_pci_probe(struct pci_dev *pdev,
546 const struct pci_device_id *entry)
548 struct crystalhd_adp *pinfo;
550 enum BC_STATUS sts = BC_STS_SUCCESS;
553 "PCI_INFO: Vendor:0x%04x Device:0x%04x s_vendor:0x%04x s_device: 0x%04x\n",
554 pdev->vendor, pdev->device, pdev->subsystem_vendor,
555 pdev->subsystem_device);
557 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
559 BCMLOG_ERR("Failed to allocate memory\n");
565 rc = pci_enable_device(pdev);
567 BCMLOG_ERR("Failed to enable PCI device\n");
571 snprintf(pinfo->name, sizeof(pinfo->name), "crystalhd_pci_e:%d:%d:%d",
572 pdev->bus->number, PCI_SLOT(pdev->devfn),
573 PCI_FUNC(pdev->devfn));
575 rc = chd_pci_reserve_mem(pinfo);
577 BCMLOG_ERR("Failed to setup memory regions.\n");
578 pci_disable_device(pdev);
584 pinfo->drv_data = entry->driver_data;
586 /* Setup adapter level lock.. */
587 spin_lock_init(&pinfo->lock);
589 /* setup api stuff.. */
590 chd_dec_init_chdev(pinfo);
591 rc = chd_dec_enable_int(pinfo);
593 BCMLOG_ERR("_enable_int err:%d\n", rc);
594 pci_disable_device(pdev);
599 /* Set dma mask... */
600 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
601 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
603 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
604 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
607 BCMLOG_ERR("Unabled to setup DMA %d\n", rc);
608 pci_disable_device(pdev);
613 sts = crystalhd_setup_cmd_context(&pinfo->cmds, pinfo);
614 if (sts != BC_STS_SUCCESS) {
615 BCMLOG_ERR("cmd setup :%d\n", sts);
616 pci_disable_device(pdev);
621 pci_set_master(pdev);
623 pci_set_drvdata(pdev, pinfo);
635 static int chd_dec_pci_suspend(struct pci_dev *pdev, pm_message_t state)
637 struct crystalhd_adp *adp;
638 struct crystalhd_ioctl_data *temp;
639 enum BC_STATUS sts = BC_STS_SUCCESS;
641 adp = pci_get_drvdata(pdev);
643 BCMLOG_ERR("could not get adp\n");
647 temp = chd_dec_alloc_iodata(adp, false);
649 BCMLOG_ERR("could not get ioctl data\n");
653 sts = crystalhd_suspend(&adp->cmds, temp);
654 if (sts != BC_STS_SUCCESS) {
655 BCMLOG_ERR("BCM70012 Suspend %d\n", sts);
659 chd_dec_free_iodata(adp, temp, false);
660 chd_dec_disable_int(adp);
661 pci_save_state(pdev);
663 /* Disable IO/bus master/irq router */
664 pci_disable_device(pdev);
665 pci_set_power_state(pdev, pci_choose_state(pdev, state));
669 static int chd_dec_pci_resume(struct pci_dev *pdev)
671 struct crystalhd_adp *adp;
672 enum BC_STATUS sts = BC_STS_SUCCESS;
675 adp = pci_get_drvdata(pdev);
677 BCMLOG_ERR("could not get adp\n");
681 pci_set_power_state(pdev, PCI_D0);
682 pci_restore_state(pdev);
684 /* device's irq possibly is changed, driver should take care */
685 if (pci_enable_device(pdev)) {
686 BCMLOG_ERR("Failed to enable PCI device\n");
690 pci_set_master(pdev);
692 rc = chd_dec_enable_int(adp);
694 BCMLOG_ERR("_enable_int err:%d\n", rc);
695 pci_disable_device(pdev);
699 sts = crystalhd_resume(&adp->cmds);
700 if (sts != BC_STS_SUCCESS) {
701 BCMLOG_ERR("BCM70012 Resume %d\n", sts);
702 pci_disable_device(pdev);
710 static const struct pci_device_id chd_dec_pci_id_table[] = {
711 { PCI_VDEVICE(BROADCOM, 0x1612), 8 },
714 MODULE_DEVICE_TABLE(pci, chd_dec_pci_id_table);
716 static struct pci_driver bc_chd_70012_driver = {
717 .name = "Broadcom 70012 Decoder",
718 .probe = chd_dec_pci_probe,
719 .remove = chd_dec_pci_remove,
720 .id_table = chd_dec_pci_id_table,
722 .suspend = chd_dec_pci_suspend,
723 .resume = chd_dec_pci_resume
727 void chd_set_log_level(struct crystalhd_adp *adp, char *arg)
729 if ((!arg) || (strlen(arg) < 3))
730 g_linklog_level = BCMLOG_ERROR | BCMLOG_DATA;
731 else if (!strncmp(arg, "sstep", 5))
732 g_linklog_level = BCMLOG_INFO | BCMLOG_DATA | BCMLOG_DBG |
733 BCMLOG_SSTEP | BCMLOG_ERROR;
734 else if (!strncmp(arg, "info", 4))
735 g_linklog_level = BCMLOG_ERROR | BCMLOG_DATA | BCMLOG_INFO;
736 else if (!strncmp(arg, "debug", 5))
737 g_linklog_level = BCMLOG_ERROR | BCMLOG_DATA | BCMLOG_INFO |
739 else if (!strncmp(arg, "pball", 5))
740 g_linklog_level = 0xFFFFFFFF & ~(BCMLOG_SPINLOCK);
741 else if (!strncmp(arg, "silent", 6))
747 struct crystalhd_adp *chd_get_adp(void)
752 static int __init chd_dec_module_init(void)
756 chd_set_log_level(NULL, "debug");
757 BCMLOG(BCMLOG_DATA, "Loading crystalhd %d.%d.%d\n",
758 crystalhd_kmod_major, crystalhd_kmod_minor, crystalhd_kmod_rev);
760 rc = pci_register_driver(&bc_chd_70012_driver);
763 BCMLOG_ERR("Could not find any devices. err:%d\n", rc);
767 module_init(chd_dec_module_init);
769 static void __exit chd_dec_module_cleanup(void)
771 BCMLOG(BCMLOG_DATA, "unloading crystalhd %d.%d.%d\n",
772 crystalhd_kmod_major, crystalhd_kmod_minor, crystalhd_kmod_rev);
774 pci_unregister_driver(&bc_chd_70012_driver);
776 module_exit(chd_dec_module_cleanup);
778 MODULE_AUTHOR("Naren Sankar <nsankar@broadcom.com>");
779 MODULE_AUTHOR("Prasad Bolisetty <prasadb@broadcom.com>");
780 MODULE_DESCRIPTION(CRYSTAL_HD_NAME);
781 MODULE_LICENSE("GPL");
782 MODULE_ALIAS("bcm70012");