blob: 8b332dd21dcd25c8107d361352f3d4a69bd6cd99 [file] [log] [blame]
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001/*
2 * Intel Langwell USB Device Controller driver
3 * Copyright (C) 2008-2009, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20
21/* #undef DEBUG */
JiebingLi5f81f4b2010-08-05 14:17:54 +010022/* #undef VERBOSE_DEBUG */
Xiaochen Shen5be19a92009-06-04 15:34:49 +080023
24#if defined(CONFIG_USB_LANGWELL_OTG)
25#define OTG_TRANSCEIVER
26#endif
27
28
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/dma-mapping.h>
32#include <linux/kernel.h>
33#include <linux/delay.h>
34#include <linux/ioport.h>
35#include <linux/sched.h>
36#include <linux/slab.h>
Xiaochen Shen5be19a92009-06-04 15:34:49 +080037#include <linux/errno.h>
38#include <linux/init.h>
39#include <linux/timer.h>
40#include <linux/list.h>
41#include <linux/interrupt.h>
42#include <linux/moduleparam.h>
43#include <linux/device.h>
44#include <linux/usb/ch9.h>
45#include <linux/usb/gadget.h>
46#include <linux/usb/otg.h>
47#include <linux/pm.h>
48#include <linux/io.h>
49#include <linux/irq.h>
50#include <asm/system.h>
51#include <asm/unaligned.h>
52
53#include "langwell_udc.h"
54
55
56#define DRIVER_DESC "Intel Langwell USB Device Controller driver"
57#define DRIVER_VERSION "16 May 2009"
58
59static const char driver_name[] = "langwell_udc";
60static const char driver_desc[] = DRIVER_DESC;
61
62
63/* controller device global variable */
64static struct langwell_udc *the_controller;
65
66/* for endpoint 0 operations */
67static const struct usb_endpoint_descriptor
68langwell_ep0_desc = {
69 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
71 .bEndpointAddress = 0,
72 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
73 .wMaxPacketSize = EP0_MAX_PKT_SIZE,
74};
75
76
77/*-------------------------------------------------------------------------*/
78/* debugging */
79
JiebingLi5f81f4b2010-08-05 14:17:54 +010080#ifdef VERBOSE_DEBUG
Xiaochen Shen5be19a92009-06-04 15:34:49 +080081static inline void print_all_registers(struct langwell_udc *dev)
82{
83 int i;
84
85 /* Capability Registers */
JiebingLi5f81f4b2010-08-05 14:17:54 +010086 dev_dbg(&dev->pdev->dev,
87 "Capability Registers (offset: 0x%04x, length: 0x%08x)\n",
88 CAP_REG_OFFSET, (u32)sizeof(struct langwell_cap_regs));
89 dev_dbg(&dev->pdev->dev, "caplength=0x%02x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +080090 readb(&dev->cap_regs->caplength));
JiebingLi5f81f4b2010-08-05 14:17:54 +010091 dev_dbg(&dev->pdev->dev, "hciversion=0x%04x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +080092 readw(&dev->cap_regs->hciversion));
JiebingLi5f81f4b2010-08-05 14:17:54 +010093 dev_dbg(&dev->pdev->dev, "hcsparams=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +080094 readl(&dev->cap_regs->hcsparams));
JiebingLi5f81f4b2010-08-05 14:17:54 +010095 dev_dbg(&dev->pdev->dev, "hccparams=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +080096 readl(&dev->cap_regs->hccparams));
JiebingLi5f81f4b2010-08-05 14:17:54 +010097 dev_dbg(&dev->pdev->dev, "dciversion=0x%04x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +080098 readw(&dev->cap_regs->dciversion));
JiebingLi5f81f4b2010-08-05 14:17:54 +010099 dev_dbg(&dev->pdev->dev, "dccparams=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800100 readl(&dev->cap_regs->dccparams));
101
102 /* Operational Registers */
JiebingLi5f81f4b2010-08-05 14:17:54 +0100103 dev_dbg(&dev->pdev->dev,
104 "Operational Registers (offset: 0x%04x, length: 0x%08x)\n",
105 OP_REG_OFFSET, (u32)sizeof(struct langwell_op_regs));
106 dev_dbg(&dev->pdev->dev, "extsts=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800107 readl(&dev->op_regs->extsts));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100108 dev_dbg(&dev->pdev->dev, "extintr=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800109 readl(&dev->op_regs->extintr));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100110 dev_dbg(&dev->pdev->dev, "usbcmd=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800111 readl(&dev->op_regs->usbcmd));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100112 dev_dbg(&dev->pdev->dev, "usbsts=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800113 readl(&dev->op_regs->usbsts));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100114 dev_dbg(&dev->pdev->dev, "usbintr=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800115 readl(&dev->op_regs->usbintr));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100116 dev_dbg(&dev->pdev->dev, "frindex=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800117 readl(&dev->op_regs->frindex));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100118 dev_dbg(&dev->pdev->dev, "ctrldssegment=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800119 readl(&dev->op_regs->ctrldssegment));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100120 dev_dbg(&dev->pdev->dev, "deviceaddr=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800121 readl(&dev->op_regs->deviceaddr));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100122 dev_dbg(&dev->pdev->dev, "endpointlistaddr=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800123 readl(&dev->op_regs->endpointlistaddr));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100124 dev_dbg(&dev->pdev->dev, "ttctrl=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800125 readl(&dev->op_regs->ttctrl));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100126 dev_dbg(&dev->pdev->dev, "burstsize=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800127 readl(&dev->op_regs->burstsize));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100128 dev_dbg(&dev->pdev->dev, "txfilltuning=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800129 readl(&dev->op_regs->txfilltuning));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100130 dev_dbg(&dev->pdev->dev, "txttfilltuning=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800131 readl(&dev->op_regs->txttfilltuning));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100132 dev_dbg(&dev->pdev->dev, "ic_usb=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800133 readl(&dev->op_regs->ic_usb));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100134 dev_dbg(&dev->pdev->dev, "ulpi_viewport=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800135 readl(&dev->op_regs->ulpi_viewport));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100136 dev_dbg(&dev->pdev->dev, "configflag=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800137 readl(&dev->op_regs->configflag));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100138 dev_dbg(&dev->pdev->dev, "portsc1=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800139 readl(&dev->op_regs->portsc1));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100140 dev_dbg(&dev->pdev->dev, "devlc=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800141 readl(&dev->op_regs->devlc));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100142 dev_dbg(&dev->pdev->dev, "otgsc=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800143 readl(&dev->op_regs->otgsc));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100144 dev_dbg(&dev->pdev->dev, "usbmode=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800145 readl(&dev->op_regs->usbmode));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100146 dev_dbg(&dev->pdev->dev, "endptnak=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800147 readl(&dev->op_regs->endptnak));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100148 dev_dbg(&dev->pdev->dev, "endptnaken=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800149 readl(&dev->op_regs->endptnaken));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100150 dev_dbg(&dev->pdev->dev, "endptsetupstat=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800151 readl(&dev->op_regs->endptsetupstat));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100152 dev_dbg(&dev->pdev->dev, "endptprime=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800153 readl(&dev->op_regs->endptprime));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100154 dev_dbg(&dev->pdev->dev, "endptflush=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800155 readl(&dev->op_regs->endptflush));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100156 dev_dbg(&dev->pdev->dev, "endptstat=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800157 readl(&dev->op_regs->endptstat));
JiebingLi5f81f4b2010-08-05 14:17:54 +0100158 dev_dbg(&dev->pdev->dev, "endptcomplete=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800159 readl(&dev->op_regs->endptcomplete));
160
161 for (i = 0; i < dev->ep_max / 2; i++) {
JiebingLi5f81f4b2010-08-05 14:17:54 +0100162 dev_dbg(&dev->pdev->dev, "endptctrl[%d]=0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800163 i, readl(&dev->op_regs->endptctrl[i]));
164 }
165}
JiebingLi5f81f4b2010-08-05 14:17:54 +0100166#else
167
168#define print_all_registers(dev) do { } while (0)
169
170#endif /* VERBOSE_DEBUG */
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800171
172
173/*-------------------------------------------------------------------------*/
174
JiebingLi5f81f4b2010-08-05 14:17:54 +0100175#define is_in(ep) (((ep)->ep_num == 0) ? ((ep)->dev->ep0_dir == \
176 USB_DIR_IN) : (usb_endpoint_dir_in((ep)->desc)))
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800177
JiebingLi5f81f4b2010-08-05 14:17:54 +0100178#define DIR_STRING(ep) (is_in(ep) ? "in" : "out")
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800179
180
JiebingLi5f81f4b2010-08-05 14:17:54 +0100181static char *type_string(const struct usb_endpoint_descriptor *desc)
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800182{
JiebingLi5f81f4b2010-08-05 14:17:54 +0100183 switch (usb_endpoint_type(desc)) {
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800184 case USB_ENDPOINT_XFER_BULK:
185 return "bulk";
186 case USB_ENDPOINT_XFER_ISOC:
187 return "iso";
188 case USB_ENDPOINT_XFER_INT:
189 return "int";
190 };
191
192 return "control";
193}
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800194
195
196/* configure endpoint control registers */
197static void ep_reset(struct langwell_ep *ep, unsigned char ep_num,
198 unsigned char is_in, unsigned char ep_type)
199{
200 struct langwell_udc *dev;
201 u32 endptctrl;
202
203 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100204 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800205
206 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
207 if (is_in) { /* TX */
208 if (ep_num)
209 endptctrl |= EPCTRL_TXR;
210 endptctrl |= EPCTRL_TXE;
211 endptctrl |= ep_type << EPCTRL_TXT_SHIFT;
212 } else { /* RX */
213 if (ep_num)
214 endptctrl |= EPCTRL_RXR;
215 endptctrl |= EPCTRL_RXE;
216 endptctrl |= ep_type << EPCTRL_RXT_SHIFT;
217 }
218
219 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
220
JiebingLi5f81f4b2010-08-05 14:17:54 +0100221 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800222}
223
224
225/* reset ep0 dQH and endptctrl */
226static void ep0_reset(struct langwell_udc *dev)
227{
228 struct langwell_ep *ep;
229 int i;
230
JiebingLi5f81f4b2010-08-05 14:17:54 +0100231 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800232
233 /* ep0 in and out */
234 for (i = 0; i < 2; i++) {
235 ep = &dev->ep[i];
236 ep->dev = dev;
237
238 /* ep0 dQH */
239 ep->dqh = &dev->ep_dqh[i];
240
241 /* configure ep0 endpoint capabilities in dQH */
242 ep->dqh->dqh_ios = 1;
243 ep->dqh->dqh_mpl = EP0_MAX_PKT_SIZE;
244
245 /* FIXME: enable ep0-in HW zero length termination select */
246 if (is_in(ep))
247 ep->dqh->dqh_zlt = 0;
248 ep->dqh->dqh_mult = 0;
249
250 /* configure ep0 control registers */
251 ep_reset(&dev->ep[0], 0, i, USB_ENDPOINT_XFER_CONTROL);
252 }
253
JiebingLi5f81f4b2010-08-05 14:17:54 +0100254 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800255 return;
256}
257
258
259/*-------------------------------------------------------------------------*/
260
261/* endpoints operations */
262
263/* configure endpoint, making it usable */
264static int langwell_ep_enable(struct usb_ep *_ep,
265 const struct usb_endpoint_descriptor *desc)
266{
267 struct langwell_udc *dev;
268 struct langwell_ep *ep;
269 u16 max = 0;
270 unsigned long flags;
271 int retval = 0;
272 unsigned char zlt, ios = 0, mult = 0;
273
274 ep = container_of(_ep, struct langwell_ep, ep);
275 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100276 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800277
278 if (!_ep || !desc || ep->desc
279 || desc->bDescriptorType != USB_DT_ENDPOINT)
280 return -EINVAL;
281
282 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
283 return -ESHUTDOWN;
284
285 max = le16_to_cpu(desc->wMaxPacketSize);
286
287 /*
288 * disable HW zero length termination select
289 * driver handles zero length packet through req->req.zero
290 */
291 zlt = 1;
292
293 /*
294 * sanity check type, direction, address, and then
295 * initialize the endpoint capabilities fields in dQH
296 */
JiebingLi5f81f4b2010-08-05 14:17:54 +0100297 switch (usb_endpoint_type(desc)) {
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800298 case USB_ENDPOINT_XFER_CONTROL:
299 ios = 1;
300 break;
301 case USB_ENDPOINT_XFER_BULK:
302 if ((dev->gadget.speed == USB_SPEED_HIGH
303 && max != 512)
304 || (dev->gadget.speed == USB_SPEED_FULL
305 && max > 64)) {
306 goto done;
307 }
308 break;
309 case USB_ENDPOINT_XFER_INT:
310 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
311 goto done;
312
313 switch (dev->gadget.speed) {
314 case USB_SPEED_HIGH:
315 if (max <= 1024)
316 break;
317 case USB_SPEED_FULL:
318 if (max <= 64)
319 break;
320 default:
321 if (max <= 8)
322 break;
323 goto done;
324 }
325 break;
326 case USB_ENDPOINT_XFER_ISOC:
327 if (strstr(ep->ep.name, "-bulk")
328 || strstr(ep->ep.name, "-int"))
329 goto done;
330
331 switch (dev->gadget.speed) {
332 case USB_SPEED_HIGH:
333 if (max <= 1024)
334 break;
335 case USB_SPEED_FULL:
336 if (max <= 1023)
337 break;
338 default:
339 goto done;
340 }
341 /*
342 * FIXME:
343 * calculate transactions needed for high bandwidth iso
344 */
345 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
346 max = max & 0x8ff; /* bit 0~10 */
347 /* 3 transactions at most */
348 if (mult > 3)
349 goto done;
350 break;
351 default:
352 goto done;
353 }
354
355 spin_lock_irqsave(&dev->lock, flags);
356
357 /* configure endpoint capabilities in dQH */
358 ep->dqh->dqh_ios = ios;
359 ep->dqh->dqh_mpl = cpu_to_le16(max);
360 ep->dqh->dqh_zlt = zlt;
361 ep->dqh->dqh_mult = mult;
362
363 ep->ep.maxpacket = max;
364 ep->desc = desc;
365 ep->stopped = 0;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100366 ep->ep_num = usb_endpoint_num(desc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800367
368 /* ep_type */
JiebingLi5f81f4b2010-08-05 14:17:54 +0100369 ep->ep_type = usb_endpoint_type(desc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800370
371 /* configure endpoint control registers */
372 ep_reset(ep, ep->ep_num, is_in(ep), ep->ep_type);
373
JiebingLi5f81f4b2010-08-05 14:17:54 +0100374 dev_dbg(&dev->pdev->dev, "enabled %s (ep%d%s-%s), max %04x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800375 _ep->name,
376 ep->ep_num,
JiebingLi5f81f4b2010-08-05 14:17:54 +0100377 DIR_STRING(ep),
378 type_string(desc),
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800379 max);
380
381 spin_unlock_irqrestore(&dev->lock, flags);
382done:
JiebingLi5f81f4b2010-08-05 14:17:54 +0100383 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800384 return retval;
385}
386
387
388/*-------------------------------------------------------------------------*/
389
390/* retire a request */
391static void done(struct langwell_ep *ep, struct langwell_request *req,
392 int status)
393{
394 struct langwell_udc *dev = ep->dev;
395 unsigned stopped = ep->stopped;
396 struct langwell_dtd *curr_dtd, *next_dtd;
397 int i;
398
JiebingLi5f81f4b2010-08-05 14:17:54 +0100399 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800400
401 /* remove the req from ep->queue */
402 list_del_init(&req->queue);
403
404 if (req->req.status == -EINPROGRESS)
405 req->req.status = status;
406 else
407 status = req->req.status;
408
409 /* free dTD for the request */
410 next_dtd = req->head;
411 for (i = 0; i < req->dtd_count; i++) {
412 curr_dtd = next_dtd;
413 if (i != req->dtd_count - 1)
414 next_dtd = curr_dtd->next_dtd_virt;
415 dma_pool_free(dev->dtd_pool, curr_dtd, curr_dtd->dtd_dma);
416 }
417
418 if (req->mapped) {
JiebingLi5f81f4b2010-08-05 14:17:54 +0100419 dma_unmap_single(&dev->pdev->dev,
420 req->req.dma, req->req.length,
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800421 is_in(ep) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
422 req->req.dma = DMA_ADDR_INVALID;
423 req->mapped = 0;
424 } else
425 dma_sync_single_for_cpu(&dev->pdev->dev, req->req.dma,
426 req->req.length,
427 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
428
429 if (status != -ESHUTDOWN)
JiebingLi5f81f4b2010-08-05 14:17:54 +0100430 dev_dbg(&dev->pdev->dev,
431 "complete %s, req %p, stat %d, len %u/%u\n",
432 ep->ep.name, &req->req, status,
433 req->req.actual, req->req.length);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800434
435 /* don't modify queue heads during completion callback */
436 ep->stopped = 1;
437
438 spin_unlock(&dev->lock);
439 /* complete routine from gadget driver */
440 if (req->req.complete)
441 req->req.complete(&ep->ep, &req->req);
442
443 spin_lock(&dev->lock);
444 ep->stopped = stopped;
445
JiebingLi5f81f4b2010-08-05 14:17:54 +0100446 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800447}
448
449
450static void langwell_ep_fifo_flush(struct usb_ep *_ep);
451
452/* delete all endpoint requests, called with spinlock held */
453static void nuke(struct langwell_ep *ep, int status)
454{
455 /* called with spinlock held */
456 ep->stopped = 1;
457
458 /* endpoint fifo flush */
459 if (&ep->ep && ep->desc)
460 langwell_ep_fifo_flush(&ep->ep);
461
462 while (!list_empty(&ep->queue)) {
463 struct langwell_request *req = NULL;
464 req = list_entry(ep->queue.next, struct langwell_request,
465 queue);
466 done(ep, req, status);
467 }
468}
469
470
471/*-------------------------------------------------------------------------*/
472
473/* endpoint is no longer usable */
474static int langwell_ep_disable(struct usb_ep *_ep)
475{
476 struct langwell_ep *ep;
477 unsigned long flags;
478 struct langwell_udc *dev;
479 int ep_num;
480 u32 endptctrl;
481
482 ep = container_of(_ep, struct langwell_ep, ep);
483 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100484 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800485
486 if (!_ep || !ep->desc)
487 return -EINVAL;
488
489 spin_lock_irqsave(&dev->lock, flags);
490
491 /* disable endpoint control register */
492 ep_num = ep->ep_num;
493 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
494 if (is_in(ep))
495 endptctrl &= ~EPCTRL_TXE;
496 else
497 endptctrl &= ~EPCTRL_RXE;
498 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
499
500 /* nuke all pending requests (does flush) */
501 nuke(ep, -ESHUTDOWN);
502
503 ep->desc = NULL;
504 ep->stopped = 1;
505
506 spin_unlock_irqrestore(&dev->lock, flags);
507
JiebingLi5f81f4b2010-08-05 14:17:54 +0100508 dev_dbg(&dev->pdev->dev, "disabled %s\n", _ep->name);
509 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800510
511 return 0;
512}
513
514
515/* allocate a request object to use with this endpoint */
516static struct usb_request *langwell_alloc_request(struct usb_ep *_ep,
517 gfp_t gfp_flags)
518{
519 struct langwell_ep *ep;
520 struct langwell_udc *dev;
521 struct langwell_request *req = NULL;
522
523 if (!_ep)
524 return NULL;
525
526 ep = container_of(_ep, struct langwell_ep, ep);
527 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100528 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800529
530 req = kzalloc(sizeof(*req), gfp_flags);
531 if (!req)
532 return NULL;
533
534 req->req.dma = DMA_ADDR_INVALID;
535 INIT_LIST_HEAD(&req->queue);
536
JiebingLi5f81f4b2010-08-05 14:17:54 +0100537 dev_vdbg(&dev->pdev->dev, "alloc request for %s\n", _ep->name);
538 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800539 return &req->req;
540}
541
542
543/* free a request object */
544static void langwell_free_request(struct usb_ep *_ep,
545 struct usb_request *_req)
546{
547 struct langwell_ep *ep;
548 struct langwell_udc *dev;
549 struct langwell_request *req = NULL;
550
551 ep = container_of(_ep, struct langwell_ep, ep);
552 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100553 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800554
555 if (!_ep || !_req)
556 return;
557
558 req = container_of(_req, struct langwell_request, req);
559 WARN_ON(!list_empty(&req->queue));
560
561 if (_req)
562 kfree(req);
563
JiebingLi5f81f4b2010-08-05 14:17:54 +0100564 dev_vdbg(&dev->pdev->dev, "free request for %s\n", _ep->name);
565 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800566}
567
568
569/*-------------------------------------------------------------------------*/
570
571/* queue dTD and PRIME endpoint */
572static int queue_dtd(struct langwell_ep *ep, struct langwell_request *req)
573{
574 u32 bit_mask, usbcmd, endptstat, dtd_dma;
575 u8 dtd_status;
576 int i;
577 struct langwell_dqh *dqh;
578 struct langwell_udc *dev;
579
580 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100581 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800582
583 i = ep->ep_num * 2 + is_in(ep);
584 dqh = &dev->ep_dqh[i];
585
586 if (ep->ep_num)
JiebingLi5f81f4b2010-08-05 14:17:54 +0100587 dev_vdbg(&dev->pdev->dev, "%s\n", ep->name);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800588 else
589 /* ep0 */
JiebingLi5f81f4b2010-08-05 14:17:54 +0100590 dev_vdbg(&dev->pdev->dev, "%s-%s\n", ep->name, DIR_STRING(ep));
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800591
JiebingLi5f81f4b2010-08-05 14:17:54 +0100592 dev_vdbg(&dev->pdev->dev, "ep_dqh[%d] addr: 0x%08x\n",
593 i, (u32)&(dev->ep_dqh[i]));
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800594
595 bit_mask = is_in(ep) ?
596 (1 << (ep->ep_num + 16)) : (1 << (ep->ep_num));
597
JiebingLi5f81f4b2010-08-05 14:17:54 +0100598 dev_vdbg(&dev->pdev->dev, "bit_mask = 0x%08x\n", bit_mask);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800599
600 /* check if the pipe is empty */
601 if (!(list_empty(&ep->queue))) {
602 /* add dTD to the end of linked list */
603 struct langwell_request *lastreq;
604 lastreq = list_entry(ep->queue.prev,
605 struct langwell_request, queue);
606
607 lastreq->tail->dtd_next =
608 cpu_to_le32(req->head->dtd_dma & DTD_NEXT_MASK);
609
610 /* read prime bit, if 1 goto out */
611 if (readl(&dev->op_regs->endptprime) & bit_mask)
612 goto out;
613
614 do {
615 /* set ATDTW bit in USBCMD */
616 usbcmd = readl(&dev->op_regs->usbcmd);
617 writel(usbcmd | CMD_ATDTW, &dev->op_regs->usbcmd);
618
619 /* read correct status bit */
620 endptstat = readl(&dev->op_regs->endptstat) & bit_mask;
621
622 } while (!(readl(&dev->op_regs->usbcmd) & CMD_ATDTW));
623
624 /* write ATDTW bit to 0 */
625 usbcmd = readl(&dev->op_regs->usbcmd);
626 writel(usbcmd & ~CMD_ATDTW, &dev->op_regs->usbcmd);
627
628 if (endptstat)
629 goto out;
630 }
631
632 /* write dQH next pointer and terminate bit to 0 */
633 dtd_dma = req->head->dtd_dma & DTD_NEXT_MASK;
634 dqh->dtd_next = cpu_to_le32(dtd_dma);
635
636 /* clear active and halt bit */
637 dtd_status = (u8) ~(DTD_STS_ACTIVE | DTD_STS_HALTED);
638 dqh->dtd_status &= dtd_status;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100639 dev_vdbg(&dev->pdev->dev, "dqh->dtd_status = 0x%x\n", dqh->dtd_status);
640
641 /* ensure that updates to the dQH will occure before priming */
642 wmb();
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800643
644 /* write 1 to endptprime register to PRIME endpoint */
645 bit_mask = is_in(ep) ? (1 << (ep->ep_num + 16)) : (1 << ep->ep_num);
JiebingLi5f81f4b2010-08-05 14:17:54 +0100646 dev_vdbg(&dev->pdev->dev, "endprime bit_mask = 0x%08x\n", bit_mask);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800647 writel(bit_mask, &dev->op_regs->endptprime);
648out:
JiebingLi5f81f4b2010-08-05 14:17:54 +0100649 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800650 return 0;
651}
652
653
654/* fill in the dTD structure to build a transfer descriptor */
655static struct langwell_dtd *build_dtd(struct langwell_request *req,
656 unsigned *length, dma_addr_t *dma, int *is_last)
657{
658 u32 buf_ptr;
659 struct langwell_dtd *dtd;
660 struct langwell_udc *dev;
661 int i;
662
663 dev = req->ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100664 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800665
666 /* the maximum transfer length, up to 16k bytes */
667 *length = min(req->req.length - req->req.actual,
668 (unsigned)DTD_MAX_TRANSFER_LENGTH);
669
670 /* create dTD dma_pool resource */
671 dtd = dma_pool_alloc(dev->dtd_pool, GFP_KERNEL, dma);
672 if (dtd == NULL)
673 return dtd;
674 dtd->dtd_dma = *dma;
675
676 /* initialize buffer page pointers */
677 buf_ptr = (u32)(req->req.dma + req->req.actual);
678 for (i = 0; i < 5; i++)
679 dtd->dtd_buf[i] = cpu_to_le32(buf_ptr + i * PAGE_SIZE);
680
681 req->req.actual += *length;
682
683 /* fill in total bytes with transfer size */
684 dtd->dtd_total = cpu_to_le16(*length);
JiebingLi5f81f4b2010-08-05 14:17:54 +0100685 dev_vdbg(&dev->pdev->dev, "dtd->dtd_total = %d\n", dtd->dtd_total);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800686
687 /* set is_last flag if req->req.zero is set or not */
688 if (req->req.zero) {
689 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
690 *is_last = 1;
691 else
692 *is_last = 0;
693 } else if (req->req.length == req->req.actual) {
694 *is_last = 1;
695 } else
696 *is_last = 0;
697
698 if (*is_last == 0)
JiebingLi5f81f4b2010-08-05 14:17:54 +0100699 dev_vdbg(&dev->pdev->dev, "multi-dtd request!\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800700
701 /* set interrupt on complete bit for the last dTD */
702 if (*is_last && !req->req.no_interrupt)
703 dtd->dtd_ioc = 1;
704
705 /* set multiplier override 0 for non-ISO and non-TX endpoint */
706 dtd->dtd_multo = 0;
707
708 /* set the active bit of status field to 1 */
709 dtd->dtd_status = DTD_STS_ACTIVE;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100710 dev_vdbg(&dev->pdev->dev, "dtd->dtd_status = 0x%02x\n",
711 dtd->dtd_status);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800712
JiebingLi5f81f4b2010-08-05 14:17:54 +0100713 dev_vdbg(&dev->pdev->dev, "length = %d, dma addr= 0x%08x\n",
714 *length, (int)*dma);
715 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800716 return dtd;
717}
718
719
720/* generate dTD linked list for a request */
721static int req_to_dtd(struct langwell_request *req)
722{
723 unsigned count;
724 int is_last, is_first = 1;
725 struct langwell_dtd *dtd, *last_dtd = NULL;
726 struct langwell_udc *dev;
727 dma_addr_t dma;
728
729 dev = req->ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100730 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800731 do {
732 dtd = build_dtd(req, &count, &dma, &is_last);
733 if (dtd == NULL)
734 return -ENOMEM;
735
736 if (is_first) {
737 is_first = 0;
738 req->head = dtd;
739 } else {
740 last_dtd->dtd_next = cpu_to_le32(dma);
741 last_dtd->next_dtd_virt = dtd;
742 }
743 last_dtd = dtd;
744 req->dtd_count++;
745 } while (!is_last);
746
747 /* set terminate bit to 1 for the last dTD */
748 dtd->dtd_next = DTD_TERM;
749
750 req->tail = dtd;
751
JiebingLi5f81f4b2010-08-05 14:17:54 +0100752 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800753 return 0;
754}
755
756/*-------------------------------------------------------------------------*/
757
758/* queue (submits) an I/O requests to an endpoint */
759static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
760 gfp_t gfp_flags)
761{
762 struct langwell_request *req;
763 struct langwell_ep *ep;
764 struct langwell_udc *dev;
765 unsigned long flags;
766 int is_iso = 0, zlflag = 0;
767
768 /* always require a cpu-view buffer */
769 req = container_of(_req, struct langwell_request, req);
770 ep = container_of(_ep, struct langwell_ep, ep);
771
772 if (!_req || !_req->complete || !_req->buf
773 || !list_empty(&req->queue)) {
774 return -EINVAL;
775 }
776
777 if (unlikely(!_ep || !ep->desc))
778 return -EINVAL;
779
780 dev = ep->dev;
781 req->ep = ep;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100782 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800783
JiebingLi5f81f4b2010-08-05 14:17:54 +0100784 if (usb_endpoint_xfer_isoc(ep->desc)) {
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800785 if (req->req.length > ep->ep.maxpacket)
786 return -EMSGSIZE;
787 is_iso = 1;
788 }
789
790 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
791 return -ESHUTDOWN;
792
793 /* set up dma mapping in case the caller didn't */
794 if (_req->dma == DMA_ADDR_INVALID) {
795 /* WORKAROUND: WARN_ON(size == 0) */
796 if (_req->length == 0) {
JiebingLi5f81f4b2010-08-05 14:17:54 +0100797 dev_vdbg(&dev->pdev->dev, "req->length: 0->1\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800798 zlflag = 1;
799 _req->length++;
800 }
801
802 _req->dma = dma_map_single(&dev->pdev->dev,
803 _req->buf, _req->length,
804 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
805 if (zlflag && (_req->length == 1)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +0100806 dev_vdbg(&dev->pdev->dev, "req->length: 1->0\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800807 zlflag = 0;
808 _req->length = 0;
809 }
810
811 req->mapped = 1;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100812 dev_vdbg(&dev->pdev->dev, "req->mapped = 1\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800813 } else {
814 dma_sync_single_for_device(&dev->pdev->dev,
815 _req->dma, _req->length,
816 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
817 req->mapped = 0;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100818 dev_vdbg(&dev->pdev->dev, "req->mapped = 0\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800819 }
820
JiebingLi5f81f4b2010-08-05 14:17:54 +0100821 dev_dbg(&dev->pdev->dev,
822 "%s queue req %p, len %u, buf %p, dma 0x%08x\n",
823 _ep->name,
824 _req, _req->length, _req->buf, (int)_req->dma);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800825
826 _req->status = -EINPROGRESS;
827 _req->actual = 0;
828 req->dtd_count = 0;
829
830 spin_lock_irqsave(&dev->lock, flags);
831
832 /* build and put dTDs to endpoint queue */
833 if (!req_to_dtd(req)) {
834 queue_dtd(ep, req);
835 } else {
836 spin_unlock_irqrestore(&dev->lock, flags);
837 return -ENOMEM;
838 }
839
840 /* update ep0 state */
841 if (ep->ep_num == 0)
842 dev->ep0_state = DATA_STATE_XMIT;
843
844 if (likely(req != NULL)) {
845 list_add_tail(&req->queue, &ep->queue);
JiebingLi5f81f4b2010-08-05 14:17:54 +0100846 dev_vdbg(&dev->pdev->dev, "list_add_tail()\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800847 }
848
849 spin_unlock_irqrestore(&dev->lock, flags);
850
JiebingLi5f81f4b2010-08-05 14:17:54 +0100851 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800852 return 0;
853}
854
855
856/* dequeue (cancels, unlinks) an I/O request from an endpoint */
857static int langwell_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
858{
859 struct langwell_ep *ep;
860 struct langwell_udc *dev;
861 struct langwell_request *req;
862 unsigned long flags;
863 int stopped, ep_num, retval = 0;
864 u32 endptctrl;
865
866 ep = container_of(_ep, struct langwell_ep, ep);
867 dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100868 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800869
870 if (!_ep || !ep->desc || !_req)
871 return -EINVAL;
872
873 if (!dev->driver)
874 return -ESHUTDOWN;
875
876 spin_lock_irqsave(&dev->lock, flags);
877 stopped = ep->stopped;
878
879 /* quiesce dma while we patch the queue */
880 ep->stopped = 1;
881 ep_num = ep->ep_num;
882
883 /* disable endpoint control register */
884 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
885 if (is_in(ep))
886 endptctrl &= ~EPCTRL_TXE;
887 else
888 endptctrl &= ~EPCTRL_RXE;
889 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
890
891 /* make sure it's still queued on this endpoint */
892 list_for_each_entry(req, &ep->queue, queue) {
893 if (&req->req == _req)
894 break;
895 }
896
897 if (&req->req != _req) {
898 retval = -EINVAL;
899 goto done;
900 }
901
902 /* queue head may be partially complete. */
903 if (ep->queue.next == &req->queue) {
JiebingLi5f81f4b2010-08-05 14:17:54 +0100904 dev_dbg(&dev->pdev->dev, "unlink (%s) dma\n", _ep->name);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800905 _req->status = -ECONNRESET;
906 langwell_ep_fifo_flush(&ep->ep);
907
908 /* not the last request in endpoint queue */
909 if (likely(ep->queue.next == &req->queue)) {
910 struct langwell_dqh *dqh;
911 struct langwell_request *next_req;
912
913 dqh = ep->dqh;
914 next_req = list_entry(req->queue.next,
915 struct langwell_request, queue);
916
917 /* point the dQH to the first dTD of next request */
918 writel((u32) next_req->head, &dqh->dqh_current);
919 }
920 } else {
921 struct langwell_request *prev_req;
922
923 prev_req = list_entry(req->queue.prev,
924 struct langwell_request, queue);
925 writel(readl(&req->tail->dtd_next),
926 &prev_req->tail->dtd_next);
927 }
928
929 done(ep, req, -ECONNRESET);
930
931done:
932 /* enable endpoint again */
933 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
934 if (is_in(ep))
935 endptctrl |= EPCTRL_TXE;
936 else
937 endptctrl |= EPCTRL_RXE;
938 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
939
940 ep->stopped = stopped;
941 spin_unlock_irqrestore(&dev->lock, flags);
942
JiebingLi5f81f4b2010-08-05 14:17:54 +0100943 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800944 return retval;
945}
946
947
948/*-------------------------------------------------------------------------*/
949
950/* endpoint set/clear halt */
951static void ep_set_halt(struct langwell_ep *ep, int value)
952{
953 u32 endptctrl = 0;
954 int ep_num;
955 struct langwell_udc *dev = ep->dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +0100956 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800957
958 ep_num = ep->ep_num;
959 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
960
961 /* value: 1 - set halt, 0 - clear halt */
962 if (value) {
963 /* set the stall bit */
964 if (is_in(ep))
965 endptctrl |= EPCTRL_TXS;
966 else
967 endptctrl |= EPCTRL_RXS;
968 } else {
969 /* clear the stall bit and reset data toggle */
970 if (is_in(ep)) {
971 endptctrl &= ~EPCTRL_TXS;
972 endptctrl |= EPCTRL_TXR;
973 } else {
974 endptctrl &= ~EPCTRL_RXS;
975 endptctrl |= EPCTRL_RXR;
976 }
977 }
978
979 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
980
JiebingLi5f81f4b2010-08-05 14:17:54 +0100981 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800982}
983
984
985/* set the endpoint halt feature */
986static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
987{
988 struct langwell_ep *ep;
989 struct langwell_udc *dev;
990 unsigned long flags;
991 int retval = 0;
992
993 ep = container_of(_ep, struct langwell_ep, ep);
994 dev = ep->dev;
995
JiebingLi5f81f4b2010-08-05 14:17:54 +0100996 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +0800997
998 if (!_ep || !ep->desc)
999 return -EINVAL;
1000
1001 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1002 return -ESHUTDOWN;
1003
JiebingLi5f81f4b2010-08-05 14:17:54 +01001004 if (usb_endpoint_xfer_isoc(ep->desc))
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001005 return -EOPNOTSUPP;
1006
1007 spin_lock_irqsave(&dev->lock, flags);
1008
1009 /*
1010 * attempt to halt IN ep will fail if any transfer requests
1011 * are still queue
1012 */
1013 if (!list_empty(&ep->queue) && is_in(ep) && value) {
1014 /* IN endpoint FIFO holds bytes */
JiebingLi5f81f4b2010-08-05 14:17:54 +01001015 dev_dbg(&dev->pdev->dev, "%s FIFO holds bytes\n", _ep->name);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001016 retval = -EAGAIN;
1017 goto done;
1018 }
1019
1020 /* endpoint set/clear halt */
1021 if (ep->ep_num) {
1022 ep_set_halt(ep, value);
1023 } else { /* endpoint 0 */
1024 dev->ep0_state = WAIT_FOR_SETUP;
1025 dev->ep0_dir = USB_DIR_OUT;
1026 }
1027done:
1028 spin_unlock_irqrestore(&dev->lock, flags);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001029 dev_dbg(&dev->pdev->dev, "%s %s halt\n",
1030 _ep->name, value ? "set" : "clear");
1031 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001032 return retval;
1033}
1034
1035
1036/* set the halt feature and ignores clear requests */
1037static int langwell_ep_set_wedge(struct usb_ep *_ep)
1038{
1039 struct langwell_ep *ep;
1040 struct langwell_udc *dev;
1041
1042 ep = container_of(_ep, struct langwell_ep, ep);
1043 dev = ep->dev;
1044
JiebingLi5f81f4b2010-08-05 14:17:54 +01001045 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001046
1047 if (!_ep || !ep->desc)
1048 return -EINVAL;
1049
JiebingLi5f81f4b2010-08-05 14:17:54 +01001050 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001051 return usb_ep_set_halt(_ep);
1052}
1053
1054
1055/* flush contents of a fifo */
1056static void langwell_ep_fifo_flush(struct usb_ep *_ep)
1057{
1058 struct langwell_ep *ep;
1059 struct langwell_udc *dev;
1060 u32 flush_bit;
1061 unsigned long timeout;
1062
1063 ep = container_of(_ep, struct langwell_ep, ep);
1064 dev = ep->dev;
1065
JiebingLi5f81f4b2010-08-05 14:17:54 +01001066 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001067
1068 if (!_ep || !ep->desc) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01001069 dev_vdbg(&dev->pdev->dev, "ep or ep->desc is NULL\n");
1070 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001071 return;
1072 }
1073
JiebingLi5f81f4b2010-08-05 14:17:54 +01001074 dev_vdbg(&dev->pdev->dev, "%s-%s fifo flush\n",
1075 _ep->name, DIR_STRING(ep));
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001076
1077 /* flush endpoint buffer */
1078 if (ep->ep_num == 0)
1079 flush_bit = (1 << 16) | 1;
1080 else if (is_in(ep))
1081 flush_bit = 1 << (ep->ep_num + 16); /* TX */
1082 else
1083 flush_bit = 1 << ep->ep_num; /* RX */
1084
1085 /* wait until flush complete */
1086 timeout = jiffies + FLUSH_TIMEOUT;
1087 do {
1088 writel(flush_bit, &dev->op_regs->endptflush);
1089 while (readl(&dev->op_regs->endptflush)) {
1090 if (time_after(jiffies, timeout)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01001091 dev_err(&dev->pdev->dev, "ep flush timeout\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001092 goto done;
1093 }
1094 cpu_relax();
1095 }
1096 } while (readl(&dev->op_regs->endptstat) & flush_bit);
1097done:
JiebingLi5f81f4b2010-08-05 14:17:54 +01001098 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001099}
1100
1101
1102/* endpoints operations structure */
1103static const struct usb_ep_ops langwell_ep_ops = {
1104
1105 /* configure endpoint, making it usable */
1106 .enable = langwell_ep_enable,
1107
1108 /* endpoint is no longer usable */
1109 .disable = langwell_ep_disable,
1110
1111 /* allocate a request object to use with this endpoint */
1112 .alloc_request = langwell_alloc_request,
1113
1114 /* free a request object */
1115 .free_request = langwell_free_request,
1116
1117 /* queue (submits) an I/O requests to an endpoint */
1118 .queue = langwell_ep_queue,
1119
1120 /* dequeue (cancels, unlinks) an I/O request from an endpoint */
1121 .dequeue = langwell_ep_dequeue,
1122
1123 /* set the endpoint halt feature */
1124 .set_halt = langwell_ep_set_halt,
1125
1126 /* set the halt feature and ignores clear requests */
1127 .set_wedge = langwell_ep_set_wedge,
1128
1129 /* flush contents of a fifo */
1130 .fifo_flush = langwell_ep_fifo_flush,
1131};
1132
1133
1134/*-------------------------------------------------------------------------*/
1135
1136/* device controller usb_gadget_ops structure */
1137
1138/* returns the current frame number */
1139static int langwell_get_frame(struct usb_gadget *_gadget)
1140{
1141 struct langwell_udc *dev;
1142 u16 retval;
1143
1144 if (!_gadget)
1145 return -ENODEV;
1146
1147 dev = container_of(_gadget, struct langwell_udc, gadget);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001148 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001149
1150 retval = readl(&dev->op_regs->frindex) & FRINDEX_MASK;
1151
JiebingLi5f81f4b2010-08-05 14:17:54 +01001152 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001153 return retval;
1154}
1155
1156
1157/* tries to wake up the host connected to this gadget */
1158static int langwell_wakeup(struct usb_gadget *_gadget)
1159{
1160 struct langwell_udc *dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +01001161 u32 portsc1, devlc;
1162 unsigned long flags;
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001163
1164 if (!_gadget)
1165 return 0;
1166
1167 dev = container_of(_gadget, struct langwell_udc, gadget);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001168 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001169
JiebingLi5f81f4b2010-08-05 14:17:54 +01001170 /* remote wakeup feature not enabled by host */
1171 if (!dev->remote_wakeup) {
1172 dev_info(&dev->pdev->dev, "remote wakeup is disabled\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001173 return -ENOTSUPP;
JiebingLi5f81f4b2010-08-05 14:17:54 +01001174 }
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001175
1176 spin_lock_irqsave(&dev->lock, flags);
1177
1178 portsc1 = readl(&dev->op_regs->portsc1);
1179 if (!(portsc1 & PORTS_SUSP)) {
1180 spin_unlock_irqrestore(&dev->lock, flags);
1181 return 0;
1182 }
1183
1184 /* LPM L1 to L0, remote wakeup */
1185 if (dev->lpm && dev->lpm_state == LPM_L1) {
1186 portsc1 |= PORTS_SLP;
1187 writel(portsc1, &dev->op_regs->portsc1);
1188 }
1189
1190 /* force port resume */
1191 if (dev->usb_state == USB_STATE_SUSPENDED) {
1192 portsc1 |= PORTS_FPR;
1193 writel(portsc1, &dev->op_regs->portsc1);
1194 }
1195
1196 /* exit PHY low power suspend */
1197 devlc = readl(&dev->op_regs->devlc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001198 devlc &= ~LPM_PHCD;
1199 writel(devlc, &dev->op_regs->devlc);
1200
1201 spin_unlock_irqrestore(&dev->lock, flags);
1202
JiebingLi5f81f4b2010-08-05 14:17:54 +01001203 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001204 return 0;
1205}
1206
1207
1208/* notify controller that VBUS is powered or not */
1209static int langwell_vbus_session(struct usb_gadget *_gadget, int is_active)
1210{
1211 struct langwell_udc *dev;
1212 unsigned long flags;
JiebingLi5f81f4b2010-08-05 14:17:54 +01001213 u32 usbcmd;
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001214
1215 if (!_gadget)
1216 return -ENODEV;
1217
1218 dev = container_of(_gadget, struct langwell_udc, gadget);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001219 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001220
1221 spin_lock_irqsave(&dev->lock, flags);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001222 dev_vdbg(&dev->pdev->dev, "VBUS status: %s\n",
1223 is_active ? "on" : "off");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001224
1225 dev->vbus_active = (is_active != 0);
1226 if (dev->driver && dev->softconnected && dev->vbus_active) {
1227 usbcmd = readl(&dev->op_regs->usbcmd);
1228 usbcmd |= CMD_RUNSTOP;
1229 writel(usbcmd, &dev->op_regs->usbcmd);
1230 } else {
1231 usbcmd = readl(&dev->op_regs->usbcmd);
1232 usbcmd &= ~CMD_RUNSTOP;
1233 writel(usbcmd, &dev->op_regs->usbcmd);
1234 }
1235
1236 spin_unlock_irqrestore(&dev->lock, flags);
1237
JiebingLi5f81f4b2010-08-05 14:17:54 +01001238 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001239 return 0;
1240}
1241
1242
1243/* constrain controller's VBUS power usage */
1244static int langwell_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1245{
1246 struct langwell_udc *dev;
1247
1248 if (!_gadget)
1249 return -ENODEV;
1250
1251 dev = container_of(_gadget, struct langwell_udc, gadget);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001252 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001253
1254 if (dev->transceiver) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01001255 dev_vdbg(&dev->pdev->dev, "otg_set_power\n");
1256 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001257 return otg_set_power(dev->transceiver, mA);
1258 }
1259
JiebingLi5f81f4b2010-08-05 14:17:54 +01001260 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001261 return -ENOTSUPP;
1262}
1263
1264
1265/* D+ pullup, software-controlled connect/disconnect to USB host */
1266static int langwell_pullup(struct usb_gadget *_gadget, int is_on)
1267{
1268 struct langwell_udc *dev;
JiebingLi5f81f4b2010-08-05 14:17:54 +01001269 u32 usbcmd;
1270 unsigned long flags;
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001271
1272 if (!_gadget)
1273 return -ENODEV;
1274
1275 dev = container_of(_gadget, struct langwell_udc, gadget);
1276
JiebingLi5f81f4b2010-08-05 14:17:54 +01001277 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001278
1279 spin_lock_irqsave(&dev->lock, flags);
1280 dev->softconnected = (is_on != 0);
1281
1282 if (dev->driver && dev->softconnected && dev->vbus_active) {
1283 usbcmd = readl(&dev->op_regs->usbcmd);
1284 usbcmd |= CMD_RUNSTOP;
1285 writel(usbcmd, &dev->op_regs->usbcmd);
1286 } else {
1287 usbcmd = readl(&dev->op_regs->usbcmd);
1288 usbcmd &= ~CMD_RUNSTOP;
1289 writel(usbcmd, &dev->op_regs->usbcmd);
1290 }
1291 spin_unlock_irqrestore(&dev->lock, flags);
1292
JiebingLi5f81f4b2010-08-05 14:17:54 +01001293 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001294 return 0;
1295}
1296
1297
1298/* device controller usb_gadget_ops structure */
1299static const struct usb_gadget_ops langwell_ops = {
1300
1301 /* returns the current frame number */
1302 .get_frame = langwell_get_frame,
1303
1304 /* tries to wake up the host connected to this gadget */
1305 .wakeup = langwell_wakeup,
1306
1307 /* set the device selfpowered feature, always selfpowered */
1308 /* .set_selfpowered = langwell_set_selfpowered, */
1309
1310 /* notify controller that VBUS is powered or not */
1311 .vbus_session = langwell_vbus_session,
1312
1313 /* constrain controller's VBUS power usage */
1314 .vbus_draw = langwell_vbus_draw,
1315
1316 /* D+ pullup, software-controlled connect/disconnect to USB host */
1317 .pullup = langwell_pullup,
1318};
1319
1320
1321/*-------------------------------------------------------------------------*/
1322
1323/* device controller operations */
1324
1325/* reset device controller */
1326static int langwell_udc_reset(struct langwell_udc *dev)
1327{
1328 u32 usbcmd, usbmode, devlc, endpointlistaddr;
1329 unsigned long timeout;
1330
1331 if (!dev)
1332 return -EINVAL;
1333
JiebingLi5f81f4b2010-08-05 14:17:54 +01001334 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001335
1336 /* set controller to stop state */
1337 usbcmd = readl(&dev->op_regs->usbcmd);
1338 usbcmd &= ~CMD_RUNSTOP;
1339 writel(usbcmd, &dev->op_regs->usbcmd);
1340
1341 /* reset device controller */
1342 usbcmd = readl(&dev->op_regs->usbcmd);
1343 usbcmd |= CMD_RST;
1344 writel(usbcmd, &dev->op_regs->usbcmd);
1345
1346 /* wait for reset to complete */
1347 timeout = jiffies + RESET_TIMEOUT;
1348 while (readl(&dev->op_regs->usbcmd) & CMD_RST) {
1349 if (time_after(jiffies, timeout)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01001350 dev_err(&dev->pdev->dev, "device reset timeout\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001351 return -ETIMEDOUT;
1352 }
1353 cpu_relax();
1354 }
1355
1356 /* set controller to device mode */
1357 usbmode = readl(&dev->op_regs->usbmode);
1358 usbmode |= MODE_DEVICE;
1359
1360 /* turn setup lockout off, require setup tripwire in usbcmd */
1361 usbmode |= MODE_SLOM;
1362
1363 writel(usbmode, &dev->op_regs->usbmode);
1364 usbmode = readl(&dev->op_regs->usbmode);
JiebingLi5f81f4b2010-08-05 14:17:54 +01001365 dev_vdbg(&dev->pdev->dev, "usbmode=0x%08x\n", usbmode);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001366
1367 /* Write-Clear setup status */
1368 writel(0, &dev->op_regs->usbsts);
1369
1370 /* if support USB LPM, ACK all LPM token */
1371 if (dev->lpm) {
1372 devlc = readl(&dev->op_regs->devlc);
1373 devlc &= ~LPM_STL; /* don't STALL LPM token */
1374 devlc &= ~LPM_NYT_ACK; /* ACK LPM token */
1375 writel(devlc, &dev->op_regs->devlc);
1376 }
1377
1378 /* fill endpointlistaddr register */
1379 endpointlistaddr = dev->ep_dqh_dma;
1380 endpointlistaddr &= ENDPOINTLISTADDR_MASK;
1381 writel(endpointlistaddr, &dev->op_regs->endpointlistaddr);
1382
JiebingLi5f81f4b2010-08-05 14:17:54 +01001383 dev_vdbg(&dev->pdev->dev,
1384 "dQH base (vir: %p, phy: 0x%08x), endpointlistaddr=0x%08x\n",
1385 dev->ep_dqh, endpointlistaddr,
1386 readl(&dev->op_regs->endpointlistaddr));
1387 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001388 return 0;
1389}
1390
1391
1392/* reinitialize device controller endpoints */
1393static int eps_reinit(struct langwell_udc *dev)
1394{
1395 struct langwell_ep *ep;
1396 char name[14];
1397 int i;
1398
JiebingLi5f81f4b2010-08-05 14:17:54 +01001399 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001400
1401 /* initialize ep0 */
1402 ep = &dev->ep[0];
1403 ep->dev = dev;
1404 strncpy(ep->name, "ep0", sizeof(ep->name));
1405 ep->ep.name = ep->name;
1406 ep->ep.ops = &langwell_ep_ops;
1407 ep->stopped = 0;
1408 ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
1409 ep->ep_num = 0;
1410 ep->desc = &langwell_ep0_desc;
1411 INIT_LIST_HEAD(&ep->queue);
1412
1413 ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
1414
1415 /* initialize other endpoints */
1416 for (i = 2; i < dev->ep_max; i++) {
1417 ep = &dev->ep[i];
1418 if (i % 2)
1419 snprintf(name, sizeof(name), "ep%din", i / 2);
1420 else
1421 snprintf(name, sizeof(name), "ep%dout", i / 2);
1422 ep->dev = dev;
1423 strncpy(ep->name, name, sizeof(ep->name));
1424 ep->ep.name = ep->name;
1425
1426 ep->ep.ops = &langwell_ep_ops;
1427 ep->stopped = 0;
1428 ep->ep.maxpacket = (unsigned short) ~0;
1429 ep->ep_num = i / 2;
1430
1431 INIT_LIST_HEAD(&ep->queue);
1432 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1433
1434 ep->dqh = &dev->ep_dqh[i];
1435 }
1436
JiebingLi5f81f4b2010-08-05 14:17:54 +01001437 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001438 return 0;
1439}
1440
1441
1442/* enable interrupt and set controller to run state */
1443static void langwell_udc_start(struct langwell_udc *dev)
1444{
1445 u32 usbintr, usbcmd;
JiebingLi5f81f4b2010-08-05 14:17:54 +01001446 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001447
1448 /* enable interrupts */
1449 usbintr = INTR_ULPIE /* ULPI */
1450 | INTR_SLE /* suspend */
1451 /* | INTR_SRE SOF received */
1452 | INTR_URE /* USB reset */
1453 | INTR_AAE /* async advance */
1454 | INTR_SEE /* system error */
1455 | INTR_FRE /* frame list rollover */
1456 | INTR_PCE /* port change detect */
1457 | INTR_UEE /* USB error interrupt */
1458 | INTR_UE; /* USB interrupt */
1459 writel(usbintr, &dev->op_regs->usbintr);
1460
1461 /* clear stopped bit */
1462 dev->stopped = 0;
1463
1464 /* set controller to run */
1465 usbcmd = readl(&dev->op_regs->usbcmd);
1466 usbcmd |= CMD_RUNSTOP;
1467 writel(usbcmd, &dev->op_regs->usbcmd);
1468
JiebingLi5f81f4b2010-08-05 14:17:54 +01001469 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001470 return;
1471}
1472
1473
1474/* disable interrupt and set controller to stop state */
1475static void langwell_udc_stop(struct langwell_udc *dev)
1476{
1477 u32 usbcmd;
1478
JiebingLi5f81f4b2010-08-05 14:17:54 +01001479 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001480
1481 /* disable all interrupts */
1482 writel(0, &dev->op_regs->usbintr);
1483
1484 /* set stopped bit */
1485 dev->stopped = 1;
1486
1487 /* set controller to stop state */
1488 usbcmd = readl(&dev->op_regs->usbcmd);
1489 usbcmd &= ~CMD_RUNSTOP;
1490 writel(usbcmd, &dev->op_regs->usbcmd);
1491
JiebingLi5f81f4b2010-08-05 14:17:54 +01001492 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001493 return;
1494}
1495
1496
1497/* stop all USB activities */
1498static void stop_activity(struct langwell_udc *dev,
1499 struct usb_gadget_driver *driver)
1500{
1501 struct langwell_ep *ep;
JiebingLi5f81f4b2010-08-05 14:17:54 +01001502 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001503
1504 nuke(&dev->ep[0], -ESHUTDOWN);
1505
1506 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1507 nuke(ep, -ESHUTDOWN);
1508 }
1509
1510 /* report disconnect; the driver is already quiesced */
1511 if (driver) {
1512 spin_unlock(&dev->lock);
1513 driver->disconnect(&dev->gadget);
1514 spin_lock(&dev->lock);
1515 }
1516
JiebingLi5f81f4b2010-08-05 14:17:54 +01001517 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001518}
1519
1520
1521/*-------------------------------------------------------------------------*/
1522
1523/* device "function" sysfs attribute file */
1524static ssize_t show_function(struct device *_dev,
1525 struct device_attribute *attr, char *buf)
1526{
1527 struct langwell_udc *dev = the_controller;
1528
1529 if (!dev->driver || !dev->driver->function
1530 || strlen(dev->driver->function) > PAGE_SIZE)
1531 return 0;
1532
1533 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1534}
1535static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
1536
1537
1538/* device "langwell_udc" sysfs attribute file */
1539static ssize_t show_langwell_udc(struct device *_dev,
1540 struct device_attribute *attr, char *buf)
1541{
1542 struct langwell_udc *dev = the_controller;
1543 struct langwell_request *req;
1544 struct langwell_ep *ep = NULL;
1545 char *next;
1546 unsigned size;
1547 unsigned t;
1548 unsigned i;
1549 unsigned long flags;
1550 u32 tmp_reg;
1551
1552 next = buf;
1553 size = PAGE_SIZE;
1554 spin_lock_irqsave(&dev->lock, flags);
1555
1556 /* driver basic information */
1557 t = scnprintf(next, size,
1558 DRIVER_DESC "\n"
1559 "%s version: %s\n"
1560 "Gadget driver: %s\n\n",
1561 driver_name, DRIVER_VERSION,
1562 dev->driver ? dev->driver->driver.name : "(none)");
1563 size -= t;
1564 next += t;
1565
1566 /* device registers */
1567 tmp_reg = readl(&dev->op_regs->usbcmd);
1568 t = scnprintf(next, size,
1569 "USBCMD reg:\n"
1570 "SetupTW: %d\n"
1571 "Run/Stop: %s\n\n",
1572 (tmp_reg & CMD_SUTW) ? 1 : 0,
1573 (tmp_reg & CMD_RUNSTOP) ? "Run" : "Stop");
1574 size -= t;
1575 next += t;
1576
1577 tmp_reg = readl(&dev->op_regs->usbsts);
1578 t = scnprintf(next, size,
1579 "USB Status Reg:\n"
1580 "Device Suspend: %d\n"
1581 "Reset Received: %d\n"
1582 "System Error: %s\n"
1583 "USB Error Interrupt: %s\n\n",
1584 (tmp_reg & STS_SLI) ? 1 : 0,
1585 (tmp_reg & STS_URI) ? 1 : 0,
1586 (tmp_reg & STS_SEI) ? "Error" : "No error",
1587 (tmp_reg & STS_UEI) ? "Error detected" : "No error");
1588 size -= t;
1589 next += t;
1590
1591 tmp_reg = readl(&dev->op_regs->usbintr);
1592 t = scnprintf(next, size,
1593 "USB Intrrupt Enable Reg:\n"
1594 "Sleep Enable: %d\n"
1595 "SOF Received Enable: %d\n"
1596 "Reset Enable: %d\n"
1597 "System Error Enable: %d\n"
1598 "Port Change Dectected Enable: %d\n"
1599 "USB Error Intr Enable: %d\n"
1600 "USB Intr Enable: %d\n\n",
1601 (tmp_reg & INTR_SLE) ? 1 : 0,
1602 (tmp_reg & INTR_SRE) ? 1 : 0,
1603 (tmp_reg & INTR_URE) ? 1 : 0,
1604 (tmp_reg & INTR_SEE) ? 1 : 0,
1605 (tmp_reg & INTR_PCE) ? 1 : 0,
1606 (tmp_reg & INTR_UEE) ? 1 : 0,
1607 (tmp_reg & INTR_UE) ? 1 : 0);
1608 size -= t;
1609 next += t;
1610
1611 tmp_reg = readl(&dev->op_regs->frindex);
1612 t = scnprintf(next, size,
1613 "USB Frame Index Reg:\n"
1614 "Frame Number is 0x%08x\n\n",
1615 (tmp_reg & FRINDEX_MASK));
1616 size -= t;
1617 next += t;
1618
1619 tmp_reg = readl(&dev->op_regs->deviceaddr);
1620 t = scnprintf(next, size,
1621 "USB Device Address Reg:\n"
1622 "Device Addr is 0x%x\n\n",
1623 USBADR(tmp_reg));
1624 size -= t;
1625 next += t;
1626
1627 tmp_reg = readl(&dev->op_regs->endpointlistaddr);
1628 t = scnprintf(next, size,
1629 "USB Endpoint List Address Reg:\n"
1630 "Endpoint List Pointer is 0x%x\n\n",
1631 EPBASE(tmp_reg));
1632 size -= t;
1633 next += t;
1634
1635 tmp_reg = readl(&dev->op_regs->portsc1);
1636 t = scnprintf(next, size,
1637 "USB Port Status & Control Reg:\n"
1638 "Port Reset: %s\n"
1639 "Port Suspend Mode: %s\n"
1640 "Over-current Change: %s\n"
1641 "Port Enable/Disable Change: %s\n"
1642 "Port Enabled/Disabled: %s\n"
JiebingLi5f81f4b2010-08-05 14:17:54 +01001643 "Current Connect Status: %s\n"
1644 "LPM Suspend Status: %s\n\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001645 (tmp_reg & PORTS_PR) ? "Reset" : "Not Reset",
1646 (tmp_reg & PORTS_SUSP) ? "Suspend " : "Not Suspend",
1647 (tmp_reg & PORTS_OCC) ? "Detected" : "No",
1648 (tmp_reg & PORTS_PEC) ? "Changed" : "Not Changed",
1649 (tmp_reg & PORTS_PE) ? "Enable" : "Not Correct",
JiebingLi5f81f4b2010-08-05 14:17:54 +01001650 (tmp_reg & PORTS_CCS) ? "Attached" : "Not Attached",
1651 (tmp_reg & PORTS_SLP) ? "LPM L1" : "LPM L0");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001652 size -= t;
1653 next += t;
1654
1655 tmp_reg = readl(&dev->op_regs->devlc);
1656 t = scnprintf(next, size,
1657 "Device LPM Control Reg:\n"
1658 "Parallel Transceiver : %d\n"
1659 "Serial Transceiver : %d\n"
1660 "Port Speed: %s\n"
1661 "Port Force Full Speed Connenct: %s\n"
JiebingLi5f81f4b2010-08-05 14:17:54 +01001662 "PHY Low Power Suspend Clock: %s\n"
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001663 "BmAttributes: %d\n\n",
1664 LPM_PTS(tmp_reg),
1665 (tmp_reg & LPM_STS) ? 1 : 0,
1666 ({
1667 char *s;
1668 switch (LPM_PSPD(tmp_reg)) {
1669 case LPM_SPEED_FULL:
1670 s = "Full Speed"; break;
1671 case LPM_SPEED_LOW:
1672 s = "Low Speed"; break;
1673 case LPM_SPEED_HIGH:
1674 s = "High Speed"; break;
1675 default:
1676 s = "Unknown Speed"; break;
1677 }
1678 s;
1679 }),
1680 (tmp_reg & LPM_PFSC) ? "Force Full Speed" : "Not Force",
1681 (tmp_reg & LPM_PHCD) ? "Disabled" : "Enabled",
1682 LPM_BA(tmp_reg));
1683 size -= t;
1684 next += t;
1685
1686 tmp_reg = readl(&dev->op_regs->usbmode);
1687 t = scnprintf(next, size,
1688 "USB Mode Reg:\n"
1689 "Controller Mode is : %s\n\n", ({
1690 char *s;
1691 switch (MODE_CM(tmp_reg)) {
1692 case MODE_IDLE:
1693 s = "Idle"; break;
1694 case MODE_DEVICE:
1695 s = "Device Controller"; break;
1696 case MODE_HOST:
1697 s = "Host Controller"; break;
1698 default:
1699 s = "None"; break;
1700 }
1701 s;
1702 }));
1703 size -= t;
1704 next += t;
1705
1706 tmp_reg = readl(&dev->op_regs->endptsetupstat);
1707 t = scnprintf(next, size,
1708 "Endpoint Setup Status Reg:\n"
1709 "SETUP on ep 0x%04x\n\n",
1710 tmp_reg & SETUPSTAT_MASK);
1711 size -= t;
1712 next += t;
1713
1714 for (i = 0; i < dev->ep_max / 2; i++) {
1715 tmp_reg = readl(&dev->op_regs->endptctrl[i]);
1716 t = scnprintf(next, size, "EP Ctrl Reg [%d]: 0x%08x\n",
1717 i, tmp_reg);
1718 size -= t;
1719 next += t;
1720 }
1721 tmp_reg = readl(&dev->op_regs->endptprime);
1722 t = scnprintf(next, size, "EP Prime Reg: 0x%08x\n\n", tmp_reg);
1723 size -= t;
1724 next += t;
1725
1726 /* langwell_udc, langwell_ep, langwell_request structure information */
1727 ep = &dev->ep[0];
1728 t = scnprintf(next, size, "%s MaxPacketSize: 0x%x, ep_num: %d\n",
1729 ep->ep.name, ep->ep.maxpacket, ep->ep_num);
1730 size -= t;
1731 next += t;
1732
1733 if (list_empty(&ep->queue)) {
1734 t = scnprintf(next, size, "its req queue is empty\n\n");
1735 size -= t;
1736 next += t;
1737 } else {
1738 list_for_each_entry(req, &ep->queue, queue) {
1739 t = scnprintf(next, size,
1740 "req %p actual 0x%x length 0x%x buf %p\n",
1741 &req->req, req->req.actual,
1742 req->req.length, req->req.buf);
1743 size -= t;
1744 next += t;
1745 }
1746 }
1747 /* other gadget->eplist ep */
1748 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1749 if (ep->desc) {
1750 t = scnprintf(next, size,
1751 "\n%s MaxPacketSize: 0x%x, "
1752 "ep_num: %d\n",
1753 ep->ep.name, ep->ep.maxpacket,
1754 ep->ep_num);
1755 size -= t;
1756 next += t;
1757
1758 if (list_empty(&ep->queue)) {
1759 t = scnprintf(next, size,
1760 "its req queue is empty\n\n");
1761 size -= t;
1762 next += t;
1763 } else {
1764 list_for_each_entry(req, &ep->queue, queue) {
1765 t = scnprintf(next, size,
1766 "req %p actual 0x%x length "
1767 "0x%x buf %p\n",
1768 &req->req, req->req.actual,
1769 req->req.length, req->req.buf);
1770 size -= t;
1771 next += t;
1772 }
1773 }
1774 }
1775 }
1776
1777 spin_unlock_irqrestore(&dev->lock, flags);
1778 return PAGE_SIZE - size;
1779}
1780static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL);
1781
1782
1783/*-------------------------------------------------------------------------*/
1784
1785/*
1786 * when a driver is successfully registered, it will receive
1787 * control requests including set_configuration(), which enables
1788 * non-control requests. then usb traffic follows until a
1789 * disconnect is reported. then a host may connect again, or
1790 * the driver might get unbound.
1791 */
1792
1793int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1794{
1795 struct langwell_udc *dev = the_controller;
1796 unsigned long flags;
1797 int retval;
1798
1799 if (!dev)
1800 return -ENODEV;
1801
JiebingLi5f81f4b2010-08-05 14:17:54 +01001802 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001803
1804 if (dev->driver)
1805 return -EBUSY;
1806
1807 spin_lock_irqsave(&dev->lock, flags);
1808
1809 /* hook up the driver ... */
1810 driver->driver.bus = NULL;
1811 dev->driver = driver;
1812 dev->gadget.dev.driver = &driver->driver;
1813
1814 spin_unlock_irqrestore(&dev->lock, flags);
1815
1816 retval = driver->bind(&dev->gadget);
1817 if (retval) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01001818 dev_dbg(&dev->pdev->dev, "bind to driver %s --> %d\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001819 driver->driver.name, retval);
1820 dev->driver = NULL;
1821 dev->gadget.dev.driver = NULL;
1822 return retval;
1823 }
1824
1825 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
1826 if (retval)
1827 goto err_unbind;
1828
1829 dev->usb_state = USB_STATE_ATTACHED;
1830 dev->ep0_state = WAIT_FOR_SETUP;
1831 dev->ep0_dir = USB_DIR_OUT;
1832
1833 /* enable interrupt and set controller to run state */
1834 if (dev->got_irq)
1835 langwell_udc_start(dev);
1836
JiebingLi5f81f4b2010-08-05 14:17:54 +01001837 dev_vdbg(&dev->pdev->dev,
1838 "After langwell_udc_start(), print all registers:\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001839 print_all_registers(dev);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001840
JiebingLi5f81f4b2010-08-05 14:17:54 +01001841 dev_info(&dev->pdev->dev, "register driver: %s\n",
1842 driver->driver.name);
1843 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001844 return 0;
1845
1846err_unbind:
1847 driver->unbind(&dev->gadget);
1848 dev->gadget.dev.driver = NULL;
1849 dev->driver = NULL;
1850
JiebingLi5f81f4b2010-08-05 14:17:54 +01001851 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001852 return retval;
1853}
1854EXPORT_SYMBOL(usb_gadget_register_driver);
1855
1856
1857/* unregister gadget driver */
1858int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1859{
1860 struct langwell_udc *dev = the_controller;
1861 unsigned long flags;
1862
1863 if (!dev)
1864 return -ENODEV;
1865
JiebingLi5f81f4b2010-08-05 14:17:54 +01001866 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001867
1868 if (unlikely(!driver || !driver->bind || !driver->unbind))
1869 return -EINVAL;
1870
1871 /* unbind OTG transceiver */
1872 if (dev->transceiver)
1873 (void)otg_set_peripheral(dev->transceiver, 0);
1874
1875 /* disable interrupt and set controller to stop state */
1876 langwell_udc_stop(dev);
1877
1878 dev->usb_state = USB_STATE_ATTACHED;
1879 dev->ep0_state = WAIT_FOR_SETUP;
1880 dev->ep0_dir = USB_DIR_OUT;
1881
1882 spin_lock_irqsave(&dev->lock, flags);
1883
1884 /* stop all usb activities */
1885 dev->gadget.speed = USB_SPEED_UNKNOWN;
1886 stop_activity(dev, driver);
1887 spin_unlock_irqrestore(&dev->lock, flags);
1888
1889 /* unbind gadget driver */
1890 driver->unbind(&dev->gadget);
1891 dev->gadget.dev.driver = NULL;
1892 dev->driver = NULL;
1893
1894 device_remove_file(&dev->pdev->dev, &dev_attr_function);
1895
JiebingLi5f81f4b2010-08-05 14:17:54 +01001896 dev_info(&dev->pdev->dev, "unregistered driver '%s'\n",
1897 driver->driver.name);
1898 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001899 return 0;
1900}
1901EXPORT_SYMBOL(usb_gadget_unregister_driver);
1902
1903
1904/*-------------------------------------------------------------------------*/
1905
1906/*
1907 * setup tripwire is used as a semaphore to ensure that the setup data
1908 * payload is extracted from a dQH without being corrupted
1909 */
1910static void setup_tripwire(struct langwell_udc *dev)
1911{
1912 u32 usbcmd,
1913 endptsetupstat;
1914 unsigned long timeout;
1915 struct langwell_dqh *dqh;
1916
JiebingLi5f81f4b2010-08-05 14:17:54 +01001917 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001918
1919 /* ep0 OUT dQH */
1920 dqh = &dev->ep_dqh[EP_DIR_OUT];
1921
1922 /* Write-Clear endptsetupstat */
1923 endptsetupstat = readl(&dev->op_regs->endptsetupstat);
1924 writel(endptsetupstat, &dev->op_regs->endptsetupstat);
1925
1926 /* wait until endptsetupstat is cleared */
1927 timeout = jiffies + SETUPSTAT_TIMEOUT;
1928 while (readl(&dev->op_regs->endptsetupstat)) {
1929 if (time_after(jiffies, timeout)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01001930 dev_err(&dev->pdev->dev, "setup_tripwire timeout\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001931 break;
1932 }
1933 cpu_relax();
1934 }
1935
1936 /* while a hazard exists when setup packet arrives */
1937 do {
1938 /* set setup tripwire bit */
1939 usbcmd = readl(&dev->op_regs->usbcmd);
1940 writel(usbcmd | CMD_SUTW, &dev->op_regs->usbcmd);
1941
1942 /* copy the setup packet to local buffer */
1943 memcpy(&dev->local_setup_buff, &dqh->dqh_setup, 8);
1944 } while (!(readl(&dev->op_regs->usbcmd) & CMD_SUTW));
1945
1946 /* Write-Clear setup tripwire bit */
1947 usbcmd = readl(&dev->op_regs->usbcmd);
1948 writel(usbcmd & ~CMD_SUTW, &dev->op_regs->usbcmd);
1949
JiebingLi5f81f4b2010-08-05 14:17:54 +01001950 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001951}
1952
1953
1954/* protocol ep0 stall, will automatically be cleared on new transaction */
1955static void ep0_stall(struct langwell_udc *dev)
1956{
1957 u32 endptctrl;
1958
JiebingLi5f81f4b2010-08-05 14:17:54 +01001959 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001960
1961 /* set TX and RX to stall */
1962 endptctrl = readl(&dev->op_regs->endptctrl[0]);
1963 endptctrl |= EPCTRL_TXS | EPCTRL_RXS;
1964 writel(endptctrl, &dev->op_regs->endptctrl[0]);
1965
1966 /* update ep0 state */
1967 dev->ep0_state = WAIT_FOR_SETUP;
1968 dev->ep0_dir = USB_DIR_OUT;
1969
JiebingLi5f81f4b2010-08-05 14:17:54 +01001970 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001971}
1972
1973
1974/* PRIME a status phase for ep0 */
1975static int prime_status_phase(struct langwell_udc *dev, int dir)
1976{
1977 struct langwell_request *req;
1978 struct langwell_ep *ep;
1979 int status = 0;
1980
JiebingLi5f81f4b2010-08-05 14:17:54 +01001981 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08001982
1983 if (dir == EP_DIR_IN)
1984 dev->ep0_dir = USB_DIR_IN;
1985 else
1986 dev->ep0_dir = USB_DIR_OUT;
1987
1988 ep = &dev->ep[0];
1989 dev->ep0_state = WAIT_FOR_OUT_STATUS;
1990
1991 req = dev->status_req;
1992
1993 req->ep = ep;
1994 req->req.length = 0;
1995 req->req.status = -EINPROGRESS;
1996 req->req.actual = 0;
1997 req->req.complete = NULL;
1998 req->dtd_count = 0;
1999
2000 if (!req_to_dtd(req))
2001 status = queue_dtd(ep, req);
2002 else
2003 return -ENOMEM;
2004
2005 if (status)
JiebingLi5f81f4b2010-08-05 14:17:54 +01002006 dev_err(&dev->pdev->dev, "can't queue ep0 status request\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002007
2008 list_add_tail(&req->queue, &ep->queue);
2009
JiebingLi5f81f4b2010-08-05 14:17:54 +01002010 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002011 return status;
2012}
2013
2014
2015/* SET_ADDRESS request routine */
2016static void set_address(struct langwell_udc *dev, u16 value,
2017 u16 index, u16 length)
2018{
JiebingLi5f81f4b2010-08-05 14:17:54 +01002019 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002020
2021 /* save the new address to device struct */
2022 dev->dev_addr = (u8) value;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002023 dev_vdbg(&dev->pdev->dev, "dev->dev_addr = %d\n", dev->dev_addr);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002024
2025 /* update usb state */
2026 dev->usb_state = USB_STATE_ADDRESS;
2027
2028 /* STATUS phase */
2029 if (prime_status_phase(dev, EP_DIR_IN))
2030 ep0_stall(dev);
2031
JiebingLi5f81f4b2010-08-05 14:17:54 +01002032 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002033}
2034
2035
2036/* return endpoint by windex */
2037static struct langwell_ep *get_ep_by_windex(struct langwell_udc *dev,
2038 u16 wIndex)
2039{
2040 struct langwell_ep *ep;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002041 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002042
2043 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2044 return &dev->ep[0];
2045
2046 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
2047 u8 bEndpointAddress;
2048 if (!ep->desc)
2049 continue;
2050
2051 bEndpointAddress = ep->desc->bEndpointAddress;
2052 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2053 continue;
2054
2055 if ((wIndex & USB_ENDPOINT_NUMBER_MASK)
2056 == (bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
2057 return ep;
2058 }
2059
JiebingLi5f81f4b2010-08-05 14:17:54 +01002060 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002061 return NULL;
2062}
2063
2064
2065/* return whether endpoint is stalled, 0: not stalled; 1: stalled */
2066static int ep_is_stall(struct langwell_ep *ep)
2067{
2068 struct langwell_udc *dev = ep->dev;
2069 u32 endptctrl;
2070 int retval;
2071
JiebingLi5f81f4b2010-08-05 14:17:54 +01002072 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002073
2074 endptctrl = readl(&dev->op_regs->endptctrl[ep->ep_num]);
2075 if (is_in(ep))
2076 retval = endptctrl & EPCTRL_TXS ? 1 : 0;
2077 else
2078 retval = endptctrl & EPCTRL_RXS ? 1 : 0;
2079
JiebingLi5f81f4b2010-08-05 14:17:54 +01002080 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002081 return retval;
2082}
2083
2084
2085/* GET_STATUS request routine */
2086static void get_status(struct langwell_udc *dev, u8 request_type, u16 value,
2087 u16 index, u16 length)
2088{
2089 struct langwell_request *req;
2090 struct langwell_ep *ep;
2091 u16 status_data = 0; /* 16 bits cpu view status data */
2092 int status = 0;
2093
JiebingLi5f81f4b2010-08-05 14:17:54 +01002094 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002095
2096 ep = &dev->ep[0];
2097
2098 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
2099 /* get device status */
2100 status_data = 1 << USB_DEVICE_SELF_POWERED;
2101 status_data |= dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
2102 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
2103 /* get interface status */
2104 status_data = 0;
2105 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
2106 /* get endpoint status */
2107 struct langwell_ep *epn;
2108 epn = get_ep_by_windex(dev, index);
2109 /* stall if endpoint doesn't exist */
2110 if (!epn)
2111 goto stall;
2112
2113 status_data = ep_is_stall(epn) << USB_ENDPOINT_HALT;
2114 }
2115
JiebingLi5f81f4b2010-08-05 14:17:54 +01002116 dev_dbg(&dev->pdev->dev, "get status data: 0x%04x\n", status_data);
2117
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002118 dev->ep0_dir = USB_DIR_IN;
2119
2120 /* borrow the per device status_req */
2121 req = dev->status_req;
2122
2123 /* fill in the reqest structure */
2124 *((u16 *) req->req.buf) = cpu_to_le16(status_data);
2125 req->ep = ep;
2126 req->req.length = 2;
2127 req->req.status = -EINPROGRESS;
2128 req->req.actual = 0;
2129 req->req.complete = NULL;
2130 req->dtd_count = 0;
2131
2132 /* prime the data phase */
2133 if (!req_to_dtd(req))
2134 status = queue_dtd(ep, req);
2135 else /* no mem */
2136 goto stall;
2137
2138 if (status) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002139 dev_err(&dev->pdev->dev,
2140 "response error on GET_STATUS request\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002141 goto stall;
2142 }
2143
2144 list_add_tail(&req->queue, &ep->queue);
2145 dev->ep0_state = DATA_STATE_XMIT;
2146
JiebingLi5f81f4b2010-08-05 14:17:54 +01002147 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002148 return;
2149stall:
2150 ep0_stall(dev);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002151 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002152}
2153
2154
2155/* setup packet interrupt handler */
2156static void handle_setup_packet(struct langwell_udc *dev,
2157 struct usb_ctrlrequest *setup)
2158{
2159 u16 wValue = le16_to_cpu(setup->wValue);
2160 u16 wIndex = le16_to_cpu(setup->wIndex);
2161 u16 wLength = le16_to_cpu(setup->wLength);
2162
JiebingLi5f81f4b2010-08-05 14:17:54 +01002163 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002164
2165 /* ep0 fifo flush */
2166 nuke(&dev->ep[0], -ESHUTDOWN);
2167
JiebingLi5f81f4b2010-08-05 14:17:54 +01002168 dev_dbg(&dev->pdev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002169 setup->bRequestType, setup->bRequest,
2170 wValue, wIndex, wLength);
2171
2172 /* RNDIS gadget delegate */
2173 if ((setup->bRequestType == 0x21) && (setup->bRequest == 0x00)) {
2174 /* USB_CDC_SEND_ENCAPSULATED_COMMAND */
2175 goto delegate;
2176 }
2177
2178 /* USB_CDC_GET_ENCAPSULATED_RESPONSE */
2179 if ((setup->bRequestType == 0xa1) && (setup->bRequest == 0x01)) {
2180 /* USB_CDC_GET_ENCAPSULATED_RESPONSE */
2181 goto delegate;
2182 }
2183
2184 /* We process some stardard setup requests here */
2185 switch (setup->bRequest) {
2186 case USB_REQ_GET_STATUS:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002187 dev_dbg(&dev->pdev->dev, "SETUP: USB_REQ_GET_STATUS\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002188 /* get status, DATA and STATUS phase */
2189 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
2190 != (USB_DIR_IN | USB_TYPE_STANDARD))
2191 break;
2192 get_status(dev, setup->bRequestType, wValue, wIndex, wLength);
2193 goto end;
2194
2195 case USB_REQ_SET_ADDRESS:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002196 dev_dbg(&dev->pdev->dev, "SETUP: USB_REQ_SET_ADDRESS\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002197 /* STATUS phase */
2198 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
2199 | USB_RECIP_DEVICE))
2200 break;
2201 set_address(dev, wValue, wIndex, wLength);
2202 goto end;
2203
2204 case USB_REQ_CLEAR_FEATURE:
2205 case USB_REQ_SET_FEATURE:
2206 /* STATUS phase */
2207 {
2208 int rc = -EOPNOTSUPP;
2209 if (setup->bRequest == USB_REQ_SET_FEATURE)
JiebingLi5f81f4b2010-08-05 14:17:54 +01002210 dev_dbg(&dev->pdev->dev,
2211 "SETUP: USB_REQ_SET_FEATURE\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002212 else if (setup->bRequest == USB_REQ_CLEAR_FEATURE)
JiebingLi5f81f4b2010-08-05 14:17:54 +01002213 dev_dbg(&dev->pdev->dev,
2214 "SETUP: USB_REQ_CLEAR_FEATURE\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002215
2216 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
2217 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
2218 struct langwell_ep *epn;
2219 epn = get_ep_by_windex(dev, wIndex);
2220 /* stall if endpoint doesn't exist */
2221 if (!epn) {
2222 ep0_stall(dev);
2223 goto end;
2224 }
2225
2226 if (wValue != 0 || wLength != 0
2227 || epn->ep_num > dev->ep_max)
2228 break;
2229
2230 spin_unlock(&dev->lock);
2231 rc = langwell_ep_set_halt(&epn->ep,
JiebingLi5f81f4b2010-08-05 14:17:54 +01002232 (setup->bRequest == USB_REQ_SET_FEATURE)
2233 ? 1 : 0);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002234 spin_lock(&dev->lock);
2235
2236 } else if ((setup->bRequestType & (USB_RECIP_MASK
2237 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
2238 | USB_TYPE_STANDARD)) {
2239 if (!gadget_is_otg(&dev->gadget))
2240 break;
2241 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE) {
2242 dev->gadget.b_hnp_enable = 1;
2243#ifdef OTG_TRANSCEIVER
2244 if (!dev->lotg->otg.default_a)
2245 dev->lotg->hsm.b_hnp_enable = 1;
2246#endif
2247 } else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT)
2248 dev->gadget.a_hnp_support = 1;
2249 else if (setup->bRequest ==
2250 USB_DEVICE_A_ALT_HNP_SUPPORT)
2251 dev->gadget.a_alt_hnp_support = 1;
2252 else
2253 break;
2254 rc = 0;
2255 } else
2256 break;
2257
2258 if (rc == 0) {
2259 if (prime_status_phase(dev, EP_DIR_IN))
2260 ep0_stall(dev);
2261 }
2262 goto end;
2263 }
2264
2265 case USB_REQ_GET_DESCRIPTOR:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002266 dev_dbg(&dev->pdev->dev,
2267 "SETUP: USB_REQ_GET_DESCRIPTOR\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002268 goto delegate;
2269
2270 case USB_REQ_SET_DESCRIPTOR:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002271 dev_dbg(&dev->pdev->dev,
2272 "SETUP: USB_REQ_SET_DESCRIPTOR unsupported\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002273 goto delegate;
2274
2275 case USB_REQ_GET_CONFIGURATION:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002276 dev_dbg(&dev->pdev->dev,
2277 "SETUP: USB_REQ_GET_CONFIGURATION\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002278 goto delegate;
2279
2280 case USB_REQ_SET_CONFIGURATION:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002281 dev_dbg(&dev->pdev->dev,
2282 "SETUP: USB_REQ_SET_CONFIGURATION\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002283 goto delegate;
2284
2285 case USB_REQ_GET_INTERFACE:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002286 dev_dbg(&dev->pdev->dev,
2287 "SETUP: USB_REQ_GET_INTERFACE\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002288 goto delegate;
2289
2290 case USB_REQ_SET_INTERFACE:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002291 dev_dbg(&dev->pdev->dev,
2292 "SETUP: USB_REQ_SET_INTERFACE\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002293 goto delegate;
2294
2295 case USB_REQ_SYNCH_FRAME:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002296 dev_dbg(&dev->pdev->dev,
2297 "SETUP: USB_REQ_SYNCH_FRAME unsupported\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002298 goto delegate;
2299
2300 default:
2301 /* delegate USB standard requests to the gadget driver */
2302 goto delegate;
2303delegate:
2304 /* USB requests handled by gadget */
2305 if (wLength) {
2306 /* DATA phase from gadget, STATUS phase from udc */
2307 dev->ep0_dir = (setup->bRequestType & USB_DIR_IN)
2308 ? USB_DIR_IN : USB_DIR_OUT;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002309 dev_vdbg(&dev->pdev->dev,
2310 "dev->ep0_dir = 0x%x, wLength = %d\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002311 dev->ep0_dir, wLength);
2312 spin_unlock(&dev->lock);
2313 if (dev->driver->setup(&dev->gadget,
2314 &dev->local_setup_buff) < 0)
2315 ep0_stall(dev);
2316 spin_lock(&dev->lock);
2317 dev->ep0_state = (setup->bRequestType & USB_DIR_IN)
2318 ? DATA_STATE_XMIT : DATA_STATE_RECV;
2319 } else {
2320 /* no DATA phase, IN STATUS phase from gadget */
2321 dev->ep0_dir = USB_DIR_IN;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002322 dev_vdbg(&dev->pdev->dev,
2323 "dev->ep0_dir = 0x%x, wLength = %d\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002324 dev->ep0_dir, wLength);
2325 spin_unlock(&dev->lock);
2326 if (dev->driver->setup(&dev->gadget,
2327 &dev->local_setup_buff) < 0)
2328 ep0_stall(dev);
2329 spin_lock(&dev->lock);
2330 dev->ep0_state = WAIT_FOR_OUT_STATUS;
2331 }
2332 break;
2333 }
2334end:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002335 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002336 return;
2337}
2338
2339
2340/* transfer completion, process endpoint request and free the completed dTDs
2341 * for this request
2342 */
2343static int process_ep_req(struct langwell_udc *dev, int index,
2344 struct langwell_request *curr_req)
2345{
2346 struct langwell_dtd *curr_dtd;
2347 struct langwell_dqh *curr_dqh;
2348 int td_complete, actual, remaining_length;
2349 int i, dir;
2350 u8 dtd_status = 0;
2351 int retval = 0;
2352
2353 curr_dqh = &dev->ep_dqh[index];
2354 dir = index % 2;
2355
2356 curr_dtd = curr_req->head;
2357 td_complete = 0;
2358 actual = curr_req->req.length;
2359
JiebingLi5f81f4b2010-08-05 14:17:54 +01002360 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002361
2362 for (i = 0; i < curr_req->dtd_count; i++) {
2363 remaining_length = le16_to_cpu(curr_dtd->dtd_total);
2364 actual -= remaining_length;
2365
2366 /* command execution states by dTD */
2367 dtd_status = curr_dtd->dtd_status;
2368
2369 if (!dtd_status) {
2370 /* transfers completed successfully */
2371 if (!remaining_length) {
2372 td_complete++;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002373 dev_vdbg(&dev->pdev->dev,
2374 "dTD transmitted successfully\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002375 } else {
2376 if (dir) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002377 dev_vdbg(&dev->pdev->dev,
2378 "TX dTD remains data\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002379 retval = -EPROTO;
2380 break;
2381
2382 } else {
2383 td_complete++;
2384 break;
2385 }
2386 }
2387 } else {
2388 /* transfers completed with errors */
2389 if (dtd_status & DTD_STS_ACTIVE) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002390 dev_dbg(&dev->pdev->dev,
2391 "dTD status ACTIVE dQH[%d]\n", index);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002392 retval = 1;
2393 return retval;
2394 } else if (dtd_status & DTD_STS_HALTED) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002395 dev_err(&dev->pdev->dev,
2396 "dTD error %08x dQH[%d]\n",
2397 dtd_status, index);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002398 /* clear the errors and halt condition */
2399 curr_dqh->dtd_status = 0;
2400 retval = -EPIPE;
2401 break;
2402 } else if (dtd_status & DTD_STS_DBE) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002403 dev_dbg(&dev->pdev->dev,
2404 "data buffer (overflow) error\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002405 retval = -EPROTO;
2406 break;
2407 } else if (dtd_status & DTD_STS_TRE) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002408 dev_dbg(&dev->pdev->dev,
2409 "transaction(ISO) error\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002410 retval = -EILSEQ;
2411 break;
2412 } else
JiebingLi5f81f4b2010-08-05 14:17:54 +01002413 dev_err(&dev->pdev->dev,
2414 "unknown error (0x%x)!\n",
2415 dtd_status);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002416 }
2417
2418 if (i != curr_req->dtd_count - 1)
2419 curr_dtd = (struct langwell_dtd *)
2420 curr_dtd->next_dtd_virt;
2421 }
2422
2423 if (retval)
2424 return retval;
2425
2426 curr_req->req.actual = actual;
2427
JiebingLi5f81f4b2010-08-05 14:17:54 +01002428 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002429 return 0;
2430}
2431
2432
2433/* complete DATA or STATUS phase of ep0 prime status phase if needed */
2434static void ep0_req_complete(struct langwell_udc *dev,
2435 struct langwell_ep *ep0, struct langwell_request *req)
2436{
2437 u32 new_addr;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002438 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002439
2440 if (dev->usb_state == USB_STATE_ADDRESS) {
2441 /* set the new address */
2442 new_addr = (u32)dev->dev_addr;
2443 writel(new_addr << USBADR_SHIFT, &dev->op_regs->deviceaddr);
2444
2445 new_addr = USBADR(readl(&dev->op_regs->deviceaddr));
JiebingLi5f81f4b2010-08-05 14:17:54 +01002446 dev_vdbg(&dev->pdev->dev, "new_addr = %d\n", new_addr);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002447 }
2448
2449 done(ep0, req, 0);
2450
2451 switch (dev->ep0_state) {
2452 case DATA_STATE_XMIT:
2453 /* receive status phase */
2454 if (prime_status_phase(dev, EP_DIR_OUT))
2455 ep0_stall(dev);
2456 break;
2457 case DATA_STATE_RECV:
2458 /* send status phase */
2459 if (prime_status_phase(dev, EP_DIR_IN))
2460 ep0_stall(dev);
2461 break;
2462 case WAIT_FOR_OUT_STATUS:
2463 dev->ep0_state = WAIT_FOR_SETUP;
2464 break;
2465 case WAIT_FOR_SETUP:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002466 dev_err(&dev->pdev->dev, "unexpect ep0 packets\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002467 break;
2468 default:
2469 ep0_stall(dev);
2470 break;
2471 }
2472
JiebingLi5f81f4b2010-08-05 14:17:54 +01002473 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002474}
2475
2476
2477/* USB transfer completion interrupt */
2478static void handle_trans_complete(struct langwell_udc *dev)
2479{
2480 u32 complete_bits;
2481 int i, ep_num, dir, bit_mask, status;
2482 struct langwell_ep *epn;
2483 struct langwell_request *curr_req, *temp_req;
2484
JiebingLi5f81f4b2010-08-05 14:17:54 +01002485 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002486
2487 complete_bits = readl(&dev->op_regs->endptcomplete);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002488 dev_vdbg(&dev->pdev->dev, "endptcomplete register: 0x%08x\n",
2489 complete_bits);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002490
2491 /* Write-Clear the bits in endptcomplete register */
2492 writel(complete_bits, &dev->op_regs->endptcomplete);
2493
2494 if (!complete_bits) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002495 dev_dbg(&dev->pdev->dev, "complete_bits = 0\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002496 goto done;
2497 }
2498
2499 for (i = 0; i < dev->ep_max; i++) {
2500 ep_num = i / 2;
2501 dir = i % 2;
2502
2503 bit_mask = 1 << (ep_num + 16 * dir);
2504
2505 if (!(complete_bits & bit_mask))
2506 continue;
2507
2508 /* ep0 */
2509 if (i == 1)
2510 epn = &dev->ep[0];
2511 else
2512 epn = &dev->ep[i];
2513
2514 if (epn->name == NULL) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002515 dev_warn(&dev->pdev->dev, "invalid endpoint\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002516 continue;
2517 }
2518
2519 if (i < 2)
2520 /* ep0 in and out */
JiebingLi5f81f4b2010-08-05 14:17:54 +01002521 dev_dbg(&dev->pdev->dev, "%s-%s transfer completed\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002522 epn->name,
2523 is_in(epn) ? "in" : "out");
2524 else
JiebingLi5f81f4b2010-08-05 14:17:54 +01002525 dev_dbg(&dev->pdev->dev, "%s transfer completed\n",
2526 epn->name);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002527
2528 /* process the req queue until an uncomplete request */
2529 list_for_each_entry_safe(curr_req, temp_req,
2530 &epn->queue, queue) {
2531 status = process_ep_req(dev, i, curr_req);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002532 dev_vdbg(&dev->pdev->dev, "%s req status: %d\n",
2533 epn->name, status);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002534
2535 if (status)
2536 break;
2537
2538 /* write back status to req */
2539 curr_req->req.status = status;
2540
2541 /* ep0 request completion */
2542 if (ep_num == 0) {
2543 ep0_req_complete(dev, epn, curr_req);
2544 break;
2545 } else {
2546 done(epn, curr_req, status);
2547 }
2548 }
2549 }
2550done:
JiebingLi5f81f4b2010-08-05 14:17:54 +01002551 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002552 return;
2553}
2554
2555
2556/* port change detect interrupt handler */
2557static void handle_port_change(struct langwell_udc *dev)
2558{
2559 u32 portsc1, devlc;
2560 u32 speed;
2561
JiebingLi5f81f4b2010-08-05 14:17:54 +01002562 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002563
2564 if (dev->bus_reset)
2565 dev->bus_reset = 0;
2566
2567 portsc1 = readl(&dev->op_regs->portsc1);
2568 devlc = readl(&dev->op_regs->devlc);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002569 dev_vdbg(&dev->pdev->dev, "portsc1 = 0x%08x, devlc = 0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002570 portsc1, devlc);
2571
2572 /* bus reset is finished */
2573 if (!(portsc1 & PORTS_PR)) {
2574 /* get the speed */
2575 speed = LPM_PSPD(devlc);
2576 switch (speed) {
2577 case LPM_SPEED_HIGH:
2578 dev->gadget.speed = USB_SPEED_HIGH;
2579 break;
2580 case LPM_SPEED_FULL:
2581 dev->gadget.speed = USB_SPEED_FULL;
2582 break;
2583 case LPM_SPEED_LOW:
2584 dev->gadget.speed = USB_SPEED_LOW;
2585 break;
2586 default:
2587 dev->gadget.speed = USB_SPEED_UNKNOWN;
2588 break;
2589 }
JiebingLi5f81f4b2010-08-05 14:17:54 +01002590 dev_vdbg(&dev->pdev->dev,
2591 "speed = %d, dev->gadget.speed = %d\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002592 speed, dev->gadget.speed);
2593 }
2594
2595 /* LPM L0 to L1 */
2596 if (dev->lpm && dev->lpm_state == LPM_L0)
2597 if (portsc1 & PORTS_SUSP && portsc1 & PORTS_SLP) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002598 dev_info(&dev->pdev->dev, "LPM L0 to L1\n");
2599 dev->lpm_state = LPM_L1;
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002600 }
2601
2602 /* LPM L1 to L0, force resume or remote wakeup finished */
2603 if (dev->lpm && dev->lpm_state == LPM_L1)
2604 if (!(portsc1 & PORTS_SUSP)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002605 dev_info(&dev->pdev->dev, "LPM L1 to L0\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002606 dev->lpm_state = LPM_L0;
2607 }
2608
2609 /* update USB state */
2610 if (!dev->resume_state)
2611 dev->usb_state = USB_STATE_DEFAULT;
2612
JiebingLi5f81f4b2010-08-05 14:17:54 +01002613 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002614}
2615
2616
2617/* USB reset interrupt handler */
2618static void handle_usb_reset(struct langwell_udc *dev)
2619{
2620 u32 deviceaddr,
2621 endptsetupstat,
2622 endptcomplete;
2623 unsigned long timeout;
2624
JiebingLi5f81f4b2010-08-05 14:17:54 +01002625 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002626
2627 /* Write-Clear the device address */
2628 deviceaddr = readl(&dev->op_regs->deviceaddr);
2629 writel(deviceaddr & ~USBADR_MASK, &dev->op_regs->deviceaddr);
2630
2631 dev->dev_addr = 0;
2632
2633 /* clear usb state */
2634 dev->resume_state = 0;
2635
2636 /* LPM L1 to L0, reset */
2637 if (dev->lpm)
2638 dev->lpm_state = LPM_L0;
2639
2640 dev->ep0_dir = USB_DIR_OUT;
2641 dev->ep0_state = WAIT_FOR_SETUP;
2642 dev->remote_wakeup = 0; /* default to 0 on reset */
2643 dev->gadget.b_hnp_enable = 0;
2644 dev->gadget.a_hnp_support = 0;
2645 dev->gadget.a_alt_hnp_support = 0;
2646
2647 /* Write-Clear all the setup token semaphores */
2648 endptsetupstat = readl(&dev->op_regs->endptsetupstat);
2649 writel(endptsetupstat, &dev->op_regs->endptsetupstat);
2650
2651 /* Write-Clear all the endpoint complete status bits */
2652 endptcomplete = readl(&dev->op_regs->endptcomplete);
2653 writel(endptcomplete, &dev->op_regs->endptcomplete);
2654
2655 /* wait until all endptprime bits cleared */
2656 timeout = jiffies + PRIME_TIMEOUT;
2657 while (readl(&dev->op_regs->endptprime)) {
2658 if (time_after(jiffies, timeout)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002659 dev_err(&dev->pdev->dev, "USB reset timeout\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002660 break;
2661 }
2662 cpu_relax();
2663 }
2664
2665 /* write 1s to endptflush register to clear any primed buffers */
2666 writel((u32) ~0, &dev->op_regs->endptflush);
2667
2668 if (readl(&dev->op_regs->portsc1) & PORTS_PR) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002669 dev_vdbg(&dev->pdev->dev, "USB bus reset\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002670 /* bus is reseting */
2671 dev->bus_reset = 1;
2672
2673 /* reset all the queues, stop all USB activities */
2674 stop_activity(dev, dev->driver);
2675 dev->usb_state = USB_STATE_DEFAULT;
2676 } else {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002677 dev_vdbg(&dev->pdev->dev, "device controller reset\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002678 /* controller reset */
2679 langwell_udc_reset(dev);
2680
2681 /* reset all the queues, stop all USB activities */
2682 stop_activity(dev, dev->driver);
2683
2684 /* reset ep0 dQH and endptctrl */
2685 ep0_reset(dev);
2686
2687 /* enable interrupt and set controller to run state */
2688 langwell_udc_start(dev);
2689
2690 dev->usb_state = USB_STATE_ATTACHED;
2691 }
2692
2693#ifdef OTG_TRANSCEIVER
2694 /* refer to USB OTG 6.6.2.3 b_hnp_en is cleared */
2695 if (!dev->lotg->otg.default_a)
2696 dev->lotg->hsm.b_hnp_enable = 0;
2697#endif
2698
JiebingLi5f81f4b2010-08-05 14:17:54 +01002699 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002700}
2701
2702
2703/* USB bus suspend/resume interrupt */
2704static void handle_bus_suspend(struct langwell_udc *dev)
2705{
2706 u32 devlc;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002707 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002708
2709 dev->resume_state = dev->usb_state;
2710 dev->usb_state = USB_STATE_SUSPENDED;
2711
2712#ifdef OTG_TRANSCEIVER
2713 if (dev->lotg->otg.default_a) {
2714 if (dev->lotg->hsm.b_bus_suspend_vld == 1) {
2715 dev->lotg->hsm.b_bus_suspend = 1;
2716 /* notify transceiver the state changes */
2717 if (spin_trylock(&dev->lotg->wq_lock)) {
2718 langwell_update_transceiver();
2719 spin_unlock(&dev->lotg->wq_lock);
2720 }
2721 }
2722 dev->lotg->hsm.b_bus_suspend_vld++;
2723 } else {
2724 if (!dev->lotg->hsm.a_bus_suspend) {
2725 dev->lotg->hsm.a_bus_suspend = 1;
2726 /* notify transceiver the state changes */
2727 if (spin_trylock(&dev->lotg->wq_lock)) {
2728 langwell_update_transceiver();
2729 spin_unlock(&dev->lotg->wq_lock);
2730 }
2731 }
2732 }
2733#endif
2734
2735 /* report suspend to the driver */
2736 if (dev->driver) {
2737 if (dev->driver->suspend) {
2738 spin_unlock(&dev->lock);
2739 dev->driver->suspend(&dev->gadget);
2740 spin_lock(&dev->lock);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002741 dev_dbg(&dev->pdev->dev, "suspend %s\n",
2742 dev->driver->driver.name);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002743 }
2744 }
2745
2746 /* enter PHY low power suspend */
2747 devlc = readl(&dev->op_regs->devlc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002748 devlc |= LPM_PHCD;
2749 writel(devlc, &dev->op_regs->devlc);
2750
JiebingLi5f81f4b2010-08-05 14:17:54 +01002751 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002752}
2753
2754
2755static void handle_bus_resume(struct langwell_udc *dev)
2756{
2757 u32 devlc;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002758 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002759
2760 dev->usb_state = dev->resume_state;
2761 dev->resume_state = 0;
2762
2763 /* exit PHY low power suspend */
2764 devlc = readl(&dev->op_regs->devlc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002765 devlc &= ~LPM_PHCD;
2766 writel(devlc, &dev->op_regs->devlc);
2767
2768#ifdef OTG_TRANSCEIVER
2769 if (dev->lotg->otg.default_a == 0)
2770 dev->lotg->hsm.a_bus_suspend = 0;
2771#endif
2772
2773 /* report resume to the driver */
2774 if (dev->driver) {
2775 if (dev->driver->resume) {
2776 spin_unlock(&dev->lock);
2777 dev->driver->resume(&dev->gadget);
2778 spin_lock(&dev->lock);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002779 dev_dbg(&dev->pdev->dev, "resume %s\n",
2780 dev->driver->driver.name);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002781 }
2782 }
2783
JiebingLi5f81f4b2010-08-05 14:17:54 +01002784 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002785}
2786
2787
2788/* USB device controller interrupt handler */
2789static irqreturn_t langwell_irq(int irq, void *_dev)
2790{
2791 struct langwell_udc *dev = _dev;
2792 u32 usbsts,
2793 usbintr,
2794 irq_sts,
2795 portsc1;
2796
JiebingLi5f81f4b2010-08-05 14:17:54 +01002797 dev_vdbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002798
2799 if (dev->stopped) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002800 dev_vdbg(&dev->pdev->dev, "handle IRQ_NONE\n");
2801 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002802 return IRQ_NONE;
2803 }
2804
2805 spin_lock(&dev->lock);
2806
2807 /* USB status */
2808 usbsts = readl(&dev->op_regs->usbsts);
2809
2810 /* USB interrupt enable */
2811 usbintr = readl(&dev->op_regs->usbintr);
2812
2813 irq_sts = usbsts & usbintr;
JiebingLi5f81f4b2010-08-05 14:17:54 +01002814 dev_vdbg(&dev->pdev->dev,
2815 "usbsts = 0x%08x, usbintr = 0x%08x, irq_sts = 0x%08x\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002816 usbsts, usbintr, irq_sts);
2817
2818 if (!irq_sts) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002819 dev_vdbg(&dev->pdev->dev, "handle IRQ_NONE\n");
2820 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002821 spin_unlock(&dev->lock);
2822 return IRQ_NONE;
2823 }
2824
2825 /* Write-Clear interrupt status bits */
2826 writel(irq_sts, &dev->op_regs->usbsts);
2827
2828 /* resume from suspend */
2829 portsc1 = readl(&dev->op_regs->portsc1);
2830 if (dev->usb_state == USB_STATE_SUSPENDED)
2831 if (!(portsc1 & PORTS_SUSP))
2832 handle_bus_resume(dev);
2833
2834 /* USB interrupt */
2835 if (irq_sts & STS_UI) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002836 dev_vdbg(&dev->pdev->dev, "USB interrupt\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002837
2838 /* setup packet received from ep0 */
2839 if (readl(&dev->op_regs->endptsetupstat)
2840 & EP0SETUPSTAT_MASK) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002841 dev_vdbg(&dev->pdev->dev,
2842 "USB SETUP packet received interrupt\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002843 /* setup tripwire semaphone */
2844 setup_tripwire(dev);
2845 handle_setup_packet(dev, &dev->local_setup_buff);
2846 }
2847
2848 /* USB transfer completion */
2849 if (readl(&dev->op_regs->endptcomplete)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002850 dev_vdbg(&dev->pdev->dev,
2851 "USB transfer completion interrupt\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002852 handle_trans_complete(dev);
2853 }
2854 }
2855
2856 /* SOF received interrupt (for ISO transfer) */
2857 if (irq_sts & STS_SRI) {
2858 /* FIXME */
JiebingLi5f81f4b2010-08-05 14:17:54 +01002859 /* dev_vdbg(&dev->pdev->dev, "SOF received interrupt\n"); */
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002860 }
2861
2862 /* port change detect interrupt */
2863 if (irq_sts & STS_PCI) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002864 dev_vdbg(&dev->pdev->dev, "port change detect interrupt\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002865 handle_port_change(dev);
2866 }
2867
2868 /* suspend interrrupt */
2869 if (irq_sts & STS_SLI) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002870 dev_vdbg(&dev->pdev->dev, "suspend interrupt\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002871 handle_bus_suspend(dev);
2872 }
2873
2874 /* USB reset interrupt */
2875 if (irq_sts & STS_URI) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01002876 dev_vdbg(&dev->pdev->dev, "USB reset interrupt\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002877 handle_usb_reset(dev);
2878 }
2879
2880 /* USB error or system error interrupt */
2881 if (irq_sts & (STS_UEI | STS_SEI)) {
2882 /* FIXME */
JiebingLi5f81f4b2010-08-05 14:17:54 +01002883 dev_warn(&dev->pdev->dev, "error IRQ, irq_sts: %x\n", irq_sts);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002884 }
2885
2886 spin_unlock(&dev->lock);
2887
JiebingLi5f81f4b2010-08-05 14:17:54 +01002888 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002889 return IRQ_HANDLED;
2890}
2891
2892
2893/*-------------------------------------------------------------------------*/
2894
2895/* release device structure */
2896static void gadget_release(struct device *_dev)
2897{
2898 struct langwell_udc *dev = the_controller;
2899
JiebingLi5f81f4b2010-08-05 14:17:54 +01002900 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002901
2902 complete(dev->done);
2903
JiebingLi5f81f4b2010-08-05 14:17:54 +01002904 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002905 kfree(dev);
2906}
2907
2908
2909/* tear down the binding between this driver and the pci device */
2910static void langwell_udc_remove(struct pci_dev *pdev)
2911{
2912 struct langwell_udc *dev = the_controller;
2913
2914 DECLARE_COMPLETION(done);
2915
2916 BUG_ON(dev->driver);
JiebingLi5f81f4b2010-08-05 14:17:54 +01002917 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002918
2919 dev->done = &done;
2920
2921 /* free memory allocated in probe */
2922 if (dev->dtd_pool)
2923 dma_pool_destroy(dev->dtd_pool);
2924
2925 if (dev->status_req) {
2926 kfree(dev->status_req->req.buf);
2927 kfree(dev->status_req);
2928 }
2929
2930 if (dev->ep_dqh)
2931 dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
2932 dev->ep_dqh, dev->ep_dqh_dma);
2933
2934 kfree(dev->ep);
2935
2936 /* diable IRQ handler */
2937 if (dev->got_irq)
2938 free_irq(pdev->irq, dev);
2939
2940#ifndef OTG_TRANSCEIVER
2941 if (dev->cap_regs)
2942 iounmap(dev->cap_regs);
2943
2944 if (dev->region)
2945 release_mem_region(pci_resource_start(pdev, 0),
2946 pci_resource_len(pdev, 0));
2947
2948 if (dev->enabled)
2949 pci_disable_device(pdev);
2950#else
2951 if (dev->transceiver) {
2952 otg_put_transceiver(dev->transceiver);
2953 dev->transceiver = NULL;
2954 dev->lotg = NULL;
2955 }
2956#endif
2957
2958 dev->cap_regs = NULL;
2959
JiebingLi5f81f4b2010-08-05 14:17:54 +01002960 dev_info(&dev->pdev->dev, "unbind\n");
2961 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08002962
2963 device_unregister(&dev->gadget.dev);
2964 device_remove_file(&pdev->dev, &dev_attr_langwell_udc);
2965
2966#ifndef OTG_TRANSCEIVER
2967 pci_set_drvdata(pdev, NULL);
2968#endif
2969
2970 /* free dev, wait for the release() finished */
2971 wait_for_completion(&done);
2972
2973 the_controller = NULL;
2974}
2975
2976
2977/*
2978 * wrap this driver around the specified device, but
2979 * don't respond over USB until a gadget driver binds to us.
2980 */
2981static int langwell_udc_probe(struct pci_dev *pdev,
2982 const struct pci_device_id *id)
2983{
2984 struct langwell_udc *dev;
2985#ifndef OTG_TRANSCEIVER
2986 unsigned long resource, len;
2987#endif
2988 void __iomem *base = NULL;
2989 size_t size;
2990 int retval;
2991
2992 if (the_controller) {
2993 dev_warn(&pdev->dev, "ignoring\n");
2994 return -EBUSY;
2995 }
2996
2997 /* alloc, and start init */
2998 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2999 if (dev == NULL) {
3000 retval = -ENOMEM;
3001 goto error;
3002 }
3003
3004 /* initialize device spinlock */
3005 spin_lock_init(&dev->lock);
3006
3007 dev->pdev = pdev;
JiebingLi5f81f4b2010-08-05 14:17:54 +01003008 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003009
3010#ifdef OTG_TRANSCEIVER
3011 /* PCI device is already enabled by otg_transceiver driver */
3012 dev->enabled = 1;
3013
3014 /* mem region and register base */
3015 dev->region = 1;
3016 dev->transceiver = otg_get_transceiver();
3017 dev->lotg = otg_to_langwell(dev->transceiver);
3018 base = dev->lotg->regs;
3019#else
3020 pci_set_drvdata(pdev, dev);
3021
3022 /* now all the pci goodies ... */
3023 if (pci_enable_device(pdev) < 0) {
3024 retval = -ENODEV;
3025 goto error;
3026 }
3027 dev->enabled = 1;
3028
3029 /* control register: BAR 0 */
3030 resource = pci_resource_start(pdev, 0);
3031 len = pci_resource_len(pdev, 0);
3032 if (!request_mem_region(resource, len, driver_name)) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003033 dev_err(&dev->pdev->dev, "controller already in use\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003034 retval = -EBUSY;
3035 goto error;
3036 }
3037 dev->region = 1;
3038
3039 base = ioremap_nocache(resource, len);
3040#endif
3041 if (base == NULL) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003042 dev_err(&dev->pdev->dev, "can't map memory\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003043 retval = -EFAULT;
3044 goto error;
3045 }
3046
3047 dev->cap_regs = (struct langwell_cap_regs __iomem *) base;
JiebingLi5f81f4b2010-08-05 14:17:54 +01003048 dev_vdbg(&dev->pdev->dev, "dev->cap_regs: %p\n", dev->cap_regs);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003049 dev->op_regs = (struct langwell_op_regs __iomem *)
3050 (base + OP_REG_OFFSET);
JiebingLi5f81f4b2010-08-05 14:17:54 +01003051 dev_vdbg(&dev->pdev->dev, "dev->op_regs: %p\n", dev->op_regs);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003052
3053 /* irq setup after old hardware is cleaned up */
3054 if (!pdev->irq) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003055 dev_err(&dev->pdev->dev, "No IRQ. Check PCI setup!\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003056 retval = -ENODEV;
3057 goto error;
3058 }
3059
3060#ifndef OTG_TRANSCEIVER
JiebingLi5f81f4b2010-08-05 14:17:54 +01003061 dev_info(&dev->pdev->dev,
3062 "irq %d, io mem: 0x%08lx, len: 0x%08lx, pci mem 0x%p\n",
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003063 pdev->irq, resource, len, base);
3064 /* enables bus-mastering for device dev */
3065 pci_set_master(pdev);
3066
3067 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
3068 driver_name, dev) != 0) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003069 dev_err(&dev->pdev->dev,
3070 "request interrupt %d failed\n", pdev->irq);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003071 retval = -EBUSY;
3072 goto error;
3073 }
3074 dev->got_irq = 1;
3075#endif
3076
3077 /* set stopped bit */
3078 dev->stopped = 1;
3079
3080 /* capabilities and endpoint number */
3081 dev->lpm = (readl(&dev->cap_regs->hccparams) & HCC_LEN) ? 1 : 0;
3082 dev->dciversion = readw(&dev->cap_regs->dciversion);
3083 dev->devcap = (readl(&dev->cap_regs->dccparams) & DEVCAP) ? 1 : 0;
JiebingLi5f81f4b2010-08-05 14:17:54 +01003084 dev_vdbg(&dev->pdev->dev, "dev->lpm: %d\n", dev->lpm);
3085 dev_vdbg(&dev->pdev->dev, "dev->dciversion: 0x%04x\n",
3086 dev->dciversion);
3087 dev_vdbg(&dev->pdev->dev, "dccparams: 0x%08x\n",
3088 readl(&dev->cap_regs->dccparams));
3089 dev_vdbg(&dev->pdev->dev, "dev->devcap: %d\n", dev->devcap);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003090 if (!dev->devcap) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003091 dev_err(&dev->pdev->dev, "can't support device mode\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003092 retval = -ENODEV;
3093 goto error;
3094 }
3095
3096 /* a pair of endpoints (out/in) for each address */
3097 dev->ep_max = DEN(readl(&dev->cap_regs->dccparams)) * 2;
JiebingLi5f81f4b2010-08-05 14:17:54 +01003098 dev_vdbg(&dev->pdev->dev, "dev->ep_max: %d\n", dev->ep_max);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003099
3100 /* allocate endpoints memory */
3101 dev->ep = kzalloc(sizeof(struct langwell_ep) * dev->ep_max,
3102 GFP_KERNEL);
3103 if (!dev->ep) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003104 dev_err(&dev->pdev->dev, "allocate endpoints memory failed\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003105 retval = -ENOMEM;
3106 goto error;
3107 }
3108
3109 /* allocate device dQH memory */
3110 size = dev->ep_max * sizeof(struct langwell_dqh);
JiebingLi5f81f4b2010-08-05 14:17:54 +01003111 dev_vdbg(&dev->pdev->dev, "orig size = %d\n", size);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003112 if (size < DQH_ALIGNMENT)
3113 size = DQH_ALIGNMENT;
3114 else if ((size % DQH_ALIGNMENT) != 0) {
3115 size += DQH_ALIGNMENT + 1;
3116 size &= ~(DQH_ALIGNMENT - 1);
3117 }
3118 dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
3119 &dev->ep_dqh_dma, GFP_KERNEL);
3120 if (!dev->ep_dqh) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003121 dev_err(&dev->pdev->dev, "allocate dQH memory failed\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003122 retval = -ENOMEM;
3123 goto error;
3124 }
3125 dev->ep_dqh_size = size;
JiebingLi5f81f4b2010-08-05 14:17:54 +01003126 dev_vdbg(&dev->pdev->dev, "ep_dqh_size = %d\n", dev->ep_dqh_size);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003127
3128 /* initialize ep0 status request structure */
3129 dev->status_req = kzalloc(sizeof(struct langwell_request), GFP_KERNEL);
3130 if (!dev->status_req) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003131 dev_err(&dev->pdev->dev,
3132 "allocate status_req memory failed\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003133 retval = -ENOMEM;
3134 goto error;
3135 }
3136 INIT_LIST_HEAD(&dev->status_req->queue);
3137
3138 /* allocate a small amount of memory to get valid address */
3139 dev->status_req->req.buf = kmalloc(8, GFP_KERNEL);
3140 dev->status_req->req.dma = virt_to_phys(dev->status_req->req.buf);
3141
3142 dev->resume_state = USB_STATE_NOTATTACHED;
3143 dev->usb_state = USB_STATE_POWERED;
3144 dev->ep0_dir = USB_DIR_OUT;
3145 dev->remote_wakeup = 0; /* default to 0 on reset */
3146
3147#ifndef OTG_TRANSCEIVER
3148 /* reset device controller */
3149 langwell_udc_reset(dev);
3150#endif
3151
3152 /* initialize gadget structure */
3153 dev->gadget.ops = &langwell_ops; /* usb_gadget_ops */
3154 dev->gadget.ep0 = &dev->ep[0].ep; /* gadget ep0 */
3155 INIT_LIST_HEAD(&dev->gadget.ep_list); /* ep_list */
3156 dev->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
3157 dev->gadget.is_dualspeed = 1; /* support dual speed */
3158#ifdef OTG_TRANSCEIVER
3159 dev->gadget.is_otg = 1; /* support otg mode */
3160#endif
3161
3162 /* the "gadget" abstracts/virtualizes the controller */
3163 dev_set_name(&dev->gadget.dev, "gadget");
3164 dev->gadget.dev.parent = &pdev->dev;
3165 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
3166 dev->gadget.dev.release = gadget_release;
3167 dev->gadget.name = driver_name; /* gadget name */
3168
3169 /* controller endpoints reinit */
3170 eps_reinit(dev);
3171
3172#ifndef OTG_TRANSCEIVER
3173 /* reset ep0 dQH and endptctrl */
3174 ep0_reset(dev);
3175#endif
3176
3177 /* create dTD dma_pool resource */
3178 dev->dtd_pool = dma_pool_create("langwell_dtd",
3179 &dev->pdev->dev,
3180 sizeof(struct langwell_dtd),
3181 DTD_ALIGNMENT,
3182 DMA_BOUNDARY);
3183
3184 if (!dev->dtd_pool) {
3185 retval = -ENOMEM;
3186 goto error;
3187 }
3188
3189 /* done */
JiebingLi5f81f4b2010-08-05 14:17:54 +01003190 dev_info(&dev->pdev->dev, "%s\n", driver_desc);
3191 dev_info(&dev->pdev->dev, "irq %d, pci mem %p\n", pdev->irq, base);
3192 dev_info(&dev->pdev->dev, "Driver version: " DRIVER_VERSION "\n");
3193 dev_info(&dev->pdev->dev, "Support (max) %d endpoints\n", dev->ep_max);
3194 dev_info(&dev->pdev->dev, "Device interface version: 0x%04x\n",
3195 dev->dciversion);
3196 dev_info(&dev->pdev->dev, "Controller mode: %s\n",
3197 dev->devcap ? "Device" : "Host");
3198 dev_info(&dev->pdev->dev, "Support USB LPM: %s\n",
3199 dev->lpm ? "Yes" : "No");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003200
JiebingLi5f81f4b2010-08-05 14:17:54 +01003201 dev_vdbg(&dev->pdev->dev,
3202 "After langwell_udc_probe(), print all registers:\n");
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003203 print_all_registers(dev);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003204
3205 the_controller = dev;
3206
3207 retval = device_register(&dev->gadget.dev);
3208 if (retval)
3209 goto error;
3210
3211 retval = device_create_file(&pdev->dev, &dev_attr_langwell_udc);
3212 if (retval)
3213 goto error;
3214
JiebingLi5f81f4b2010-08-05 14:17:54 +01003215 dev_vdbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003216 return 0;
3217
3218error:
3219 if (dev) {
JiebingLi5f81f4b2010-08-05 14:17:54 +01003220 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003221 langwell_udc_remove(pdev);
3222 }
3223
3224 return retval;
3225}
3226
3227
3228/* device controller suspend */
3229static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
3230{
3231 struct langwell_udc *dev = the_controller;
3232 u32 devlc;
3233
JiebingLi5f81f4b2010-08-05 14:17:54 +01003234 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003235
3236 /* disable interrupt and set controller to stop state */
3237 langwell_udc_stop(dev);
3238
3239 /* diable IRQ handler */
3240 if (dev->got_irq)
3241 free_irq(pdev->irq, dev);
3242 dev->got_irq = 0;
3243
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003244 /* save PCI state */
3245 pci_save_state(pdev);
3246
3247 /* set device power state */
3248 pci_set_power_state(pdev, PCI_D3hot);
3249
3250 /* enter PHY low power suspend */
3251 devlc = readl(&dev->op_regs->devlc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003252 devlc |= LPM_PHCD;
3253 writel(devlc, &dev->op_regs->devlc);
3254
JiebingLi5f81f4b2010-08-05 14:17:54 +01003255 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003256 return 0;
3257}
3258
3259
3260/* device controller resume */
3261static int langwell_udc_resume(struct pci_dev *pdev)
3262{
3263 struct langwell_udc *dev = the_controller;
3264 u32 devlc;
3265
JiebingLi5f81f4b2010-08-05 14:17:54 +01003266 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003267
3268 /* exit PHY low power suspend */
3269 devlc = readl(&dev->op_regs->devlc);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003270 devlc &= ~LPM_PHCD;
3271 writel(devlc, &dev->op_regs->devlc);
3272
3273 /* set device D0 power state */
3274 pci_set_power_state(pdev, PCI_D0);
3275
3276 /* restore PCI state */
3277 pci_restore_state(pdev);
3278
3279 /* enable IRQ handler */
JiebingLi5f81f4b2010-08-05 14:17:54 +01003280 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
3281 driver_name, dev) != 0) {
3282 dev_err(&dev->pdev->dev, "request interrupt %d failed\n",
3283 pdev->irq);
3284 return -EBUSY;
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003285 }
3286 dev->got_irq = 1;
3287
3288 /* reset and start controller to run state */
3289 if (dev->stopped) {
3290 /* reset device controller */
3291 langwell_udc_reset(dev);
3292
3293 /* reset ep0 dQH and endptctrl */
3294 ep0_reset(dev);
3295
3296 /* start device if gadget is loaded */
3297 if (dev->driver)
3298 langwell_udc_start(dev);
3299 }
3300
3301 /* reset USB status */
3302 dev->usb_state = USB_STATE_ATTACHED;
3303 dev->ep0_state = WAIT_FOR_SETUP;
3304 dev->ep0_dir = USB_DIR_OUT;
3305
JiebingLi5f81f4b2010-08-05 14:17:54 +01003306 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003307 return 0;
3308}
3309
3310
3311/* pci driver shutdown */
3312static void langwell_udc_shutdown(struct pci_dev *pdev)
3313{
3314 struct langwell_udc *dev = the_controller;
3315 u32 usbmode;
3316
JiebingLi5f81f4b2010-08-05 14:17:54 +01003317 dev_dbg(&dev->pdev->dev, "---> %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003318
3319 /* reset controller mode to IDLE */
3320 usbmode = readl(&dev->op_regs->usbmode);
JiebingLi5f81f4b2010-08-05 14:17:54 +01003321 dev_dbg(&dev->pdev->dev, "usbmode = 0x%08x\n", usbmode);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003322 usbmode &= (~3 | MODE_IDLE);
3323 writel(usbmode, &dev->op_regs->usbmode);
3324
JiebingLi5f81f4b2010-08-05 14:17:54 +01003325 dev_dbg(&dev->pdev->dev, "<--- %s()\n", __func__);
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003326}
3327
3328/*-------------------------------------------------------------------------*/
3329
3330static const struct pci_device_id pci_ids[] = { {
3331 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3332 .class_mask = ~0,
3333 .vendor = 0x8086,
3334 .device = 0x0811,
3335 .subvendor = PCI_ANY_ID,
3336 .subdevice = PCI_ANY_ID,
3337}, { /* end: all zeroes */ }
3338};
3339
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003340MODULE_DEVICE_TABLE(pci, pci_ids);
3341
3342
3343static struct pci_driver langwell_pci_driver = {
3344 .name = (char *) driver_name,
3345 .id_table = pci_ids,
3346
3347 .probe = langwell_udc_probe,
3348 .remove = langwell_udc_remove,
3349
3350 /* device controller suspend/resume */
3351 .suspend = langwell_udc_suspend,
3352 .resume = langwell_udc_resume,
3353
3354 .shutdown = langwell_udc_shutdown,
3355};
3356
3357
Xiaochen Shen5be19a92009-06-04 15:34:49 +08003358static int __init init(void)
3359{
3360#ifdef OTG_TRANSCEIVER
3361 return langwell_register_peripheral(&langwell_pci_driver);
3362#else
3363 return pci_register_driver(&langwell_pci_driver);
3364#endif
3365}
3366module_init(init);
3367
3368
3369static void __exit cleanup(void)
3370{
3371#ifdef OTG_TRANSCEIVER
3372 return langwell_unregister_peripheral(&langwell_pci_driver);
3373#else
3374 pci_unregister_driver(&langwell_pci_driver);
3375#endif
3376}
3377module_exit(cleanup);
3378
JiebingLi5f81f4b2010-08-05 14:17:54 +01003379
3380MODULE_DESCRIPTION(DRIVER_DESC);
3381MODULE_AUTHOR("Xiaochen Shen <xiaochen.shen@intel.com>");
3382MODULE_VERSION(DRIVER_VERSION);
3383MODULE_LICENSE("GPL");
3384