Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Atmel USBA high speed USB device controller |
| 3 | * |
| 4 | * Copyright (C) 2005-2007 Atmel Corporation |
| 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 version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/usb/ch9.h> |
| 21 | #include <linux/usb/gadget.h> |
Stelian Pop | 8d85531 | 2008-03-05 00:00:00 +0100 | [diff] [blame] | 22 | #include <linux/usb/atmel_usba_udc.h> |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
Jean-Christophe PLAGNIOL-VILLARD | bcd2360 | 2012-10-30 05:12:23 +0800 | [diff] [blame] | 24 | #include <linux/platform_data/atmel.h> |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_gpio.h> |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/gpio.h> |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 29 | |
| 30 | #include "atmel_usba_udc.h" |
| 31 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 32 | #ifdef CONFIG_USB_GADGET_DEBUG_FS |
| 33 | #include <linux/debugfs.h> |
| 34 | #include <linux/uaccess.h> |
| 35 | |
| 36 | static int queue_dbg_open(struct inode *inode, struct file *file) |
| 37 | { |
| 38 | struct usba_ep *ep = inode->i_private; |
| 39 | struct usba_request *req, *req_copy; |
| 40 | struct list_head *queue_data; |
| 41 | |
| 42 | queue_data = kmalloc(sizeof(*queue_data), GFP_KERNEL); |
| 43 | if (!queue_data) |
| 44 | return -ENOMEM; |
| 45 | INIT_LIST_HEAD(queue_data); |
| 46 | |
| 47 | spin_lock_irq(&ep->udc->lock); |
| 48 | list_for_each_entry(req, &ep->queue, queue) { |
Julia Lawall | 4b8e123 | 2010-05-15 23:15:44 +0200 | [diff] [blame] | 49 | req_copy = kmemdup(req, sizeof(*req_copy), GFP_ATOMIC); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 50 | if (!req_copy) |
| 51 | goto fail; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 52 | list_add_tail(&req_copy->queue, queue_data); |
| 53 | } |
| 54 | spin_unlock_irq(&ep->udc->lock); |
| 55 | |
| 56 | file->private_data = queue_data; |
| 57 | return 0; |
| 58 | |
| 59 | fail: |
| 60 | spin_unlock_irq(&ep->udc->lock); |
| 61 | list_for_each_entry_safe(req, req_copy, queue_data, queue) { |
| 62 | list_del(&req->queue); |
| 63 | kfree(req); |
| 64 | } |
| 65 | kfree(queue_data); |
| 66 | return -ENOMEM; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0 |
| 71 | * |
| 72 | * b: buffer address |
| 73 | * l: buffer length |
| 74 | * I/i: interrupt/no interrupt |
| 75 | * Z/z: zero/no zero |
| 76 | * S/s: short ok/short not ok |
| 77 | * s: status |
| 78 | * n: nr_packets |
| 79 | * F/f: submitted/not submitted to FIFO |
| 80 | * D/d: using/not using DMA |
| 81 | * L/l: last transaction/not last transaction |
| 82 | */ |
| 83 | static ssize_t queue_dbg_read(struct file *file, char __user *buf, |
| 84 | size_t nbytes, loff_t *ppos) |
| 85 | { |
| 86 | struct list_head *queue = file->private_data; |
| 87 | struct usba_request *req, *tmp_req; |
| 88 | size_t len, remaining, actual = 0; |
| 89 | char tmpbuf[38]; |
| 90 | |
| 91 | if (!access_ok(VERIFY_WRITE, buf, nbytes)) |
| 92 | return -EFAULT; |
| 93 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 94 | mutex_lock(&file_inode(file)->i_mutex); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 95 | list_for_each_entry_safe(req, tmp_req, queue, queue) { |
| 96 | len = snprintf(tmpbuf, sizeof(tmpbuf), |
| 97 | "%8p %08x %c%c%c %5d %c%c%c\n", |
| 98 | req->req.buf, req->req.length, |
| 99 | req->req.no_interrupt ? 'i' : 'I', |
| 100 | req->req.zero ? 'Z' : 'z', |
| 101 | req->req.short_not_ok ? 's' : 'S', |
| 102 | req->req.status, |
| 103 | req->submitted ? 'F' : 'f', |
| 104 | req->using_dma ? 'D' : 'd', |
| 105 | req->last_transaction ? 'L' : 'l'); |
| 106 | len = min(len, sizeof(tmpbuf)); |
| 107 | if (len > nbytes) |
| 108 | break; |
| 109 | |
| 110 | list_del(&req->queue); |
| 111 | kfree(req); |
| 112 | |
| 113 | remaining = __copy_to_user(buf, tmpbuf, len); |
| 114 | actual += len - remaining; |
| 115 | if (remaining) |
| 116 | break; |
| 117 | |
| 118 | nbytes -= len; |
| 119 | buf += len; |
| 120 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 121 | mutex_unlock(&file_inode(file)->i_mutex); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 122 | |
| 123 | return actual; |
| 124 | } |
| 125 | |
| 126 | static int queue_dbg_release(struct inode *inode, struct file *file) |
| 127 | { |
| 128 | struct list_head *queue_data = file->private_data; |
| 129 | struct usba_request *req, *tmp_req; |
| 130 | |
| 131 | list_for_each_entry_safe(req, tmp_req, queue_data, queue) { |
| 132 | list_del(&req->queue); |
| 133 | kfree(req); |
| 134 | } |
| 135 | kfree(queue_data); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int regs_dbg_open(struct inode *inode, struct file *file) |
| 140 | { |
| 141 | struct usba_udc *udc; |
| 142 | unsigned int i; |
| 143 | u32 *data; |
| 144 | int ret = -ENOMEM; |
| 145 | |
| 146 | mutex_lock(&inode->i_mutex); |
| 147 | udc = inode->i_private; |
| 148 | data = kmalloc(inode->i_size, GFP_KERNEL); |
| 149 | if (!data) |
| 150 | goto out; |
| 151 | |
| 152 | spin_lock_irq(&udc->lock); |
| 153 | for (i = 0; i < inode->i_size / 4; i++) |
| 154 | data[i] = __raw_readl(udc->regs + i * 4); |
| 155 | spin_unlock_irq(&udc->lock); |
| 156 | |
| 157 | file->private_data = data; |
| 158 | ret = 0; |
| 159 | |
| 160 | out: |
| 161 | mutex_unlock(&inode->i_mutex); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static ssize_t regs_dbg_read(struct file *file, char __user *buf, |
| 167 | size_t nbytes, loff_t *ppos) |
| 168 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 169 | struct inode *inode = file_inode(file); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 170 | int ret; |
| 171 | |
| 172 | mutex_lock(&inode->i_mutex); |
| 173 | ret = simple_read_from_buffer(buf, nbytes, ppos, |
| 174 | file->private_data, |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 175 | file_inode(file)->i_size); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 176 | mutex_unlock(&inode->i_mutex); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int regs_dbg_release(struct inode *inode, struct file *file) |
| 182 | { |
| 183 | kfree(file->private_data); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | const struct file_operations queue_dbg_fops = { |
| 188 | .owner = THIS_MODULE, |
| 189 | .open = queue_dbg_open, |
| 190 | .llseek = no_llseek, |
| 191 | .read = queue_dbg_read, |
| 192 | .release = queue_dbg_release, |
| 193 | }; |
| 194 | |
| 195 | const struct file_operations regs_dbg_fops = { |
| 196 | .owner = THIS_MODULE, |
| 197 | .open = regs_dbg_open, |
| 198 | .llseek = generic_file_llseek, |
| 199 | .read = regs_dbg_read, |
| 200 | .release = regs_dbg_release, |
| 201 | }; |
| 202 | |
| 203 | static void usba_ep_init_debugfs(struct usba_udc *udc, |
| 204 | struct usba_ep *ep) |
| 205 | { |
| 206 | struct dentry *ep_root; |
| 207 | |
| 208 | ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root); |
| 209 | if (!ep_root) |
| 210 | goto err_root; |
| 211 | ep->debugfs_dir = ep_root; |
| 212 | |
| 213 | ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root, |
| 214 | ep, &queue_dbg_fops); |
| 215 | if (!ep->debugfs_queue) |
| 216 | goto err_queue; |
| 217 | |
| 218 | if (ep->can_dma) { |
| 219 | ep->debugfs_dma_status |
| 220 | = debugfs_create_u32("dma_status", 0400, ep_root, |
| 221 | &ep->last_dma_status); |
| 222 | if (!ep->debugfs_dma_status) |
| 223 | goto err_dma_status; |
| 224 | } |
| 225 | if (ep_is_control(ep)) { |
| 226 | ep->debugfs_state |
| 227 | = debugfs_create_u32("state", 0400, ep_root, |
| 228 | &ep->state); |
| 229 | if (!ep->debugfs_state) |
| 230 | goto err_state; |
| 231 | } |
| 232 | |
| 233 | return; |
| 234 | |
| 235 | err_state: |
| 236 | if (ep->can_dma) |
| 237 | debugfs_remove(ep->debugfs_dma_status); |
| 238 | err_dma_status: |
| 239 | debugfs_remove(ep->debugfs_queue); |
| 240 | err_queue: |
| 241 | debugfs_remove(ep_root); |
| 242 | err_root: |
| 243 | dev_err(&ep->udc->pdev->dev, |
| 244 | "failed to create debugfs directory for %s\n", ep->ep.name); |
| 245 | } |
| 246 | |
| 247 | static void usba_ep_cleanup_debugfs(struct usba_ep *ep) |
| 248 | { |
| 249 | debugfs_remove(ep->debugfs_queue); |
| 250 | debugfs_remove(ep->debugfs_dma_status); |
| 251 | debugfs_remove(ep->debugfs_state); |
| 252 | debugfs_remove(ep->debugfs_dir); |
| 253 | ep->debugfs_dma_status = NULL; |
| 254 | ep->debugfs_dir = NULL; |
| 255 | } |
| 256 | |
| 257 | static void usba_init_debugfs(struct usba_udc *udc) |
| 258 | { |
| 259 | struct dentry *root, *regs; |
| 260 | struct resource *regs_resource; |
| 261 | |
| 262 | root = debugfs_create_dir(udc->gadget.name, NULL); |
| 263 | if (IS_ERR(root) || !root) |
| 264 | goto err_root; |
| 265 | udc->debugfs_root = root; |
| 266 | |
| 267 | regs = debugfs_create_file("regs", 0400, root, udc, ®s_dbg_fops); |
| 268 | if (!regs) |
| 269 | goto err_regs; |
| 270 | |
| 271 | regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM, |
| 272 | CTRL_IOMEM_ID); |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 273 | regs->d_inode->i_size = resource_size(regs_resource); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 274 | udc->debugfs_regs = regs; |
| 275 | |
| 276 | usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0)); |
| 277 | |
| 278 | return; |
| 279 | |
| 280 | err_regs: |
| 281 | debugfs_remove(root); |
| 282 | err_root: |
| 283 | udc->debugfs_root = NULL; |
| 284 | dev_err(&udc->pdev->dev, "debugfs is not available\n"); |
| 285 | } |
| 286 | |
| 287 | static void usba_cleanup_debugfs(struct usba_udc *udc) |
| 288 | { |
| 289 | usba_ep_cleanup_debugfs(to_usba_ep(udc->gadget.ep0)); |
| 290 | debugfs_remove(udc->debugfs_regs); |
| 291 | debugfs_remove(udc->debugfs_root); |
| 292 | udc->debugfs_regs = NULL; |
| 293 | udc->debugfs_root = NULL; |
| 294 | } |
| 295 | #else |
| 296 | static inline void usba_ep_init_debugfs(struct usba_udc *udc, |
| 297 | struct usba_ep *ep) |
| 298 | { |
| 299 | |
| 300 | } |
| 301 | |
| 302 | static inline void usba_ep_cleanup_debugfs(struct usba_ep *ep) |
| 303 | { |
| 304 | |
| 305 | } |
| 306 | |
| 307 | static inline void usba_init_debugfs(struct usba_udc *udc) |
| 308 | { |
| 309 | |
| 310 | } |
| 311 | |
| 312 | static inline void usba_cleanup_debugfs(struct usba_udc *udc) |
| 313 | { |
| 314 | |
| 315 | } |
| 316 | #endif |
| 317 | |
| 318 | static int vbus_is_present(struct usba_udc *udc) |
| 319 | { |
Hans-Christian Egtvedt | 472a678 | 2009-03-24 15:45:20 +0100 | [diff] [blame] | 320 | if (gpio_is_valid(udc->vbus_pin)) |
Eirik Aanonsen | 640e95a | 2010-02-05 09:49:25 +0100 | [diff] [blame] | 321 | return gpio_get_value(udc->vbus_pin) ^ udc->vbus_pin_inverted; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 322 | |
| 323 | /* No Vbus detection: Assume always present */ |
| 324 | return 1; |
| 325 | } |
| 326 | |
Nicolas Ferre | e60c65d | 2009-06-05 17:46:16 +0200 | [diff] [blame] | 327 | #if defined(CONFIG_ARCH_AT91SAM9RL) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 328 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 329 | #include <mach/at91_pmc.h> |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 330 | |
| 331 | static void toggle_bias(int is_on) |
| 332 | { |
Jean-Christophe PLAGNIOL-VILLARD | b551495 | 2011-11-25 09:59:46 +0800 | [diff] [blame] | 333 | unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR); |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 334 | |
| 335 | if (is_on) |
Jean-Christophe PLAGNIOL-VILLARD | b551495 | 2011-11-25 09:59:46 +0800 | [diff] [blame] | 336 | at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN); |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 337 | else |
Jean-Christophe PLAGNIOL-VILLARD | b551495 | 2011-11-25 09:59:46 +0800 | [diff] [blame] | 338 | at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN)); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Nicolas Ferre | e60c65d | 2009-06-05 17:46:16 +0200 | [diff] [blame] | 341 | #else |
| 342 | |
| 343 | static void toggle_bias(int is_on) |
| 344 | { |
| 345 | } |
| 346 | |
| 347 | #endif /* CONFIG_ARCH_AT91SAM9RL */ |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 348 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 349 | static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req) |
| 350 | { |
| 351 | unsigned int transaction_len; |
| 352 | |
| 353 | transaction_len = req->req.length - req->req.actual; |
| 354 | req->last_transaction = 1; |
| 355 | if (transaction_len > ep->ep.maxpacket) { |
| 356 | transaction_len = ep->ep.maxpacket; |
| 357 | req->last_transaction = 0; |
| 358 | } else if (transaction_len == ep->ep.maxpacket && req->req.zero) |
| 359 | req->last_transaction = 0; |
| 360 | |
| 361 | DBG(DBG_QUEUE, "%s: submit_transaction, req %p (length %d)%s\n", |
| 362 | ep->ep.name, req, transaction_len, |
| 363 | req->last_transaction ? ", done" : ""); |
| 364 | |
Haavard Skinnemoen | 5d4c270 | 2008-03-04 15:48:37 +0100 | [diff] [blame] | 365 | memcpy_toio(ep->fifo, req->req.buf + req->req.actual, transaction_len); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 366 | usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); |
| 367 | req->req.actual += transaction_len; |
| 368 | } |
| 369 | |
| 370 | static void submit_request(struct usba_ep *ep, struct usba_request *req) |
| 371 | { |
| 372 | DBG(DBG_QUEUE, "%s: submit_request: req %p (length %d)\n", |
| 373 | ep->ep.name, req, req->req.length); |
| 374 | |
| 375 | req->req.actual = 0; |
| 376 | req->submitted = 1; |
| 377 | |
| 378 | if (req->using_dma) { |
| 379 | if (req->req.length == 0) { |
| 380 | usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | if (req->req.zero) |
| 385 | usba_ep_writel(ep, CTL_ENB, USBA_SHORT_PACKET); |
| 386 | else |
| 387 | usba_ep_writel(ep, CTL_DIS, USBA_SHORT_PACKET); |
| 388 | |
| 389 | usba_dma_writel(ep, ADDRESS, req->req.dma); |
| 390 | usba_dma_writel(ep, CONTROL, req->ctrl); |
| 391 | } else { |
| 392 | next_fifo_transaction(ep, req); |
| 393 | if (req->last_transaction) { |
| 394 | usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY); |
| 395 | usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE); |
| 396 | } else { |
| 397 | usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); |
| 398 | usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static void submit_next_request(struct usba_ep *ep) |
| 404 | { |
| 405 | struct usba_request *req; |
| 406 | |
| 407 | if (list_empty(&ep->queue)) { |
| 408 | usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY | USBA_RX_BK_RDY); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | req = list_entry(ep->queue.next, struct usba_request, queue); |
| 413 | if (!req->submitted) |
| 414 | submit_request(ep, req); |
| 415 | } |
| 416 | |
| 417 | static void send_status(struct usba_udc *udc, struct usba_ep *ep) |
| 418 | { |
| 419 | ep->state = STATUS_STAGE_IN; |
| 420 | usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); |
| 421 | usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE); |
| 422 | } |
| 423 | |
| 424 | static void receive_data(struct usba_ep *ep) |
| 425 | { |
| 426 | struct usba_udc *udc = ep->udc; |
| 427 | struct usba_request *req; |
| 428 | unsigned long status; |
| 429 | unsigned int bytecount, nr_busy; |
| 430 | int is_complete = 0; |
| 431 | |
| 432 | status = usba_ep_readl(ep, STA); |
| 433 | nr_busy = USBA_BFEXT(BUSY_BANKS, status); |
| 434 | |
| 435 | DBG(DBG_QUEUE, "receive data: nr_busy=%u\n", nr_busy); |
| 436 | |
| 437 | while (nr_busy > 0) { |
| 438 | if (list_empty(&ep->queue)) { |
| 439 | usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); |
| 440 | break; |
| 441 | } |
| 442 | req = list_entry(ep->queue.next, |
| 443 | struct usba_request, queue); |
| 444 | |
| 445 | bytecount = USBA_BFEXT(BYTE_COUNT, status); |
| 446 | |
| 447 | if (status & (1 << 31)) |
| 448 | is_complete = 1; |
| 449 | if (req->req.actual + bytecount >= req->req.length) { |
| 450 | is_complete = 1; |
| 451 | bytecount = req->req.length - req->req.actual; |
| 452 | } |
| 453 | |
Haavard Skinnemoen | 5d4c270 | 2008-03-04 15:48:37 +0100 | [diff] [blame] | 454 | memcpy_fromio(req->req.buf + req->req.actual, |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 455 | ep->fifo, bytecount); |
| 456 | req->req.actual += bytecount; |
| 457 | |
| 458 | usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); |
| 459 | |
| 460 | if (is_complete) { |
| 461 | DBG(DBG_QUEUE, "%s: request done\n", ep->ep.name); |
| 462 | req->req.status = 0; |
| 463 | list_del_init(&req->queue); |
| 464 | usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); |
| 465 | spin_unlock(&udc->lock); |
| 466 | req->req.complete(&ep->ep, &req->req); |
| 467 | spin_lock(&udc->lock); |
| 468 | } |
| 469 | |
| 470 | status = usba_ep_readl(ep, STA); |
| 471 | nr_busy = USBA_BFEXT(BUSY_BANKS, status); |
| 472 | |
| 473 | if (is_complete && ep_is_control(ep)) { |
| 474 | send_status(udc, ep); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static void |
| 481 | request_complete(struct usba_ep *ep, struct usba_request *req, int status) |
| 482 | { |
| 483 | struct usba_udc *udc = ep->udc; |
| 484 | |
| 485 | WARN_ON(!list_empty(&req->queue)); |
| 486 | |
| 487 | if (req->req.status == -EINPROGRESS) |
| 488 | req->req.status = status; |
| 489 | |
Felipe Balbi | 1bda9df | 2013-01-28 16:57:02 +0200 | [diff] [blame] | 490 | if (req->using_dma) |
| 491 | usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 492 | |
| 493 | DBG(DBG_GADGET | DBG_REQ, |
| 494 | "%s: req %p complete: status %d, actual %u\n", |
| 495 | ep->ep.name, req, req->req.status, req->req.actual); |
| 496 | |
| 497 | spin_unlock(&udc->lock); |
| 498 | req->req.complete(&ep->ep, &req->req); |
| 499 | spin_lock(&udc->lock); |
| 500 | } |
| 501 | |
| 502 | static void |
| 503 | request_complete_list(struct usba_ep *ep, struct list_head *list, int status) |
| 504 | { |
| 505 | struct usba_request *req, *tmp_req; |
| 506 | |
| 507 | list_for_each_entry_safe(req, tmp_req, list, queue) { |
| 508 | list_del_init(&req->queue); |
| 509 | request_complete(ep, req, status); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | static int |
| 514 | usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) |
| 515 | { |
| 516 | struct usba_ep *ep = to_usba_ep(_ep); |
| 517 | struct usba_udc *udc = ep->udc; |
| 518 | unsigned long flags, ept_cfg, maxpacket; |
| 519 | unsigned int nr_trans; |
| 520 | |
| 521 | DBG(DBG_GADGET, "%s: ep_enable: desc=%p\n", ep->ep.name, desc); |
| 522 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 523 | maxpacket = usb_endpoint_maxp(desc) & 0x7ff; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 524 | |
| 525 | if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) != ep->index) |
| 526 | || ep->index == 0 |
| 527 | || desc->bDescriptorType != USB_DT_ENDPOINT |
| 528 | || maxpacket == 0 |
| 529 | || maxpacket > ep->fifo_size) { |
| 530 | DBG(DBG_ERR, "ep_enable: Invalid argument"); |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | |
| 534 | ep->is_isoc = 0; |
| 535 | ep->is_in = 0; |
| 536 | |
| 537 | if (maxpacket <= 8) |
| 538 | ept_cfg = USBA_BF(EPT_SIZE, USBA_EPT_SIZE_8); |
| 539 | else |
| 540 | /* LSB is bit 1, not 0 */ |
| 541 | ept_cfg = USBA_BF(EPT_SIZE, fls(maxpacket - 1) - 3); |
| 542 | |
| 543 | DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n", |
| 544 | ep->ep.name, ept_cfg, maxpacket); |
| 545 | |
Matthias Kaehlcke | 71de6b6 | 2009-04-15 22:27:49 +0200 | [diff] [blame] | 546 | if (usb_endpoint_dir_in(desc)) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 547 | ep->is_in = 1; |
| 548 | ept_cfg |= USBA_EPT_DIR_IN; |
| 549 | } |
| 550 | |
Matthias Kaehlcke | 71de6b6 | 2009-04-15 22:27:49 +0200 | [diff] [blame] | 551 | switch (usb_endpoint_type(desc)) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 552 | case USB_ENDPOINT_XFER_CONTROL: |
| 553 | ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL); |
| 554 | ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE); |
| 555 | break; |
| 556 | case USB_ENDPOINT_XFER_ISOC: |
| 557 | if (!ep->can_isoc) { |
| 558 | DBG(DBG_ERR, "ep_enable: %s is not isoc capable\n", |
| 559 | ep->ep.name); |
| 560 | return -EINVAL; |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * Bits 11:12 specify number of _additional_ |
| 565 | * transactions per microframe. |
| 566 | */ |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 567 | nr_trans = ((usb_endpoint_maxp(desc) >> 11) & 3) + 1; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 568 | if (nr_trans > 3) |
| 569 | return -EINVAL; |
| 570 | |
| 571 | ep->is_isoc = 1; |
| 572 | ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_ISO); |
| 573 | |
| 574 | /* |
| 575 | * Do triple-buffering on high-bandwidth iso endpoints. |
| 576 | */ |
| 577 | if (nr_trans > 1 && ep->nr_banks == 3) |
| 578 | ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_TRIPLE); |
| 579 | else |
| 580 | ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE); |
| 581 | ept_cfg |= USBA_BF(NB_TRANS, nr_trans); |
| 582 | break; |
| 583 | case USB_ENDPOINT_XFER_BULK: |
| 584 | ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK); |
| 585 | ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE); |
| 586 | break; |
| 587 | case USB_ENDPOINT_XFER_INT: |
| 588 | ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_INT); |
| 589 | ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE); |
| 590 | break; |
| 591 | } |
| 592 | |
| 593 | spin_lock_irqsave(&ep->udc->lock, flags); |
| 594 | |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 595 | ep->ep.desc = desc; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 596 | ep->ep.maxpacket = maxpacket; |
| 597 | |
| 598 | usba_ep_writel(ep, CFG, ept_cfg); |
| 599 | usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE); |
| 600 | |
| 601 | if (ep->can_dma) { |
| 602 | u32 ctrl; |
| 603 | |
| 604 | usba_writel(udc, INT_ENB, |
| 605 | (usba_readl(udc, INT_ENB) |
| 606 | | USBA_BF(EPT_INT, 1 << ep->index) |
| 607 | | USBA_BF(DMA_INT, 1 << ep->index))); |
| 608 | ctrl = USBA_AUTO_VALID | USBA_INTDIS_DMA; |
| 609 | usba_ep_writel(ep, CTL_ENB, ctrl); |
| 610 | } else { |
| 611 | usba_writel(udc, INT_ENB, |
| 612 | (usba_readl(udc, INT_ENB) |
| 613 | | USBA_BF(EPT_INT, 1 << ep->index))); |
| 614 | } |
| 615 | |
| 616 | spin_unlock_irqrestore(&udc->lock, flags); |
| 617 | |
| 618 | DBG(DBG_HW, "EPT_CFG%d after init: %#08lx\n", ep->index, |
| 619 | (unsigned long)usba_ep_readl(ep, CFG)); |
| 620 | DBG(DBG_HW, "INT_ENB after init: %#08lx\n", |
| 621 | (unsigned long)usba_readl(udc, INT_ENB)); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int usba_ep_disable(struct usb_ep *_ep) |
| 627 | { |
| 628 | struct usba_ep *ep = to_usba_ep(_ep); |
| 629 | struct usba_udc *udc = ep->udc; |
| 630 | LIST_HEAD(req_list); |
| 631 | unsigned long flags; |
| 632 | |
| 633 | DBG(DBG_GADGET, "ep_disable: %s\n", ep->ep.name); |
| 634 | |
| 635 | spin_lock_irqsave(&udc->lock, flags); |
| 636 | |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 637 | if (!ep->ep.desc) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 638 | spin_unlock_irqrestore(&udc->lock, flags); |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 639 | /* REVISIT because this driver disables endpoints in |
| 640 | * reset_all_endpoints() before calling disconnect(), |
| 641 | * most gadget drivers would trigger this non-error ... |
| 642 | */ |
| 643 | if (udc->gadget.speed != USB_SPEED_UNKNOWN) |
| 644 | DBG(DBG_ERR, "ep_disable: %s not enabled\n", |
| 645 | ep->ep.name); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 646 | return -EINVAL; |
| 647 | } |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 648 | ep->ep.desc = NULL; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 649 | |
| 650 | list_splice_init(&ep->queue, &req_list); |
| 651 | if (ep->can_dma) { |
| 652 | usba_dma_writel(ep, CONTROL, 0); |
| 653 | usba_dma_writel(ep, ADDRESS, 0); |
| 654 | usba_dma_readl(ep, STATUS); |
| 655 | } |
| 656 | usba_ep_writel(ep, CTL_DIS, USBA_EPT_ENABLE); |
| 657 | usba_writel(udc, INT_ENB, |
| 658 | usba_readl(udc, INT_ENB) |
| 659 | & ~USBA_BF(EPT_INT, 1 << ep->index)); |
| 660 | |
| 661 | request_complete_list(ep, &req_list, -ESHUTDOWN); |
| 662 | |
| 663 | spin_unlock_irqrestore(&udc->lock, flags); |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static struct usb_request * |
| 669 | usba_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) |
| 670 | { |
| 671 | struct usba_request *req; |
| 672 | |
| 673 | DBG(DBG_GADGET, "ep_alloc_request: %p, 0x%x\n", _ep, gfp_flags); |
| 674 | |
| 675 | req = kzalloc(sizeof(*req), gfp_flags); |
| 676 | if (!req) |
| 677 | return NULL; |
| 678 | |
| 679 | INIT_LIST_HEAD(&req->queue); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 680 | |
| 681 | return &req->req; |
| 682 | } |
| 683 | |
| 684 | static void |
| 685 | usba_ep_free_request(struct usb_ep *_ep, struct usb_request *_req) |
| 686 | { |
| 687 | struct usba_request *req = to_usba_req(_req); |
| 688 | |
| 689 | DBG(DBG_GADGET, "ep_free_request: %p, %p\n", _ep, _req); |
| 690 | |
| 691 | kfree(req); |
| 692 | } |
| 693 | |
| 694 | static int queue_dma(struct usba_udc *udc, struct usba_ep *ep, |
| 695 | struct usba_request *req, gfp_t gfp_flags) |
| 696 | { |
| 697 | unsigned long flags; |
| 698 | int ret; |
| 699 | |
| 700 | DBG(DBG_DMA, "%s: req l/%u d/%08x %c%c%c\n", |
| 701 | ep->ep.name, req->req.length, req->req.dma, |
| 702 | req->req.zero ? 'Z' : 'z', |
| 703 | req->req.short_not_ok ? 'S' : 's', |
| 704 | req->req.no_interrupt ? 'I' : 'i'); |
| 705 | |
| 706 | if (req->req.length > 0x10000) { |
| 707 | /* Lengths from 0 to 65536 (inclusive) are supported */ |
| 708 | DBG(DBG_ERR, "invalid request length %u\n", req->req.length); |
| 709 | return -EINVAL; |
| 710 | } |
| 711 | |
Felipe Balbi | 1bda9df | 2013-01-28 16:57:02 +0200 | [diff] [blame] | 712 | ret = usb_gadget_map_request(&udc->gadget, &req->req, ep->is_in); |
| 713 | if (ret) |
| 714 | return ret; |
| 715 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 716 | req->using_dma = 1; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 717 | req->ctrl = USBA_BF(DMA_BUF_LEN, req->req.length) |
| 718 | | USBA_DMA_CH_EN | USBA_DMA_END_BUF_IE |
| 719 | | USBA_DMA_END_TR_EN | USBA_DMA_END_TR_IE; |
| 720 | |
| 721 | if (ep->is_in) |
| 722 | req->ctrl |= USBA_DMA_END_BUF_EN; |
| 723 | |
| 724 | /* |
| 725 | * Add this request to the queue and submit for DMA if |
| 726 | * possible. Check if we're still alive first -- we may have |
| 727 | * received a reset since last time we checked. |
| 728 | */ |
| 729 | ret = -ESHUTDOWN; |
| 730 | spin_lock_irqsave(&udc->lock, flags); |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 731 | if (ep->ep.desc) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 732 | if (list_empty(&ep->queue)) |
| 733 | submit_request(ep, req); |
| 734 | |
| 735 | list_add_tail(&req->queue, &ep->queue); |
| 736 | ret = 0; |
| 737 | } |
| 738 | spin_unlock_irqrestore(&udc->lock, flags); |
| 739 | |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | usba_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags) |
| 745 | { |
| 746 | struct usba_request *req = to_usba_req(_req); |
| 747 | struct usba_ep *ep = to_usba_ep(_ep); |
| 748 | struct usba_udc *udc = ep->udc; |
| 749 | unsigned long flags; |
| 750 | int ret; |
| 751 | |
| 752 | DBG(DBG_GADGET | DBG_QUEUE | DBG_REQ, "%s: queue req %p, len %u\n", |
| 753 | ep->ep.name, req, _req->length); |
| 754 | |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 755 | if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN || |
| 756 | !ep->ep.desc) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 757 | return -ESHUTDOWN; |
| 758 | |
| 759 | req->submitted = 0; |
| 760 | req->using_dma = 0; |
| 761 | req->last_transaction = 0; |
| 762 | |
| 763 | _req->status = -EINPROGRESS; |
| 764 | _req->actual = 0; |
| 765 | |
| 766 | if (ep->can_dma) |
| 767 | return queue_dma(udc, ep, req, gfp_flags); |
| 768 | |
| 769 | /* May have received a reset since last time we checked */ |
| 770 | ret = -ESHUTDOWN; |
| 771 | spin_lock_irqsave(&udc->lock, flags); |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 772 | if (ep->ep.desc) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 773 | list_add_tail(&req->queue, &ep->queue); |
| 774 | |
Martin Fuzzey | f42706c | 2009-05-01 17:21:11 +0200 | [diff] [blame] | 775 | if ((!ep_is_control(ep) && ep->is_in) || |
| 776 | (ep_is_control(ep) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 777 | && (ep->state == DATA_STAGE_IN |
| 778 | || ep->state == STATUS_STAGE_IN))) |
| 779 | usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY); |
| 780 | else |
| 781 | usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY); |
| 782 | ret = 0; |
| 783 | } |
| 784 | spin_unlock_irqrestore(&udc->lock, flags); |
| 785 | |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | static void |
| 790 | usba_update_req(struct usba_ep *ep, struct usba_request *req, u32 status) |
| 791 | { |
| 792 | req->req.actual = req->req.length - USBA_BFEXT(DMA_BUF_LEN, status); |
| 793 | } |
| 794 | |
| 795 | static int stop_dma(struct usba_ep *ep, u32 *pstatus) |
| 796 | { |
| 797 | unsigned int timeout; |
| 798 | u32 status; |
| 799 | |
| 800 | /* |
| 801 | * Stop the DMA controller. When writing both CH_EN |
| 802 | * and LINK to 0, the other bits are not affected. |
| 803 | */ |
| 804 | usba_dma_writel(ep, CONTROL, 0); |
| 805 | |
| 806 | /* Wait for the FIFO to empty */ |
| 807 | for (timeout = 40; timeout; --timeout) { |
| 808 | status = usba_dma_readl(ep, STATUS); |
| 809 | if (!(status & USBA_DMA_CH_EN)) |
| 810 | break; |
| 811 | udelay(1); |
| 812 | } |
| 813 | |
| 814 | if (pstatus) |
| 815 | *pstatus = status; |
| 816 | |
| 817 | if (timeout == 0) { |
| 818 | dev_err(&ep->udc->pdev->dev, |
| 819 | "%s: timed out waiting for DMA FIFO to empty\n", |
| 820 | ep->ep.name); |
| 821 | return -ETIMEDOUT; |
| 822 | } |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static int usba_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) |
| 828 | { |
| 829 | struct usba_ep *ep = to_usba_ep(_ep); |
| 830 | struct usba_udc *udc = ep->udc; |
| 831 | struct usba_request *req = to_usba_req(_req); |
| 832 | unsigned long flags; |
| 833 | u32 status; |
| 834 | |
| 835 | DBG(DBG_GADGET | DBG_QUEUE, "ep_dequeue: %s, req %p\n", |
| 836 | ep->ep.name, req); |
| 837 | |
| 838 | spin_lock_irqsave(&udc->lock, flags); |
| 839 | |
| 840 | if (req->using_dma) { |
| 841 | /* |
| 842 | * If this request is currently being transferred, |
| 843 | * stop the DMA controller and reset the FIFO. |
| 844 | */ |
| 845 | if (ep->queue.next == &req->queue) { |
| 846 | status = usba_dma_readl(ep, STATUS); |
| 847 | if (status & USBA_DMA_CH_EN) |
| 848 | stop_dma(ep, &status); |
| 849 | |
| 850 | #ifdef CONFIG_USB_GADGET_DEBUG_FS |
| 851 | ep->last_dma_status = status; |
| 852 | #endif |
| 853 | |
| 854 | usba_writel(udc, EPT_RST, 1 << ep->index); |
| 855 | |
| 856 | usba_update_req(ep, req, status); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Errors should stop the queue from advancing until the |
| 862 | * completion function returns. |
| 863 | */ |
| 864 | list_del_init(&req->queue); |
| 865 | |
| 866 | request_complete(ep, req, -ECONNRESET); |
| 867 | |
| 868 | /* Process the next request if any */ |
| 869 | submit_next_request(ep); |
| 870 | spin_unlock_irqrestore(&udc->lock, flags); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int usba_ep_set_halt(struct usb_ep *_ep, int value) |
| 876 | { |
| 877 | struct usba_ep *ep = to_usba_ep(_ep); |
| 878 | struct usba_udc *udc = ep->udc; |
| 879 | unsigned long flags; |
| 880 | int ret = 0; |
| 881 | |
| 882 | DBG(DBG_GADGET, "endpoint %s: %s HALT\n", ep->ep.name, |
| 883 | value ? "set" : "clear"); |
| 884 | |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 885 | if (!ep->ep.desc) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 886 | DBG(DBG_ERR, "Attempted to halt uninitialized ep %s\n", |
| 887 | ep->ep.name); |
| 888 | return -ENODEV; |
| 889 | } |
| 890 | if (ep->is_isoc) { |
| 891 | DBG(DBG_ERR, "Attempted to halt isochronous ep %s\n", |
| 892 | ep->ep.name); |
| 893 | return -ENOTTY; |
| 894 | } |
| 895 | |
| 896 | spin_lock_irqsave(&udc->lock, flags); |
| 897 | |
| 898 | /* |
| 899 | * We can't halt IN endpoints while there are still data to be |
| 900 | * transferred |
| 901 | */ |
| 902 | if (!list_empty(&ep->queue) |
| 903 | || ((value && ep->is_in && (usba_ep_readl(ep, STA) |
| 904 | & USBA_BF(BUSY_BANKS, -1L))))) { |
| 905 | ret = -EAGAIN; |
| 906 | } else { |
| 907 | if (value) |
| 908 | usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL); |
| 909 | else |
| 910 | usba_ep_writel(ep, CLR_STA, |
| 911 | USBA_FORCE_STALL | USBA_TOGGLE_CLR); |
| 912 | usba_ep_readl(ep, STA); |
| 913 | } |
| 914 | |
| 915 | spin_unlock_irqrestore(&udc->lock, flags); |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | static int usba_ep_fifo_status(struct usb_ep *_ep) |
| 921 | { |
| 922 | struct usba_ep *ep = to_usba_ep(_ep); |
| 923 | |
| 924 | return USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA)); |
| 925 | } |
| 926 | |
| 927 | static void usba_ep_fifo_flush(struct usb_ep *_ep) |
| 928 | { |
| 929 | struct usba_ep *ep = to_usba_ep(_ep); |
| 930 | struct usba_udc *udc = ep->udc; |
| 931 | |
| 932 | usba_writel(udc, EPT_RST, 1 << ep->index); |
| 933 | } |
| 934 | |
| 935 | static const struct usb_ep_ops usba_ep_ops = { |
| 936 | .enable = usba_ep_enable, |
| 937 | .disable = usba_ep_disable, |
| 938 | .alloc_request = usba_ep_alloc_request, |
| 939 | .free_request = usba_ep_free_request, |
| 940 | .queue = usba_ep_queue, |
| 941 | .dequeue = usba_ep_dequeue, |
| 942 | .set_halt = usba_ep_set_halt, |
| 943 | .fifo_status = usba_ep_fifo_status, |
| 944 | .fifo_flush = usba_ep_fifo_flush, |
| 945 | }; |
| 946 | |
| 947 | static int usba_udc_get_frame(struct usb_gadget *gadget) |
| 948 | { |
| 949 | struct usba_udc *udc = to_usba_udc(gadget); |
| 950 | |
| 951 | return USBA_BFEXT(FRAME_NUMBER, usba_readl(udc, FNUM)); |
| 952 | } |
| 953 | |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 954 | static int usba_udc_wakeup(struct usb_gadget *gadget) |
| 955 | { |
| 956 | struct usba_udc *udc = to_usba_udc(gadget); |
| 957 | unsigned long flags; |
| 958 | u32 ctrl; |
| 959 | int ret = -EINVAL; |
| 960 | |
| 961 | spin_lock_irqsave(&udc->lock, flags); |
| 962 | if (udc->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { |
| 963 | ctrl = usba_readl(udc, CTRL); |
| 964 | usba_writel(udc, CTRL, ctrl | USBA_REMOTE_WAKE_UP); |
| 965 | ret = 0; |
| 966 | } |
| 967 | spin_unlock_irqrestore(&udc->lock, flags); |
| 968 | |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | static int |
| 973 | usba_udc_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered) |
| 974 | { |
| 975 | struct usba_udc *udc = to_usba_udc(gadget); |
| 976 | unsigned long flags; |
| 977 | |
| 978 | spin_lock_irqsave(&udc->lock, flags); |
| 979 | if (is_selfpowered) |
| 980 | udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED; |
| 981 | else |
| 982 | udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); |
| 983 | spin_unlock_irqrestore(&udc->lock, flags); |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Sebastian Andrzej Siewior | d809f78 | 2012-02-04 18:55:24 +0100 | [diff] [blame] | 988 | static int atmel_usba_start(struct usb_gadget *gadget, |
| 989 | struct usb_gadget_driver *driver); |
| 990 | static int atmel_usba_stop(struct usb_gadget *gadget, |
| 991 | struct usb_gadget_driver *driver); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 992 | static const struct usb_gadget_ops usba_udc_ops = { |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 993 | .get_frame = usba_udc_get_frame, |
| 994 | .wakeup = usba_udc_wakeup, |
| 995 | .set_selfpowered = usba_udc_set_selfpowered, |
Sebastian Andrzej Siewior | d809f78 | 2012-02-04 18:55:24 +0100 | [diff] [blame] | 996 | .udc_start = atmel_usba_start, |
| 997 | .udc_stop = atmel_usba_stop, |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 998 | }; |
| 999 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1000 | static struct usb_endpoint_descriptor usba_ep0_desc = { |
| 1001 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1002 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1003 | .bEndpointAddress = 0, |
| 1004 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1005 | .wMaxPacketSize = cpu_to_le16(64), |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1006 | /* FIXME: I have no idea what to put here */ |
| 1007 | .bInterval = 1, |
| 1008 | }; |
| 1009 | |
| 1010 | static void nop_release(struct device *dev) |
| 1011 | { |
| 1012 | |
| 1013 | } |
| 1014 | |
Jean-Christophe PLAGNIOL-VILLARD | e8f2ea3 | 2013-05-20 22:46:02 +0800 | [diff] [blame] | 1015 | struct usb_gadget usba_gadget_template = { |
| 1016 | .ops = &usba_udc_ops, |
| 1017 | .max_speed = USB_SPEED_HIGH, |
| 1018 | .name = "atmel_usba_udc", |
| 1019 | .dev = { |
| 1020 | .init_name = "gadget", |
| 1021 | .release = nop_release, |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1022 | }, |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1023 | }; |
| 1024 | |
| 1025 | /* |
| 1026 | * Called with interrupts disabled and udc->lock held. |
| 1027 | */ |
| 1028 | static void reset_all_endpoints(struct usba_udc *udc) |
| 1029 | { |
| 1030 | struct usba_ep *ep; |
| 1031 | struct usba_request *req, *tmp_req; |
| 1032 | |
| 1033 | usba_writel(udc, EPT_RST, ~0UL); |
| 1034 | |
| 1035 | ep = to_usba_ep(udc->gadget.ep0); |
| 1036 | list_for_each_entry_safe(req, tmp_req, &ep->queue, queue) { |
| 1037 | list_del_init(&req->queue); |
| 1038 | request_complete(ep, req, -ECONNRESET); |
| 1039 | } |
| 1040 | |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 1041 | /* NOTE: normally, the next call to the gadget driver is in |
| 1042 | * charge of disabling endpoints... usually disconnect(). |
| 1043 | * The exception would be entering a high speed test mode. |
| 1044 | * |
| 1045 | * FIXME remove this code ... and retest thoroughly. |
| 1046 | */ |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1047 | list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) { |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 1048 | if (ep->ep.desc) { |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 1049 | spin_unlock(&udc->lock); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1050 | usba_ep_disable(&ep->ep); |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 1051 | spin_lock(&udc->lock); |
| 1052 | } |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | static struct usba_ep *get_ep_by_addr(struct usba_udc *udc, u16 wIndex) |
| 1057 | { |
| 1058 | struct usba_ep *ep; |
| 1059 | |
| 1060 | if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0) |
| 1061 | return to_usba_ep(udc->gadget.ep0); |
| 1062 | |
| 1063 | list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) { |
| 1064 | u8 bEndpointAddress; |
| 1065 | |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 1066 | if (!ep->ep.desc) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1067 | continue; |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 1068 | bEndpointAddress = ep->ep.desc->bEndpointAddress; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1069 | if ((wIndex ^ bEndpointAddress) & USB_DIR_IN) |
| 1070 | continue; |
| 1071 | if ((bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) |
| 1072 | == (wIndex & USB_ENDPOINT_NUMBER_MASK)) |
| 1073 | return ep; |
| 1074 | } |
| 1075 | |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
| 1079 | /* Called with interrupts disabled and udc->lock held */ |
| 1080 | static inline void set_protocol_stall(struct usba_udc *udc, struct usba_ep *ep) |
| 1081 | { |
| 1082 | usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL); |
| 1083 | ep->state = WAIT_FOR_SETUP; |
| 1084 | } |
| 1085 | |
| 1086 | static inline int is_stalled(struct usba_udc *udc, struct usba_ep *ep) |
| 1087 | { |
| 1088 | if (usba_ep_readl(ep, STA) & USBA_FORCE_STALL) |
| 1089 | return 1; |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | static inline void set_address(struct usba_udc *udc, unsigned int addr) |
| 1094 | { |
| 1095 | u32 regval; |
| 1096 | |
| 1097 | DBG(DBG_BUS, "setting address %u...\n", addr); |
| 1098 | regval = usba_readl(udc, CTRL); |
| 1099 | regval = USBA_BFINS(DEV_ADDR, addr, regval); |
| 1100 | usba_writel(udc, CTRL, regval); |
| 1101 | } |
| 1102 | |
| 1103 | static int do_test_mode(struct usba_udc *udc) |
| 1104 | { |
| 1105 | static const char test_packet_buffer[] = { |
| 1106 | /* JKJKJKJK * 9 */ |
| 1107 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1108 | /* JJKKJJKK * 8 */ |
| 1109 | 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, |
| 1110 | /* JJKKJJKK * 8 */ |
| 1111 | 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, |
| 1112 | /* JJJJJJJKKKKKKK * 8 */ |
| 1113 | 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1114 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 1115 | /* JJJJJJJK * 8 */ |
| 1116 | 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, |
| 1117 | /* {JKKKKKKK * 10}, JK */ |
| 1118 | 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E |
| 1119 | }; |
| 1120 | struct usba_ep *ep; |
| 1121 | struct device *dev = &udc->pdev->dev; |
| 1122 | int test_mode; |
| 1123 | |
| 1124 | test_mode = udc->test_mode; |
| 1125 | |
| 1126 | /* Start from a clean slate */ |
| 1127 | reset_all_endpoints(udc); |
| 1128 | |
| 1129 | switch (test_mode) { |
| 1130 | case 0x0100: |
| 1131 | /* Test_J */ |
| 1132 | usba_writel(udc, TST, USBA_TST_J_MODE); |
| 1133 | dev_info(dev, "Entering Test_J mode...\n"); |
| 1134 | break; |
| 1135 | case 0x0200: |
| 1136 | /* Test_K */ |
| 1137 | usba_writel(udc, TST, USBA_TST_K_MODE); |
| 1138 | dev_info(dev, "Entering Test_K mode...\n"); |
| 1139 | break; |
| 1140 | case 0x0300: |
| 1141 | /* |
| 1142 | * Test_SE0_NAK: Force high-speed mode and set up ep0 |
| 1143 | * for Bulk IN transfers |
| 1144 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 1145 | ep = &udc->usba_ep[0]; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1146 | usba_writel(udc, TST, |
| 1147 | USBA_BF(SPEED_CFG, USBA_SPEED_CFG_FORCE_HIGH)); |
| 1148 | usba_ep_writel(ep, CFG, |
| 1149 | USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64) |
| 1150 | | USBA_EPT_DIR_IN |
| 1151 | | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK) |
| 1152 | | USBA_BF(BK_NUMBER, 1)); |
| 1153 | if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) { |
| 1154 | set_protocol_stall(udc, ep); |
| 1155 | dev_err(dev, "Test_SE0_NAK: ep0 not mapped\n"); |
| 1156 | } else { |
| 1157 | usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE); |
| 1158 | dev_info(dev, "Entering Test_SE0_NAK mode...\n"); |
| 1159 | } |
| 1160 | break; |
| 1161 | case 0x0400: |
| 1162 | /* Test_Packet */ |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 1163 | ep = &udc->usba_ep[0]; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1164 | usba_ep_writel(ep, CFG, |
| 1165 | USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64) |
| 1166 | | USBA_EPT_DIR_IN |
| 1167 | | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK) |
| 1168 | | USBA_BF(BK_NUMBER, 1)); |
| 1169 | if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) { |
| 1170 | set_protocol_stall(udc, ep); |
| 1171 | dev_err(dev, "Test_Packet: ep0 not mapped\n"); |
| 1172 | } else { |
| 1173 | usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE); |
| 1174 | usba_writel(udc, TST, USBA_TST_PKT_MODE); |
Haavard Skinnemoen | 5d4c270 | 2008-03-04 15:48:37 +0100 | [diff] [blame] | 1175 | memcpy_toio(ep->fifo, test_packet_buffer, |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1176 | sizeof(test_packet_buffer)); |
| 1177 | usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); |
| 1178 | dev_info(dev, "Entering Test_Packet mode...\n"); |
| 1179 | } |
| 1180 | break; |
| 1181 | default: |
| 1182 | dev_err(dev, "Invalid test mode: 0x%04x\n", test_mode); |
| 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /* Avoid overly long expressions */ |
| 1190 | static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest *crq) |
| 1191 | { |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1192 | if (crq->wValue == cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP)) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1193 | return true; |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest *crq) |
| 1198 | { |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1199 | if (crq->wValue == cpu_to_le16(USB_DEVICE_TEST_MODE)) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1200 | return true; |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq) |
| 1205 | { |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1206 | if (crq->wValue == cpu_to_le16(USB_ENDPOINT_HALT)) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1207 | return true; |
| 1208 | return false; |
| 1209 | } |
| 1210 | |
| 1211 | static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep, |
| 1212 | struct usb_ctrlrequest *crq) |
| 1213 | { |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 1214 | int retval = 0; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1215 | |
| 1216 | switch (crq->bRequest) { |
| 1217 | case USB_REQ_GET_STATUS: { |
| 1218 | u16 status; |
| 1219 | |
| 1220 | if (crq->bRequestType == (USB_DIR_IN | USB_RECIP_DEVICE)) { |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 1221 | status = cpu_to_le16(udc->devstatus); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1222 | } else if (crq->bRequestType |
| 1223 | == (USB_DIR_IN | USB_RECIP_INTERFACE)) { |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1224 | status = cpu_to_le16(0); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1225 | } else if (crq->bRequestType |
| 1226 | == (USB_DIR_IN | USB_RECIP_ENDPOINT)) { |
| 1227 | struct usba_ep *target; |
| 1228 | |
| 1229 | target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex)); |
| 1230 | if (!target) |
| 1231 | goto stall; |
| 1232 | |
| 1233 | status = 0; |
| 1234 | if (is_stalled(udc, target)) |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1235 | status |= cpu_to_le16(1); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1236 | } else |
| 1237 | goto delegate; |
| 1238 | |
| 1239 | /* Write directly to the FIFO. No queueing is done. */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1240 | if (crq->wLength != cpu_to_le16(sizeof(status))) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1241 | goto stall; |
| 1242 | ep->state = DATA_STAGE_IN; |
| 1243 | __raw_writew(status, ep->fifo); |
| 1244 | usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY); |
| 1245 | break; |
| 1246 | } |
| 1247 | |
| 1248 | case USB_REQ_CLEAR_FEATURE: { |
| 1249 | if (crq->bRequestType == USB_RECIP_DEVICE) { |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 1250 | if (feature_is_dev_remote_wakeup(crq)) |
| 1251 | udc->devstatus |
| 1252 | &= ~(1 << USB_DEVICE_REMOTE_WAKEUP); |
| 1253 | else |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1254 | /* Can't CLEAR_FEATURE TEST_MODE */ |
| 1255 | goto stall; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1256 | } else if (crq->bRequestType == USB_RECIP_ENDPOINT) { |
| 1257 | struct usba_ep *target; |
| 1258 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1259 | if (crq->wLength != cpu_to_le16(0) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1260 | || !feature_is_ep_halt(crq)) |
| 1261 | goto stall; |
| 1262 | target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex)); |
| 1263 | if (!target) |
| 1264 | goto stall; |
| 1265 | |
| 1266 | usba_ep_writel(target, CLR_STA, USBA_FORCE_STALL); |
| 1267 | if (target->index != 0) |
| 1268 | usba_ep_writel(target, CLR_STA, |
| 1269 | USBA_TOGGLE_CLR); |
| 1270 | } else { |
| 1271 | goto delegate; |
| 1272 | } |
| 1273 | |
| 1274 | send_status(udc, ep); |
| 1275 | break; |
| 1276 | } |
| 1277 | |
| 1278 | case USB_REQ_SET_FEATURE: { |
| 1279 | if (crq->bRequestType == USB_RECIP_DEVICE) { |
| 1280 | if (feature_is_dev_test_mode(crq)) { |
| 1281 | send_status(udc, ep); |
| 1282 | ep->state = STATUS_STAGE_TEST; |
| 1283 | udc->test_mode = le16_to_cpu(crq->wIndex); |
| 1284 | return 0; |
| 1285 | } else if (feature_is_dev_remote_wakeup(crq)) { |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 1286 | udc->devstatus |= 1 << USB_DEVICE_REMOTE_WAKEUP; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1287 | } else { |
| 1288 | goto stall; |
| 1289 | } |
| 1290 | } else if (crq->bRequestType == USB_RECIP_ENDPOINT) { |
| 1291 | struct usba_ep *target; |
| 1292 | |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1293 | if (crq->wLength != cpu_to_le16(0) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1294 | || !feature_is_ep_halt(crq)) |
| 1295 | goto stall; |
| 1296 | |
| 1297 | target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex)); |
| 1298 | if (!target) |
| 1299 | goto stall; |
| 1300 | |
| 1301 | usba_ep_writel(target, SET_STA, USBA_FORCE_STALL); |
| 1302 | } else |
| 1303 | goto delegate; |
| 1304 | |
| 1305 | send_status(udc, ep); |
| 1306 | break; |
| 1307 | } |
| 1308 | |
| 1309 | case USB_REQ_SET_ADDRESS: |
| 1310 | if (crq->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE)) |
| 1311 | goto delegate; |
| 1312 | |
| 1313 | set_address(udc, le16_to_cpu(crq->wValue)); |
| 1314 | send_status(udc, ep); |
| 1315 | ep->state = STATUS_STAGE_ADDR; |
| 1316 | break; |
| 1317 | |
| 1318 | default: |
| 1319 | delegate: |
| 1320 | spin_unlock(&udc->lock); |
| 1321 | retval = udc->driver->setup(&udc->gadget, crq); |
| 1322 | spin_lock(&udc->lock); |
| 1323 | } |
| 1324 | |
| 1325 | return retval; |
| 1326 | |
| 1327 | stall: |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1328 | pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, " |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1329 | "halting endpoint...\n", |
| 1330 | ep->ep.name, crq->bRequestType, crq->bRequest, |
| 1331 | le16_to_cpu(crq->wValue), le16_to_cpu(crq->wIndex), |
| 1332 | le16_to_cpu(crq->wLength)); |
| 1333 | set_protocol_stall(udc, ep); |
| 1334 | return -1; |
| 1335 | } |
| 1336 | |
| 1337 | static void usba_control_irq(struct usba_udc *udc, struct usba_ep *ep) |
| 1338 | { |
| 1339 | struct usba_request *req; |
| 1340 | u32 epstatus; |
| 1341 | u32 epctrl; |
| 1342 | |
| 1343 | restart: |
| 1344 | epstatus = usba_ep_readl(ep, STA); |
| 1345 | epctrl = usba_ep_readl(ep, CTL); |
| 1346 | |
| 1347 | DBG(DBG_INT, "%s [%d]: s/%08x c/%08x\n", |
| 1348 | ep->ep.name, ep->state, epstatus, epctrl); |
| 1349 | |
| 1350 | req = NULL; |
| 1351 | if (!list_empty(&ep->queue)) |
| 1352 | req = list_entry(ep->queue.next, |
| 1353 | struct usba_request, queue); |
| 1354 | |
| 1355 | if ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) { |
| 1356 | if (req->submitted) |
| 1357 | next_fifo_transaction(ep, req); |
| 1358 | else |
| 1359 | submit_request(ep, req); |
| 1360 | |
| 1361 | if (req->last_transaction) { |
| 1362 | usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY); |
| 1363 | usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE); |
| 1364 | } |
| 1365 | goto restart; |
| 1366 | } |
| 1367 | if ((epstatus & epctrl) & USBA_TX_COMPLETE) { |
| 1368 | usba_ep_writel(ep, CLR_STA, USBA_TX_COMPLETE); |
| 1369 | |
| 1370 | switch (ep->state) { |
| 1371 | case DATA_STAGE_IN: |
| 1372 | usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY); |
| 1373 | usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); |
| 1374 | ep->state = STATUS_STAGE_OUT; |
| 1375 | break; |
| 1376 | case STATUS_STAGE_ADDR: |
| 1377 | /* Activate our new address */ |
| 1378 | usba_writel(udc, CTRL, (usba_readl(udc, CTRL) |
| 1379 | | USBA_FADDR_EN)); |
| 1380 | usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); |
| 1381 | ep->state = WAIT_FOR_SETUP; |
| 1382 | break; |
| 1383 | case STATUS_STAGE_IN: |
| 1384 | if (req) { |
| 1385 | list_del_init(&req->queue); |
| 1386 | request_complete(ep, req, 0); |
| 1387 | submit_next_request(ep); |
| 1388 | } |
| 1389 | usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); |
| 1390 | ep->state = WAIT_FOR_SETUP; |
| 1391 | break; |
| 1392 | case STATUS_STAGE_TEST: |
| 1393 | usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE); |
| 1394 | ep->state = WAIT_FOR_SETUP; |
| 1395 | if (do_test_mode(udc)) |
| 1396 | set_protocol_stall(udc, ep); |
| 1397 | break; |
| 1398 | default: |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1399 | pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, " |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1400 | "halting endpoint...\n", |
| 1401 | ep->ep.name, ep->state); |
| 1402 | set_protocol_stall(udc, ep); |
| 1403 | break; |
| 1404 | } |
| 1405 | |
| 1406 | goto restart; |
| 1407 | } |
| 1408 | if ((epstatus & epctrl) & USBA_RX_BK_RDY) { |
| 1409 | switch (ep->state) { |
| 1410 | case STATUS_STAGE_OUT: |
| 1411 | usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); |
| 1412 | usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); |
| 1413 | |
| 1414 | if (req) { |
| 1415 | list_del_init(&req->queue); |
| 1416 | request_complete(ep, req, 0); |
| 1417 | } |
| 1418 | ep->state = WAIT_FOR_SETUP; |
| 1419 | break; |
| 1420 | |
| 1421 | case DATA_STAGE_OUT: |
| 1422 | receive_data(ep); |
| 1423 | break; |
| 1424 | |
| 1425 | default: |
| 1426 | usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); |
| 1427 | usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1428 | pr_err("udc: %s: RXRDY: Invalid endpoint state %d, " |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1429 | "halting endpoint...\n", |
| 1430 | ep->ep.name, ep->state); |
| 1431 | set_protocol_stall(udc, ep); |
| 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | goto restart; |
| 1436 | } |
| 1437 | if (epstatus & USBA_RX_SETUP) { |
| 1438 | union { |
| 1439 | struct usb_ctrlrequest crq; |
| 1440 | unsigned long data[2]; |
| 1441 | } crq; |
| 1442 | unsigned int pkt_len; |
| 1443 | int ret; |
| 1444 | |
| 1445 | if (ep->state != WAIT_FOR_SETUP) { |
| 1446 | /* |
| 1447 | * Didn't expect a SETUP packet at this |
| 1448 | * point. Clean up any pending requests (which |
| 1449 | * may be successful). |
| 1450 | */ |
| 1451 | int status = -EPROTO; |
| 1452 | |
| 1453 | /* |
| 1454 | * RXRDY and TXCOMP are dropped when SETUP |
| 1455 | * packets arrive. Just pretend we received |
| 1456 | * the status packet. |
| 1457 | */ |
| 1458 | if (ep->state == STATUS_STAGE_OUT |
| 1459 | || ep->state == STATUS_STAGE_IN) { |
| 1460 | usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY); |
| 1461 | status = 0; |
| 1462 | } |
| 1463 | |
| 1464 | if (req) { |
| 1465 | list_del_init(&req->queue); |
| 1466 | request_complete(ep, req, status); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | pkt_len = USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA)); |
| 1471 | DBG(DBG_HW, "Packet length: %u\n", pkt_len); |
| 1472 | if (pkt_len != sizeof(crq)) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 1473 | pr_warning("udc: Invalid packet length %u " |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1474 | "(expected %zu)\n", pkt_len, sizeof(crq)); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1475 | set_protocol_stall(udc, ep); |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | DBG(DBG_FIFO, "Copying ctrl request from 0x%p:\n", ep->fifo); |
Haavard Skinnemoen | 5d4c270 | 2008-03-04 15:48:37 +0100 | [diff] [blame] | 1480 | memcpy_fromio(crq.data, ep->fifo, sizeof(crq)); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1481 | |
| 1482 | /* Free up one bank in the FIFO so that we can |
| 1483 | * generate or receive a reply right away. */ |
| 1484 | usba_ep_writel(ep, CLR_STA, USBA_RX_SETUP); |
| 1485 | |
| 1486 | /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n", |
| 1487 | ep->state, crq.crq.bRequestType, |
| 1488 | crq.crq.bRequest); */ |
| 1489 | |
| 1490 | if (crq.crq.bRequestType & USB_DIR_IN) { |
| 1491 | /* |
| 1492 | * The USB 2.0 spec states that "if wLength is |
| 1493 | * zero, there is no data transfer phase." |
| 1494 | * However, testusb #14 seems to actually |
| 1495 | * expect a data phase even if wLength = 0... |
| 1496 | */ |
| 1497 | ep->state = DATA_STAGE_IN; |
| 1498 | } else { |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 1499 | if (crq.crq.wLength != cpu_to_le16(0)) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1500 | ep->state = DATA_STAGE_OUT; |
| 1501 | else |
| 1502 | ep->state = STATUS_STAGE_IN; |
| 1503 | } |
| 1504 | |
| 1505 | ret = -1; |
| 1506 | if (ep->index == 0) |
| 1507 | ret = handle_ep0_setup(udc, ep, &crq.crq); |
| 1508 | else { |
| 1509 | spin_unlock(&udc->lock); |
| 1510 | ret = udc->driver->setup(&udc->gadget, &crq.crq); |
| 1511 | spin_lock(&udc->lock); |
| 1512 | } |
| 1513 | |
| 1514 | DBG(DBG_BUS, "req %02x.%02x, length %d, state %d, ret %d\n", |
| 1515 | crq.crq.bRequestType, crq.crq.bRequest, |
| 1516 | le16_to_cpu(crq.crq.wLength), ep->state, ret); |
| 1517 | |
| 1518 | if (ret < 0) { |
| 1519 | /* Let the host know that we failed */ |
| 1520 | set_protocol_stall(udc, ep); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | static void usba_ep_irq(struct usba_udc *udc, struct usba_ep *ep) |
| 1526 | { |
| 1527 | struct usba_request *req; |
| 1528 | u32 epstatus; |
| 1529 | u32 epctrl; |
| 1530 | |
| 1531 | epstatus = usba_ep_readl(ep, STA); |
| 1532 | epctrl = usba_ep_readl(ep, CTL); |
| 1533 | |
| 1534 | DBG(DBG_INT, "%s: interrupt, status: 0x%08x\n", ep->ep.name, epstatus); |
| 1535 | |
| 1536 | while ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) { |
| 1537 | DBG(DBG_BUS, "%s: TX PK ready\n", ep->ep.name); |
| 1538 | |
| 1539 | if (list_empty(&ep->queue)) { |
| 1540 | dev_warn(&udc->pdev->dev, "ep_irq: queue empty\n"); |
| 1541 | usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | req = list_entry(ep->queue.next, struct usba_request, queue); |
| 1546 | |
| 1547 | if (req->using_dma) { |
| 1548 | /* Send a zero-length packet */ |
| 1549 | usba_ep_writel(ep, SET_STA, |
| 1550 | USBA_TX_PK_RDY); |
| 1551 | usba_ep_writel(ep, CTL_DIS, |
| 1552 | USBA_TX_PK_RDY); |
| 1553 | list_del_init(&req->queue); |
| 1554 | submit_next_request(ep); |
| 1555 | request_complete(ep, req, 0); |
| 1556 | } else { |
| 1557 | if (req->submitted) |
| 1558 | next_fifo_transaction(ep, req); |
| 1559 | else |
| 1560 | submit_request(ep, req); |
| 1561 | |
| 1562 | if (req->last_transaction) { |
| 1563 | list_del_init(&req->queue); |
| 1564 | submit_next_request(ep); |
| 1565 | request_complete(ep, req, 0); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | epstatus = usba_ep_readl(ep, STA); |
| 1570 | epctrl = usba_ep_readl(ep, CTL); |
| 1571 | } |
| 1572 | if ((epstatus & epctrl) & USBA_RX_BK_RDY) { |
| 1573 | DBG(DBG_BUS, "%s: RX data ready\n", ep->ep.name); |
| 1574 | receive_data(ep); |
| 1575 | usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | static void usba_dma_irq(struct usba_udc *udc, struct usba_ep *ep) |
| 1580 | { |
| 1581 | struct usba_request *req; |
| 1582 | u32 status, control, pending; |
| 1583 | |
| 1584 | status = usba_dma_readl(ep, STATUS); |
| 1585 | control = usba_dma_readl(ep, CONTROL); |
| 1586 | #ifdef CONFIG_USB_GADGET_DEBUG_FS |
| 1587 | ep->last_dma_status = status; |
| 1588 | #endif |
| 1589 | pending = status & control; |
| 1590 | DBG(DBG_INT | DBG_DMA, "dma irq, s/%#08x, c/%#08x\n", status, control); |
| 1591 | |
| 1592 | if (status & USBA_DMA_CH_EN) { |
| 1593 | dev_err(&udc->pdev->dev, |
| 1594 | "DMA_CH_EN is set after transfer is finished!\n"); |
| 1595 | dev_err(&udc->pdev->dev, |
| 1596 | "status=%#08x, pending=%#08x, control=%#08x\n", |
| 1597 | status, pending, control); |
| 1598 | |
| 1599 | /* |
| 1600 | * try to pretend nothing happened. We might have to |
| 1601 | * do something here... |
| 1602 | */ |
| 1603 | } |
| 1604 | |
| 1605 | if (list_empty(&ep->queue)) |
| 1606 | /* Might happen if a reset comes along at the right moment */ |
| 1607 | return; |
| 1608 | |
| 1609 | if (pending & (USBA_DMA_END_TR_ST | USBA_DMA_END_BUF_ST)) { |
| 1610 | req = list_entry(ep->queue.next, struct usba_request, queue); |
| 1611 | usba_update_req(ep, req, status); |
| 1612 | |
| 1613 | list_del_init(&req->queue); |
| 1614 | submit_next_request(ep); |
| 1615 | request_complete(ep, req, 0); |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | static irqreturn_t usba_udc_irq(int irq, void *devid) |
| 1620 | { |
| 1621 | struct usba_udc *udc = devid; |
| 1622 | u32 status; |
| 1623 | u32 dma_status; |
| 1624 | u32 ep_status; |
| 1625 | |
| 1626 | spin_lock(&udc->lock); |
| 1627 | |
| 1628 | status = usba_readl(udc, INT_STA); |
| 1629 | DBG(DBG_INT, "irq, status=%#08x\n", status); |
| 1630 | |
| 1631 | if (status & USBA_DET_SUSPEND) { |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1632 | toggle_bias(0); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1633 | usba_writel(udc, INT_CLR, USBA_DET_SUSPEND); |
| 1634 | DBG(DBG_BUS, "Suspend detected\n"); |
| 1635 | if (udc->gadget.speed != USB_SPEED_UNKNOWN |
| 1636 | && udc->driver && udc->driver->suspend) { |
| 1637 | spin_unlock(&udc->lock); |
| 1638 | udc->driver->suspend(&udc->gadget); |
| 1639 | spin_lock(&udc->lock); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (status & USBA_WAKE_UP) { |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1644 | toggle_bias(1); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1645 | usba_writel(udc, INT_CLR, USBA_WAKE_UP); |
| 1646 | DBG(DBG_BUS, "Wake Up CPU detected\n"); |
| 1647 | } |
| 1648 | |
| 1649 | if (status & USBA_END_OF_RESUME) { |
| 1650 | usba_writel(udc, INT_CLR, USBA_END_OF_RESUME); |
| 1651 | DBG(DBG_BUS, "Resume detected\n"); |
| 1652 | if (udc->gadget.speed != USB_SPEED_UNKNOWN |
| 1653 | && udc->driver && udc->driver->resume) { |
| 1654 | spin_unlock(&udc->lock); |
| 1655 | udc->driver->resume(&udc->gadget); |
| 1656 | spin_lock(&udc->lock); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | dma_status = USBA_BFEXT(DMA_INT, status); |
| 1661 | if (dma_status) { |
| 1662 | int i; |
| 1663 | |
| 1664 | for (i = 1; i < USBA_NR_ENDPOINTS; i++) |
| 1665 | if (dma_status & (1 << i)) |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 1666 | usba_dma_irq(udc, &udc->usba_ep[i]); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | ep_status = USBA_BFEXT(EPT_INT, status); |
| 1670 | if (ep_status) { |
| 1671 | int i; |
| 1672 | |
| 1673 | for (i = 0; i < USBA_NR_ENDPOINTS; i++) |
| 1674 | if (ep_status & (1 << i)) { |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 1675 | if (ep_is_control(&udc->usba_ep[i])) |
| 1676 | usba_control_irq(udc, &udc->usba_ep[i]); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1677 | else |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 1678 | usba_ep_irq(udc, &udc->usba_ep[i]); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | if (status & USBA_END_OF_RESET) { |
| 1683 | struct usba_ep *ep0; |
| 1684 | |
| 1685 | usba_writel(udc, INT_CLR, USBA_END_OF_RESET); |
| 1686 | reset_all_endpoints(udc); |
| 1687 | |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 1688 | if (udc->gadget.speed != USB_SPEED_UNKNOWN |
| 1689 | && udc->driver->disconnect) { |
| 1690 | udc->gadget.speed = USB_SPEED_UNKNOWN; |
| 1691 | spin_unlock(&udc->lock); |
| 1692 | udc->driver->disconnect(&udc->gadget); |
| 1693 | spin_lock(&udc->lock); |
| 1694 | } |
| 1695 | |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 1696 | if (status & USBA_HIGH_SPEED) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1697 | udc->gadget.speed = USB_SPEED_HIGH; |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 1698 | else |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1699 | udc->gadget.speed = USB_SPEED_FULL; |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 1700 | DBG(DBG_BUS, "%s bus reset detected\n", |
| 1701 | usb_speed_string(udc->gadget.speed)); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1702 | |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 1703 | ep0 = &udc->usba_ep[0]; |
Ido Shayevitz | 978def1 | 2012-03-12 20:25:27 +0200 | [diff] [blame] | 1704 | ep0->ep.desc = &usba_ep0_desc; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1705 | ep0->state = WAIT_FOR_SETUP; |
| 1706 | usba_ep_writel(ep0, CFG, |
| 1707 | (USBA_BF(EPT_SIZE, EP0_EPT_SIZE) |
| 1708 | | USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL) |
| 1709 | | USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE))); |
| 1710 | usba_ep_writel(ep0, CTL_ENB, |
| 1711 | USBA_EPT_ENABLE | USBA_RX_SETUP); |
| 1712 | usba_writel(udc, INT_ENB, |
| 1713 | (usba_readl(udc, INT_ENB) |
| 1714 | | USBA_BF(EPT_INT, 1) |
| 1715 | | USBA_DET_SUSPEND |
| 1716 | | USBA_END_OF_RESUME)); |
| 1717 | |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 1718 | /* |
| 1719 | * Unclear why we hit this irregularly, e.g. in usbtest, |
| 1720 | * but it's clearly harmless... |
| 1721 | */ |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1722 | if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED)) |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 1723 | dev_dbg(&udc->pdev->dev, |
| 1724 | "ODD: EP0 configuration is invalid!\n"); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | spin_unlock(&udc->lock); |
| 1728 | |
| 1729 | return IRQ_HANDLED; |
| 1730 | } |
| 1731 | |
| 1732 | static irqreturn_t usba_vbus_irq(int irq, void *devid) |
| 1733 | { |
| 1734 | struct usba_udc *udc = devid; |
| 1735 | int vbus; |
| 1736 | |
| 1737 | /* debounce */ |
| 1738 | udelay(10); |
| 1739 | |
| 1740 | spin_lock(&udc->lock); |
| 1741 | |
| 1742 | /* May happen if Vbus pin toggles during probe() */ |
| 1743 | if (!udc->driver) |
| 1744 | goto out; |
| 1745 | |
Eirik Aanonsen | 640e95a | 2010-02-05 09:49:25 +0100 | [diff] [blame] | 1746 | vbus = vbus_is_present(udc); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1747 | if (vbus != udc->vbus_prev) { |
| 1748 | if (vbus) { |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1749 | toggle_bias(1); |
| 1750 | usba_writel(udc, CTRL, USBA_ENABLE_MASK); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1751 | usba_writel(udc, INT_ENB, USBA_END_OF_RESET); |
| 1752 | } else { |
| 1753 | udc->gadget.speed = USB_SPEED_UNKNOWN; |
| 1754 | reset_all_endpoints(udc); |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1755 | toggle_bias(0); |
| 1756 | usba_writel(udc, CTRL, USBA_DISABLE_MASK); |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 1757 | if (udc->driver->disconnect) { |
| 1758 | spin_unlock(&udc->lock); |
| 1759 | udc->driver->disconnect(&udc->gadget); |
| 1760 | spin_lock(&udc->lock); |
| 1761 | } |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1762 | } |
| 1763 | udc->vbus_prev = vbus; |
| 1764 | } |
| 1765 | |
| 1766 | out: |
| 1767 | spin_unlock(&udc->lock); |
| 1768 | |
| 1769 | return IRQ_HANDLED; |
| 1770 | } |
| 1771 | |
Sebastian Andrzej Siewior | d809f78 | 2012-02-04 18:55:24 +0100 | [diff] [blame] | 1772 | static int atmel_usba_start(struct usb_gadget *gadget, |
| 1773 | struct usb_gadget_driver *driver) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1774 | { |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 1775 | int ret = 0; |
Sebastian Andrzej Siewior | d809f78 | 2012-02-04 18:55:24 +0100 | [diff] [blame] | 1776 | struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1777 | unsigned long flags; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1778 | |
| 1779 | spin_lock_irqsave(&udc->lock, flags); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1780 | |
Haavard Skinnemoen | 58ed7b9 | 2007-10-11 13:40:30 -0700 | [diff] [blame] | 1781 | udc->devstatus = 1 << USB_DEVICE_SELF_POWERED; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1782 | udc->driver = driver; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1783 | spin_unlock_irqrestore(&udc->lock, flags); |
| 1784 | |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 1785 | ret = clk_prepare_enable(udc->pclk); |
| 1786 | if (ret) |
| 1787 | goto out; |
| 1788 | ret = clk_prepare_enable(udc->hclk); |
| 1789 | if (ret) { |
| 1790 | clk_disable_unprepare(udc->pclk); |
| 1791 | goto out; |
| 1792 | } |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1793 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1794 | DBG(DBG_GADGET, "registered driver `%s'\n", driver->driver.name); |
| 1795 | |
| 1796 | udc->vbus_prev = 0; |
Hans-Christian Egtvedt | 472a678 | 2009-03-24 15:45:20 +0100 | [diff] [blame] | 1797 | if (gpio_is_valid(udc->vbus_pin)) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1798 | enable_irq(gpio_to_irq(udc->vbus_pin)); |
| 1799 | |
| 1800 | /* If Vbus is present, enable the controller and wait for reset */ |
| 1801 | spin_lock_irqsave(&udc->lock, flags); |
| 1802 | if (vbus_is_present(udc) && udc->vbus_prev == 0) { |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1803 | toggle_bias(1); |
| 1804 | usba_writel(udc, CTRL, USBA_ENABLE_MASK); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1805 | usba_writel(udc, INT_ENB, USBA_END_OF_RESET); |
| 1806 | } |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 1807 | |
| 1808 | out: |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1809 | spin_unlock_irqrestore(&udc->lock, flags); |
| 1810 | |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 1811 | return ret; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1812 | } |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1813 | |
Sebastian Andrzej Siewior | d809f78 | 2012-02-04 18:55:24 +0100 | [diff] [blame] | 1814 | static int atmel_usba_stop(struct usb_gadget *gadget, |
| 1815 | struct usb_gadget_driver *driver) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1816 | { |
Sebastian Andrzej Siewior | d809f78 | 2012-02-04 18:55:24 +0100 | [diff] [blame] | 1817 | struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1818 | unsigned long flags; |
| 1819 | |
Hans-Christian Egtvedt | 472a678 | 2009-03-24 15:45:20 +0100 | [diff] [blame] | 1820 | if (gpio_is_valid(udc->vbus_pin)) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1821 | disable_irq(gpio_to_irq(udc->vbus_pin)); |
| 1822 | |
| 1823 | spin_lock_irqsave(&udc->lock, flags); |
| 1824 | udc->gadget.speed = USB_SPEED_UNKNOWN; |
| 1825 | reset_all_endpoints(udc); |
| 1826 | spin_unlock_irqrestore(&udc->lock, flags); |
| 1827 | |
| 1828 | /* This will also disable the DP pullup */ |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 1829 | toggle_bias(0); |
| 1830 | usba_writel(udc, CTRL, USBA_DISABLE_MASK); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1831 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1832 | udc->driver = NULL; |
| 1833 | |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 1834 | clk_disable_unprepare(udc->hclk); |
| 1835 | clk_disable_unprepare(udc->pclk); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1836 | |
| 1837 | DBG(DBG_GADGET, "unregistered driver `%s'\n", driver->driver.name); |
| 1838 | |
| 1839 | return 0; |
| 1840 | } |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1841 | |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 1842 | #ifdef CONFIG_OF |
| 1843 | static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev, |
| 1844 | struct usba_udc *udc) |
| 1845 | { |
| 1846 | u32 val; |
| 1847 | const char *name; |
| 1848 | enum of_gpio_flags flags; |
| 1849 | struct device_node *np = pdev->dev.of_node; |
| 1850 | struct device_node *pp; |
| 1851 | int i, ret; |
| 1852 | struct usba_ep *eps, *ep; |
| 1853 | |
| 1854 | udc->num_ep = 0; |
| 1855 | |
| 1856 | udc->vbus_pin = of_get_named_gpio_flags(np, "atmel,vbus-gpio", 0, |
| 1857 | &flags); |
| 1858 | udc->vbus_pin_inverted = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0; |
| 1859 | |
| 1860 | pp = NULL; |
| 1861 | while ((pp = of_get_next_child(np, pp))) |
| 1862 | udc->num_ep++; |
| 1863 | |
| 1864 | eps = devm_kzalloc(&pdev->dev, sizeof(struct usba_ep) * udc->num_ep, |
| 1865 | GFP_KERNEL); |
| 1866 | if (!eps) |
| 1867 | return ERR_PTR(-ENOMEM); |
| 1868 | |
| 1869 | udc->gadget.ep0 = &eps[0].ep; |
| 1870 | |
| 1871 | INIT_LIST_HEAD(&eps[0].ep.ep_list); |
| 1872 | |
| 1873 | pp = NULL; |
| 1874 | i = 0; |
| 1875 | while ((pp = of_get_next_child(np, pp))) { |
| 1876 | ep = &eps[i]; |
| 1877 | |
| 1878 | ret = of_property_read_u32(pp, "reg", &val); |
| 1879 | if (ret) { |
| 1880 | dev_err(&pdev->dev, "of_probe: reg error(%d)\n", ret); |
| 1881 | goto err; |
| 1882 | } |
| 1883 | ep->index = val; |
| 1884 | |
| 1885 | ret = of_property_read_u32(pp, "atmel,fifo-size", &val); |
| 1886 | if (ret) { |
| 1887 | dev_err(&pdev->dev, "of_probe: fifo-size error(%d)\n", ret); |
| 1888 | goto err; |
| 1889 | } |
| 1890 | ep->fifo_size = val; |
| 1891 | |
| 1892 | ret = of_property_read_u32(pp, "atmel,nb-banks", &val); |
| 1893 | if (ret) { |
| 1894 | dev_err(&pdev->dev, "of_probe: nb-banks error(%d)\n", ret); |
| 1895 | goto err; |
| 1896 | } |
| 1897 | ep->nr_banks = val; |
| 1898 | |
| 1899 | ep->can_dma = of_property_read_bool(pp, "atmel,can-dma"); |
| 1900 | ep->can_isoc = of_property_read_bool(pp, "atmel,can-isoc"); |
| 1901 | |
| 1902 | ret = of_property_read_string(pp, "name", &name); |
| 1903 | ep->ep.name = name; |
| 1904 | |
| 1905 | ep->ep_regs = udc->regs + USBA_EPT_BASE(i); |
| 1906 | ep->dma_regs = udc->regs + USBA_DMA_BASE(i); |
| 1907 | ep->fifo = udc->fifo + USBA_FIFO_BASE(i); |
| 1908 | ep->ep.ops = &usba_ep_ops; |
| 1909 | ep->ep.maxpacket = ep->fifo_size; |
| 1910 | ep->udc = udc; |
| 1911 | INIT_LIST_HEAD(&ep->queue); |
| 1912 | |
| 1913 | if (i) |
| 1914 | list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); |
| 1915 | |
| 1916 | i++; |
| 1917 | } |
| 1918 | |
| 1919 | return eps; |
| 1920 | err: |
| 1921 | return ERR_PTR(ret); |
| 1922 | } |
| 1923 | #else |
| 1924 | static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev, |
| 1925 | struct usba_udc *udc) |
| 1926 | { |
| 1927 | return ERR_PTR(-ENOSYS); |
| 1928 | } |
| 1929 | #endif |
| 1930 | |
| 1931 | static struct usba_ep * usba_udc_pdata(struct platform_device *pdev, |
| 1932 | struct usba_udc *udc) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1933 | { |
| 1934 | struct usba_platform_data *pdata = pdev->dev.platform_data; |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 1935 | struct usba_ep *eps; |
| 1936 | int i; |
| 1937 | |
| 1938 | if (!pdata) |
| 1939 | return ERR_PTR(-ENXIO); |
| 1940 | |
| 1941 | eps = devm_kzalloc(&pdev->dev, sizeof(struct usba_ep) * pdata->num_ep, |
| 1942 | GFP_KERNEL); |
| 1943 | if (!eps) |
| 1944 | return ERR_PTR(-ENOMEM); |
| 1945 | |
| 1946 | udc->gadget.ep0 = &eps[0].ep; |
| 1947 | |
| 1948 | udc->vbus_pin = pdata->vbus_pin; |
| 1949 | udc->vbus_pin_inverted = pdata->vbus_pin_inverted; |
| 1950 | udc->num_ep = pdata->num_ep; |
| 1951 | |
| 1952 | INIT_LIST_HEAD(&eps[0].ep.ep_list); |
| 1953 | |
| 1954 | for (i = 0; i < pdata->num_ep; i++) { |
| 1955 | struct usba_ep *ep = &eps[i]; |
| 1956 | |
| 1957 | ep->ep_regs = udc->regs + USBA_EPT_BASE(i); |
| 1958 | ep->dma_regs = udc->regs + USBA_DMA_BASE(i); |
| 1959 | ep->fifo = udc->fifo + USBA_FIFO_BASE(i); |
| 1960 | ep->ep.ops = &usba_ep_ops; |
| 1961 | ep->ep.name = pdata->ep[i].name; |
| 1962 | ep->fifo_size = ep->ep.maxpacket = pdata->ep[i].fifo_size; |
| 1963 | ep->udc = udc; |
| 1964 | INIT_LIST_HEAD(&ep->queue); |
| 1965 | ep->nr_banks = pdata->ep[i].nr_banks; |
| 1966 | ep->index = pdata->ep[i].index; |
| 1967 | ep->can_dma = pdata->ep[i].can_dma; |
| 1968 | ep->can_isoc = pdata->ep[i].can_isoc; |
| 1969 | |
| 1970 | if (i) |
| 1971 | list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); |
| 1972 | } |
| 1973 | |
| 1974 | return eps; |
| 1975 | } |
| 1976 | |
| 1977 | static int __init usba_udc_probe(struct platform_device *pdev) |
| 1978 | { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1979 | struct resource *regs, *fifo; |
| 1980 | struct clk *pclk, *hclk; |
Jean-Christophe PLAGNIOL-VILLARD | e8f2ea3 | 2013-05-20 22:46:02 +0800 | [diff] [blame] | 1981 | struct usba_udc *udc; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1982 | int irq, ret, i; |
| 1983 | |
Jean-Christophe PLAGNIOL-VILLARD | e8f2ea3 | 2013-05-20 22:46:02 +0800 | [diff] [blame] | 1984 | udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL); |
| 1985 | if (!udc) |
| 1986 | return -ENOMEM; |
| 1987 | |
| 1988 | udc->gadget = usba_gadget_template; |
| 1989 | INIT_LIST_HEAD(&udc->gadget.ep_list); |
| 1990 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1991 | regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID); |
| 1992 | fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID); |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 1993 | if (!regs || !fifo) |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 1994 | return -ENXIO; |
| 1995 | |
| 1996 | irq = platform_get_irq(pdev, 0); |
| 1997 | if (irq < 0) |
| 1998 | return irq; |
| 1999 | |
| 2000 | pclk = clk_get(&pdev->dev, "pclk"); |
| 2001 | if (IS_ERR(pclk)) |
| 2002 | return PTR_ERR(pclk); |
| 2003 | hclk = clk_get(&pdev->dev, "hclk"); |
| 2004 | if (IS_ERR(hclk)) { |
| 2005 | ret = PTR_ERR(hclk); |
| 2006 | goto err_get_hclk; |
| 2007 | } |
| 2008 | |
David Brownell | 4051770 | 2008-05-10 22:46:38 -0700 | [diff] [blame] | 2009 | spin_lock_init(&udc->lock); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2010 | udc->pdev = pdev; |
| 2011 | udc->pclk = pclk; |
| 2012 | udc->hclk = hclk; |
Hans-Christian Egtvedt | 472a678 | 2009-03-24 15:45:20 +0100 | [diff] [blame] | 2013 | udc->vbus_pin = -ENODEV; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2014 | |
| 2015 | ret = -ENOMEM; |
H Hartley Sweeten | 3ba37bd | 2009-12-14 18:04:49 -0500 | [diff] [blame] | 2016 | udc->regs = ioremap(regs->start, resource_size(regs)); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2017 | if (!udc->regs) { |
| 2018 | dev_err(&pdev->dev, "Unable to map I/O memory, aborting.\n"); |
| 2019 | goto err_map_regs; |
| 2020 | } |
| 2021 | dev_info(&pdev->dev, "MMIO registers at 0x%08lx mapped at %p\n", |
| 2022 | (unsigned long)regs->start, udc->regs); |
H Hartley Sweeten | 3ba37bd | 2009-12-14 18:04:49 -0500 | [diff] [blame] | 2023 | udc->fifo = ioremap(fifo->start, resource_size(fifo)); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2024 | if (!udc->fifo) { |
| 2025 | dev_err(&pdev->dev, "Unable to map FIFO, aborting.\n"); |
| 2026 | goto err_map_fifo; |
| 2027 | } |
| 2028 | dev_info(&pdev->dev, "FIFO at 0x%08lx mapped at %p\n", |
| 2029 | (unsigned long)fifo->start, udc->fifo); |
| 2030 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2031 | platform_set_drvdata(pdev, udc); |
| 2032 | |
| 2033 | /* Make sure we start from a clean slate */ |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 2034 | ret = clk_prepare_enable(pclk); |
| 2035 | if (ret) { |
| 2036 | dev_err(&pdev->dev, "Unable to enable pclk, aborting.\n"); |
| 2037 | goto err_clk_enable; |
| 2038 | } |
Stelian Pop | 16a45bc | 2008-04-05 22:25:47 +0200 | [diff] [blame] | 2039 | toggle_bias(0); |
| 2040 | usba_writel(udc, CTRL, USBA_DISABLE_MASK); |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 2041 | clk_disable_unprepare(pclk); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2042 | |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2043 | if (pdev->dev.of_node) |
| 2044 | udc->usba_ep = atmel_udc_of_init(pdev, udc); |
| 2045 | else |
| 2046 | udc->usba_ep = usba_udc_pdata(pdev, udc); |
| 2047 | |
| 2048 | if (IS_ERR(udc->usba_ep)) { |
| 2049 | ret = PTR_ERR(udc->usba_ep); |
Stelian Pop | 8d85531 | 2008-03-05 00:00:00 +0100 | [diff] [blame] | 2050 | goto err_alloc_ep; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | ret = request_irq(irq, usba_udc_irq, 0, "atmel_usba_udc", udc); |
| 2054 | if (ret) { |
| 2055 | dev_err(&pdev->dev, "Cannot request irq %d (error %d)\n", |
| 2056 | irq, ret); |
| 2057 | goto err_request_irq; |
| 2058 | } |
| 2059 | udc->irq = irq; |
| 2060 | |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2061 | if (gpio_is_valid(udc->vbus_pin)) { |
| 2062 | if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) { |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2063 | ret = request_irq(gpio_to_irq(udc->vbus_pin), |
| 2064 | usba_vbus_irq, 0, |
| 2065 | "atmel_usba_udc", udc); |
| 2066 | if (ret) { |
Hans-Christian Egtvedt | 472a678 | 2009-03-24 15:45:20 +0100 | [diff] [blame] | 2067 | udc->vbus_pin = -ENODEV; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2068 | dev_warn(&udc->pdev->dev, |
| 2069 | "failed to request vbus irq; " |
| 2070 | "assuming always on\n"); |
| 2071 | } else { |
| 2072 | disable_irq(gpio_to_irq(udc->vbus_pin)); |
| 2073 | } |
Jean-Christophe PLAGNIOL-VILLARD | 969afff | 2010-09-20 18:31:07 +0200 | [diff] [blame] | 2074 | } else { |
| 2075 | /* gpio_request fail so use -EINVAL for gpio_is_valid */ |
Josh Wu | b488095 | 2010-11-16 11:51:32 +0100 | [diff] [blame] | 2076 | udc->vbus_pin = -EINVAL; |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2080 | ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget); |
| 2081 | if (ret) |
| 2082 | goto err_add_udc; |
| 2083 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2084 | usba_init_debugfs(udc); |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2085 | for (i = 1; i < udc->num_ep; i++) |
| 2086 | usba_ep_init_debugfs(udc, &udc->usba_ep[i]); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2087 | |
| 2088 | return 0; |
| 2089 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2090 | err_add_udc: |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2091 | if (gpio_is_valid(udc->vbus_pin)) |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2092 | free_irq(gpio_to_irq(udc->vbus_pin), udc); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2093 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2094 | free_irq(irq, udc); |
| 2095 | err_request_irq: |
Stelian Pop | 8d85531 | 2008-03-05 00:00:00 +0100 | [diff] [blame] | 2096 | err_alloc_ep: |
Boris BREZILLON | 0d98b9d | 2013-07-18 09:43:08 +0200 | [diff] [blame] | 2097 | err_clk_enable: |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2098 | iounmap(udc->fifo); |
| 2099 | err_map_fifo: |
| 2100 | iounmap(udc->regs); |
| 2101 | err_map_regs: |
| 2102 | clk_put(hclk); |
| 2103 | err_get_hclk: |
| 2104 | clk_put(pclk); |
| 2105 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2106 | return ret; |
| 2107 | } |
| 2108 | |
| 2109 | static int __exit usba_udc_remove(struct platform_device *pdev) |
| 2110 | { |
| 2111 | struct usba_udc *udc; |
| 2112 | int i; |
| 2113 | |
| 2114 | udc = platform_get_drvdata(pdev); |
| 2115 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2116 | usb_del_gadget_udc(&udc->gadget); |
| 2117 | |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2118 | for (i = 1; i < udc->num_ep; i++) |
Jean-Christophe PLAGNIOL-VILLARD | 68522de | 2013-05-20 22:36:23 +0800 | [diff] [blame] | 2119 | usba_ep_cleanup_debugfs(&udc->usba_ep[i]); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2120 | usba_cleanup_debugfs(udc); |
| 2121 | |
Rob Emanuele | 2c8245c | 2010-12-14 19:00:30 +0100 | [diff] [blame] | 2122 | if (gpio_is_valid(udc->vbus_pin)) { |
| 2123 | free_irq(gpio_to_irq(udc->vbus_pin), udc); |
Rob Emanuele | 2c8245c | 2010-12-14 19:00:30 +0100 | [diff] [blame] | 2124 | } |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2125 | |
| 2126 | free_irq(udc->irq, udc); |
| 2127 | iounmap(udc->fifo); |
| 2128 | iounmap(udc->regs); |
| 2129 | clk_put(udc->hclk); |
| 2130 | clk_put(udc->pclk); |
| 2131 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2132 | return 0; |
| 2133 | } |
| 2134 | |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2135 | #if defined(CONFIG_OF) |
| 2136 | static const struct of_device_id atmel_udc_dt_ids[] = { |
| 2137 | { .compatible = "atmel,at91sam9rl-udc" }, |
| 2138 | { /* sentinel */ } |
| 2139 | }; |
| 2140 | |
| 2141 | MODULE_DEVICE_TABLE(of, atmel_udc_dt_ids); |
| 2142 | #endif |
| 2143 | |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2144 | static struct platform_driver udc_driver = { |
| 2145 | .remove = __exit_p(usba_udc_remove), |
| 2146 | .driver = { |
| 2147 | .name = "atmel_usba_udc", |
Kay Sievers | f34c32f | 2008-04-10 21:29:21 -0700 | [diff] [blame] | 2148 | .owner = THIS_MODULE, |
Jean-Christophe PLAGNIOL-VILLARD | 4a3ae93 | 2013-05-03 20:22:57 +0800 | [diff] [blame] | 2149 | .of_match_table = of_match_ptr(atmel_udc_dt_ids), |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2150 | }, |
| 2151 | }; |
| 2152 | |
Fabio Porcedda | 52f7a82 | 2013-01-09 12:15:28 +0100 | [diff] [blame] | 2153 | module_platform_driver_probe(udc_driver, usba_udc_probe); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2154 | |
| 2155 | MODULE_DESCRIPTION("Atmel USBA UDC driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 2156 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 914a3f3 | 2007-10-10 02:29:43 -0700 | [diff] [blame] | 2157 | MODULE_LICENSE("GPL"); |
Kay Sievers | f34c32f | 2008-04-10 21:29:21 -0700 | [diff] [blame] | 2158 | MODULE_ALIAS("platform:atmel_usba_udc"); |