hcm's fixes
[kernel-power] / usbhost / drivers / usb2 / otg / otg.c
1 /*
2  * otg.c -- USB OTG utility code
3  *
4  * Copyright (C) 2004 Texas Instruments
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/byteorder/generic.h>
16 #include <linux/gfp.h>
17 #include <linux/usb.h>
18 #include <linux/usb/otg.h>
19
20 #include "../core/otg_whitelist.h"
21
22 static struct otg_transceiver *xceiv;
23
24 /**
25  * otg_get_transceiver - find the (single) OTG transceiver
26  *
27  * Returns the transceiver driver, after getting a refcount to it; or
28  * null if there is no such transceiver.  The caller is responsible for
29  * calling otg_put_transceiver() to release that count.
30  *
31  * For use by USB host and peripheral drivers.
32  */
33 struct otg_transceiver *otg_get_transceiver(void)
34 {
35         if (xceiv)
36                 get_device(xceiv->dev);
37         return xceiv;
38 }
39 EXPORT_SYMBOL(otg_get_transceiver);
40
41 /**
42  * otg_put_transceiver - release the (single) OTG transceiver
43  * @x: the transceiver returned by otg_get_transceiver()
44  *
45  * Releases a refcount the caller received from otg_get_transceiver().
46  *
47  * For use by USB host and peripheral drivers.
48  */
49 void otg_put_transceiver(struct otg_transceiver *x)
50 {
51         put_device(x->dev);
52 }
53 EXPORT_SYMBOL(otg_put_transceiver);
54
55 /**
56  * otg_set_transceiver - declare the (single) OTG transceiver
57  * @x: the USB OTG transceiver to be used; or NULL
58  *
59  * This call is exclusively for use by transceiver drivers, which
60  * coordinate the activities of drivers for host and peripheral
61  * controllers, and in some cases for VBUS current regulation.
62  */
63 int otg_set_transceiver(struct otg_transceiver *x)
64 {
65         if (xceiv && x)
66                 return -EBUSY;
67         xceiv = x;
68         return 0;
69 }
70 EXPORT_SYMBOL(otg_set_transceiver);
71
72 #ifdef  CONFIG_USB_OTG_WHITELIST
73
74 /*
75  * This OTG Whitelist is the OTG "Targeted Peripheral List".  It should
76  * mostly use of USB_DEVICE() or USB_DEVICE_VER() entries..
77  *
78  * YOU _SHOULD_ CHANGE THIS LIST TO MATCH YOUR PRODUCT AND ITS TESTING!
79  */
80
81 static struct usb_device_id whitelist_table[] = {
82
83 /* hubs are optional in OTG, but very handy ... */
84 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
85 { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
86
87 #ifdef  CONFIG_USB_PRINTER              /* ignoring nonstatic linkage! */
88 /* FIXME actually, printers are NOT supposed to use device classes;
89  * they're supposed to use interface classes...
90  */
91 { USB_DEVICE_INFO(7, 1, 1) },
92 { USB_DEVICE_INFO(7, 1, 2) },
93 { USB_DEVICE_INFO(7, 1, 3) },
94 #endif
95
96 #ifdef  CONFIG_USB_NET_CDCETHER
97 /* Linux-USB CDC Ethernet gadget */
98 { USB_DEVICE(0x0525, 0xa4a1), },
99 /* Linux-USB CDC Ethernet + RNDIS gadget */
100 { USB_DEVICE(0x0525, 0xa4a2), },
101 #endif
102
103 #if     defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
104 /* gadget zero, for testing */
105 { USB_DEVICE(0x0525, 0xa4a0), },
106 #endif
107
108 { }     /* Terminating entry */
109 };
110
111 int is_targeted(struct usb_device *dev)
112 {
113         struct usb_device_id    *id = whitelist_table;
114
115         /* possible in developer configs only! */
116         if (!dev->bus->otg_port)
117                 return 1;
118
119         /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
120         if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
121              le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
122                 return 0;
123
124         /* NOTE: can't use usb_match_id() since interface caches
125          * aren't set up yet. this is cut/paste from that code.
126          */
127         for (id = whitelist_table; id->match_flags; id++) {
128                 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
129                     id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
130                         continue;
131
132                 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
133                     id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
134                         continue;
135
136                 /* No need to test id->bcdDevice_lo != 0, since 0 is never
137                    greater than any unsigned number. */
138                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
139                     (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
140                         continue;
141
142                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
143                     (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
144                         continue;
145
146                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
147                     (id->bDeviceClass != dev->descriptor.bDeviceClass))
148                         continue;
149
150                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
151                     (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
152                         continue;
153
154                 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
155                     (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
156                         continue;
157
158                 return 1;
159         }
160
161         /* add other match criteria here ... */
162
163         return 1;
164         
165         /* OTG MESSAGE: report errors here, customize to match your product */
166         dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
167                 le16_to_cpu(dev->descriptor.idVendor),
168                 le16_to_cpu(dev->descriptor.idProduct));
169
170         return 0;
171 }
172
173 #endif  /* CONFIG_USB_OTG_WHITELIST */