Blame view

drivers/usb/gadget/udc/atmel_usba_udc.c 50.7 KB
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /*
   * Driver for the Atmel USBA high speed USB device controller
   *
   * Copyright (C) 2005-2007 Atmel Corporation
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
  #include <linux/clk.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/io.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
15
  #include <linux/slab.h>
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
16
17
18
19
20
21
  #include <linux/device.h>
  #include <linux/dma-mapping.h>
  #include <linux/list.h>
  #include <linux/platform_device.h>
  #include <linux/usb/ch9.h>
  #include <linux/usb/gadget.h>
8d855317f   Stelian Pop   atmel_usba_udc: m...
22
  #include <linux/usb/atmel_usba_udc.h>
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
23
  #include <linux/delay.h>
bcd2360c1   Jean-Christophe PLAGNIOL-VILLARD   arm: at91: move p...
24
  #include <linux/platform_data/atmel.h>
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
25
26
  #include <linux/of.h>
  #include <linux/of_gpio.h>
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
27
28
  
  #include <asm/gpio.h>
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
29
30
  
  #include "atmel_usba_udc.h"
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #ifdef CONFIG_USB_GADGET_DEBUG_FS
  #include <linux/debugfs.h>
  #include <linux/uaccess.h>
  
  static int queue_dbg_open(struct inode *inode, struct file *file)
  {
  	struct usba_ep *ep = inode->i_private;
  	struct usba_request *req, *req_copy;
  	struct list_head *queue_data;
  
  	queue_data = kmalloc(sizeof(*queue_data), GFP_KERNEL);
  	if (!queue_data)
  		return -ENOMEM;
  	INIT_LIST_HEAD(queue_data);
  
  	spin_lock_irq(&ep->udc->lock);
  	list_for_each_entry(req, &ep->queue, queue) {
4b8e12336   Julia Lawall   USB: gadget: Use ...
48
  		req_copy = kmemdup(req, sizeof(*req_copy), GFP_ATOMIC);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
49
50
  		if (!req_copy)
  			goto fail;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  		list_add_tail(&req_copy->queue, queue_data);
  	}
  	spin_unlock_irq(&ep->udc->lock);
  
  	file->private_data = queue_data;
  	return 0;
  
  fail:
  	spin_unlock_irq(&ep->udc->lock);
  	list_for_each_entry_safe(req, req_copy, queue_data, queue) {
  		list_del(&req->queue);
  		kfree(req);
  	}
  	kfree(queue_data);
  	return -ENOMEM;
  }
  
  /*
   * bbbbbbbb llllllll IZS sssss nnnn FDL
  \0
   *
   * b: buffer address
   * l: buffer length
   * I/i: interrupt/no interrupt
   * Z/z: zero/no zero
   * S/s: short ok/short not ok
   * s: status
   * n: nr_packets
   * F/f: submitted/not submitted to FIFO
   * D/d: using/not using DMA
   * L/l: last transaction/not last transaction
   */
  static ssize_t queue_dbg_read(struct file *file, char __user *buf,
  		size_t nbytes, loff_t *ppos)
  {
  	struct list_head *queue = file->private_data;
  	struct usba_request *req, *tmp_req;
  	size_t len, remaining, actual = 0;
  	char tmpbuf[38];
  
  	if (!access_ok(VERIFY_WRITE, buf, nbytes))
  		return -EFAULT;
496ad9aa8   Al Viro   new helper: file_...
93
  	mutex_lock(&file_inode(file)->i_mutex);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  	list_for_each_entry_safe(req, tmp_req, queue, queue) {
  		len = snprintf(tmpbuf, sizeof(tmpbuf),
  				"%8p %08x %c%c%c %5d %c%c%c
  ",
  				req->req.buf, req->req.length,
  				req->req.no_interrupt ? 'i' : 'I',
  				req->req.zero ? 'Z' : 'z',
  				req->req.short_not_ok ? 's' : 'S',
  				req->req.status,
  				req->submitted ? 'F' : 'f',
  				req->using_dma ? 'D' : 'd',
  				req->last_transaction ? 'L' : 'l');
  		len = min(len, sizeof(tmpbuf));
  		if (len > nbytes)
  			break;
  
  		list_del(&req->queue);
  		kfree(req);
  
  		remaining = __copy_to_user(buf, tmpbuf, len);
  		actual += len - remaining;
  		if (remaining)
  			break;
  
  		nbytes -= len;
  		buf += len;
  	}
496ad9aa8   Al Viro   new helper: file_...
121
  	mutex_unlock(&file_inode(file)->i_mutex);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  
  	return actual;
  }
  
  static int queue_dbg_release(struct inode *inode, struct file *file)
  {
  	struct list_head *queue_data = file->private_data;
  	struct usba_request *req, *tmp_req;
  
  	list_for_each_entry_safe(req, tmp_req, queue_data, queue) {
  		list_del(&req->queue);
  		kfree(req);
  	}
  	kfree(queue_data);
  	return 0;
  }
  
  static int regs_dbg_open(struct inode *inode, struct file *file)
  {
  	struct usba_udc *udc;
  	unsigned int i;
  	u32 *data;
  	int ret = -ENOMEM;
  
  	mutex_lock(&inode->i_mutex);
  	udc = inode->i_private;
  	data = kmalloc(inode->i_size, GFP_KERNEL);
  	if (!data)
  		goto out;
  
  	spin_lock_irq(&udc->lock);
  	for (i = 0; i < inode->i_size / 4; i++)
  		data[i] = __raw_readl(udc->regs + i * 4);
  	spin_unlock_irq(&udc->lock);
  
  	file->private_data = data;
  	ret = 0;
  
  out:
  	mutex_unlock(&inode->i_mutex);
  
  	return ret;
  }
  
  static ssize_t regs_dbg_read(struct file *file, char __user *buf,
  		size_t nbytes, loff_t *ppos)
  {
496ad9aa8   Al Viro   new helper: file_...
169
  	struct inode *inode = file_inode(file);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
170
171
172
173
174
  	int ret;
  
  	mutex_lock(&inode->i_mutex);
  	ret = simple_read_from_buffer(buf, nbytes, ppos,
  			file->private_data,
496ad9aa8   Al Viro   new helper: file_...
175
  			file_inode(file)->i_size);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  	mutex_unlock(&inode->i_mutex);
  
  	return ret;
  }
  
  static int regs_dbg_release(struct inode *inode, struct file *file)
  {
  	kfree(file->private_data);
  	return 0;
  }
  
  const struct file_operations queue_dbg_fops = {
  	.owner		= THIS_MODULE,
  	.open		= queue_dbg_open,
  	.llseek		= no_llseek,
  	.read		= queue_dbg_read,
  	.release	= queue_dbg_release,
  };
  
  const struct file_operations regs_dbg_fops = {
  	.owner		= THIS_MODULE,
  	.open		= regs_dbg_open,
  	.llseek		= generic_file_llseek,
  	.read		= regs_dbg_read,
  	.release	= regs_dbg_release,
  };
  
  static void usba_ep_init_debugfs(struct usba_udc *udc,
  		struct usba_ep *ep)
  {
  	struct dentry *ep_root;
  
  	ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root);
  	if (!ep_root)
  		goto err_root;
  	ep->debugfs_dir = ep_root;
  
  	ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root,
  						ep, &queue_dbg_fops);
  	if (!ep->debugfs_queue)
  		goto err_queue;
  
  	if (ep->can_dma) {
  		ep->debugfs_dma_status
  			= debugfs_create_u32("dma_status", 0400, ep_root,
  					&ep->last_dma_status);
  		if (!ep->debugfs_dma_status)
  			goto err_dma_status;
  	}
  	if (ep_is_control(ep)) {
  		ep->debugfs_state
  			= debugfs_create_u32("state", 0400, ep_root,
  					&ep->state);
  		if (!ep->debugfs_state)
  			goto err_state;
  	}
  
  	return;
  
  err_state:
  	if (ep->can_dma)
  		debugfs_remove(ep->debugfs_dma_status);
  err_dma_status:
  	debugfs_remove(ep->debugfs_queue);
  err_queue:
  	debugfs_remove(ep_root);
  err_root:
  	dev_err(&ep->udc->pdev->dev,
  		"failed to create debugfs directory for %s
  ", ep->ep.name);
  }
  
  static void usba_ep_cleanup_debugfs(struct usba_ep *ep)
  {
  	debugfs_remove(ep->debugfs_queue);
  	debugfs_remove(ep->debugfs_dma_status);
  	debugfs_remove(ep->debugfs_state);
  	debugfs_remove(ep->debugfs_dir);
  	ep->debugfs_dma_status = NULL;
  	ep->debugfs_dir = NULL;
  }
  
  static void usba_init_debugfs(struct usba_udc *udc)
  {
  	struct dentry *root, *regs;
  	struct resource *regs_resource;
  
  	root = debugfs_create_dir(udc->gadget.name, NULL);
  	if (IS_ERR(root) || !root)
  		goto err_root;
  	udc->debugfs_root = root;
  
  	regs = debugfs_create_file("regs", 0400, root, udc, &regs_dbg_fops);
  	if (!regs)
  		goto err_regs;
  
  	regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM,
  				CTRL_IOMEM_ID);
28f65c11f   Joe Perches   treewide: Convert...
274
  	regs->d_inode->i_size = resource_size(regs_resource);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  	udc->debugfs_regs = regs;
  
  	usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0));
  
  	return;
  
  err_regs:
  	debugfs_remove(root);
  err_root:
  	udc->debugfs_root = NULL;
  	dev_err(&udc->pdev->dev, "debugfs is not available
  ");
  }
  
  static void usba_cleanup_debugfs(struct usba_udc *udc)
  {
  	usba_ep_cleanup_debugfs(to_usba_ep(udc->gadget.ep0));
  	debugfs_remove(udc->debugfs_regs);
  	debugfs_remove(udc->debugfs_root);
  	udc->debugfs_regs = NULL;
  	udc->debugfs_root = NULL;
  }
  #else
  static inline void usba_ep_init_debugfs(struct usba_udc *udc,
  					 struct usba_ep *ep)
  {
  
  }
  
  static inline void usba_ep_cleanup_debugfs(struct usba_ep *ep)
  {
  
  }
  
  static inline void usba_init_debugfs(struct usba_udc *udc)
  {
  
  }
  
  static inline void usba_cleanup_debugfs(struct usba_udc *udc)
  {
  
  }
  #endif
  
  static int vbus_is_present(struct usba_udc *udc)
  {
472a6786b   Hans-Christian Egtvedt   atmel-usba-udc: u...
322
  	if (gpio_is_valid(udc->vbus_pin))
640e95abd   Eirik Aanonsen   USB: atmel uaba: ...
323
  		return gpio_get_value(udc->vbus_pin) ^ udc->vbus_pin_inverted;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
324
325
326
327
  
  	/* No Vbus detection: Assume always present */
  	return 1;
  }
e60c65d35   Nicolas Ferre   USB: atmel_usba_u...
328
  #if defined(CONFIG_ARCH_AT91SAM9RL)
16a45bc82   Stelian Pop   atmel_usba_udc: A...
329

2edb90ae4   Boris BREZILLON   ARM: at91: move a...
330
  #include <linux/clk/at91_pmc.h>
16a45bc82   Stelian Pop   atmel_usba_udc: A...
331
332
  
  static void toggle_bias(int is_on)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
333
  {
b55149529   Jean-Christophe PLAGNIOL-VILLARD   ARM: at91/PMC: ma...
334
  	unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
16a45bc82   Stelian Pop   atmel_usba_udc: A...
335
336
  
  	if (is_on)
b55149529   Jean-Christophe PLAGNIOL-VILLARD   ARM: at91/PMC: ma...
337
  		at91_pmc_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
16a45bc82   Stelian Pop   atmel_usba_udc: A...
338
  	else
b55149529   Jean-Christophe PLAGNIOL-VILLARD   ARM: at91/PMC: ma...
339
  		at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
340
  }
e60c65d35   Nicolas Ferre   USB: atmel_usba_u...
341
342
343
344
345
346
347
  #else
  
  static void toggle_bias(int is_on)
  {
  }
  
  #endif /* CONFIG_ARCH_AT91SAM9RL */
16a45bc82   Stelian Pop   atmel_usba_udc: A...
348

914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req)
  {
  	unsigned int transaction_len;
  
  	transaction_len = req->req.length - req->req.actual;
  	req->last_transaction = 1;
  	if (transaction_len > ep->ep.maxpacket) {
  		transaction_len = ep->ep.maxpacket;
  		req->last_transaction = 0;
  	} else if (transaction_len == ep->ep.maxpacket && req->req.zero)
  		req->last_transaction = 0;
  
  	DBG(DBG_QUEUE, "%s: submit_transaction, req %p (length %d)%s
  ",
  		ep->ep.name, req, transaction_len,
  		req->last_transaction ? ", done" : "");
5d4c2707c   Haavard Skinnemoen   atmel_usba: Kill ...
365
  	memcpy_toio(ep->fifo, req->req.buf + req->req.actual, transaction_len);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  	usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
  	req->req.actual += transaction_len;
  }
  
  static void submit_request(struct usba_ep *ep, struct usba_request *req)
  {
  	DBG(DBG_QUEUE, "%s: submit_request: req %p (length %d)
  ",
  		ep->ep.name, req, req->req.length);
  
  	req->req.actual = 0;
  	req->submitted = 1;
  
  	if (req->using_dma) {
  		if (req->req.length == 0) {
  			usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
  			return;
  		}
  
  		if (req->req.zero)
  			usba_ep_writel(ep, CTL_ENB, USBA_SHORT_PACKET);
  		else
  			usba_ep_writel(ep, CTL_DIS, USBA_SHORT_PACKET);
  
  		usba_dma_writel(ep, ADDRESS, req->req.dma);
  		usba_dma_writel(ep, CONTROL, req->ctrl);
  	} else {
  		next_fifo_transaction(ep, req);
  		if (req->last_transaction) {
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
  			usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
  		} else {
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
  			usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
  		}
  	}
  }
  
  static void submit_next_request(struct usba_ep *ep)
  {
  	struct usba_request *req;
  
  	if (list_empty(&ep->queue)) {
  		usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY | USBA_RX_BK_RDY);
  		return;
  	}
  
  	req = list_entry(ep->queue.next, struct usba_request, queue);
  	if (!req->submitted)
  		submit_request(ep, req);
  }
  
  static void send_status(struct usba_udc *udc, struct usba_ep *ep)
  {
  	ep->state = STATUS_STAGE_IN;
  	usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
  	usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
  }
  
  static void receive_data(struct usba_ep *ep)
  {
  	struct usba_udc *udc = ep->udc;
  	struct usba_request *req;
  	unsigned long status;
  	unsigned int bytecount, nr_busy;
  	int is_complete = 0;
  
  	status = usba_ep_readl(ep, STA);
  	nr_busy = USBA_BFEXT(BUSY_BANKS, status);
  
  	DBG(DBG_QUEUE, "receive data: nr_busy=%u
  ", nr_busy);
  
  	while (nr_busy > 0) {
  		if (list_empty(&ep->queue)) {
  			usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
  			break;
  		}
  		req = list_entry(ep->queue.next,
  				 struct usba_request, queue);
  
  		bytecount = USBA_BFEXT(BYTE_COUNT, status);
  
  		if (status & (1 << 31))
  			is_complete = 1;
  		if (req->req.actual + bytecount >= req->req.length) {
  			is_complete = 1;
  			bytecount = req->req.length - req->req.actual;
  		}
5d4c2707c   Haavard Skinnemoen   atmel_usba: Kill ...
455
  		memcpy_fromio(req->req.buf + req->req.actual,
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
456
457
458
459
460
461
462
463
464
465
466
467
  				ep->fifo, bytecount);
  		req->req.actual += bytecount;
  
  		usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
  
  		if (is_complete) {
  			DBG(DBG_QUEUE, "%s: request done
  ", ep->ep.name);
  			req->req.status = 0;
  			list_del_init(&req->queue);
  			usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
  			spin_unlock(&udc->lock);
304f7e5e1   Michal Sojka   usb: gadget: Refa...
468
  			usb_gadget_giveback_request(&ep->ep, &req->req);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  			spin_lock(&udc->lock);
  		}
  
  		status = usba_ep_readl(ep, STA);
  		nr_busy = USBA_BFEXT(BUSY_BANKS, status);
  
  		if (is_complete && ep_is_control(ep)) {
  			send_status(udc, ep);
  			break;
  		}
  	}
  }
  
  static void
  request_complete(struct usba_ep *ep, struct usba_request *req, int status)
  {
  	struct usba_udc *udc = ep->udc;
  
  	WARN_ON(!list_empty(&req->queue));
  
  	if (req->req.status == -EINPROGRESS)
  		req->req.status = status;
1bda9df8d   Felipe Balbi   usb: gadget: atme...
491
492
  	if (req->using_dma)
  		usb_gadget_unmap_request(&udc->gadget, &req->req, ep->is_in);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
493
494
495
496
497
498
499
  
  	DBG(DBG_GADGET | DBG_REQ,
  		"%s: req %p complete: status %d, actual %u
  ",
  		ep->ep.name, req, req->req.status, req->req.actual);
  
  	spin_unlock(&udc->lock);
304f7e5e1   Michal Sojka   usb: gadget: Refa...
500
  	usb_gadget_giveback_request(&ep->ep, &req->req);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
  	spin_lock(&udc->lock);
  }
  
  static void
  request_complete_list(struct usba_ep *ep, struct list_head *list, int status)
  {
  	struct usba_request *req, *tmp_req;
  
  	list_for_each_entry_safe(req, tmp_req, list, queue) {
  		list_del_init(&req->queue);
  		request_complete(ep, req, status);
  	}
  }
  
  static int
  usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  {
  	struct usba_ep *ep = to_usba_ep(_ep);
  	struct usba_udc *udc = ep->udc;
  	unsigned long flags, ept_cfg, maxpacket;
  	unsigned int nr_trans;
  
  	DBG(DBG_GADGET, "%s: ep_enable: desc=%p
  ", ep->ep.name, desc);
29cc88979   Kuninori Morimoto   USB: use usb_endp...
525
  	maxpacket = usb_endpoint_maxp(desc) & 0x7ff;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  
  	if (((desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) != ep->index)
  			|| ep->index == 0
  			|| desc->bDescriptorType != USB_DT_ENDPOINT
  			|| maxpacket == 0
  			|| maxpacket > ep->fifo_size) {
  		DBG(DBG_ERR, "ep_enable: Invalid argument");
  		return -EINVAL;
  	}
  
  	ep->is_isoc = 0;
  	ep->is_in = 0;
  
  	if (maxpacket <= 8)
  		ept_cfg = USBA_BF(EPT_SIZE, USBA_EPT_SIZE_8);
  	else
  		/* LSB is bit 1, not 0 */
  		ept_cfg = USBA_BF(EPT_SIZE, fls(maxpacket - 1) - 3);
  
  	DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)
  ",
  			ep->ep.name, ept_cfg, maxpacket);
71de6b63f   Matthias Kaehlcke   USB: atmel_usba_u...
548
  	if (usb_endpoint_dir_in(desc)) {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
549
550
551
  		ep->is_in = 1;
  		ept_cfg |= USBA_EPT_DIR_IN;
  	}
71de6b63f   Matthias Kaehlcke   USB: atmel_usba_u...
552
  	switch (usb_endpoint_type(desc)) {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  	case USB_ENDPOINT_XFER_CONTROL:
  		ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL);
  		ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE);
  		break;
  	case USB_ENDPOINT_XFER_ISOC:
  		if (!ep->can_isoc) {
  			DBG(DBG_ERR, "ep_enable: %s is not isoc capable
  ",
  					ep->ep.name);
  			return -EINVAL;
  		}
  
  		/*
  		 * Bits 11:12 specify number of _additional_
  		 * transactions per microframe.
  		 */
29cc88979   Kuninori Morimoto   USB: use usb_endp...
569
  		nr_trans = ((usb_endpoint_maxp(desc) >> 11) & 3) + 1;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  		if (nr_trans > 3)
  			return -EINVAL;
  
  		ep->is_isoc = 1;
  		ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_ISO);
  
  		/*
  		 * Do triple-buffering on high-bandwidth iso endpoints.
  		 */
  		if (nr_trans > 1 && ep->nr_banks == 3)
  			ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_TRIPLE);
  		else
  			ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
  		ept_cfg |= USBA_BF(NB_TRANS, nr_trans);
  		break;
  	case USB_ENDPOINT_XFER_BULK:
  		ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK);
  		ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
  		break;
  	case USB_ENDPOINT_XFER_INT:
  		ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_INT);
  		ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_DOUBLE);
  		break;
  	}
  
  	spin_lock_irqsave(&ep->udc->lock, flags);
978def1c4   Ido Shayevitz   usb: gadget: Upda...
596
  	ep->ep.desc = desc;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  	ep->ep.maxpacket = maxpacket;
  
  	usba_ep_writel(ep, CFG, ept_cfg);
  	usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
  
  	if (ep->can_dma) {
  		u32 ctrl;
  
  		usba_writel(udc, INT_ENB,
  				(usba_readl(udc, INT_ENB)
  					| USBA_BF(EPT_INT, 1 << ep->index)
  					| USBA_BF(DMA_INT, 1 << ep->index)));
  		ctrl = USBA_AUTO_VALID | USBA_INTDIS_DMA;
  		usba_ep_writel(ep, CTL_ENB, ctrl);
  	} else {
  		usba_writel(udc, INT_ENB,
  				(usba_readl(udc, INT_ENB)
  					| USBA_BF(EPT_INT, 1 << ep->index)));
  	}
  
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	DBG(DBG_HW, "EPT_CFG%d after init: %#08lx
  ", ep->index,
  			(unsigned long)usba_ep_readl(ep, CFG));
  	DBG(DBG_HW, "INT_ENB after init: %#08lx
  ",
  			(unsigned long)usba_readl(udc, INT_ENB));
  
  	return 0;
  }
  
  static int usba_ep_disable(struct usb_ep *_ep)
  {
  	struct usba_ep *ep = to_usba_ep(_ep);
  	struct usba_udc *udc = ep->udc;
  	LIST_HEAD(req_list);
  	unsigned long flags;
  
  	DBG(DBG_GADGET, "ep_disable: %s
  ", ep->ep.name);
  
  	spin_lock_irqsave(&udc->lock, flags);
978def1c4   Ido Shayevitz   usb: gadget: Upda...
640
  	if (!ep->ep.desc) {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
641
  		spin_unlock_irqrestore(&udc->lock, flags);
405177070   David Brownell   USB: atmel_usba_u...
642
643
644
645
646
647
648
649
  		/* REVISIT because this driver disables endpoints in
  		 * reset_all_endpoints() before calling disconnect(),
  		 * most gadget drivers would trigger this non-error ...
  		 */
  		if (udc->gadget.speed != USB_SPEED_UNKNOWN)
  			DBG(DBG_ERR, "ep_disable: %s not enabled
  ",
  					ep->ep.name);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
650
651
  		return -EINVAL;
  	}
f9c56cdd3   Ido Shayevitz   usb: gadget: Clea...
652
  	ep->ep.desc = NULL;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  
  	list_splice_init(&ep->queue, &req_list);
  	if (ep->can_dma) {
  		usba_dma_writel(ep, CONTROL, 0);
  		usba_dma_writel(ep, ADDRESS, 0);
  		usba_dma_readl(ep, STATUS);
  	}
  	usba_ep_writel(ep, CTL_DIS, USBA_EPT_ENABLE);
  	usba_writel(udc, INT_ENB,
  			usba_readl(udc, INT_ENB)
  			& ~USBA_BF(EPT_INT, 1 << ep->index));
  
  	request_complete_list(ep, &req_list, -ESHUTDOWN);
  
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return 0;
  }
  
  static struct usb_request *
  usba_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  {
  	struct usba_request *req;
  
  	DBG(DBG_GADGET, "ep_alloc_request: %p, 0x%x
  ", _ep, gfp_flags);
  
  	req = kzalloc(sizeof(*req), gfp_flags);
  	if (!req)
  		return NULL;
  
  	INIT_LIST_HEAD(&req->queue);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  
  	return &req->req;
  }
  
  static void
  usba_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  {
  	struct usba_request *req = to_usba_req(_req);
  
  	DBG(DBG_GADGET, "ep_free_request: %p, %p
  ", _ep, _req);
  
  	kfree(req);
  }
  
  static int queue_dma(struct usba_udc *udc, struct usba_ep *ep,
  		struct usba_request *req, gfp_t gfp_flags)
  {
  	unsigned long flags;
  	int ret;
  
  	DBG(DBG_DMA, "%s: req l/%u d/%08x %c%c%c
  ",
  		ep->ep.name, req->req.length, req->req.dma,
  		req->req.zero ? 'Z' : 'z',
  		req->req.short_not_ok ? 'S' : 's',
  		req->req.no_interrupt ? 'I' : 'i');
  
  	if (req->req.length > 0x10000) {
  		/* Lengths from 0 to 65536 (inclusive) are supported */
  		DBG(DBG_ERR, "invalid request length %u
  ", req->req.length);
  		return -EINVAL;
  	}
1bda9df8d   Felipe Balbi   usb: gadget: atme...
719
720
721
  	ret = usb_gadget_map_request(&udc->gadget, &req->req, ep->is_in);
  	if (ret)
  		return ret;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
722

1bda9df8d   Felipe Balbi   usb: gadget: atme...
723
  	req->using_dma = 1;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
724
725
  	req->ctrl = USBA_BF(DMA_BUF_LEN, req->req.length)
  			| USBA_DMA_CH_EN | USBA_DMA_END_BUF_IE
c81118845   Bo Shen   usb: gadget: udc:...
726
  			| USBA_DMA_END_BUF_EN;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
727

c81118845   Bo Shen   usb: gadget: udc:...
728
729
  	if (!ep->is_in)
  		req->ctrl |= USBA_DMA_END_TR_EN | USBA_DMA_END_TR_IE;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
730
731
732
733
734
735
736
737
  
  	/*
  	 * Add this request to the queue and submit for DMA if
  	 * possible. Check if we're still alive first -- we may have
  	 * received a reset since last time we checked.
  	 */
  	ret = -ESHUTDOWN;
  	spin_lock_irqsave(&udc->lock, flags);
978def1c4   Ido Shayevitz   usb: gadget: Upda...
738
  	if (ep->ep.desc) {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
  		if (list_empty(&ep->queue))
  			submit_request(ep, req);
  
  		list_add_tail(&req->queue, &ep->queue);
  		ret = 0;
  	}
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return ret;
  }
  
  static int
  usba_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  {
  	struct usba_request *req = to_usba_req(_req);
  	struct usba_ep *ep = to_usba_ep(_ep);
  	struct usba_udc *udc = ep->udc;
  	unsigned long flags;
  	int ret;
  
  	DBG(DBG_GADGET | DBG_QUEUE | DBG_REQ, "%s: queue req %p, len %u
  ",
  			ep->ep.name, req, _req->length);
978def1c4   Ido Shayevitz   usb: gadget: Upda...
762
763
  	if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN ||
  	    !ep->ep.desc)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  		return -ESHUTDOWN;
  
  	req->submitted = 0;
  	req->using_dma = 0;
  	req->last_transaction = 0;
  
  	_req->status = -EINPROGRESS;
  	_req->actual = 0;
  
  	if (ep->can_dma)
  		return queue_dma(udc, ep, req, gfp_flags);
  
  	/* May have received a reset since last time we checked */
  	ret = -ESHUTDOWN;
  	spin_lock_irqsave(&udc->lock, flags);
978def1c4   Ido Shayevitz   usb: gadget: Upda...
779
  	if (ep->ep.desc) {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
780
  		list_add_tail(&req->queue, &ep->queue);
f42706c90   Martin Fuzzey   USB: atmel-usba-u...
781
782
  		if ((!ep_is_control(ep) && ep->is_in) ||
  			(ep_is_control(ep)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  				&& (ep->state == DATA_STAGE_IN
  					|| ep->state == STATUS_STAGE_IN)))
  			usba_ep_writel(ep, CTL_ENB, USBA_TX_PK_RDY);
  		else
  			usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY);
  		ret = 0;
  	}
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return ret;
  }
  
  static void
  usba_update_req(struct usba_ep *ep, struct usba_request *req, u32 status)
  {
  	req->req.actual = req->req.length - USBA_BFEXT(DMA_BUF_LEN, status);
  }
  
  static int stop_dma(struct usba_ep *ep, u32 *pstatus)
  {
  	unsigned int timeout;
  	u32 status;
  
  	/*
  	 * Stop the DMA controller. When writing both CH_EN
  	 * and LINK to 0, the other bits are not affected.
  	 */
  	usba_dma_writel(ep, CONTROL, 0);
  
  	/* Wait for the FIFO to empty */
  	for (timeout = 40; timeout; --timeout) {
  		status = usba_dma_readl(ep, STATUS);
  		if (!(status & USBA_DMA_CH_EN))
  			break;
  		udelay(1);
  	}
  
  	if (pstatus)
  		*pstatus = status;
  
  	if (timeout == 0) {
  		dev_err(&ep->udc->pdev->dev,
  			"%s: timed out waiting for DMA FIFO to empty
  ",
  			ep->ep.name);
  		return -ETIMEDOUT;
  	}
  
  	return 0;
  }
  
  static int usba_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  {
  	struct usba_ep *ep = to_usba_ep(_ep);
  	struct usba_udc *udc = ep->udc;
  	struct usba_request *req = to_usba_req(_req);
  	unsigned long flags;
  	u32 status;
  
  	DBG(DBG_GADGET | DBG_QUEUE, "ep_dequeue: %s, req %p
  ",
  			ep->ep.name, req);
  
  	spin_lock_irqsave(&udc->lock, flags);
  
  	if (req->using_dma) {
  		/*
  		 * If this request is currently being transferred,
  		 * stop the DMA controller and reset the FIFO.
  		 */
  		if (ep->queue.next == &req->queue) {
  			status = usba_dma_readl(ep, STATUS);
  			if (status & USBA_DMA_CH_EN)
  				stop_dma(ep, &status);
  
  #ifdef CONFIG_USB_GADGET_DEBUG_FS
  			ep->last_dma_status = status;
  #endif
  
  			usba_writel(udc, EPT_RST, 1 << ep->index);
  
  			usba_update_req(ep, req, status);
  		}
  	}
  
  	/*
  	 * Errors should stop the queue from advancing until the
  	 * completion function returns.
  	 */
  	list_del_init(&req->queue);
  
  	request_complete(ep, req, -ECONNRESET);
  
  	/* Process the next request if any */
  	submit_next_request(ep);
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return 0;
  }
  
  static int usba_ep_set_halt(struct usb_ep *_ep, int value)
  {
  	struct usba_ep *ep = to_usba_ep(_ep);
  	struct usba_udc *udc = ep->udc;
  	unsigned long flags;
  	int ret = 0;
  
  	DBG(DBG_GADGET, "endpoint %s: %s HALT
  ", ep->ep.name,
  			value ? "set" : "clear");
978def1c4   Ido Shayevitz   usb: gadget: Upda...
893
  	if (!ep->ep.desc) {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
  		DBG(DBG_ERR, "Attempted to halt uninitialized ep %s
  ",
  				ep->ep.name);
  		return -ENODEV;
  	}
  	if (ep->is_isoc) {
  		DBG(DBG_ERR, "Attempted to halt isochronous ep %s
  ",
  				ep->ep.name);
  		return -ENOTTY;
  	}
  
  	spin_lock_irqsave(&udc->lock, flags);
  
  	/*
  	 * We can't halt IN endpoints while there are still data to be
  	 * transferred
  	 */
  	if (!list_empty(&ep->queue)
  			|| ((value && ep->is_in && (usba_ep_readl(ep, STA)
  					& USBA_BF(BUSY_BANKS, -1L))))) {
  		ret = -EAGAIN;
  	} else {
  		if (value)
  			usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL);
  		else
  			usba_ep_writel(ep, CLR_STA,
  					USBA_FORCE_STALL | USBA_TOGGLE_CLR);
  		usba_ep_readl(ep, STA);
  	}
  
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return ret;
  }
  
  static int usba_ep_fifo_status(struct usb_ep *_ep)
  {
  	struct usba_ep *ep = to_usba_ep(_ep);
  
  	return USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA));
  }
  
  static void usba_ep_fifo_flush(struct usb_ep *_ep)
  {
  	struct usba_ep *ep = to_usba_ep(_ep);
  	struct usba_udc *udc = ep->udc;
  
  	usba_writel(udc, EPT_RST, 1 << ep->index);
  }
  
  static const struct usb_ep_ops usba_ep_ops = {
  	.enable		= usba_ep_enable,
  	.disable	= usba_ep_disable,
  	.alloc_request	= usba_ep_alloc_request,
  	.free_request	= usba_ep_free_request,
  	.queue		= usba_ep_queue,
  	.dequeue	= usba_ep_dequeue,
  	.set_halt	= usba_ep_set_halt,
  	.fifo_status	= usba_ep_fifo_status,
  	.fifo_flush	= usba_ep_fifo_flush,
  };
  
  static int usba_udc_get_frame(struct usb_gadget *gadget)
  {
  	struct usba_udc *udc = to_usba_udc(gadget);
  
  	return USBA_BFEXT(FRAME_NUMBER, usba_readl(udc, FNUM));
  }
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
  static int usba_udc_wakeup(struct usb_gadget *gadget)
  {
  	struct usba_udc *udc = to_usba_udc(gadget);
  	unsigned long flags;
  	u32 ctrl;
  	int ret = -EINVAL;
  
  	spin_lock_irqsave(&udc->lock, flags);
  	if (udc->devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
  		ctrl = usba_readl(udc, CTRL);
  		usba_writel(udc, CTRL, ctrl | USBA_REMOTE_WAKE_UP);
  		ret = 0;
  	}
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return ret;
  }
  
  static int
  usba_udc_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
  {
  	struct usba_udc *udc = to_usba_udc(gadget);
  	unsigned long flags;
  
  	spin_lock_irqsave(&udc->lock, flags);
  	if (is_selfpowered)
  		udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
  	else
  		udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	return 0;
  }
d809f78f8   Sebastian Andrzej Siewior   usb: gadget: atme...
996
997
998
999
  static int atmel_usba_start(struct usb_gadget *gadget,
  		struct usb_gadget_driver *driver);
  static int atmel_usba_stop(struct usb_gadget *gadget,
  		struct usb_gadget_driver *driver);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1000
  static const struct usb_gadget_ops usba_udc_ops = {
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1001
1002
1003
  	.get_frame		= usba_udc_get_frame,
  	.wakeup			= usba_udc_wakeup,
  	.set_selfpowered	= usba_udc_set_selfpowered,
d809f78f8   Sebastian Andrzej Siewior   usb: gadget: atme...
1004
1005
  	.udc_start		= atmel_usba_start,
  	.udc_stop		= atmel_usba_stop,
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1006
  };
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1007
1008
1009
1010
1011
  static struct usb_endpoint_descriptor usba_ep0_desc = {
  	.bLength = USB_DT_ENDPOINT_SIZE,
  	.bDescriptorType = USB_DT_ENDPOINT,
  	.bEndpointAddress = 0,
  	.bmAttributes = USB_ENDPOINT_XFER_CONTROL,
551509d26   Harvey Harrison   USB: replace uses...
1012
  	.wMaxPacketSize = cpu_to_le16(64),
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1013
1014
1015
1016
1017
1018
1019
1020
  	/* FIXME: I have no idea what to put here */
  	.bInterval = 1,
  };
  
  static void nop_release(struct device *dev)
  {
  
  }
5056ee83e   Jingoo Han   usb: gadget: atme...
1021
  static struct usb_gadget usba_gadget_template = {
e8f2ea393   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1022
1023
1024
1025
1026
1027
  	.ops		= &usba_udc_ops,
  	.max_speed	= USB_SPEED_HIGH,
  	.name		= "atmel_usba_udc",
  	.dev	= {
  		.init_name	= "gadget",
  		.release	= nop_release,
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1028
  	},
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
  };
  
  /*
   * Called with interrupts disabled and udc->lock held.
   */
  static void reset_all_endpoints(struct usba_udc *udc)
  {
  	struct usba_ep *ep;
  	struct usba_request *req, *tmp_req;
  
  	usba_writel(udc, EPT_RST, ~0UL);
  
  	ep = to_usba_ep(udc->gadget.ep0);
  	list_for_each_entry_safe(req, tmp_req, &ep->queue, queue) {
  		list_del_init(&req->queue);
  		request_complete(ep, req, -ECONNRESET);
  	}
405177070   David Brownell   USB: atmel_usba_u...
1046
1047
1048
1049
1050
1051
  	/* NOTE:  normally, the next call to the gadget driver is in
  	 * charge of disabling endpoints... usually disconnect().
  	 * The exception would be entering a high speed test mode.
  	 *
  	 * FIXME remove this code ... and retest thoroughly.
  	 */
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1052
  	list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
978def1c4   Ido Shayevitz   usb: gadget: Upda...
1053
  		if (ep->ep.desc) {
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1054
  			spin_unlock(&udc->lock);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1055
  			usba_ep_disable(&ep->ep);
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1056
1057
  			spin_lock(&udc->lock);
  		}
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
  	}
  }
  
  static struct usba_ep *get_ep_by_addr(struct usba_udc *udc, u16 wIndex)
  {
  	struct usba_ep *ep;
  
  	if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
  		return to_usba_ep(udc->gadget.ep0);
  
  	list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  		u8 bEndpointAddress;
978def1c4   Ido Shayevitz   usb: gadget: Upda...
1070
  		if (!ep->ep.desc)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1071
  			continue;
978def1c4   Ido Shayevitz   usb: gadget: Upda...
1072
  		bEndpointAddress = ep->ep.desc->bEndpointAddress;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
  		if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
  			continue;
  		if ((bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
  				== (wIndex & USB_ENDPOINT_NUMBER_MASK))
  			return ep;
  	}
  
  	return NULL;
  }
  
  /* Called with interrupts disabled and udc->lock held */
  static inline void set_protocol_stall(struct usba_udc *udc, struct usba_ep *ep)
  {
  	usba_ep_writel(ep, SET_STA, USBA_FORCE_STALL);
  	ep->state = WAIT_FOR_SETUP;
  }
  
  static inline int is_stalled(struct usba_udc *udc, struct usba_ep *ep)
  {
  	if (usba_ep_readl(ep, STA) & USBA_FORCE_STALL)
  		return 1;
  	return 0;
  }
  
  static inline void set_address(struct usba_udc *udc, unsigned int addr)
  {
  	u32 regval;
  
  	DBG(DBG_BUS, "setting address %u...
  ", addr);
  	regval = usba_readl(udc, CTRL);
  	regval = USBA_BFINS(DEV_ADDR, addr, regval);
  	usba_writel(udc, CTRL, regval);
  }
  
  static int do_test_mode(struct usba_udc *udc)
  {
  	static const char test_packet_buffer[] = {
  		/* JKJKJKJK * 9 */
  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  		/* JJKKJJKK * 8 */
  		0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  		/* JJKKJJKK * 8 */
  		0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  		/* JJJJJJJKKKKKKK * 8 */
  		0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  		/* JJJJJJJK * 8 */
  		0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
  		/* {JKKKKKKK * 10}, JK */
  		0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
  	};
  	struct usba_ep *ep;
  	struct device *dev = &udc->pdev->dev;
  	int test_mode;
  
  	test_mode = udc->test_mode;
  
  	/* Start from a clean slate */
  	reset_all_endpoints(udc);
  
  	switch (test_mode) {
  	case 0x0100:
  		/* Test_J */
  		usba_writel(udc, TST, USBA_TST_J_MODE);
  		dev_info(dev, "Entering Test_J mode...
  ");
  		break;
  	case 0x0200:
  		/* Test_K */
  		usba_writel(udc, TST, USBA_TST_K_MODE);
  		dev_info(dev, "Entering Test_K mode...
  ");
  		break;
  	case 0x0300:
  		/*
  		 * Test_SE0_NAK: Force high-speed mode and set up ep0
  		 * for Bulk IN transfers
  		 */
68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1152
  		ep = &udc->usba_ep[0];
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
  		usba_writel(udc, TST,
  				USBA_BF(SPEED_CFG, USBA_SPEED_CFG_FORCE_HIGH));
  		usba_ep_writel(ep, CFG,
  				USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64)
  				| USBA_EPT_DIR_IN
  				| USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK)
  				| USBA_BF(BK_NUMBER, 1));
  		if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) {
  			set_protocol_stall(udc, ep);
  			dev_err(dev, "Test_SE0_NAK: ep0 not mapped
  ");
  		} else {
  			usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
  			dev_info(dev, "Entering Test_SE0_NAK mode...
  ");
  		}
  		break;
  	case 0x0400:
  		/* Test_Packet */
68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1172
  		ep = &udc->usba_ep[0];
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
  		usba_ep_writel(ep, CFG,
  				USBA_BF(EPT_SIZE, USBA_EPT_SIZE_64)
  				| USBA_EPT_DIR_IN
  				| USBA_BF(EPT_TYPE, USBA_EPT_TYPE_BULK)
  				| USBA_BF(BK_NUMBER, 1));
  		if (!(usba_ep_readl(ep, CFG) & USBA_EPT_MAPPED)) {
  			set_protocol_stall(udc, ep);
  			dev_err(dev, "Test_Packet: ep0 not mapped
  ");
  		} else {
  			usba_ep_writel(ep, CTL_ENB, USBA_EPT_ENABLE);
  			usba_writel(udc, TST, USBA_TST_PKT_MODE);
5d4c2707c   Haavard Skinnemoen   atmel_usba: Kill ...
1185
  			memcpy_toio(ep->fifo, test_packet_buffer,
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
  					sizeof(test_packet_buffer));
  			usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
  			dev_info(dev, "Entering Test_Packet mode...
  ");
  		}
  		break;
  	default:
  		dev_err(dev, "Invalid test mode: 0x%04x
  ", test_mode);
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  /* Avoid overly long expressions */
  static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest *crq)
  {
551509d26   Harvey Harrison   USB: replace uses...
1204
  	if (crq->wValue == cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1205
1206
1207
1208
1209
1210
  		return true;
  	return false;
  }
  
  static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest *crq)
  {
551509d26   Harvey Harrison   USB: replace uses...
1211
  	if (crq->wValue == cpu_to_le16(USB_DEVICE_TEST_MODE))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1212
1213
1214
1215
1216
1217
  		return true;
  	return false;
  }
  
  static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq)
  {
551509d26   Harvey Harrison   USB: replace uses...
1218
  	if (crq->wValue == cpu_to_le16(USB_ENDPOINT_HALT))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1219
1220
1221
1222
1223
1224
1225
  		return true;
  	return false;
  }
  
  static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
  		struct usb_ctrlrequest *crq)
  {
405177070   David Brownell   USB: atmel_usba_u...
1226
  	int retval = 0;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1227
1228
1229
1230
1231
1232
  
  	switch (crq->bRequest) {
  	case USB_REQ_GET_STATUS: {
  		u16 status;
  
  		if (crq->bRequestType == (USB_DIR_IN | USB_RECIP_DEVICE)) {
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1233
  			status = cpu_to_le16(udc->devstatus);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1234
1235
  		} else if (crq->bRequestType
  				== (USB_DIR_IN | USB_RECIP_INTERFACE)) {
551509d26   Harvey Harrison   USB: replace uses...
1236
  			status = cpu_to_le16(0);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
  		} else if (crq->bRequestType
  				== (USB_DIR_IN | USB_RECIP_ENDPOINT)) {
  			struct usba_ep *target;
  
  			target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
  			if (!target)
  				goto stall;
  
  			status = 0;
  			if (is_stalled(udc, target))
551509d26   Harvey Harrison   USB: replace uses...
1247
  				status |= cpu_to_le16(1);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1248
1249
1250
1251
  		} else
  			goto delegate;
  
  		/* Write directly to the FIFO. No queueing is done. */
551509d26   Harvey Harrison   USB: replace uses...
1252
  		if (crq->wLength != cpu_to_le16(sizeof(status)))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1253
1254
1255
1256
1257
1258
1259
1260
1261
  			goto stall;
  		ep->state = DATA_STAGE_IN;
  		__raw_writew(status, ep->fifo);
  		usba_ep_writel(ep, SET_STA, USBA_TX_PK_RDY);
  		break;
  	}
  
  	case USB_REQ_CLEAR_FEATURE: {
  		if (crq->bRequestType == USB_RECIP_DEVICE) {
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1262
1263
1264
1265
  			if (feature_is_dev_remote_wakeup(crq))
  				udc->devstatus
  					&= ~(1 << USB_DEVICE_REMOTE_WAKEUP);
  			else
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1266
1267
  				/* Can't CLEAR_FEATURE TEST_MODE */
  				goto stall;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1268
1269
  		} else if (crq->bRequestType == USB_RECIP_ENDPOINT) {
  			struct usba_ep *target;
551509d26   Harvey Harrison   USB: replace uses...
1270
  			if (crq->wLength != cpu_to_le16(0)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
  					|| !feature_is_ep_halt(crq))
  				goto stall;
  			target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
  			if (!target)
  				goto stall;
  
  			usba_ep_writel(target, CLR_STA, USBA_FORCE_STALL);
  			if (target->index != 0)
  				usba_ep_writel(target, CLR_STA,
  						USBA_TOGGLE_CLR);
  		} else {
  			goto delegate;
  		}
  
  		send_status(udc, ep);
  		break;
  	}
  
  	case USB_REQ_SET_FEATURE: {
  		if (crq->bRequestType == USB_RECIP_DEVICE) {
  			if (feature_is_dev_test_mode(crq)) {
  				send_status(udc, ep);
  				ep->state = STATUS_STAGE_TEST;
  				udc->test_mode = le16_to_cpu(crq->wIndex);
  				return 0;
  			} else if (feature_is_dev_remote_wakeup(crq)) {
58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1297
  				udc->devstatus |= 1 << USB_DEVICE_REMOTE_WAKEUP;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1298
1299
1300
1301
1302
  			} else {
  				goto stall;
  			}
  		} else if (crq->bRequestType == USB_RECIP_ENDPOINT) {
  			struct usba_ep *target;
551509d26   Harvey Harrison   USB: replace uses...
1303
  			if (crq->wLength != cpu_to_le16(0)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
  					|| !feature_is_ep_halt(crq))
  				goto stall;
  
  			target = get_ep_by_addr(udc, le16_to_cpu(crq->wIndex));
  			if (!target)
  				goto stall;
  
  			usba_ep_writel(target, SET_STA, USBA_FORCE_STALL);
  		} else
  			goto delegate;
  
  		send_status(udc, ep);
  		break;
  	}
  
  	case USB_REQ_SET_ADDRESS:
  		if (crq->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
  			goto delegate;
  
  		set_address(udc, le16_to_cpu(crq->wValue));
  		send_status(udc, ep);
  		ep->state = STATUS_STAGE_ADDR;
  		break;
  
  	default:
  delegate:
  		spin_unlock(&udc->lock);
  		retval = udc->driver->setup(&udc->gadget, crq);
  		spin_lock(&udc->lock);
  	}
  
  	return retval;
  
  stall:
00274921a   David Brownell   USB: gadget code ...
1338
  	pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
  		"halting endpoint...
  ",
  		ep->ep.name, crq->bRequestType, crq->bRequest,
  		le16_to_cpu(crq->wValue), le16_to_cpu(crq->wIndex),
  		le16_to_cpu(crq->wLength));
  	set_protocol_stall(udc, ep);
  	return -1;
  }
  
  static void usba_control_irq(struct usba_udc *udc, struct usba_ep *ep)
  {
  	struct usba_request *req;
  	u32 epstatus;
  	u32 epctrl;
  
  restart:
  	epstatus = usba_ep_readl(ep, STA);
  	epctrl = usba_ep_readl(ep, CTL);
  
  	DBG(DBG_INT, "%s [%d]: s/%08x c/%08x
  ",
  			ep->ep.name, ep->state, epstatus, epctrl);
  
  	req = NULL;
  	if (!list_empty(&ep->queue))
  		req = list_entry(ep->queue.next,
  				 struct usba_request, queue);
  
  	if ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
  		if (req->submitted)
  			next_fifo_transaction(ep, req);
  		else
  			submit_request(ep, req);
  
  		if (req->last_transaction) {
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
  			usba_ep_writel(ep, CTL_ENB, USBA_TX_COMPLETE);
  		}
  		goto restart;
  	}
  	if ((epstatus & epctrl) & USBA_TX_COMPLETE) {
  		usba_ep_writel(ep, CLR_STA, USBA_TX_COMPLETE);
  
  		switch (ep->state) {
  		case DATA_STAGE_IN:
  			usba_ep_writel(ep, CTL_ENB, USBA_RX_BK_RDY);
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
  			ep->state = STATUS_STAGE_OUT;
  			break;
  		case STATUS_STAGE_ADDR:
  			/* Activate our new address */
  			usba_writel(udc, CTRL, (usba_readl(udc, CTRL)
  						| USBA_FADDR_EN));
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
  			ep->state = WAIT_FOR_SETUP;
  			break;
  		case STATUS_STAGE_IN:
  			if (req) {
  				list_del_init(&req->queue);
  				request_complete(ep, req, 0);
  				submit_next_request(ep);
  			}
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
  			ep->state = WAIT_FOR_SETUP;
  			break;
  		case STATUS_STAGE_TEST:
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_COMPLETE);
  			ep->state = WAIT_FOR_SETUP;
  			if (do_test_mode(udc))
  				set_protocol_stall(udc, ep);
  			break;
  		default:
00274921a   David Brownell   USB: gadget code ...
1411
  			pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
  				"halting endpoint...
  ",
  				ep->ep.name, ep->state);
  			set_protocol_stall(udc, ep);
  			break;
  		}
  
  		goto restart;
  	}
  	if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
  		switch (ep->state) {
  		case STATUS_STAGE_OUT:
  			usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
  			usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
  
  			if (req) {
  				list_del_init(&req->queue);
  				request_complete(ep, req, 0);
  			}
  			ep->state = WAIT_FOR_SETUP;
  			break;
  
  		case DATA_STAGE_OUT:
  			receive_data(ep);
  			break;
  
  		default:
  			usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
  			usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
00274921a   David Brownell   USB: gadget code ...
1441
  			pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
  				"halting endpoint...
  ",
  				ep->ep.name, ep->state);
  			set_protocol_stall(udc, ep);
  			break;
  		}
  
  		goto restart;
  	}
  	if (epstatus & USBA_RX_SETUP) {
  		union {
  			struct usb_ctrlrequest crq;
  			unsigned long data[2];
  		} crq;
  		unsigned int pkt_len;
  		int ret;
  
  		if (ep->state != WAIT_FOR_SETUP) {
  			/*
  			 * Didn't expect a SETUP packet at this
  			 * point. Clean up any pending requests (which
  			 * may be successful).
  			 */
  			int status = -EPROTO;
  
  			/*
  			 * RXRDY and TXCOMP are dropped when SETUP
  			 * packets arrive.  Just pretend we received
  			 * the status packet.
  			 */
  			if (ep->state == STATUS_STAGE_OUT
  					|| ep->state == STATUS_STAGE_IN) {
  				usba_ep_writel(ep, CTL_DIS, USBA_RX_BK_RDY);
  				status = 0;
  			}
  
  			if (req) {
  				list_del_init(&req->queue);
  				request_complete(ep, req, status);
  			}
  		}
  
  		pkt_len = USBA_BFEXT(BYTE_COUNT, usba_ep_readl(ep, STA));
  		DBG(DBG_HW, "Packet length: %u
  ", pkt_len);
  		if (pkt_len != sizeof(crq)) {
00274921a   David Brownell   USB: gadget code ...
1488
  			pr_warning("udc: Invalid packet length %u "
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1489
1490
  				"(expected %zu)
  ", pkt_len, sizeof(crq));
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1491
1492
1493
1494
1495
1496
  			set_protocol_stall(udc, ep);
  			return;
  		}
  
  		DBG(DBG_FIFO, "Copying ctrl request from 0x%p:
  ", ep->fifo);
5d4c2707c   Haavard Skinnemoen   atmel_usba: Kill ...
1497
  		memcpy_fromio(crq.data, ep->fifo, sizeof(crq));
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
  
  		/* Free up one bank in the FIFO so that we can
  		 * generate or receive a reply right away. */
  		usba_ep_writel(ep, CLR_STA, USBA_RX_SETUP);
  
  		/* printk(KERN_DEBUG "setup: %d: %02x.%02x
  ",
  			ep->state, crq.crq.bRequestType,
  			crq.crq.bRequest); */
  
  		if (crq.crq.bRequestType & USB_DIR_IN) {
  			/*
  			 * The USB 2.0 spec states that "if wLength is
  			 * zero, there is no data transfer phase."
  			 * However, testusb #14 seems to actually
  			 * expect a data phase even if wLength = 0...
  			 */
  			ep->state = DATA_STAGE_IN;
  		} else {
551509d26   Harvey Harrison   USB: replace uses...
1517
  			if (crq.crq.wLength != cpu_to_le16(0))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
  				ep->state = DATA_STAGE_OUT;
  			else
  				ep->state = STATUS_STAGE_IN;
  		}
  
  		ret = -1;
  		if (ep->index == 0)
  			ret = handle_ep0_setup(udc, ep, &crq.crq);
  		else {
  			spin_unlock(&udc->lock);
  			ret = udc->driver->setup(&udc->gadget, &crq.crq);
  			spin_lock(&udc->lock);
  		}
  
  		DBG(DBG_BUS, "req %02x.%02x, length %d, state %d, ret %d
  ",
  			crq.crq.bRequestType, crq.crq.bRequest,
  			le16_to_cpu(crq.crq.wLength), ep->state, ret);
  
  		if (ret < 0) {
  			/* Let the host know that we failed */
  			set_protocol_stall(udc, ep);
  		}
  	}
  }
  
  static void usba_ep_irq(struct usba_udc *udc, struct usba_ep *ep)
  {
  	struct usba_request *req;
  	u32 epstatus;
  	u32 epctrl;
  
  	epstatus = usba_ep_readl(ep, STA);
  	epctrl = usba_ep_readl(ep, CTL);
  
  	DBG(DBG_INT, "%s: interrupt, status: 0x%08x
  ", ep->ep.name, epstatus);
  
  	while ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
  		DBG(DBG_BUS, "%s: TX PK ready
  ", ep->ep.name);
  
  		if (list_empty(&ep->queue)) {
  			dev_warn(&udc->pdev->dev, "ep_irq: queue empty
  ");
  			usba_ep_writel(ep, CTL_DIS, USBA_TX_PK_RDY);
  			return;
  		}
  
  		req = list_entry(ep->queue.next, struct usba_request, queue);
  
  		if (req->using_dma) {
  			/* Send a zero-length packet */
  			usba_ep_writel(ep, SET_STA,
  					USBA_TX_PK_RDY);
  			usba_ep_writel(ep, CTL_DIS,
  					USBA_TX_PK_RDY);
  			list_del_init(&req->queue);
  			submit_next_request(ep);
  			request_complete(ep, req, 0);
  		} else {
  			if (req->submitted)
  				next_fifo_transaction(ep, req);
  			else
  				submit_request(ep, req);
  
  			if (req->last_transaction) {
  				list_del_init(&req->queue);
  				submit_next_request(ep);
  				request_complete(ep, req, 0);
  			}
  		}
  
  		epstatus = usba_ep_readl(ep, STA);
  		epctrl = usba_ep_readl(ep, CTL);
  	}
  	if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
  		DBG(DBG_BUS, "%s: RX data ready
  ", ep->ep.name);
  		receive_data(ep);
  		usba_ep_writel(ep, CLR_STA, USBA_RX_BK_RDY);
  	}
  }
  
  static void usba_dma_irq(struct usba_udc *udc, struct usba_ep *ep)
  {
  	struct usba_request *req;
  	u32 status, control, pending;
  
  	status = usba_dma_readl(ep, STATUS);
  	control = usba_dma_readl(ep, CONTROL);
  #ifdef CONFIG_USB_GADGET_DEBUG_FS
  	ep->last_dma_status = status;
  #endif
  	pending = status & control;
  	DBG(DBG_INT | DBG_DMA, "dma irq, s/%#08x, c/%#08x
  ", status, control);
  
  	if (status & USBA_DMA_CH_EN) {
  		dev_err(&udc->pdev->dev,
  			"DMA_CH_EN is set after transfer is finished!
  ");
  		dev_err(&udc->pdev->dev,
  			"status=%#08x, pending=%#08x, control=%#08x
  ",
  			status, pending, control);
  
  		/*
  		 * try to pretend nothing happened. We might have to
  		 * do something here...
  		 */
  	}
  
  	if (list_empty(&ep->queue))
  		/* Might happen if a reset comes along at the right moment */
  		return;
  
  	if (pending & (USBA_DMA_END_TR_ST | USBA_DMA_END_BUF_ST)) {
  		req = list_entry(ep->queue.next, struct usba_request, queue);
  		usba_update_req(ep, req, status);
  
  		list_del_init(&req->queue);
  		submit_next_request(ep);
  		request_complete(ep, req, 0);
  	}
  }
  
  static irqreturn_t usba_udc_irq(int irq, void *devid)
  {
  	struct usba_udc *udc = devid;
  	u32 status;
  	u32 dma_status;
  	u32 ep_status;
  
  	spin_lock(&udc->lock);
  
  	status = usba_readl(udc, INT_STA);
  	DBG(DBG_INT, "irq, status=%#08x
  ", status);
  
  	if (status & USBA_DET_SUSPEND) {
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1659
  		toggle_bias(0);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
  		usba_writel(udc, INT_CLR, USBA_DET_SUSPEND);
  		DBG(DBG_BUS, "Suspend detected
  ");
  		if (udc->gadget.speed != USB_SPEED_UNKNOWN
  				&& udc->driver && udc->driver->suspend) {
  			spin_unlock(&udc->lock);
  			udc->driver->suspend(&udc->gadget);
  			spin_lock(&udc->lock);
  		}
  	}
  
  	if (status & USBA_WAKE_UP) {
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1672
  		toggle_bias(1);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
  		usba_writel(udc, INT_CLR, USBA_WAKE_UP);
  		DBG(DBG_BUS, "Wake Up CPU detected
  ");
  	}
  
  	if (status & USBA_END_OF_RESUME) {
  		usba_writel(udc, INT_CLR, USBA_END_OF_RESUME);
  		DBG(DBG_BUS, "Resume detected
  ");
  		if (udc->gadget.speed != USB_SPEED_UNKNOWN
  				&& udc->driver && udc->driver->resume) {
  			spin_unlock(&udc->lock);
  			udc->driver->resume(&udc->gadget);
  			spin_lock(&udc->lock);
  		}
  	}
  
  	dma_status = USBA_BFEXT(DMA_INT, status);
  	if (dma_status) {
  		int i;
bcabdc24d   Bo Shen   usb: atmel_usba_u...
1693
  		for (i = 1; i <= USBA_NR_DMAS; i++)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1694
  			if (dma_status & (1 << i))
68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1695
  				usba_dma_irq(udc, &udc->usba_ep[i]);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1696
1697
1698
1699
1700
  	}
  
  	ep_status = USBA_BFEXT(EPT_INT, status);
  	if (ep_status) {
  		int i;
aa7be0f8f   Bo Shen   usb: gadget: at91...
1701
  		for (i = 0; i < udc->num_ep; i++)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1702
  			if (ep_status & (1 << i)) {
68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1703
1704
  				if (ep_is_control(&udc->usba_ep[i]))
  					usba_control_irq(udc, &udc->usba_ep[i]);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1705
  				else
68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1706
  					usba_ep_irq(udc, &udc->usba_ep[i]);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1707
1708
1709
1710
1711
1712
1713
1714
  			}
  	}
  
  	if (status & USBA_END_OF_RESET) {
  		struct usba_ep *ep0;
  
  		usba_writel(udc, INT_CLR, USBA_END_OF_RESET);
  		reset_all_endpoints(udc);
405177070   David Brownell   USB: atmel_usba_u...
1715
  		if (udc->gadget.speed != USB_SPEED_UNKNOWN
bcdbc084e   Alexandre Belloni   usb: gadget: atme...
1716
  				&& udc->driver && udc->driver->disconnect) {
405177070   David Brownell   USB: atmel_usba_u...
1717
1718
1719
1720
1721
  			udc->gadget.speed = USB_SPEED_UNKNOWN;
  			spin_unlock(&udc->lock);
  			udc->driver->disconnect(&udc->gadget);
  			spin_lock(&udc->lock);
  		}
e538dfdae   Michal Nazarewicz   usb: Provide usb_...
1722
  		if (status & USBA_HIGH_SPEED)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1723
  			udc->gadget.speed = USB_SPEED_HIGH;
e538dfdae   Michal Nazarewicz   usb: Provide usb_...
1724
  		else
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1725
  			udc->gadget.speed = USB_SPEED_FULL;
e538dfdae   Michal Nazarewicz   usb: Provide usb_...
1726
1727
1728
  		DBG(DBG_BUS, "%s bus reset detected
  ",
  		    usb_speed_string(udc->gadget.speed));
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1729

68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1730
  		ep0 = &udc->usba_ep[0];
978def1c4   Ido Shayevitz   usb: gadget: Upda...
1731
  		ep0->ep.desc = &usba_ep0_desc;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
  		ep0->state = WAIT_FOR_SETUP;
  		usba_ep_writel(ep0, CFG,
  				(USBA_BF(EPT_SIZE, EP0_EPT_SIZE)
  				| USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL)
  				| USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE)));
  		usba_ep_writel(ep0, CTL_ENB,
  				USBA_EPT_ENABLE | USBA_RX_SETUP);
  		usba_writel(udc, INT_ENB,
  				(usba_readl(udc, INT_ENB)
  				| USBA_BF(EPT_INT, 1)
  				| USBA_DET_SUSPEND
  				| USBA_END_OF_RESUME));
405177070   David Brownell   USB: atmel_usba_u...
1744
1745
1746
1747
  		/*
  		 * Unclear why we hit this irregularly, e.g. in usbtest,
  		 * but it's clearly harmless...
  		 */
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1748
  		if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED))
405177070   David Brownell   USB: atmel_usba_u...
1749
1750
1751
  			dev_dbg(&udc->pdev->dev,
  				 "ODD: EP0 configuration is invalid!
  ");
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
  	}
  
  	spin_unlock(&udc->lock);
  
  	return IRQ_HANDLED;
  }
  
  static irqreturn_t usba_vbus_irq(int irq, void *devid)
  {
  	struct usba_udc *udc = devid;
  	int vbus;
  
  	/* debounce */
  	udelay(10);
  
  	spin_lock(&udc->lock);
  
  	/* May happen if Vbus pin toggles during probe() */
  	if (!udc->driver)
  		goto out;
640e95abd   Eirik Aanonsen   USB: atmel uaba: ...
1772
  	vbus = vbus_is_present(udc);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1773
1774
  	if (vbus != udc->vbus_prev) {
  		if (vbus) {
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1775
1776
  			toggle_bias(1);
  			usba_writel(udc, CTRL, USBA_ENABLE_MASK);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1777
1778
1779
1780
  			usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
  		} else {
  			udc->gadget.speed = USB_SPEED_UNKNOWN;
  			reset_all_endpoints(udc);
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1781
1782
  			toggle_bias(0);
  			usba_writel(udc, CTRL, USBA_DISABLE_MASK);
405177070   David Brownell   USB: atmel_usba_u...
1783
1784
1785
1786
1787
  			if (udc->driver->disconnect) {
  				spin_unlock(&udc->lock);
  				udc->driver->disconnect(&udc->gadget);
  				spin_lock(&udc->lock);
  			}
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1788
1789
1790
1791
1792
1793
1794
1795
1796
  		}
  		udc->vbus_prev = vbus;
  	}
  
  out:
  	spin_unlock(&udc->lock);
  
  	return IRQ_HANDLED;
  }
d809f78f8   Sebastian Andrzej Siewior   usb: gadget: atme...
1797
1798
  static int atmel_usba_start(struct usb_gadget *gadget,
  		struct usb_gadget_driver *driver)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1799
  {
ffb62a14c   Dan Carpenter   usb: gadget: doub...
1800
  	int ret;
d809f78f8   Sebastian Andrzej Siewior   usb: gadget: atme...
1801
  	struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1802
  	unsigned long flags;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1803
1804
  
  	spin_lock_irqsave(&udc->lock, flags);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1805

58ed7b94d   Haavard Skinnemoen   atmel_usba_udc: K...
1806
  	udc->devstatus = 1 << USB_DEVICE_SELF_POWERED;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1807
  	udc->driver = driver;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1808
  	spin_unlock_irqrestore(&udc->lock, flags);
0d98b9d64   Boris BREZILLON   usb: gadget: atme...
1809
1810
  	ret = clk_prepare_enable(udc->pclk);
  	if (ret)
ffb62a14c   Dan Carpenter   usb: gadget: doub...
1811
  		return ret;
0d98b9d64   Boris BREZILLON   usb: gadget: atme...
1812
1813
1814
  	ret = clk_prepare_enable(udc->hclk);
  	if (ret) {
  		clk_disable_unprepare(udc->pclk);
ffb62a14c   Dan Carpenter   usb: gadget: doub...
1815
  		return ret;
0d98b9d64   Boris BREZILLON   usb: gadget: atme...
1816
  	}
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1817

914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1818
1819
1820
1821
  	DBG(DBG_GADGET, "registered driver `%s'
  ", driver->driver.name);
  
  	udc->vbus_prev = 0;
472a6786b   Hans-Christian Egtvedt   atmel-usba-udc: u...
1822
  	if (gpio_is_valid(udc->vbus_pin))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1823
1824
1825
1826
1827
  		enable_irq(gpio_to_irq(udc->vbus_pin));
  
  	/* If Vbus is present, enable the controller and wait for reset */
  	spin_lock_irqsave(&udc->lock, flags);
  	if (vbus_is_present(udc) && udc->vbus_prev == 0) {
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1828
1829
  		toggle_bias(1);
  		usba_writel(udc, CTRL, USBA_ENABLE_MASK);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1830
1831
1832
  		usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
  	}
  	spin_unlock_irqrestore(&udc->lock, flags);
ffb62a14c   Dan Carpenter   usb: gadget: doub...
1833
  	return 0;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1834
  }
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1835

d809f78f8   Sebastian Andrzej Siewior   usb: gadget: atme...
1836
1837
  static int atmel_usba_stop(struct usb_gadget *gadget,
  		struct usb_gadget_driver *driver)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1838
  {
d809f78f8   Sebastian Andrzej Siewior   usb: gadget: atme...
1839
  	struct usba_udc *udc = container_of(gadget, struct usba_udc, gadget);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1840
  	unsigned long flags;
472a6786b   Hans-Christian Egtvedt   atmel-usba-udc: u...
1841
  	if (gpio_is_valid(udc->vbus_pin))
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1842
1843
1844
1845
1846
1847
1848
1849
  		disable_irq(gpio_to_irq(udc->vbus_pin));
  
  	spin_lock_irqsave(&udc->lock, flags);
  	udc->gadget.speed = USB_SPEED_UNKNOWN;
  	reset_all_endpoints(udc);
  	spin_unlock_irqrestore(&udc->lock, flags);
  
  	/* This will also disable the DP pullup */
16a45bc82   Stelian Pop   atmel_usba_udc: A...
1850
1851
  	toggle_bias(0);
  	usba_writel(udc, CTRL, USBA_DISABLE_MASK);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1852

0d98b9d64   Boris BREZILLON   usb: gadget: atme...
1853
1854
  	clk_disable_unprepare(udc->hclk);
  	clk_disable_unprepare(udc->pclk);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1855

d8eb6c653   Gregory CLEMENT   usb: gadget: atme...
1856
1857
1858
1859
  	DBG(DBG_GADGET, "unregistered driver `%s'
  ", udc->driver->driver.name);
  
  	udc->driver = NULL;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1860
1861
1862
  
  	return 0;
  }
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1863

4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
  #ifdef CONFIG_OF
  static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
  						    struct usba_udc *udc)
  {
  	u32 val;
  	const char *name;
  	enum of_gpio_flags flags;
  	struct device_node *np = pdev->dev.of_node;
  	struct device_node *pp;
  	int i, ret;
  	struct usba_ep *eps, *ep;
  
  	udc->num_ep = 0;
  
  	udc->vbus_pin = of_get_named_gpio_flags(np, "atmel,vbus-gpio", 0,
  						&flags);
  	udc->vbus_pin_inverted = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
  
  	pp = NULL;
  	while ((pp = of_get_next_child(np, pp)))
  		udc->num_ep++;
  
  	eps = devm_kzalloc(&pdev->dev, sizeof(struct usba_ep) * udc->num_ep,
  			   GFP_KERNEL);
  	if (!eps)
  		return ERR_PTR(-ENOMEM);
  
  	udc->gadget.ep0 = &eps[0].ep;
  
  	INIT_LIST_HEAD(&eps[0].ep.ep_list);
  
  	pp = NULL;
  	i = 0;
  	while ((pp = of_get_next_child(np, pp))) {
  		ep = &eps[i];
  
  		ret = of_property_read_u32(pp, "reg", &val);
  		if (ret) {
  			dev_err(&pdev->dev, "of_probe: reg error(%d)
  ", ret);
  			goto err;
  		}
  		ep->index = val;
  
  		ret = of_property_read_u32(pp, "atmel,fifo-size", &val);
  		if (ret) {
  			dev_err(&pdev->dev, "of_probe: fifo-size error(%d)
  ", ret);
  			goto err;
  		}
  		ep->fifo_size = val;
  
  		ret = of_property_read_u32(pp, "atmel,nb-banks", &val);
  		if (ret) {
  			dev_err(&pdev->dev, "of_probe: nb-banks error(%d)
  ", ret);
  			goto err;
  		}
  		ep->nr_banks = val;
  
  		ep->can_dma = of_property_read_bool(pp, "atmel,can-dma");
  		ep->can_isoc = of_property_read_bool(pp, "atmel,can-isoc");
  
  		ret = of_property_read_string(pp, "name", &name);
  		ep->ep.name = name;
  
  		ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
  		ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
  		ep->fifo = udc->fifo + USBA_FIFO_BASE(i);
  		ep->ep.ops = &usba_ep_ops;
e117e742d   Robert Baldyga   usb: gadget: add ...
1934
  		usb_ep_set_maxpacket_limit(&ep->ep, ep->fifo_size);
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1935
1936
1937
1938
1939
1940
1941
1942
  		ep->udc = udc;
  		INIT_LIST_HEAD(&ep->queue);
  
  		if (i)
  			list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  
  		i++;
  	}
fb0e139d9   Alexandre Belloni   usb: gadget: atme...
1943
1944
1945
1946
1947
1948
  	if (i == 0) {
  		dev_err(&pdev->dev, "of_probe: no endpoint specified
  ");
  		ret = -EINVAL;
  		goto err;
  	}
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
  	return eps;
  err:
  	return ERR_PTR(ret);
  }
  #else
  static struct usba_ep * atmel_udc_of_init(struct platform_device *pdev,
  						    struct usba_udc *udc)
  {
  	return ERR_PTR(-ENOSYS);
  }
  #endif
  
  static struct usba_ep * usba_udc_pdata(struct platform_device *pdev,
  						 struct usba_udc *udc)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
1963
  {
e01ee9f50   Jingoo Han   usb: gadget: use ...
1964
  	struct usba_platform_data *pdata = dev_get_platdata(&pdev->dev);
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
  	struct usba_ep *eps;
  	int i;
  
  	if (!pdata)
  		return ERR_PTR(-ENXIO);
  
  	eps = devm_kzalloc(&pdev->dev, sizeof(struct usba_ep) * pdata->num_ep,
  			   GFP_KERNEL);
  	if (!eps)
  		return ERR_PTR(-ENOMEM);
  
  	udc->gadget.ep0 = &eps[0].ep;
  
  	udc->vbus_pin = pdata->vbus_pin;
  	udc->vbus_pin_inverted = pdata->vbus_pin_inverted;
  	udc->num_ep = pdata->num_ep;
  
  	INIT_LIST_HEAD(&eps[0].ep.ep_list);
  
  	for (i = 0; i < pdata->num_ep; i++) {
  		struct usba_ep *ep = &eps[i];
  
  		ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
  		ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
  		ep->fifo = udc->fifo + USBA_FIFO_BASE(i);
  		ep->ep.ops = &usba_ep_ops;
  		ep->ep.name = pdata->ep[i].name;
e117e742d   Robert Baldyga   usb: gadget: add ...
1992
1993
  		ep->fifo_size = pdata->ep[i].fifo_size;
  		usb_ep_set_maxpacket_limit(&ep->ep, ep->fifo_size);
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
  		ep->udc = udc;
  		INIT_LIST_HEAD(&ep->queue);
  		ep->nr_banks = pdata->ep[i].nr_banks;
  		ep->index = pdata->ep[i].index;
  		ep->can_dma = pdata->ep[i].can_dma;
  		ep->can_isoc = pdata->ep[i].can_isoc;
  
  		if (i)
  			list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  	}
  
  	return eps;
  }
03d6a9c9a   Peter Chen   usb: gadget: atme...
2007
  static int usba_udc_probe(struct platform_device *pdev)
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2008
  {
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2009
2010
  	struct resource *regs, *fifo;
  	struct clk *pclk, *hclk;
e8f2ea393   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2011
  	struct usba_udc *udc;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2012
  	int irq, ret, i;
e8f2ea393   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2013
2014
2015
2016
2017
2018
  	udc = devm_kzalloc(&pdev->dev, sizeof(*udc), GFP_KERNEL);
  	if (!udc)
  		return -ENOMEM;
  
  	udc->gadget = usba_gadget_template;
  	INIT_LIST_HEAD(&udc->gadget.ep_list);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2019
2020
  	regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID);
  	fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID);
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2021
  	if (!regs || !fifo)
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2022
2023
2024
2025
2026
  		return -ENXIO;
  
  	irq = platform_get_irq(pdev, 0);
  	if (irq < 0)
  		return irq;
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2027
  	pclk = devm_clk_get(&pdev->dev, "pclk");
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2028
2029
  	if (IS_ERR(pclk))
  		return PTR_ERR(pclk);
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2030
2031
2032
  	hclk = devm_clk_get(&pdev->dev, "hclk");
  	if (IS_ERR(hclk))
  		return PTR_ERR(hclk);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2033

405177070   David Brownell   USB: atmel_usba_u...
2034
  	spin_lock_init(&udc->lock);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2035
2036
2037
  	udc->pdev = pdev;
  	udc->pclk = pclk;
  	udc->hclk = hclk;
472a6786b   Hans-Christian Egtvedt   atmel-usba-udc: u...
2038
  	udc->vbus_pin = -ENODEV;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2039
2040
  
  	ret = -ENOMEM;
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2041
  	udc->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2042
2043
2044
  	if (!udc->regs) {
  		dev_err(&pdev->dev, "Unable to map I/O memory, aborting.
  ");
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2045
  		return ret;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2046
2047
2048
2049
  	}
  	dev_info(&pdev->dev, "MMIO registers at 0x%08lx mapped at %p
  ",
  		 (unsigned long)regs->start, udc->regs);
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2050
  	udc->fifo = devm_ioremap(&pdev->dev, fifo->start, resource_size(fifo));
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2051
2052
2053
  	if (!udc->fifo) {
  		dev_err(&pdev->dev, "Unable to map FIFO, aborting.
  ");
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2054
  		return ret;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2055
2056
2057
2058
  	}
  	dev_info(&pdev->dev, "FIFO at 0x%08lx mapped at %p
  ",
  		 (unsigned long)fifo->start, udc->fifo);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2059
2060
2061
  	platform_set_drvdata(pdev, udc);
  
  	/* Make sure we start from a clean slate */
0d98b9d64   Boris BREZILLON   usb: gadget: atme...
2062
2063
2064
2065
  	ret = clk_prepare_enable(pclk);
  	if (ret) {
  		dev_err(&pdev->dev, "Unable to enable pclk, aborting.
  ");
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2066
  		return ret;
0d98b9d64   Boris BREZILLON   usb: gadget: atme...
2067
  	}
16a45bc82   Stelian Pop   atmel_usba_udc: A...
2068
2069
  	toggle_bias(0);
  	usba_writel(udc, CTRL, USBA_DISABLE_MASK);
0d98b9d64   Boris BREZILLON   usb: gadget: atme...
2070
  	clk_disable_unprepare(pclk);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2071

4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2072
2073
2074
2075
  	if (pdev->dev.of_node)
  		udc->usba_ep = atmel_udc_of_init(pdev, udc);
  	else
  		udc->usba_ep = usba_udc_pdata(pdev, udc);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2076

40a8fb2a6   Jingoo Han   usb: gadget: atme...
2077
2078
  	if (IS_ERR(udc->usba_ep))
  		return PTR_ERR(udc->usba_ep);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2079

40a8fb2a6   Jingoo Han   usb: gadget: atme...
2080
2081
  	ret = devm_request_irq(&pdev->dev, irq, usba_udc_irq, 0,
  				"atmel_usba_udc", udc);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2082
2083
2084
2085
  	if (ret) {
  		dev_err(&pdev->dev, "Cannot request irq %d (error %d)
  ",
  			irq, ret);
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2086
  		return ret;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2087
2088
  	}
  	udc->irq = irq;
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2089
2090
  	if (gpio_is_valid(udc->vbus_pin)) {
  		if (!devm_gpio_request(&pdev->dev, udc->vbus_pin, "atmel_usba_udc")) {
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2091
2092
  			ret = devm_request_irq(&pdev->dev,
  					gpio_to_irq(udc->vbus_pin),
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2093
2094
2095
  					usba_vbus_irq, 0,
  					"atmel_usba_udc", udc);
  			if (ret) {
472a6786b   Hans-Christian Egtvedt   atmel-usba-udc: u...
2096
  				udc->vbus_pin = -ENODEV;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2097
2098
2099
2100
2101
2102
2103
  				dev_warn(&udc->pdev->dev,
  					 "failed to request vbus irq; "
  					 "assuming always on
  ");
  			} else {
  				disable_irq(gpio_to_irq(udc->vbus_pin));
  			}
969affff5   Jean-Christophe PLAGNIOL-VILLARD   USB: atmel_usba_u...
2104
2105
  		} else {
  			/* gpio_request fail so use -EINVAL for gpio_is_valid */
b48809518   Josh Wu   USB: gadget: AT91...
2106
  			udc->vbus_pin = -EINVAL;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2107
2108
  		}
  	}
0f91349b8   Sebastian Andrzej Siewior   usb: gadget: conv...
2109
2110
  	ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  	if (ret)
40a8fb2a6   Jingoo Han   usb: gadget: atme...
2111
  		return ret;
0f91349b8   Sebastian Andrzej Siewior   usb: gadget: conv...
2112

914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2113
  	usba_init_debugfs(udc);
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2114
2115
  	for (i = 1; i < udc->num_ep; i++)
  		usba_ep_init_debugfs(udc, &udc->usba_ep[i]);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2116
2117
  
  	return 0;
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2118
2119
2120
2121
2122
2123
2124
2125
  }
  
  static int __exit usba_udc_remove(struct platform_device *pdev)
  {
  	struct usba_udc *udc;
  	int i;
  
  	udc = platform_get_drvdata(pdev);
0f91349b8   Sebastian Andrzej Siewior   usb: gadget: conv...
2126
  	usb_del_gadget_udc(&udc->gadget);
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2127
  	for (i = 1; i < udc->num_ep; i++)
68522de70   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2128
  		usba_ep_cleanup_debugfs(&udc->usba_ep[i]);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2129
  	usba_cleanup_debugfs(udc);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2130
2131
  	return 0;
  }
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2132
2133
2134
2135
2136
2137
2138
2139
  #if defined(CONFIG_OF)
  static const struct of_device_id atmel_udc_dt_ids[] = {
  	{ .compatible = "atmel,at91sam9rl-udc" },
  	{ /* sentinel */ }
  };
  
  MODULE_DEVICE_TABLE(of, atmel_udc_dt_ids);
  #endif
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2140
2141
2142
2143
  static struct platform_driver udc_driver = {
  	.remove		= __exit_p(usba_udc_remove),
  	.driver		= {
  		.name		= "atmel_usba_udc",
f34c32f13   Kay Sievers   usb gadget: fix p...
2144
  		.owner		= THIS_MODULE,
4a3ae9324   Jean-Christophe PLAGNIOL-VILLARD   USB: gadget: atme...
2145
  		.of_match_table	= of_match_ptr(atmel_udc_dt_ids),
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2146
2147
  	},
  };
52f7a82b5   Fabio Porcedda   usb: converto dri...
2148
  module_platform_driver_probe(udc_driver, usba_udc_probe);
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2149
2150
  
  MODULE_DESCRIPTION("Atmel USBA UDC driver");
e05503ef1   Jean Delvare   Haavard Skinnemoe...
2151
  MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
914a3f3b3   Haavard Skinnemoen   USB: add atmel_us...
2152
  MODULE_LICENSE("GPL");
f34c32f13   Kay Sievers   usb gadget: fix p...
2153
  MODULE_ALIAS("platform:atmel_usba_udc");