Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Renesas USB driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/usb.h> |
| 23 | #include <linux/usb/hcd.h> |
| 24 | #include "common.h" |
| 25 | |
| 26 | /* |
| 27 | *** HARDWARE LIMITATION *** |
| 28 | * |
| 29 | * 1) renesas_usbhs has a limited number of controllable devices. |
| 30 | * it can control only 9 devices in generally. |
| 31 | * see DEVADDn / DCPMAXP / PIPEMAXP. |
| 32 | * |
| 33 | * 2) renesas_usbhs pipe number is limited. |
| 34 | * the pipe will be re-used for each devices. |
| 35 | * so, software should control DATA0/1 sequence of each devices. |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * image of mod_host |
| 41 | * |
| 42 | * +--------+ |
| 43 | * | udev 0 | --> it is used when set address |
| 44 | * +--------+ |
| 45 | * |
| 46 | * +--------+ pipes are reused for each uep. |
| 47 | * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 48 | * +--------+ | | other device requested |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 49 | * +- [uep 1 (bulk)] --|---+ +--------------+ |
| 50 | * | +--------------> | pipe0 (dcp) | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 51 | * +- [uep 2 (bulk)] -@ | +--------------+ |
| 52 | * | | pipe1 (isoc) | |
| 53 | * +--------+ | +--------------+ |
| 54 | * | udev 2 |-+- [uep 0 (dcp) ] -@ +----------> | pipe2 (bulk) | |
| 55 | * +--------+ | +--------------+ |
| 56 | * +- [uep 1 (int) ] ----+ +------> | pipe3 (bulk) | |
| 57 | * | | +--------------+ |
| 58 | * +--------+ +-----|------> | pipe4 (int) | |
| 59 | * | udev 3 |-+- [uep 0 (dcp) ] -@ | +--------------+ |
| 60 | * +--------+ | | | .... | |
| 61 | * +- [uep 1 (bulk)] -@ | | .... | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 62 | * | | |
| 63 | * +- [uep 2 (bulk)]-----------+ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 64 | * |
| 65 | * @ : uep requested free pipe, but all have been used. |
| 66 | * now it is waiting for free pipe |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 67 | */ |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * struct |
| 72 | */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 73 | struct usbhsh_request { |
| 74 | struct urb *urb; |
| 75 | struct usbhs_pkt pkt; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct usbhsh_device { |
| 79 | struct usb_device *usbv; |
| 80 | struct list_head ep_list_head; /* list of usbhsh_ep */ |
| 81 | }; |
| 82 | |
| 83 | struct usbhsh_ep { |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 84 | struct usbhs_pipe *pipe; /* attached pipe */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 85 | struct usbhsh_device *udev; /* attached udev */ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 86 | struct usb_host_endpoint *ep; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 87 | struct list_head ep_list; /* list to usbhsh_device */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */ |
| 91 | #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */ |
| 92 | struct usbhsh_hpriv { |
| 93 | struct usbhs_mod mod; |
| 94 | struct usbhs_pipe *dcp; |
| 95 | |
| 96 | struct usbhsh_device udev[USBHSH_DEVICE_MAX]; |
| 97 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 98 | u32 port_stat; /* USB_PORT_STAT_xxx */ |
| 99 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 100 | struct completion setup_ack_done; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | |
| 104 | static const char usbhsh_hcd_name[] = "renesas_usbhs host"; |
| 105 | |
| 106 | /* |
| 107 | * macro |
| 108 | */ |
| 109 | #define usbhsh_priv_to_hpriv(priv) \ |
| 110 | container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod) |
| 111 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 112 | #define __usbhsh_for_each_udev(start, pos, h, i) \ |
| 113 | for (i = start, pos = (h)->udev + i; \ |
| 114 | i < USBHSH_DEVICE_MAX; \ |
| 115 | i++, pos = (h)->udev + i) |
| 116 | |
| 117 | #define usbhsh_for_each_udev(pos, hpriv, i) \ |
| 118 | __usbhsh_for_each_udev(1, pos, hpriv, i) |
| 119 | |
| 120 | #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \ |
| 121 | __usbhsh_for_each_udev(0, pos, hpriv, i) |
| 122 | |
| 123 | #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv) |
| 124 | #define usbhsh_hcd_to_dev(h) ((h)->self.controller) |
| 125 | |
| 126 | #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv) |
| 127 | #define usbhsh_hpriv_to_dcp(h) ((h)->dcp) |
| 128 | #define usbhsh_hpriv_to_hcd(h) \ |
| 129 | container_of((void *)h, struct usb_hcd, hcd_priv) |
| 130 | |
| 131 | #define usbhsh_ep_to_uep(u) ((u)->hcpriv) |
| 132 | #define usbhsh_uep_to_pipe(u) ((u)->pipe) |
| 133 | #define usbhsh_uep_to_udev(u) ((u)->udev) |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 134 | #define usbhsh_uep_to_ep(u) ((u)->ep) |
| 135 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 136 | #define usbhsh_urb_to_ureq(u) ((u)->hcpriv) |
| 137 | #define usbhsh_urb_to_usbv(u) ((u)->dev) |
| 138 | |
| 139 | #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev) |
| 140 | |
| 141 | #define usbhsh_udev_to_usbv(h) ((h)->usbv) |
Kuninori Morimoto | ab14230 | 2011-10-31 00:48:00 -0700 | [diff] [blame] | 142 | #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 143 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 144 | #define usbhsh_pipe_to_uep(p) ((p)->mod_private) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 145 | |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 146 | #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent)) |
| 147 | #define usbhsh_device_hubport(d) ((d)->usbv->portnum) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 148 | #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev)) |
| 149 | #define usbhsh_device_nth(h, d) ((h)->udev + d) |
| 150 | #define usbhsh_device0(h) usbhsh_device_nth(h, 0) |
| 151 | |
| 152 | #define usbhsh_port_stat_init(h) ((h)->port_stat = 0) |
| 153 | #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s)) |
| 154 | #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s)) |
| 155 | #define usbhsh_port_stat_get(h) ((h)->port_stat) |
| 156 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 157 | #define usbhsh_pkt_to_ureq(p) \ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 158 | container_of((void *)p, struct usbhsh_request, pkt) |
| 159 | |
| 160 | /* |
| 161 | * req alloc/free |
| 162 | */ |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 163 | static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 164 | struct urb *urb, |
| 165 | gfp_t mem_flags) |
| 166 | { |
| 167 | struct usbhsh_request *ureq; |
| 168 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 169 | struct device *dev = usbhs_priv_to_dev(priv); |
| 170 | |
Kuninori Morimoto | c5b963f | 2011-10-31 00:47:44 -0700 | [diff] [blame] | 171 | ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 172 | if (!ureq) { |
| 173 | dev_err(dev, "ureq alloc fail\n"); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | usbhs_pkt_init(&ureq->pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 178 | ureq->urb = urb; |
Kuninori Morimoto | fc9d5c7 | 2011-10-31 00:47:01 -0700 | [diff] [blame] | 179 | usbhsh_urb_to_ureq(urb) = ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 180 | |
| 181 | return ureq; |
| 182 | } |
| 183 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 184 | static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 185 | struct usbhsh_request *ureq) |
| 186 | { |
Kuninori Morimoto | fc9d5c7 | 2011-10-31 00:47:01 -0700 | [diff] [blame] | 187 | usbhsh_urb_to_ureq(ureq->urb) = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 188 | ureq->urb = NULL; |
Kuninori Morimoto | c5b963f | 2011-10-31 00:47:44 -0700 | [diff] [blame] | 189 | |
| 190 | kfree(ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 194 | * pipe control |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 195 | */ |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 196 | static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv, |
| 197 | struct urb *urb, |
| 198 | struct usbhs_pkt *pkt) |
| 199 | { |
| 200 | int len = urb->actual_length; |
| 201 | int maxp = usb_endpoint_maxp(&urb->ep->desc); |
| 202 | int t = 0; |
| 203 | |
| 204 | /* DCP is out of sequence control */ |
| 205 | if (usb_pipecontrol(urb->pipe)) |
| 206 | return; |
| 207 | |
| 208 | /* |
| 209 | * renesas_usbhs pipe has a limitation in a number. |
| 210 | * So, driver should re-use the limited pipe for each device/endpoint. |
| 211 | * DATA0/1 sequence should be saved for it. |
| 212 | * see [image of mod_host] |
| 213 | * [HARDWARE LIMITATION] |
| 214 | */ |
| 215 | |
| 216 | /* |
| 217 | * next sequence depends on actual_length |
| 218 | * |
| 219 | * ex) actual_length = 1147, maxp = 512 |
| 220 | * data0 : 512 |
| 221 | * data1 : 512 |
| 222 | * data0 : 123 |
| 223 | * data1 is the next sequence |
| 224 | */ |
| 225 | t = len / maxp; |
| 226 | if (len % maxp) |
| 227 | t++; |
| 228 | if (pkt->zero) |
| 229 | t++; |
| 230 | t %= 2; |
| 231 | |
| 232 | if (t) |
| 233 | usb_dotoggle(urb->dev, |
| 234 | usb_pipeendpoint(urb->pipe), |
| 235 | usb_pipeout(urb->pipe)); |
| 236 | } |
| 237 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 238 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
| 239 | struct urb *urb); |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 240 | |
| 241 | static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv, |
| 242 | struct urb *urb) |
| 243 | { |
| 244 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 245 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 246 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
| 247 | struct usbhs_pipe *pipe; |
| 248 | struct usb_endpoint_descriptor *desc = &urb->ep->desc; |
| 249 | struct device *dev = usbhs_priv_to_dev(priv); |
| 250 | unsigned long flags; |
| 251 | int dir_in_req = !!usb_pipein(urb->pipe); |
| 252 | int is_dcp = usb_endpoint_xfer_control(desc); |
| 253 | int i, dir_in; |
| 254 | int ret = -EBUSY; |
| 255 | |
| 256 | /******************** spin lock ********************/ |
| 257 | usbhs_lock(priv, flags); |
| 258 | |
| 259 | if (unlikely(usbhsh_uep_to_pipe(uep))) { |
| 260 | dev_err(dev, "uep already has pipe\n"); |
| 261 | goto usbhsh_pipe_attach_done; |
| 262 | } |
| 263 | |
| 264 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 265 | |
| 266 | /* check pipe type */ |
| 267 | if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc))) |
| 268 | continue; |
| 269 | |
| 270 | /* check pipe direction if normal pipe */ |
| 271 | if (!is_dcp) { |
| 272 | dir_in = !!usbhs_pipe_is_dir_in(pipe); |
| 273 | if (0 != (dir_in - dir_in_req)) |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | /* check pipe is free */ |
| 278 | if (usbhsh_pipe_to_uep(pipe)) |
| 279 | continue; |
| 280 | |
| 281 | /* |
| 282 | * attach pipe to uep |
| 283 | * |
| 284 | * usbhs_pipe_config_update() should be called after |
| 285 | * usbhs_set_device_config() |
| 286 | * see |
| 287 | * DCPMAXP/PIPEMAXP |
| 288 | */ |
| 289 | usbhsh_uep_to_pipe(uep) = pipe; |
| 290 | usbhsh_pipe_to_uep(pipe) = uep; |
| 291 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 292 | usbhs_pipe_config_update(pipe, |
| 293 | usbhsh_device_number(hpriv, udev), |
| 294 | usb_endpoint_num(desc), |
| 295 | usb_endpoint_maxp(desc)); |
| 296 | |
| 297 | dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__, |
| 298 | usbhsh_device_number(hpriv, udev), |
| 299 | usb_endpoint_num(desc), |
| 300 | usbhs_pipe_name(pipe), |
| 301 | dir_in_req ? "in" : "out"); |
| 302 | |
| 303 | ret = 0; |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | usbhsh_pipe_attach_done: |
| 308 | usbhs_unlock(priv, flags); |
| 309 | /******************** spin unlock ******************/ |
| 310 | |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv, |
| 315 | struct usbhsh_ep *uep) |
| 316 | { |
| 317 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 318 | struct usbhs_pipe *pipe; |
| 319 | struct device *dev = usbhs_priv_to_dev(priv); |
| 320 | unsigned long flags; |
| 321 | |
| 322 | /******************** spin lock ********************/ |
| 323 | usbhs_lock(priv, flags); |
| 324 | |
| 325 | pipe = usbhsh_uep_to_pipe(uep); |
| 326 | |
| 327 | if (unlikely(!pipe)) { |
| 328 | dev_err(dev, "uep doens't have pipe\n"); |
| 329 | } else { |
| 330 | struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep); |
| 331 | struct usbhsh_device *udev = usbhsh_uep_to_udev(uep); |
| 332 | |
| 333 | /* detach pipe from uep */ |
| 334 | usbhsh_uep_to_pipe(uep) = NULL; |
| 335 | usbhsh_pipe_to_uep(pipe) = NULL; |
| 336 | |
| 337 | dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__, |
| 338 | usbhsh_device_number(hpriv, udev), |
| 339 | usb_endpoint_num(&ep->desc), |
| 340 | usbhs_pipe_name(pipe)); |
| 341 | } |
| 342 | |
| 343 | usbhs_unlock(priv, flags); |
| 344 | /******************** spin unlock ******************/ |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * endpoint control |
| 349 | */ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 350 | static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv, |
| 351 | struct urb *urb, |
| 352 | gfp_t mem_flags) |
| 353 | { |
| 354 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 355 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
| 356 | struct usb_host_endpoint *ep = urb->ep; |
| 357 | struct usbhsh_ep *uep; |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 358 | struct device *dev = usbhs_priv_to_dev(priv); |
| 359 | struct usb_endpoint_descriptor *desc = &ep->desc; |
| 360 | unsigned long flags; |
| 361 | |
| 362 | uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags); |
| 363 | if (!uep) { |
| 364 | dev_err(dev, "usbhsh_ep alloc fail\n"); |
| 365 | return -ENOMEM; |
| 366 | } |
| 367 | |
| 368 | /******************** spin lock ********************/ |
| 369 | usbhs_lock(priv, flags); |
| 370 | |
| 371 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 372 | * init endpoint |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 373 | */ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 374 | INIT_LIST_HEAD(&uep->ep_list); |
| 375 | list_add_tail(&uep->ep_list, &udev->ep_list_head); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 376 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 377 | usbhsh_uep_to_udev(uep) = udev; |
| 378 | usbhsh_uep_to_ep(uep) = ep; |
| 379 | usbhsh_ep_to_uep(ep) = uep; |
| 380 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 381 | usbhs_unlock(priv, flags); |
| 382 | /******************** spin unlock ******************/ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 383 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 384 | dev_dbg(dev, "%s [%d-%d]\n", __func__, |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 385 | usbhsh_device_number(hpriv, udev), |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 386 | usb_endpoint_num(desc)); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv, |
| 392 | struct usb_host_endpoint *ep) |
| 393 | { |
| 394 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 395 | struct device *dev = usbhs_priv_to_dev(priv); |
| 396 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 397 | unsigned long flags; |
| 398 | |
| 399 | if (!uep) |
| 400 | return; |
| 401 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 402 | dev_dbg(dev, "%s [%d-%d]\n", __func__, |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 403 | usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)), |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 404 | usb_endpoint_num(&ep->desc)); |
| 405 | |
| 406 | if (usbhsh_uep_to_pipe(uep)) |
| 407 | usbhsh_pipe_detach(hpriv, uep); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 408 | |
| 409 | /******************** spin lock ********************/ |
| 410 | usbhs_lock(priv, flags); |
| 411 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 412 | /* remove this endpoint from udev */ |
| 413 | list_del_init(&uep->ep_list); |
| 414 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 415 | usbhsh_uep_to_udev(uep) = NULL; |
| 416 | usbhsh_uep_to_ep(uep) = NULL; |
| 417 | usbhsh_ep_to_uep(ep) = NULL; |
| 418 | |
| 419 | usbhs_unlock(priv, flags); |
| 420 | /******************** spin unlock ******************/ |
| 421 | |
| 422 | kfree(uep); |
| 423 | } |
| 424 | |
| 425 | static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv, |
| 426 | struct usbhsh_device *udev) |
| 427 | { |
| 428 | struct usbhsh_ep *uep, *next; |
| 429 | |
| 430 | list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list) |
| 431 | usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep)); |
| 432 | } |
| 433 | |
| 434 | /* |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 435 | * device control |
| 436 | */ |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 437 | static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd, |
| 438 | struct usbhsh_device *udev) |
| 439 | { |
| 440 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); |
| 441 | |
| 442 | return hcd->self.root_hub == usbv->parent; |
| 443 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 444 | |
| 445 | static int usbhsh_device_has_endpoint(struct usbhsh_device *udev) |
| 446 | { |
| 447 | return !list_empty(&udev->ep_list_head); |
| 448 | } |
| 449 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 450 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
| 451 | struct urb *urb) |
| 452 | { |
| 453 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 454 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); |
| 455 | |
| 456 | /* usbhsh_device_attach() is still not called */ |
| 457 | if (!udev) |
| 458 | return NULL; |
| 459 | |
| 460 | /* if it is device0, return it */ |
| 461 | if (0 == usb_pipedevice(urb->pipe)) |
| 462 | return usbhsh_device0(hpriv); |
| 463 | |
| 464 | /* return attached device */ |
| 465 | return udev; |
| 466 | } |
| 467 | |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 468 | static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 469 | struct urb *urb) |
| 470 | { |
| 471 | struct usbhsh_device *udev = NULL; |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 472 | struct usbhsh_device *udev0 = usbhsh_device0(hpriv); |
| 473 | struct usbhsh_device *pos; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 474 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 475 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
| 476 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 477 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 478 | unsigned long flags; |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 479 | u16 upphub, hubport; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 480 | int i; |
| 481 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 482 | /* |
| 483 | * This function should be called only while urb is pointing to device0. |
| 484 | * It will attach unused usbhsh_device to urb (usbv), |
| 485 | * and initialize device0. |
| 486 | * You can use usbhsh_device_get() to get "current" udev, |
| 487 | * and usbhsh_usbv_to_udev() is for "attached" udev. |
| 488 | */ |
| 489 | if (0 != usb_pipedevice(urb->pipe)) { |
| 490 | dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 494 | /******************** spin lock ********************/ |
| 495 | usbhs_lock(priv, flags); |
| 496 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 497 | /* |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 498 | * find unused device |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 499 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 500 | usbhsh_for_each_udev(pos, hpriv, i) { |
| 501 | if (usbhsh_udev_is_used(pos)) |
| 502 | continue; |
| 503 | udev = pos; |
| 504 | break; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 507 | if (udev) { |
| 508 | /* |
| 509 | * usbhsh_usbv_to_udev() |
| 510 | * usbhsh_udev_to_usbv() |
| 511 | * will be enable |
| 512 | */ |
| 513 | dev_set_drvdata(&usbv->dev, udev); |
| 514 | udev->usbv = usbv; |
| 515 | } |
| 516 | |
| 517 | usbhs_unlock(priv, flags); |
| 518 | /******************** spin unlock ******************/ |
| 519 | |
Kuninori Morimoto | ab14230 | 2011-10-31 00:48:00 -0700 | [diff] [blame] | 520 | if (!udev) { |
| 521 | dev_err(dev, "no free usbhsh_device\n"); |
| 522 | return NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 525 | if (usbhsh_device_has_endpoint(udev)) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 526 | dev_warn(dev, "udev have old endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 527 | usbhsh_endpoint_detach_all(hpriv, udev); |
| 528 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 529 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 530 | if (usbhsh_device_has_endpoint(udev0)) { |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 531 | dev_warn(dev, "udev0 have old endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 532 | usbhsh_endpoint_detach_all(hpriv, udev0); |
| 533 | } |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 534 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 535 | /* uep will be attached */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 536 | INIT_LIST_HEAD(&udev0->ep_list_head); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 537 | INIT_LIST_HEAD(&udev->ep_list_head); |
| 538 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 539 | /* |
| 540 | * set device0 config |
| 541 | */ |
| 542 | usbhs_set_device_config(priv, |
| 543 | 0, 0, 0, usbv->speed); |
| 544 | |
| 545 | /* |
| 546 | * set new device config |
| 547 | */ |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 548 | upphub = 0; |
| 549 | hubport = 0; |
| 550 | if (!usbhsh_connected_to_rhdev(hcd, udev)) { |
| 551 | /* if udev is not connected to rhdev, it means parent is Hub */ |
| 552 | struct usbhsh_device *parent = usbhsh_device_parent(udev); |
| 553 | |
| 554 | upphub = usbhsh_device_number(hpriv, parent); |
| 555 | hubport = usbhsh_device_hubport(udev); |
| 556 | |
| 557 | dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__, |
| 558 | upphub, hubport, parent); |
| 559 | } |
| 560 | |
Kuninori Morimoto | 3dd4926 | 2011-10-31 00:47:13 -0700 | [diff] [blame] | 561 | usbhs_set_device_config(priv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 562 | usbhsh_device_number(hpriv, udev), |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 563 | upphub, hubport, usbv->speed); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 564 | |
| 565 | dev_dbg(dev, "%s [%d](%p)\n", __func__, |
| 566 | usbhsh_device_number(hpriv, udev), udev); |
| 567 | |
| 568 | return udev; |
| 569 | } |
| 570 | |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 571 | static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 572 | struct usbhsh_device *udev) |
| 573 | { |
| 574 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 575 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 576 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
| 577 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 578 | unsigned long flags; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 579 | |
| 580 | dev_dbg(dev, "%s [%d](%p)\n", __func__, |
| 581 | usbhsh_device_number(hpriv, udev), udev); |
| 582 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 583 | if (usbhsh_device_has_endpoint(udev)) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 584 | dev_warn(dev, "udev still have endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 585 | usbhsh_endpoint_detach_all(hpriv, udev); |
| 586 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 587 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 588 | /* |
| 589 | * There is nothing to do if it is device0. |
| 590 | * see |
| 591 | * usbhsh_device_attach() |
| 592 | * usbhsh_device_get() |
| 593 | */ |
| 594 | if (0 == usbhsh_device_number(hpriv, udev)) |
| 595 | return; |
| 596 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 597 | /******************** spin lock ********************/ |
| 598 | usbhs_lock(priv, flags); |
| 599 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 600 | /* |
| 601 | * usbhsh_usbv_to_udev() |
| 602 | * usbhsh_udev_to_usbv() |
| 603 | * will be disable |
| 604 | */ |
| 605 | dev_set_drvdata(&usbv->dev, NULL); |
| 606 | udev->usbv = NULL; |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 607 | |
| 608 | usbhs_unlock(priv, flags); |
| 609 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 613 | * queue push/pop |
| 614 | */ |
| 615 | static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) |
| 616 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 617 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 618 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 619 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 620 | struct urb *urb = ureq->urb; |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 621 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 622 | struct device *dev = usbhs_priv_to_dev(priv); |
| 623 | |
| 624 | dev_dbg(dev, "%s\n", __func__); |
| 625 | |
| 626 | if (!urb) { |
| 627 | dev_warn(dev, "pkt doesn't have urb\n"); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | urb->actual_length = pkt->actual; |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 632 | usbhsh_ureq_free(hpriv, ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 633 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 634 | usbhsh_endpoint_sequence_save(hpriv, urb, pkt); |
| 635 | usbhsh_pipe_detach(hpriv, uep); |
| 636 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 637 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
| 638 | usb_hcd_giveback_urb(hcd, urb, 0); |
| 639 | } |
| 640 | |
| 641 | static int usbhsh_queue_push(struct usb_hcd *hcd, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 642 | struct urb *urb, |
| 643 | gfp_t mem_flags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 644 | { |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 645 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 646 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 647 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 648 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 649 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 650 | void *buf; |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 651 | int len, sequence; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 652 | |
| 653 | if (usb_pipeisoc(urb->pipe)) { |
| 654 | dev_err(dev, "pipe iso is not supported now\n"); |
| 655 | return -EIO; |
| 656 | } |
| 657 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 658 | /* this ureq will be freed on usbhsh_queue_done() */ |
| 659 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 660 | if (unlikely(!ureq)) { |
| 661 | dev_err(dev, "ureq alloc fail\n"); |
| 662 | return -ENOMEM; |
| 663 | } |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 664 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 665 | if (usb_pipein(urb->pipe)) |
| 666 | pipe->handler = &usbhs_fifo_pio_pop_handler; |
| 667 | else |
| 668 | pipe->handler = &usbhs_fifo_pio_push_handler; |
| 669 | |
| 670 | buf = (void *)(urb->transfer_buffer + urb->actual_length); |
| 671 | len = urb->transfer_buffer_length - urb->actual_length; |
| 672 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 673 | sequence = usb_gettoggle(urb->dev, |
| 674 | usb_pipeendpoint(urb->pipe), |
| 675 | usb_pipeout(urb->pipe)); |
| 676 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 677 | dev_dbg(dev, "%s\n", __func__); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 678 | usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 679 | buf, len, (urb->transfer_flags & URB_ZERO_PACKET), |
| 680 | sequence); |
| 681 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 682 | usbhs_pkt_start(pipe); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * DCP setup stage |
| 689 | */ |
| 690 | static int usbhsh_is_request_address(struct urb *urb) |
| 691 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 692 | struct usb_ctrlrequest *req; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 693 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 694 | req = (struct usb_ctrlrequest *)urb->setup_packet; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 695 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 696 | if ((DeviceOutRequest == req->bRequestType << 8) && |
| 697 | (USB_REQ_SET_ADDRESS == req->bRequest)) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 698 | return 1; |
| 699 | else |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv, |
| 704 | struct urb *urb, |
| 705 | struct usbhs_pipe *pipe) |
| 706 | { |
| 707 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 708 | struct usb_ctrlrequest req; |
| 709 | struct device *dev = usbhs_priv_to_dev(priv); |
| 710 | |
| 711 | /* |
| 712 | * wait setup packet ACK |
| 713 | * see |
| 714 | * usbhsh_irq_setup_ack() |
| 715 | * usbhsh_irq_setup_err() |
| 716 | */ |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 717 | init_completion(&hpriv->setup_ack_done); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 718 | |
| 719 | /* copy original request */ |
| 720 | memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest)); |
| 721 | |
| 722 | /* |
| 723 | * renesas_usbhs can not use original usb address. |
| 724 | * see HARDWARE LIMITATION. |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 725 | * modify usb address here to use attached device. |
| 726 | * see usbhsh_device_attach() |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 727 | */ |
| 728 | if (usbhsh_is_request_address(urb)) { |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 729 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 730 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); |
| 731 | |
| 732 | /* udev is a attached device */ |
| 733 | req.wValue = usbhsh_device_number(hpriv, udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 734 | dev_dbg(dev, "create new address - %d\n", req.wValue); |
| 735 | } |
| 736 | |
| 737 | /* set request */ |
| 738 | usbhs_usbreq_set_val(priv, &req); |
| 739 | |
| 740 | /* |
| 741 | * wait setup packet ACK |
| 742 | */ |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 743 | wait_for_completion(&hpriv->setup_ack_done); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 744 | |
| 745 | dev_dbg(dev, "%s done\n", __func__); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * DCP data stage |
| 750 | */ |
| 751 | static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv, |
| 752 | struct usbhs_pkt *pkt) |
| 753 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 754 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 755 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 756 | |
| 757 | /* this ureq was connected to urb when usbhsh_urb_enqueue() */ |
| 758 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 759 | usbhsh_ureq_free(hpriv, ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 762 | static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv, |
| 763 | struct urb *urb, |
| 764 | struct usbhs_pipe *pipe, |
| 765 | gfp_t mem_flags) |
| 766 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 767 | { |
| 768 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 769 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 770 | /* this ureq will be freed on usbhsh_data_stage_packet_done() */ |
| 771 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 772 | if (unlikely(!ureq)) |
| 773 | return -ENOMEM; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 774 | |
| 775 | if (usb_pipein(urb->pipe)) |
| 776 | pipe->handler = &usbhs_dcp_data_stage_in_handler; |
| 777 | else |
| 778 | pipe->handler = &usbhs_dcp_data_stage_out_handler; |
| 779 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 780 | usbhs_pkt_push(pipe, &ureq->pkt, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 781 | usbhsh_data_stage_packet_done, |
| 782 | urb->transfer_buffer, |
| 783 | urb->transfer_buffer_length, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 784 | (urb->transfer_flags & URB_ZERO_PACKET), |
| 785 | -1); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 786 | |
| 787 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
| 791 | * DCP status stage |
| 792 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 793 | static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 794 | struct urb *urb, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 795 | struct usbhs_pipe *pipe, |
| 796 | gfp_t mem_flags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 797 | { |
| 798 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 799 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 800 | /* This ureq will be freed on usbhsh_queue_done() */ |
| 801 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 802 | if (unlikely(!ureq)) |
| 803 | return -ENOMEM; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 804 | |
| 805 | if (usb_pipein(urb->pipe)) |
| 806 | pipe->handler = &usbhs_dcp_status_stage_in_handler; |
| 807 | else |
| 808 | pipe->handler = &usbhs_dcp_status_stage_out_handler; |
| 809 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 810 | usbhs_pkt_push(pipe, &ureq->pkt, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 811 | usbhsh_queue_done, |
| 812 | NULL, |
| 813 | urb->transfer_buffer_length, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame^] | 814 | 0, -1); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 815 | |
| 816 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | static int usbhsh_dcp_queue_push(struct usb_hcd *hcd, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 820 | struct urb *urb, |
| 821 | gfp_t mflags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 822 | { |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 823 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 824 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 825 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 826 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 827 | int ret; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 828 | |
| 829 | dev_dbg(dev, "%s\n", __func__); |
| 830 | |
| 831 | /* |
| 832 | * setup stage |
| 833 | * |
| 834 | * usbhsh_send_setup_stage_packet() wait SACK/SIGN |
| 835 | */ |
| 836 | usbhsh_setup_stage_packet_push(hpriv, urb, pipe); |
| 837 | |
| 838 | /* |
| 839 | * data stage |
| 840 | * |
| 841 | * It is pushed only when urb has buffer. |
| 842 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 843 | if (urb->transfer_buffer_length) { |
| 844 | ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags); |
| 845 | if (ret < 0) { |
| 846 | dev_err(dev, "data stage failed\n"); |
| 847 | return ret; |
| 848 | } |
| 849 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 850 | |
| 851 | /* |
| 852 | * status stage |
| 853 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 854 | ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags); |
| 855 | if (ret < 0) { |
| 856 | dev_err(dev, "status stage failed\n"); |
| 857 | return ret; |
| 858 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * start pushed packets |
| 862 | */ |
| 863 | usbhs_pkt_start(pipe); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * dma map functions |
| 870 | */ |
| 871 | static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map) |
| 872 | { |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * for hc_driver |
| 878 | */ |
| 879 | static int usbhsh_host_start(struct usb_hcd *hcd) |
| 880 | { |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static void usbhsh_host_stop(struct usb_hcd *hcd) |
| 885 | { |
| 886 | } |
| 887 | |
| 888 | static int usbhsh_urb_enqueue(struct usb_hcd *hcd, |
| 889 | struct urb *urb, |
| 890 | gfp_t mem_flags) |
| 891 | { |
| 892 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 893 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 894 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 895 | struct usb_host_endpoint *ep = urb->ep; |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 896 | struct usbhsh_device *new_udev = NULL; |
Kuninori Morimoto | 73ef635 | 2011-10-24 02:24:49 -0700 | [diff] [blame] | 897 | int is_dir_in = usb_pipein(urb->pipe); |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 898 | int i; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 899 | int ret; |
| 900 | |
Kuninori Morimoto | 73ef635 | 2011-10-24 02:24:49 -0700 | [diff] [blame] | 901 | dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 902 | |
| 903 | ret = usb_hcd_link_urb_to_ep(hcd, urb); |
| 904 | if (ret) |
| 905 | goto usbhsh_urb_enqueue_error_not_linked; |
| 906 | |
| 907 | /* |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 908 | * attach udev if needed |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 909 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 910 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 911 | if (!usbhsh_device_get(hpriv, urb)) { |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 912 | new_udev = usbhsh_device_attach(hpriv, urb); |
Kuninori Morimoto | 37332ee | 2011-12-08 18:25:37 -0800 | [diff] [blame] | 913 | if (!new_udev) { |
| 914 | ret = -EIO; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 915 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | 37332ee | 2011-12-08 18:25:37 -0800 | [diff] [blame] | 916 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 920 | * attach endpoint if needed |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 921 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 922 | */ |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 923 | if (!usbhsh_ep_to_uep(ep)) { |
| 924 | ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags); |
| 925 | if (ret < 0) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 926 | goto usbhsh_urb_enqueue_error_free_device; |
| 927 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 928 | |
| 929 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 930 | * attach pipe to endpoint |
| 931 | * see [image of mod_host] |
| 932 | */ |
| 933 | for (i = 0; i < 1024; i++) { |
| 934 | ret = usbhsh_pipe_attach(hpriv, urb); |
| 935 | if (ret < 0) |
| 936 | msleep(100); |
| 937 | else |
| 938 | break; |
| 939 | } |
| 940 | if (ret < 0) |
| 941 | goto usbhsh_urb_enqueue_error_free_endpoint; |
| 942 | |
| 943 | /* |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 944 | * push packet |
| 945 | */ |
| 946 | if (usb_pipecontrol(urb->pipe)) |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 947 | ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 948 | else |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 949 | ret = usbhsh_queue_push(hcd, urb, mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 950 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 951 | return ret; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 952 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 953 | usbhsh_urb_enqueue_error_free_endpoint: |
| 954 | usbhsh_endpoint_detach(hpriv, ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 955 | usbhsh_urb_enqueue_error_free_device: |
| 956 | if (new_udev) |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 957 | usbhsh_device_detach(hpriv, new_udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 958 | usbhsh_urb_enqueue_error_not_linked: |
| 959 | |
| 960 | dev_dbg(dev, "%s error\n", __func__); |
| 961 | |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 966 | { |
| 967 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 968 | struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); |
| 969 | |
Kuninori Morimoto | 5479654 | 2011-12-08 18:26:07 -0800 | [diff] [blame] | 970 | if (ureq) { |
| 971 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 972 | struct usbhs_pkt *pkt = &ureq->pkt; |
| 973 | |
| 974 | usbhs_pkt_pop(pkt->pipe, pkt); |
| 975 | usbhsh_queue_done(priv, pkt); |
| 976 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | static void usbhsh_endpoint_disable(struct usb_hcd *hcd, |
| 982 | struct usb_host_endpoint *ep) |
| 983 | { |
| 984 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); |
| 985 | struct usbhsh_device *udev; |
| 986 | struct usbhsh_hpriv *hpriv; |
| 987 | |
| 988 | /* |
| 989 | * this function might be called manytimes by same hcd/ep |
Kuninori Morimoto | fca8ab7 | 2011-10-31 00:47:24 -0700 | [diff] [blame] | 990 | * in-endpoint == out-endpoint if ep == dcp. |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 991 | */ |
| 992 | if (!uep) |
| 993 | return; |
| 994 | |
| 995 | udev = usbhsh_uep_to_udev(uep); |
| 996 | hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 997 | |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 998 | usbhsh_endpoint_detach(hpriv, ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 999 | |
| 1000 | /* |
| 1001 | * if there is no endpoint, |
| 1002 | * free device |
| 1003 | */ |
| 1004 | if (!usbhsh_device_has_endpoint(udev)) |
Kuninori Morimoto | 7aac8d15 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 1005 | usbhsh_device_detach(hpriv, udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf) |
| 1009 | { |
| 1010 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1011 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1012 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1013 | int roothub_id = 1; /* only 1 root hub */ |
| 1014 | |
| 1015 | /* |
| 1016 | * does port stat was changed ? |
| 1017 | * check USB_PORT_STAT_C_xxx << 16 |
| 1018 | */ |
| 1019 | if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000) |
| 1020 | *buf = (1 << roothub_id); |
| 1021 | else |
| 1022 | *buf = 0; |
| 1023 | |
| 1024 | dev_dbg(dev, "%s (%02x)\n", __func__, *buf); |
| 1025 | |
| 1026 | return !!(*buf); |
| 1027 | } |
| 1028 | |
| 1029 | static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv, |
| 1030 | u16 typeReq, u16 wValue, |
| 1031 | u16 wIndex, char *buf, u16 wLength) |
| 1032 | { |
| 1033 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1034 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1035 | |
| 1036 | switch (wValue) { |
| 1037 | case C_HUB_OVER_CURRENT: |
| 1038 | case C_HUB_LOCAL_POWER: |
| 1039 | dev_dbg(dev, "%s :: C_HUB_xx\n", __func__); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | return -EPIPE; |
| 1044 | } |
| 1045 | |
| 1046 | static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv, |
| 1047 | u16 typeReq, u16 wValue, |
| 1048 | u16 wIndex, char *buf, u16 wLength) |
| 1049 | { |
| 1050 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1051 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1052 | int enable = (typeReq == SetPortFeature); |
| 1053 | int speed, i, timeout = 128; |
| 1054 | int roothub_id = 1; /* only 1 root hub */ |
| 1055 | |
| 1056 | /* common error */ |
| 1057 | if (wIndex > roothub_id || wLength != 0) |
| 1058 | return -EPIPE; |
| 1059 | |
| 1060 | /* check wValue */ |
| 1061 | switch (wValue) { |
| 1062 | case USB_PORT_FEAT_POWER: |
| 1063 | usbhs_vbus_ctrl(priv, enable); |
| 1064 | dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__); |
| 1065 | break; |
| 1066 | |
| 1067 | case USB_PORT_FEAT_ENABLE: |
| 1068 | case USB_PORT_FEAT_SUSPEND: |
| 1069 | case USB_PORT_FEAT_C_ENABLE: |
| 1070 | case USB_PORT_FEAT_C_SUSPEND: |
| 1071 | case USB_PORT_FEAT_C_CONNECTION: |
| 1072 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 1073 | case USB_PORT_FEAT_C_RESET: |
| 1074 | dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__); |
| 1075 | break; |
| 1076 | |
| 1077 | case USB_PORT_FEAT_RESET: |
| 1078 | if (!enable) |
| 1079 | break; |
| 1080 | |
| 1081 | usbhsh_port_stat_clear(hpriv, |
| 1082 | USB_PORT_STAT_HIGH_SPEED | |
| 1083 | USB_PORT_STAT_LOW_SPEED); |
| 1084 | |
| 1085 | usbhs_bus_send_reset(priv); |
| 1086 | msleep(20); |
| 1087 | usbhs_bus_send_sof_enable(priv); |
| 1088 | |
| 1089 | for (i = 0; i < timeout ; i++) { |
| 1090 | switch (usbhs_bus_get_speed(priv)) { |
| 1091 | case USB_SPEED_LOW: |
| 1092 | speed = USB_PORT_STAT_LOW_SPEED; |
| 1093 | goto got_usb_bus_speed; |
| 1094 | case USB_SPEED_HIGH: |
| 1095 | speed = USB_PORT_STAT_HIGH_SPEED; |
| 1096 | goto got_usb_bus_speed; |
| 1097 | case USB_SPEED_FULL: |
| 1098 | speed = 0; |
| 1099 | goto got_usb_bus_speed; |
| 1100 | } |
| 1101 | |
| 1102 | msleep(20); |
| 1103 | } |
| 1104 | return -EPIPE; |
| 1105 | |
| 1106 | got_usb_bus_speed: |
| 1107 | usbhsh_port_stat_set(hpriv, speed); |
| 1108 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE); |
| 1109 | |
| 1110 | dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n", |
| 1111 | __func__, speed); |
| 1112 | |
| 1113 | /* status change is not needed */ |
| 1114 | return 0; |
| 1115 | |
| 1116 | default: |
| 1117 | return -EPIPE; |
| 1118 | } |
| 1119 | |
| 1120 | /* set/clear status */ |
| 1121 | if (enable) |
| 1122 | usbhsh_port_stat_set(hpriv, (1 << wValue)); |
| 1123 | else |
| 1124 | usbhsh_port_stat_clear(hpriv, (1 << wValue)); |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv, |
| 1130 | u16 typeReq, u16 wValue, |
| 1131 | u16 wIndex, char *buf, u16 wLength) |
| 1132 | { |
| 1133 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1134 | struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf; |
| 1135 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1136 | int roothub_id = 1; /* only 1 root hub */ |
| 1137 | |
| 1138 | switch (typeReq) { |
| 1139 | case GetHubStatus: |
| 1140 | dev_dbg(dev, "%s :: GetHubStatus\n", __func__); |
| 1141 | |
| 1142 | *buf = 0x00; |
| 1143 | break; |
| 1144 | |
| 1145 | case GetPortStatus: |
| 1146 | if (wIndex != roothub_id) |
| 1147 | return -EPIPE; |
| 1148 | |
| 1149 | dev_dbg(dev, "%s :: GetPortStatus\n", __func__); |
| 1150 | *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv)); |
| 1151 | break; |
| 1152 | |
| 1153 | case GetHubDescriptor: |
| 1154 | desc->bDescriptorType = 0x29; |
| 1155 | desc->bHubContrCurrent = 0; |
| 1156 | desc->bNbrPorts = roothub_id; |
| 1157 | desc->bDescLength = 9; |
| 1158 | desc->bPwrOn2PwrGood = 0; |
| 1159 | desc->wHubCharacteristics = cpu_to_le16(0x0011); |
| 1160 | desc->u.hs.DeviceRemovable[0] = (roothub_id << 1); |
| 1161 | desc->u.hs.DeviceRemovable[1] = ~0; |
| 1162 | dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__); |
| 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, |
| 1170 | u16 wIndex, char *buf, u16 wLength) |
| 1171 | { |
| 1172 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1173 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1174 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1175 | int ret = -EPIPE; |
| 1176 | |
| 1177 | switch (typeReq) { |
| 1178 | |
| 1179 | /* Hub Feature */ |
| 1180 | case ClearHubFeature: |
| 1181 | case SetHubFeature: |
| 1182 | ret = __usbhsh_hub_hub_feature(hpriv, typeReq, |
| 1183 | wValue, wIndex, buf, wLength); |
| 1184 | break; |
| 1185 | |
| 1186 | /* Port Feature */ |
| 1187 | case SetPortFeature: |
| 1188 | case ClearPortFeature: |
| 1189 | ret = __usbhsh_hub_port_feature(hpriv, typeReq, |
| 1190 | wValue, wIndex, buf, wLength); |
| 1191 | break; |
| 1192 | |
| 1193 | /* Get status */ |
| 1194 | case GetHubStatus: |
| 1195 | case GetPortStatus: |
| 1196 | case GetHubDescriptor: |
| 1197 | ret = __usbhsh_hub_get_status(hpriv, typeReq, |
| 1198 | wValue, wIndex, buf, wLength); |
| 1199 | break; |
| 1200 | } |
| 1201 | |
| 1202 | dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n", |
| 1203 | typeReq, ret, usbhsh_port_stat_get(hpriv)); |
| 1204 | |
| 1205 | return ret; |
| 1206 | } |
| 1207 | |
| 1208 | static struct hc_driver usbhsh_driver = { |
| 1209 | .description = usbhsh_hcd_name, |
| 1210 | .hcd_priv_size = sizeof(struct usbhsh_hpriv), |
| 1211 | |
| 1212 | /* |
| 1213 | * generic hardware linkage |
| 1214 | */ |
| 1215 | .flags = HCD_USB2, |
| 1216 | |
| 1217 | .start = usbhsh_host_start, |
| 1218 | .stop = usbhsh_host_stop, |
| 1219 | |
| 1220 | /* |
| 1221 | * managing i/o requests and associated device resources |
| 1222 | */ |
| 1223 | .urb_enqueue = usbhsh_urb_enqueue, |
| 1224 | .urb_dequeue = usbhsh_urb_dequeue, |
| 1225 | .endpoint_disable = usbhsh_endpoint_disable, |
| 1226 | |
| 1227 | /* |
| 1228 | * root hub |
| 1229 | */ |
| 1230 | .hub_status_data = usbhsh_hub_status_data, |
| 1231 | .hub_control = usbhsh_hub_control, |
| 1232 | }; |
| 1233 | |
| 1234 | /* |
| 1235 | * interrupt functions |
| 1236 | */ |
| 1237 | static int usbhsh_irq_attch(struct usbhs_priv *priv, |
| 1238 | struct usbhs_irq_state *irq_state) |
| 1239 | { |
| 1240 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1241 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1242 | |
| 1243 | dev_dbg(dev, "device attached\n"); |
| 1244 | |
| 1245 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION); |
| 1246 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static int usbhsh_irq_dtch(struct usbhs_priv *priv, |
| 1252 | struct usbhs_irq_state *irq_state) |
| 1253 | { |
| 1254 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1255 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1256 | |
| 1257 | dev_dbg(dev, "device detached\n"); |
| 1258 | |
| 1259 | usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION); |
| 1260 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static int usbhsh_irq_setup_ack(struct usbhs_priv *priv, |
| 1266 | struct usbhs_irq_state *irq_state) |
| 1267 | { |
| 1268 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1269 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1270 | |
| 1271 | dev_dbg(dev, "setup packet OK\n"); |
| 1272 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 1273 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1274 | |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | static int usbhsh_irq_setup_err(struct usbhs_priv *priv, |
| 1279 | struct usbhs_irq_state *irq_state) |
| 1280 | { |
| 1281 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1282 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1283 | |
| 1284 | dev_dbg(dev, "setup packet Err\n"); |
| 1285 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 1286 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * module start/stop |
| 1293 | */ |
| 1294 | static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv) |
| 1295 | { |
| 1296 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1297 | struct usbhs_pipe *pipe; |
| 1298 | u32 *pipe_type = usbhs_get_dparam(priv, pipe_type); |
| 1299 | int pipe_size = usbhs_get_dparam(priv, pipe_size); |
| 1300 | int old_type, dir_in, i; |
| 1301 | |
| 1302 | /* init all pipe */ |
| 1303 | old_type = USB_ENDPOINT_XFER_CONTROL; |
| 1304 | for (i = 0; i < pipe_size; i++) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1305 | |
| 1306 | /* |
| 1307 | * data "output" will be finished as soon as possible, |
| 1308 | * but there is no guaranty at data "input" case. |
| 1309 | * |
| 1310 | * "input" needs "standby" pipe. |
| 1311 | * So, "input" direction pipe > "output" direction pipe |
| 1312 | * is good idea. |
| 1313 | * |
| 1314 | * 1st USB_ENDPOINT_XFER_xxx will be output direction, |
| 1315 | * and the other will be input direction here. |
| 1316 | * |
| 1317 | * ex) |
| 1318 | * ... |
| 1319 | * USB_ENDPOINT_XFER_ISOC -> dir out |
| 1320 | * USB_ENDPOINT_XFER_ISOC -> dir in |
| 1321 | * USB_ENDPOINT_XFER_BULK -> dir out |
| 1322 | * USB_ENDPOINT_XFER_BULK -> dir in |
| 1323 | * USB_ENDPOINT_XFER_BULK -> dir in |
| 1324 | * ... |
| 1325 | */ |
| 1326 | dir_in = (pipe_type[i] == old_type); |
| 1327 | old_type = pipe_type[i]; |
| 1328 | |
| 1329 | if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) { |
| 1330 | pipe = usbhs_dcp_malloc(priv); |
| 1331 | usbhsh_hpriv_to_dcp(hpriv) = pipe; |
| 1332 | } else { |
| 1333 | pipe = usbhs_pipe_malloc(priv, |
| 1334 | pipe_type[i], |
| 1335 | dir_in); |
| 1336 | } |
| 1337 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 1338 | pipe->mod_private = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | static int usbhsh_start(struct usbhs_priv *priv) |
| 1343 | { |
| 1344 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1345 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 1346 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 1347 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1348 | int ret; |
| 1349 | |
| 1350 | /* add hcd */ |
| 1351 | ret = usb_add_hcd(hcd, 0, 0); |
| 1352 | if (ret < 0) |
| 1353 | return 0; |
| 1354 | |
| 1355 | /* |
| 1356 | * pipe initialize and enable DCP |
| 1357 | */ |
| 1358 | usbhs_pipe_init(priv, |
| 1359 | usbhsh_dma_map_ctrl); |
| 1360 | usbhs_fifo_init(priv); |
| 1361 | usbhsh_pipe_init_for_host(priv); |
| 1362 | |
| 1363 | /* |
| 1364 | * system config enble |
| 1365 | * - HI speed |
| 1366 | * - host |
| 1367 | * - usb module |
| 1368 | */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1369 | usbhs_sys_host_ctrl(priv, 1); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1370 | |
| 1371 | /* |
| 1372 | * enable irq callback |
| 1373 | */ |
| 1374 | mod->irq_attch = usbhsh_irq_attch; |
| 1375 | mod->irq_dtch = usbhsh_irq_dtch; |
| 1376 | mod->irq_sack = usbhsh_irq_setup_ack; |
| 1377 | mod->irq_sign = usbhsh_irq_setup_err; |
| 1378 | usbhs_irq_callback_update(priv, mod); |
| 1379 | |
| 1380 | dev_dbg(dev, "start host\n"); |
| 1381 | |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
| 1385 | static int usbhsh_stop(struct usbhs_priv *priv) |
| 1386 | { |
| 1387 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1388 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
Kuninori Morimoto | 146ee50 | 2011-10-24 02:25:07 -0700 | [diff] [blame] | 1389 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1390 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1391 | |
Kuninori Morimoto | 146ee50 | 2011-10-24 02:25:07 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * disable irq callback |
| 1394 | */ |
| 1395 | mod->irq_attch = NULL; |
| 1396 | mod->irq_dtch = NULL; |
| 1397 | mod->irq_sack = NULL; |
| 1398 | mod->irq_sign = NULL; |
| 1399 | usbhs_irq_callback_update(priv, mod); |
| 1400 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1401 | usb_remove_hcd(hcd); |
| 1402 | |
| 1403 | /* disable sys */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1404 | usbhs_sys_host_ctrl(priv, 0); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1405 | |
| 1406 | dev_dbg(dev, "quit host\n"); |
| 1407 | |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1411 | int usbhs_mod_host_probe(struct usbhs_priv *priv) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1412 | { |
| 1413 | struct usbhsh_hpriv *hpriv; |
| 1414 | struct usb_hcd *hcd; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1415 | struct usbhsh_device *udev; |
| 1416 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1417 | int i; |
| 1418 | |
| 1419 | /* initialize hcd */ |
| 1420 | hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name); |
| 1421 | if (!hcd) { |
| 1422 | dev_err(dev, "Failed to create hcd\n"); |
| 1423 | return -ENOMEM; |
| 1424 | } |
| 1425 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1426 | /* |
| 1427 | * CAUTION |
| 1428 | * |
| 1429 | * There is no guarantee that it is possible to access usb module here. |
| 1430 | * Don't accesses to it. |
| 1431 | * The accesse will be enable after "usbhsh_start" |
| 1432 | */ |
| 1433 | |
| 1434 | hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1435 | |
| 1436 | /* |
| 1437 | * register itself |
| 1438 | */ |
| 1439 | usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST); |
| 1440 | |
| 1441 | /* init hpriv */ |
| 1442 | hpriv->mod.name = "host"; |
| 1443 | hpriv->mod.start = usbhsh_start; |
| 1444 | hpriv->mod.stop = usbhsh_stop; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1445 | usbhsh_port_stat_init(hpriv); |
| 1446 | |
| 1447 | /* init all device */ |
| 1448 | usbhsh_for_each_udev_with_dev0(udev, hpriv, i) { |
| 1449 | udev->usbv = NULL; |
| 1450 | INIT_LIST_HEAD(&udev->ep_list_head); |
| 1451 | } |
| 1452 | |
| 1453 | dev_info(dev, "host probed\n"); |
| 1454 | |
| 1455 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1458 | int usbhs_mod_host_remove(struct usbhs_priv *priv) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1459 | { |
| 1460 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1461 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 1462 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1463 | usb_put_hcd(hcd); |
| 1464 | |
| 1465 | return 0; |
| 1466 | } |