USB: nop-usb-xceiv: behave when linked as a module
[firefly-linux-kernel-4.4.55.git] / drivers / usb / otg / nop-usb-xceiv.c
1 /*
2  * drivers/usb/otg/nop-usb-xceiv.c
3  *
4  * NOP USB transceiver for all USB transceiver which are either built-in
5  * into USB IP or which are mostly autonomous.
6  *
7  * Copyright (C) 2009 Texas Instruments Inc
8  * Author: Ajay Kumar Gupta <ajay.gupta@ti.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * Current status:
25  *      This provides a "nop" transceiver for PHYs which are
26  *      autonomous such as isp1504, isp1707, etc.
27  */
28
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/usb/otg.h>
33
34 struct nop_usb_xceiv {
35         struct otg_transceiver  otg;
36         struct device           *dev;
37 };
38
39 static struct platform_device *pd;
40
41 void usb_nop_xceiv_register(void)
42 {
43         if (pd)
44                 return;
45         pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0);
46         if (!pd) {
47                 printk(KERN_ERR "Unable to register usb nop transceiver\n");
48                 return;
49         }
50 }
51 EXPORT_SYMBOL(usb_nop_xceiv_register);
52
53 void usb_nop_xceiv_unregister(void)
54 {
55         platform_device_unregister(pd);
56 }
57 EXPORT_SYMBOL(usb_nop_xceiv_unregister);
58
59 static inline struct nop_usb_xceiv *xceiv_to_nop(struct otg_transceiver *x)
60 {
61         return container_of(x, struct nop_usb_xceiv, otg);
62 }
63
64 static int nop_set_suspend(struct otg_transceiver *x, int suspend)
65 {
66         return 0;
67 }
68
69 static int nop_set_peripheral(struct otg_transceiver *x,
70                 struct usb_gadget *gadget)
71 {
72         struct nop_usb_xceiv *nop;
73
74         if (!x)
75                 return -ENODEV;
76
77         nop = xceiv_to_nop(x);
78
79         if (!gadget) {
80                 nop->otg.gadget = NULL;
81                 return -ENODEV;
82         }
83
84         nop->otg.gadget = gadget;
85         nop->otg.state = OTG_STATE_B_IDLE;
86         return 0;
87 }
88
89 static int nop_set_host(struct otg_transceiver *x, struct usb_bus *host)
90 {
91         struct nop_usb_xceiv *nop;
92
93         if (!x)
94                 return -ENODEV;
95
96         nop = xceiv_to_nop(x);
97
98         if (!host) {
99                 nop->otg.host = NULL;
100                 return -ENODEV;
101         }
102
103         nop->otg.host = host;
104         return 0;
105 }
106
107 static int __devinit nop_usb_xceiv_probe(struct platform_device *pdev)
108 {
109         struct nop_usb_xceiv    *nop;
110         int err;
111
112         nop = kzalloc(sizeof *nop, GFP_KERNEL);
113         if (!nop)
114                 return -ENOMEM;
115
116         nop->dev                = &pdev->dev;
117         nop->otg.dev            = nop->dev;
118         nop->otg.label          = "nop-xceiv";
119         nop->otg.state          = OTG_STATE_UNDEFINED;
120         nop->otg.set_host       = nop_set_host;
121         nop->otg.set_peripheral = nop_set_peripheral;
122         nop->otg.set_suspend    = nop_set_suspend;
123
124         err = otg_set_transceiver(&nop->otg);
125         if (err) {
126                 dev_err(&pdev->dev, "can't register transceiver, err: %d\n",
127                         err);
128                 goto exit;
129         }
130
131         platform_set_drvdata(pdev, nop);
132
133         return 0;
134 exit:
135         kfree(nop);
136         return err;
137 }
138
139 static int __devexit nop_usb_xceiv_remove(struct platform_device *pdev)
140 {
141         struct nop_usb_xceiv *nop = platform_get_drvdata(pdev);
142
143         otg_set_transceiver(NULL);
144
145         platform_set_drvdata(pdev, NULL);
146         kfree(nop);
147
148         return 0;
149 }
150
151 static struct platform_driver nop_usb_xceiv_driver = {
152         .probe          = nop_usb_xceiv_probe,
153         .remove         = __devexit_p(nop_usb_xceiv_remove),
154         .driver         = {
155                 .name   = "nop_usb_xceiv",
156                 .owner  = THIS_MODULE,
157         },
158 };
159
160 static int __init nop_usb_xceiv_init(void)
161 {
162         return platform_driver_register(&nop_usb_xceiv_driver);
163 }
164 subsys_initcall(nop_usb_xceiv_init);
165
166 static void __exit nop_usb_xceiv_exit(void)
167 {
168         platform_driver_unregister(&nop_usb_xceiv_driver);
169 }
170 module_exit(nop_usb_xceiv_exit);
171
172 MODULE_ALIAS("platform:nop_usb_xceiv");
173 MODULE_AUTHOR("Texas Instruments Inc");
174 MODULE_DESCRIPTION("NOP USB Transceiver driver");
175 MODULE_LICENSE("GPL");