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/version.h>
20 #include "crystalhd_lnx.h"
22 static struct class *crystalhd_class;
24 static struct crystalhd_adp *g_adp_info;
26 static irqreturn_t chd_dec_isr(int irq, void *arg)
28 struct crystalhd_adp *adp = (struct crystalhd_adp *) arg;
31 rc = crystalhd_cmd_interrupt(&adp->cmds);
33 return IRQ_RETVAL(rc);
36 static int chd_dec_enable_int(struct crystalhd_adp *adp)
40 if (!adp || !adp->pdev) {
41 BCMLOG_ERR("Invalid arg!!\n");
45 if (adp->pdev->msi_enabled)
48 adp->msi = pci_enable_msi(adp->pdev);
50 rc = request_irq(adp->pdev->irq, chd_dec_isr, IRQF_SHARED,
51 adp->name, (void *)adp);
53 BCMLOG_ERR("Interrupt request failed.. \n");
54 pci_disable_msi(adp->pdev);
60 static int chd_dec_disable_int(struct crystalhd_adp *adp)
62 if (!adp || !adp->pdev) {
63 BCMLOG_ERR("Invalid arg!!\n");
67 free_irq(adp->pdev->irq, adp);
70 pci_disable_msi(adp->pdev);
75 crystalhd_ioctl_data *chd_dec_alloc_iodata(struct crystalhd_adp *adp, bool isr)
77 unsigned long flags = 0;
78 crystalhd_ioctl_data *temp;
83 spin_lock_irqsave(&adp->lock, flags);
85 temp = adp->idata_free_head;
87 adp->idata_free_head = adp->idata_free_head->next;
88 memset(temp, 0, sizeof(*temp));
91 spin_unlock_irqrestore(&adp->lock, flags);
95 void chd_dec_free_iodata(struct crystalhd_adp *adp, crystalhd_ioctl_data *iodata,
98 unsigned long flags = 0;
103 spin_lock_irqsave(&adp->lock, flags);
104 iodata->next = adp->idata_free_head;
105 adp->idata_free_head = iodata;
106 spin_unlock_irqrestore(&adp->lock, flags);
109 static inline int crystalhd_user_data(unsigned long ud, void *dr, int size, int set)
114 BCMLOG_ERR("Invalid arg \n");
119 rc = copy_to_user((void *)ud, dr, size);
121 rc = copy_from_user(dr, (void *)ud, size);
124 BCMLOG_ERR("Invalid args for command \n");
131 static int chd_dec_fetch_cdata(struct crystalhd_adp *adp, crystalhd_ioctl_data *io,
132 uint32_t m_sz, unsigned long ua)
134 unsigned long ua_off;
137 if (!adp || !io || !ua || !m_sz) {
138 BCMLOG_ERR("Invalid Arg!!\n");
142 io->add_cdata = vmalloc(m_sz);
143 if (!io->add_cdata) {
144 BCMLOG_ERR("kalloc fail for sz:%x\n", m_sz);
148 io->add_cdata_sz = m_sz;
149 ua_off = ua + sizeof(io->udata);
150 rc = crystalhd_user_data(ua_off, io->add_cdata, io->add_cdata_sz, 0);
152 BCMLOG_ERR("failed to pull add_cdata sz:%x ua_off:%x\n",
153 io->add_cdata_sz, (unsigned int)ua_off);
155 kfree(io->add_cdata);
156 io->add_cdata = NULL;
164 static int chd_dec_release_cdata(struct crystalhd_adp *adp,
165 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 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 crystalhd_ioctl_data *temp;
234 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 int chd_dec_ioctl(struct inode *in, struct file *fd,
264 unsigned int cmd, unsigned long ua)
266 struct crystalhd_adp *adp = chd_get_adp();
267 crystalhd_cmd_proc cproc;
268 struct crystalhd_user *uc;
271 BCMLOG_ERR("Invalid adp\n");
275 uc = (struct crystalhd_user *)fd->private_data;
277 BCMLOG_ERR("Failed to get uc\n");
281 cproc = crystalhd_get_cmd_proc(&adp->cmds, cmd, uc);
283 BCMLOG_ERR("Unhandled command: %d\n", cmd);
287 return chd_dec_api_cmd(adp, ua, uc->uid, cmd, cproc);
290 static int chd_dec_open(struct inode *in, struct file *fd)
292 struct crystalhd_adp *adp = chd_get_adp();
294 BC_STATUS sts = BC_STS_SUCCESS;
295 struct crystalhd_user *uc = NULL;
299 BCMLOG_ERR("Invalid adp\n");
303 if (adp->cfg_users >= BC_LINK_MAX_OPENS) {
304 BCMLOG(BCMLOG_INFO, "Already in use.%d\n", adp->cfg_users);
308 sts = crystalhd_user_open(&adp->cmds, &uc);
309 if (sts != BC_STS_SUCCESS) {
310 BCMLOG_ERR("cmd_user_open - %d \n", sts);
316 fd->private_data = uc;
321 static int chd_dec_close(struct inode *in, struct file *fd)
323 struct crystalhd_adp *adp = chd_get_adp();
324 struct crystalhd_user *uc;
328 BCMLOG_ERR("Invalid adp \n");
332 uc = (struct crystalhd_user *)fd->private_data;
334 BCMLOG_ERR("Failed to get uc\n");
338 crystalhd_user_close(&adp->cmds, uc);
345 static const struct file_operations chd_dec_fops = {
346 .owner = THIS_MODULE,
347 .ioctl = chd_dec_ioctl,
348 .open = chd_dec_open,
349 .release = chd_dec_close,
352 static int __devinit chd_dec_init_chdev(struct crystalhd_adp *adp)
354 crystalhd_ioctl_data *temp;
356 int rc = -ENODEV, i = 0;
361 adp->chd_dec_major = register_chrdev(0, CRYSTALHD_API_NAME,
363 if (adp->chd_dec_major < 0) {
364 BCMLOG_ERR("Failed to create config dev\n");
365 rc = adp->chd_dec_major;
369 /* register crystalhd class */
370 crystalhd_class = class_create(THIS_MODULE, "crystalhd");
371 if (IS_ERR(crystalhd_class)) {
372 BCMLOG_ERR("failed to create class\n");
376 dev = device_create(crystalhd_class, NULL, MKDEV(adp->chd_dec_major, 0),
379 BCMLOG_ERR("failed to create device\n");
380 goto device_create_fail;
383 rc = crystalhd_create_elem_pool(adp, BC_LINK_ELEM_POOL_SZ);
385 BCMLOG_ERR("failed to create device\n");
389 /* Allocate general purpose ioctl pool. */
390 for (i = 0; i < CHD_IODATA_POOL_SZ; i++) {
391 /* FIXME: jarod: why atomic? */
392 temp = kzalloc(sizeof(crystalhd_ioctl_data), GFP_ATOMIC);
394 BCMLOG_ERR("ioctl data pool kzalloc failed\n");
398 /* Add to global pool.. */
399 chd_dec_free_iodata(adp, temp, 0);
405 crystalhd_delete_elem_pool(adp);
407 device_destroy(crystalhd_class, MKDEV(adp->chd_dec_major, 0));
409 class_destroy(crystalhd_class);
414 static void __devexit chd_dec_release_chdev(struct crystalhd_adp *adp)
416 crystalhd_ioctl_data *temp = NULL;
420 if (adp->chd_dec_major > 0) {
421 /* unregister crystalhd class */
422 device_destroy(crystalhd_class, MKDEV(adp->chd_dec_major, 0));
423 unregister_chrdev(adp->chd_dec_major, CRYSTALHD_API_NAME);
424 BCMLOG(BCMLOG_INFO, "released api device - %d\n",
426 class_destroy(crystalhd_class);
428 adp->chd_dec_major = 0;
430 /* Clear iodata pool.. */
432 temp = chd_dec_alloc_iodata(adp, 0);
437 crystalhd_delete_elem_pool(adp);
440 static int __devinit chd_pci_reserve_mem(struct crystalhd_adp *pinfo)
443 unsigned long bar2 = pci_resource_start(pinfo->pdev, 2);
444 uint32_t mem_len = pci_resource_len(pinfo->pdev, 2);
445 unsigned long bar0 = pci_resource_start(pinfo->pdev, 0);
446 uint32_t i2o_len = pci_resource_len(pinfo->pdev, 0);
448 BCMLOG(BCMLOG_SSTEP, "bar2:0x%lx-0x%08x bar0:0x%lx-0x%08x\n",
449 bar2, mem_len, bar0, i2o_len);
451 rc = check_mem_region(bar2, mem_len);
453 BCMLOG_ERR("No valid mem region...\n");
457 pinfo->addr = ioremap_nocache(bar2, mem_len);
459 BCMLOG_ERR("Failed to remap mem region...\n");
463 pinfo->pci_mem_start = bar2;
464 pinfo->pci_mem_len = mem_len;
466 rc = check_mem_region(bar0, i2o_len);
468 BCMLOG_ERR("No valid mem region...\n");
472 pinfo->i2o_addr = ioremap_nocache(bar0, i2o_len);
473 if (!pinfo->i2o_addr) {
474 BCMLOG_ERR("Failed to remap mem region...\n");
478 pinfo->pci_i2o_start = bar0;
479 pinfo->pci_i2o_len = i2o_len;
481 rc = pci_request_regions(pinfo->pdev, pinfo->name);
483 BCMLOG_ERR("Region request failed: %d\n", rc);
487 BCMLOG(BCMLOG_SSTEP, "Mapped addr:0x%08lx i2o_addr:0x%08lx\n",
488 (unsigned long)pinfo->addr, (unsigned long)pinfo->i2o_addr);
493 static void __devexit chd_pci_release_mem(struct crystalhd_adp *pinfo)
499 iounmap(pinfo->addr);
502 iounmap(pinfo->i2o_addr);
504 pci_release_regions(pinfo->pdev);
508 static void __devexit chd_dec_pci_remove(struct pci_dev *pdev)
510 struct crystalhd_adp *pinfo;
511 BC_STATUS sts = BC_STS_SUCCESS;
515 pinfo = (struct crystalhd_adp *) pci_get_drvdata(pdev);
517 BCMLOG_ERR("could not get adp\n");
521 sts = crystalhd_delete_cmd_context(&pinfo->cmds);
522 if (sts != BC_STS_SUCCESS)
523 BCMLOG_ERR("cmd delete :%d \n", sts);
525 chd_dec_release_chdev(pinfo);
527 chd_dec_disable_int(pinfo);
529 chd_pci_release_mem(pinfo);
530 pci_disable_device(pinfo->pdev);
536 static int __devinit chd_dec_pci_probe(struct pci_dev *pdev,
537 const struct pci_device_id *entry)
539 struct crystalhd_adp *pinfo;
541 BC_STATUS sts = BC_STS_SUCCESS;
543 BCMLOG(BCMLOG_DBG, "PCI_INFO: Vendor:0x%04x Device:0x%04x "
544 "s_vendor:0x%04x s_device: 0x%04x\n",
545 pdev->vendor, pdev->device, pdev->subsystem_vendor,
546 pdev->subsystem_device);
548 /* FIXME: jarod: why atomic? */
549 pinfo = kzalloc(sizeof(struct crystalhd_adp), GFP_ATOMIC);
551 BCMLOG_ERR("Failed to allocate memory\n");
557 rc = pci_enable_device(pdev);
559 BCMLOG_ERR("Failed to enable PCI device\n");
563 snprintf(pinfo->name, 31, "crystalhd_pci_e:%d:%d:%d",
564 pdev->bus->number, PCI_SLOT(pdev->devfn),
565 PCI_FUNC(pdev->devfn));
567 rc = chd_pci_reserve_mem(pinfo);
569 BCMLOG_ERR("Failed to setup memory regions.\n");
574 pinfo->drv_data = entry->driver_data;
576 /* Setup adapter level lock.. */
577 spin_lock_init(&pinfo->lock);
579 /* setup api stuff.. */
580 chd_dec_init_chdev(pinfo);
581 rc = chd_dec_enable_int(pinfo);
583 BCMLOG_ERR("_enable_int err:%d \n", rc);
584 pci_disable_device(pdev);
588 /* Set dma mask... */
589 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
590 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
592 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
593 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
596 BCMLOG_ERR("Unabled to setup DMA %d\n", rc);
597 pci_disable_device(pdev);
601 sts = crystalhd_setup_cmd_context(&pinfo->cmds, pinfo);
602 if (sts != BC_STS_SUCCESS) {
603 BCMLOG_ERR("cmd setup :%d \n", sts);
604 pci_disable_device(pdev);
608 pci_set_master(pdev);
610 pci_set_drvdata(pdev, pinfo);
618 int chd_dec_pci_suspend(struct pci_dev *pdev, pm_message_t state)
620 struct crystalhd_adp *adp;
621 crystalhd_ioctl_data *temp;
622 BC_STATUS sts = BC_STS_SUCCESS;
624 adp = (struct crystalhd_adp *)pci_get_drvdata(pdev);
626 BCMLOG_ERR("could not get adp\n");
630 temp = chd_dec_alloc_iodata(adp, false);
632 BCMLOG_ERR("could not get ioctl data\n");
636 sts = crystalhd_suspend(&adp->cmds, temp);
637 if (sts != BC_STS_SUCCESS) {
638 BCMLOG_ERR("BCM70012 Suspend %d\n", sts);
642 chd_dec_free_iodata(adp, temp, false);
643 chd_dec_disable_int(adp);
644 pci_save_state(pdev);
646 /* Disable IO/bus master/irq router */
647 pci_disable_device(pdev);
648 pci_set_power_state(pdev, pci_choose_state(pdev, state));
652 int chd_dec_pci_resume(struct pci_dev *pdev)
654 struct crystalhd_adp *adp;
655 BC_STATUS sts = BC_STS_SUCCESS;
658 adp = (struct crystalhd_adp *)pci_get_drvdata(pdev);
660 BCMLOG_ERR("could not get adp\n");
664 pci_set_power_state(pdev, PCI_D0);
665 pci_restore_state(pdev);
667 /* device's irq possibly is changed, driver should take care */
668 if (pci_enable_device(pdev)) {
669 BCMLOG_ERR("Failed to enable PCI device\n");
673 pci_set_master(pdev);
675 rc = chd_dec_enable_int(adp);
677 BCMLOG_ERR("_enable_int err:%d \n", rc);
678 pci_disable_device(pdev);
682 sts = crystalhd_resume(&adp->cmds);
683 if (sts != BC_STS_SUCCESS) {
684 BCMLOG_ERR("BCM70012 Resume %d\n", sts);
685 pci_disable_device(pdev);
693 static DEFINE_PCI_DEVICE_TABLE(chd_dec_pci_id_table) = {
694 { PCI_VDEVICE(BROADCOM, 0x1612), 8 },
697 MODULE_DEVICE_TABLE(pci, chd_dec_pci_id_table);
699 static struct pci_driver bc_chd_70012_driver = {
700 .name = "Broadcom 70012 Decoder",
701 .probe = chd_dec_pci_probe,
702 .remove = __devexit_p(chd_dec_pci_remove),
703 .id_table = chd_dec_pci_id_table,
705 .suspend = chd_dec_pci_suspend,
706 .resume = chd_dec_pci_resume
710 void chd_set_log_level(struct crystalhd_adp *adp, char *arg)
712 if ((!arg) || (strlen(arg) < 3))
713 g_linklog_level = BCMLOG_ERROR | BCMLOG_DATA;
714 else if (!strncmp(arg, "sstep", 5))
715 g_linklog_level = BCMLOG_INFO | BCMLOG_DATA | BCMLOG_DBG |
716 BCMLOG_SSTEP | BCMLOG_ERROR;
717 else if (!strncmp(arg, "info", 4))
718 g_linklog_level = BCMLOG_ERROR | BCMLOG_DATA | BCMLOG_INFO;
719 else if (!strncmp(arg, "debug", 5))
720 g_linklog_level = BCMLOG_ERROR | BCMLOG_DATA | BCMLOG_INFO |
722 else if (!strncmp(arg, "pball", 5))
723 g_linklog_level = 0xFFFFFFFF & ~(BCMLOG_SPINLOCK);
724 else if (!strncmp(arg, "silent", 6))
730 struct crystalhd_adp *chd_get_adp(void)
735 static int __init chd_dec_module_init(void)
739 chd_set_log_level(NULL, "debug");
740 BCMLOG(BCMLOG_DATA, "Loading crystalhd %d.%d.%d \n",
741 crystalhd_kmod_major, crystalhd_kmod_minor, crystalhd_kmod_rev);
743 rc = pci_register_driver(&bc_chd_70012_driver);
746 BCMLOG_ERR("Could not find any devices. err:%d \n", rc);
750 module_init(chd_dec_module_init);
752 static void __exit chd_dec_module_cleanup(void)
754 BCMLOG(BCMLOG_DATA, "unloading crystalhd %d.%d.%d \n",
755 crystalhd_kmod_major, crystalhd_kmod_minor, crystalhd_kmod_rev);
757 pci_unregister_driver(&bc_chd_70012_driver);
759 module_exit(chd_dec_module_cleanup);
761 MODULE_AUTHOR("Naren Sankar <nsankar@broadcom.com>");
762 MODULE_AUTHOR("Prasad Bolisetty <prasadb@broadcom.com>");
763 MODULE_DESCRIPTION(CRYSTAL_HD_NAME);
764 MODULE_LICENSE("GPL");
765 MODULE_ALIAS("bcm70012");