blob: d19537a0613d3eb1b71bdb547fcbb71403984b2c [file] [log] [blame]
David Lopoaa69a802008-11-17 14:14:51 -08001/*
2 * ci13xxx_udc.c - MIPS USB IP core family device controller
3 *
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5 *
6 * Author: David Lopo
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13/*
14 * Description: MIPS USB IP core family device controller
15 * Currently it only supports IP part number CI13412
16 *
17 * This driver is composed of several blocks:
18 * - HW: hardware interface
19 * - DBG: debug facilities (optional)
20 * - UTIL: utilities
21 * - ISR: interrupts handling
22 * - ENDPT: endpoint operations (Gadget API)
23 * - GADGET: gadget operations (Gadget API)
24 * - BUS: bus glue code, bus abstraction layer
David Lopoaa69a802008-11-17 14:14:51 -080025 *
26 * Compile Options
27 * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
28 * - STALL_IN: non-empty bulk-in pipes cannot be halted
29 * if defined mass storage compliance succeeds but with warnings
30 * => case 4: Hi > Dn
31 * => case 5: Hi > Di
32 * => case 8: Hi <> Do
33 * if undefined usbtest 13 fails
34 * - TRACE: enable function tracing (depends on DEBUG)
35 *
36 * Main Features
37 * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
38 * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
39 * - Normal & LPM support
40 *
41 * USBTEST Report
42 * - OK: 0-12, 13 (STALL_IN defined) & 14
43 * - Not Supported: 15 & 16 (ISO)
44 *
45 * TODO List
46 * - OTG
47 * - Isochronous & Interrupt Traffic
48 * - Handle requests which spawns into several TDs
49 * - GET_STATUS(device) - always reports 0
50 * - Gadget API (majority of optional features)
51 * - Suspend & Remote Wakeup
52 */
Matthias Kaehlcke36825a22009-04-15 22:28:36 +020053#include <linux/delay.h>
David Lopoaa69a802008-11-17 14:14:51 -080054#include <linux/device.h>
55#include <linux/dmapool.h>
56#include <linux/dma-mapping.h>
57#include <linux/init.h>
58#include <linux/interrupt.h>
David Lopoaa69a802008-11-17 14:14:51 -080059#include <linux/io.h>
60#include <linux/irq.h>
61#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090062#include <linux/slab.h>
David Lopoaa69a802008-11-17 14:14:51 -080063#include <linux/usb/ch9.h>
64#include <linux/usb/gadget.h>
65
66#include "ci13xxx_udc.h"
67
68
69/******************************************************************************
70 * DEFINE
71 *****************************************************************************/
72/* ctrl register bank access */
73static DEFINE_SPINLOCK(udc_lock);
74
David Lopoaa69a802008-11-17 14:14:51 -080075/* control endpoint description */
76static const struct usb_endpoint_descriptor
77ctrl_endpt_desc = {
78 .bLength = USB_DT_ENDPOINT_SIZE,
79 .bDescriptorType = USB_DT_ENDPOINT,
80
81 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
82 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
83};
84
85/* UDC descriptor */
86static struct ci13xxx *_udc;
87
88/* Interrupt statistics */
89#define ISR_MASK 0x1F
90static struct {
91 u32 test;
92 u32 ui;
93 u32 uei;
94 u32 pci;
95 u32 uri;
96 u32 sli;
97 u32 none;
98 struct {
99 u32 cnt;
100 u32 buf[ISR_MASK+1];
101 u32 idx;
102 } hndl;
103} isr_statistics;
104
105/**
106 * ffs_nr: find first (least significant) bit set
107 * @x: the word to search
108 *
109 * This function returns bit number (instead of position)
110 */
111static int ffs_nr(u32 x)
112{
113 int n = ffs(x);
114
115 return n ? n-1 : 32;
116}
117
118/******************************************************************************
119 * HW block
120 *****************************************************************************/
121/* register bank descriptor */
122static struct {
123 unsigned lpm; /* is LPM? */
124 void __iomem *abs; /* bus map offset */
125 void __iomem *cap; /* bus map offset + CAP offset + CAP data */
126 size_t size; /* bank size */
127} hw_bank;
128
129/* UDC register map */
130#define ABS_CAPLENGTH (0x100UL)
131#define ABS_HCCPARAMS (0x108UL)
132#define ABS_DCCPARAMS (0x124UL)
133#define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL)
134/* offset to CAPLENTGH (addr + data) */
135#define CAP_USBCMD (0x000UL)
136#define CAP_USBSTS (0x004UL)
137#define CAP_USBINTR (0x008UL)
138#define CAP_DEVICEADDR (0x014UL)
139#define CAP_ENDPTLISTADDR (0x018UL)
140#define CAP_PORTSC (0x044UL)
David Lopof23e6492009-04-16 14:35:24 -0700141#define CAP_DEVLC (0x084UL)
David Lopoaa69a802008-11-17 14:14:51 -0800142#define CAP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL)
143#define CAP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL)
144#define CAP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL)
145#define CAP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL)
146#define CAP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL)
147#define CAP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL)
148#define CAP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL)
149#define CAP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL)
150
151/* maximum number of enpoints: valid only after hw_device_reset() */
152static unsigned hw_ep_max;
153
154/**
155 * hw_ep_bit: calculates the bit number
156 * @num: endpoint number
157 * @dir: endpoint direction
158 *
159 * This function returns bit number
160 */
161static inline int hw_ep_bit(int num, int dir)
162{
163 return num + (dir ? 16 : 0);
164}
165
166/**
167 * hw_aread: reads from register bitfield
168 * @addr: address relative to bus map
169 * @mask: bitfield mask
170 *
171 * This function returns register bitfield data
172 */
173static u32 hw_aread(u32 addr, u32 mask)
174{
175 return ioread32(addr + hw_bank.abs) & mask;
176}
177
178/**
179 * hw_awrite: writes to register bitfield
180 * @addr: address relative to bus map
181 * @mask: bitfield mask
182 * @data: new data
183 */
184static void hw_awrite(u32 addr, u32 mask, u32 data)
185{
186 iowrite32(hw_aread(addr, ~mask) | (data & mask),
187 addr + hw_bank.abs);
188}
189
190/**
191 * hw_cread: reads from register bitfield
192 * @addr: address relative to CAP offset plus content
193 * @mask: bitfield mask
194 *
195 * This function returns register bitfield data
196 */
197static u32 hw_cread(u32 addr, u32 mask)
198{
199 return ioread32(addr + hw_bank.cap) & mask;
200}
201
202/**
203 * hw_cwrite: writes to register bitfield
204 * @addr: address relative to CAP offset plus content
205 * @mask: bitfield mask
206 * @data: new data
207 */
208static void hw_cwrite(u32 addr, u32 mask, u32 data)
209{
210 iowrite32(hw_cread(addr, ~mask) | (data & mask),
211 addr + hw_bank.cap);
212}
213
214/**
215 * hw_ctest_and_clear: tests & clears register bitfield
216 * @addr: address relative to CAP offset plus content
217 * @mask: bitfield mask
218 *
219 * This function returns register bitfield data
220 */
221static u32 hw_ctest_and_clear(u32 addr, u32 mask)
222{
223 u32 reg = hw_cread(addr, mask);
224
225 iowrite32(reg, addr + hw_bank.cap);
226 return reg;
227}
228
229/**
230 * hw_ctest_and_write: tests & writes register bitfield
231 * @addr: address relative to CAP offset plus content
232 * @mask: bitfield mask
233 * @data: new data
234 *
235 * This function returns register bitfield data
236 */
237static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data)
238{
239 u32 reg = hw_cread(addr, ~0);
240
241 iowrite32((reg & ~mask) | (data & mask), addr + hw_bank.cap);
242 return (reg & mask) >> ffs_nr(mask);
243}
244
245/**
246 * hw_device_reset: resets chip (execute without interruption)
247 * @base: register base address
248 *
249 * This function returns an error code
250 */
251static int hw_device_reset(void __iomem *base)
252{
253 u32 reg;
254
255 /* bank is a module variable */
256 hw_bank.abs = base;
257
258 hw_bank.cap = hw_bank.abs;
259 hw_bank.cap += ABS_CAPLENGTH;
260 hw_bank.cap += ioread8(hw_bank.cap);
261
262 reg = hw_aread(ABS_HCCPARAMS, HCCPARAMS_LEN) >> ffs_nr(HCCPARAMS_LEN);
263 hw_bank.lpm = reg;
264 hw_bank.size = hw_bank.cap - hw_bank.abs;
265 hw_bank.size += CAP_LAST;
266 hw_bank.size /= sizeof(u32);
267
268 /* should flush & stop before reset */
269 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0);
270 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
271
272 hw_cwrite(CAP_USBCMD, USBCMD_RST, USBCMD_RST);
273 while (hw_cread(CAP_USBCMD, USBCMD_RST))
274 udelay(10); /* not RTOS friendly */
275
276 /* USBMODE should be configured step by step */
277 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
278 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
279 hw_cwrite(CAP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); /* HW >= 2.3 */
280
281 if (hw_cread(CAP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
282 pr_err("cannot enter in device mode");
283 pr_err("lpm = %i", hw_bank.lpm);
284 return -ENODEV;
285 }
286
287 reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
288 if (reg == 0 || reg > ENDPT_MAX)
289 return -ENODEV;
290
291 hw_ep_max = reg; /* cache hw ENDPT_MAX */
292
293 /* setup lock mode ? */
294
295 /* ENDPTSETUPSTAT is '0' by default */
296
297 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
298
299 return 0;
300}
301
302/**
303 * hw_device_state: enables/disables interrupts & starts/stops device (execute
304 * without interruption)
305 * @dma: 0 => disable, !0 => enable and set dma engine
306 *
307 * This function returns an error code
308 */
309static int hw_device_state(u32 dma)
310{
311 if (dma) {
312 hw_cwrite(CAP_ENDPTLISTADDR, ~0, dma);
313 /* interrupt, error, port change, reset, sleep/suspend */
314 hw_cwrite(CAP_USBINTR, ~0,
315 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
316 hw_cwrite(CAP_USBCMD, USBCMD_RS, USBCMD_RS);
317 } else {
318 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
319 hw_cwrite(CAP_USBINTR, ~0, 0);
320 }
321 return 0;
322}
323
324/**
325 * hw_ep_flush: flush endpoint fifo (execute without interruption)
326 * @num: endpoint number
327 * @dir: endpoint direction
328 *
329 * This function returns an error code
330 */
331static int hw_ep_flush(int num, int dir)
332{
333 int n = hw_ep_bit(num, dir);
334
335 do {
336 /* flush any pending transfer */
337 hw_cwrite(CAP_ENDPTFLUSH, BIT(n), BIT(n));
338 while (hw_cread(CAP_ENDPTFLUSH, BIT(n)))
339 cpu_relax();
340 } while (hw_cread(CAP_ENDPTSTAT, BIT(n)));
341
342 return 0;
343}
344
345/**
346 * hw_ep_disable: disables endpoint (execute without interruption)
347 * @num: endpoint number
348 * @dir: endpoint direction
349 *
350 * This function returns an error code
351 */
352static int hw_ep_disable(int num, int dir)
353{
354 hw_ep_flush(num, dir);
355 hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32),
356 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
357 return 0;
358}
359
360/**
361 * hw_ep_enable: enables endpoint (execute without interruption)
362 * @num: endpoint number
363 * @dir: endpoint direction
364 * @type: endpoint type
365 *
366 * This function returns an error code
367 */
368static int hw_ep_enable(int num, int dir, int type)
369{
370 u32 mask, data;
371
372 if (dir) {
373 mask = ENDPTCTRL_TXT; /* type */
374 data = type << ffs_nr(mask);
375
376 mask |= ENDPTCTRL_TXS; /* unstall */
377 mask |= ENDPTCTRL_TXR; /* reset data toggle */
378 data |= ENDPTCTRL_TXR;
379 mask |= ENDPTCTRL_TXE; /* enable */
380 data |= ENDPTCTRL_TXE;
381 } else {
382 mask = ENDPTCTRL_RXT; /* type */
383 data = type << ffs_nr(mask);
384
385 mask |= ENDPTCTRL_RXS; /* unstall */
386 mask |= ENDPTCTRL_RXR; /* reset data toggle */
387 data |= ENDPTCTRL_RXR;
388 mask |= ENDPTCTRL_RXE; /* enable */
389 data |= ENDPTCTRL_RXE;
390 }
391 hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32), mask, data);
392 return 0;
393}
394
395/**
396 * hw_ep_get_halt: return endpoint halt status
397 * @num: endpoint number
398 * @dir: endpoint direction
399 *
400 * This function returns 1 if endpoint halted
401 */
402static int hw_ep_get_halt(int num, int dir)
403{
404 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
405
406 return hw_cread(CAP_ENDPTCTRL + num * sizeof(u32), mask) ? 1 : 0;
407}
408
409/**
410 * hw_ep_is_primed: test if endpoint is primed (execute without interruption)
411 * @num: endpoint number
412 * @dir: endpoint direction
413 *
414 * This function returns true if endpoint primed
415 */
416static int hw_ep_is_primed(int num, int dir)
417{
418 u32 reg = hw_cread(CAP_ENDPTPRIME, ~0) | hw_cread(CAP_ENDPTSTAT, ~0);
419
420 return test_bit(hw_ep_bit(num, dir), (void *)&reg);
421}
422
423/**
424 * hw_test_and_clear_setup_status: test & clear setup status (execute without
425 * interruption)
426 * @n: bit number (endpoint)
427 *
428 * This function returns setup status
429 */
430static int hw_test_and_clear_setup_status(int n)
431{
432 return hw_ctest_and_clear(CAP_ENDPTSETUPSTAT, BIT(n));
433}
434
435/**
436 * hw_ep_prime: primes endpoint (execute without interruption)
437 * @num: endpoint number
438 * @dir: endpoint direction
439 * @is_ctrl: true if control endpoint
440 *
441 * This function returns an error code
442 */
443static int hw_ep_prime(int num, int dir, int is_ctrl)
444{
445 int n = hw_ep_bit(num, dir);
446
447 /* the caller should flush first */
448 if (hw_ep_is_primed(num, dir))
449 return -EBUSY;
450
451 if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
452 return -EAGAIN;
453
454 hw_cwrite(CAP_ENDPTPRIME, BIT(n), BIT(n));
455
456 while (hw_cread(CAP_ENDPTPRIME, BIT(n)))
457 cpu_relax();
458 if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
459 return -EAGAIN;
460
461 /* status shoult be tested according with manual but it doesn't work */
462 return 0;
463}
464
465/**
466 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
467 * without interruption)
468 * @num: endpoint number
469 * @dir: endpoint direction
470 * @value: true => stall, false => unstall
471 *
472 * This function returns an error code
473 */
474static int hw_ep_set_halt(int num, int dir, int value)
475{
476 if (value != 0 && value != 1)
477 return -EINVAL;
478
479 do {
480 u32 addr = CAP_ENDPTCTRL + num * sizeof(u32);
481 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
482 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
483
484 /* data toggle - reserved for EP0 but it's in ESS */
485 hw_cwrite(addr, mask_xs|mask_xr, value ? mask_xs : mask_xr);
486
487 } while (value != hw_ep_get_halt(num, dir));
488
489 return 0;
490}
491
492/**
493 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
494 * interruption)
495 * @n: interrupt bit
496 *
497 * This function returns an error code
498 */
499static int hw_intr_clear(int n)
500{
501 if (n >= REG_BITS)
502 return -EINVAL;
503
504 hw_cwrite(CAP_USBINTR, BIT(n), 0);
505 hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
506 return 0;
507}
508
509/**
510 * hw_intr_force: enables interrupt & forces interrupt status (execute without
511 * interruption)
512 * @n: interrupt bit
513 *
514 * This function returns an error code
515 */
516static int hw_intr_force(int n)
517{
518 if (n >= REG_BITS)
519 return -EINVAL;
520
521 hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
522 hw_cwrite(CAP_USBINTR, BIT(n), BIT(n));
523 hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
524 hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, 0);
525 return 0;
526}
527
528/**
529 * hw_is_port_high_speed: test if port is high speed
530 *
531 * This function returns true if high speed port
532 */
533static int hw_port_is_high_speed(void)
534{
535 return hw_bank.lpm ? hw_cread(CAP_DEVLC, DEVLC_PSPD) :
536 hw_cread(CAP_PORTSC, PORTSC_HSP);
537}
538
539/**
540 * hw_port_test_get: reads port test mode value
541 *
542 * This function returns port test mode value
543 */
544static u8 hw_port_test_get(void)
545{
546 return hw_cread(CAP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
547}
548
549/**
550 * hw_port_test_set: writes port test mode (execute without interruption)
551 * @mode: new value
552 *
553 * This function returns an error code
554 */
555static int hw_port_test_set(u8 mode)
556{
557 const u8 TEST_MODE_MAX = 7;
558
559 if (mode > TEST_MODE_MAX)
560 return -EINVAL;
561
562 hw_cwrite(CAP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
563 return 0;
564}
565
566/**
567 * hw_read_intr_enable: returns interrupt enable register
568 *
569 * This function returns register data
570 */
571static u32 hw_read_intr_enable(void)
572{
573 return hw_cread(CAP_USBINTR, ~0);
574}
575
576/**
577 * hw_read_intr_status: returns interrupt status register
578 *
579 * This function returns register data
580 */
581static u32 hw_read_intr_status(void)
582{
583 return hw_cread(CAP_USBSTS, ~0);
584}
585
586/**
587 * hw_register_read: reads all device registers (execute without interruption)
588 * @buf: destination buffer
589 * @size: buffer size
590 *
591 * This function returns number of registers read
592 */
593static size_t hw_register_read(u32 *buf, size_t size)
594{
595 unsigned i;
596
597 if (size > hw_bank.size)
598 size = hw_bank.size;
599
600 for (i = 0; i < size; i++)
601 buf[i] = hw_aread(i * sizeof(u32), ~0);
602
603 return size;
604}
605
606/**
607 * hw_register_write: writes to register
608 * @addr: register address
609 * @data: register value
610 *
611 * This function returns an error code
612 */
613static int hw_register_write(u16 addr, u32 data)
614{
615 /* align */
616 addr /= sizeof(u32);
617
618 if (addr >= hw_bank.size)
619 return -EINVAL;
620
621 /* align */
622 addr *= sizeof(u32);
623
624 hw_awrite(addr, ~0, data);
625 return 0;
626}
627
628/**
629 * hw_test_and_clear_complete: test & clear complete status (execute without
630 * interruption)
631 * @n: bit number (endpoint)
632 *
633 * This function returns complete status
634 */
635static int hw_test_and_clear_complete(int n)
636{
637 return hw_ctest_and_clear(CAP_ENDPTCOMPLETE, BIT(n));
638}
639
640/**
641 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
642 * without interruption)
643 *
644 * This function returns active interrutps
645 */
646static u32 hw_test_and_clear_intr_active(void)
647{
648 u32 reg = hw_read_intr_status() & hw_read_intr_enable();
649
650 hw_cwrite(CAP_USBSTS, ~0, reg);
651 return reg;
652}
653
654/**
655 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
656 * interruption)
657 *
658 * This function returns guard value
659 */
660static int hw_test_and_clear_setup_guard(void)
661{
662 return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, 0);
663}
664
665/**
666 * hw_test_and_set_setup_guard: test & set setup guard (execute without
667 * interruption)
668 *
669 * This function returns guard value
670 */
671static int hw_test_and_set_setup_guard(void)
672{
673 return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
674}
675
676/**
677 * hw_usb_set_address: configures USB address (execute without interruption)
678 * @value: new USB address
679 *
680 * This function returns an error code
681 */
682static int hw_usb_set_address(u8 value)
683{
684 /* advance */
685 hw_cwrite(CAP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
686 value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
687 return 0;
688}
689
690/**
691 * hw_usb_reset: restart device after a bus reset (execute without
692 * interruption)
693 *
694 * This function returns an error code
695 */
696static int hw_usb_reset(void)
697{
698 hw_usb_set_address(0);
699
700 /* ESS flushes only at end?!? */
701 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); /* flush all EPs */
702
703 /* clear setup token semaphores */
704 hw_cwrite(CAP_ENDPTSETUPSTAT, 0, 0); /* writes its content */
705
706 /* clear complete status */
707 hw_cwrite(CAP_ENDPTCOMPLETE, 0, 0); /* writes its content */
708
709 /* wait until all bits cleared */
710 while (hw_cread(CAP_ENDPTPRIME, ~0))
711 udelay(10); /* not RTOS friendly */
712
713 /* reset all endpoints ? */
714
715 /* reset internal status and wait for further instructions
716 no need to verify the port reset status (ESS does it) */
717
718 return 0;
719}
720
721/******************************************************************************
722 * DBG block
723 *****************************************************************************/
724/**
725 * show_device: prints information about device capabilities and status
726 *
727 * Check "device.h" for details
728 */
729static ssize_t show_device(struct device *dev, struct device_attribute *attr,
730 char *buf)
731{
732 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
733 struct usb_gadget *gadget = &udc->gadget;
734 int n = 0;
735
736 dbg_trace("[%s] %p\n", __func__, buf);
737 if (attr == NULL || buf == NULL) {
738 dev_err(dev, "[%s] EINVAL\n", __func__);
739 return 0;
740 }
741
742 n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
743 gadget->speed);
744 n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
745 gadget->is_dualspeed);
746 n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
747 gadget->is_otg);
748 n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
749 gadget->is_a_peripheral);
750 n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
751 gadget->b_hnp_enable);
752 n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
753 gadget->a_hnp_support);
754 n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
755 gadget->a_alt_hnp_support);
756 n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
757 (gadget->name ? gadget->name : ""));
758
759 return n;
760}
761static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
762
763/**
764 * show_driver: prints information about attached gadget (if any)
765 *
766 * Check "device.h" for details
767 */
768static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
769 char *buf)
770{
771 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
772 struct usb_gadget_driver *driver = udc->driver;
773 int n = 0;
774
775 dbg_trace("[%s] %p\n", __func__, buf);
776 if (attr == NULL || buf == NULL) {
777 dev_err(dev, "[%s] EINVAL\n", __func__);
778 return 0;
779 }
780
781 if (driver == NULL)
782 return scnprintf(buf, PAGE_SIZE,
783 "There is no gadget attached!\n");
784
785 n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
786 (driver->function ? driver->function : ""));
787 n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
788 driver->speed);
789
790 return n;
791}
792static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
793
794/* Maximum event message length */
795#define DBG_DATA_MSG 64UL
796
797/* Maximum event messages */
798#define DBG_DATA_MAX 128UL
799
800/* Event buffer descriptor */
801static struct {
802 char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
803 unsigned idx; /* index */
804 unsigned tty; /* print to console? */
805 rwlock_t lck; /* lock */
806} dbg_data = {
807 .idx = 0,
808 .tty = 0,
809 .lck = __RW_LOCK_UNLOCKED(lck)
810};
811
812/**
813 * dbg_dec: decrements debug event index
814 * @idx: buffer index
815 */
816static void dbg_dec(unsigned *idx)
817{
818 *idx = (*idx - 1) & (DBG_DATA_MAX-1);
819}
820
821/**
822 * dbg_inc: increments debug event index
823 * @idx: buffer index
824 */
825static void dbg_inc(unsigned *idx)
826{
827 *idx = (*idx + 1) & (DBG_DATA_MAX-1);
828}
829
830/**
831 * dbg_print: prints the common part of the event
832 * @addr: endpoint address
833 * @name: event name
834 * @status: status
835 * @extra: extra information
836 */
837static void dbg_print(u8 addr, const char *name, int status, const char *extra)
838{
839 struct timeval tval;
840 unsigned int stamp;
841 unsigned long flags;
842
843 write_lock_irqsave(&dbg_data.lck, flags);
844
845 do_gettimeofday(&tval);
846 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
847 stamp = stamp * 1000000 + tval.tv_usec;
848
849 scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
850 "%04X\t» %02X %-7.7s %4i «\t%s\n",
851 stamp, addr, name, status, extra);
852
853 dbg_inc(&dbg_data.idx);
854
855 write_unlock_irqrestore(&dbg_data.lck, flags);
856
857 if (dbg_data.tty != 0)
858 pr_notice("%04X\t» %02X %-7.7s %4i «\t%s\n",
859 stamp, addr, name, status, extra);
860}
861
862/**
863 * dbg_done: prints a DONE event
864 * @addr: endpoint address
865 * @td: transfer descriptor
866 * @status: status
867 */
868static void dbg_done(u8 addr, const u32 token, int status)
869{
870 char msg[DBG_DATA_MSG];
871
872 scnprintf(msg, sizeof(msg), "%d %02X",
873 (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
874 (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
875 dbg_print(addr, "DONE", status, msg);
876}
877
878/**
879 * dbg_event: prints a generic event
880 * @addr: endpoint address
881 * @name: event name
882 * @status: status
883 */
884static void dbg_event(u8 addr, const char *name, int status)
885{
886 if (name != NULL)
887 dbg_print(addr, name, status, "");
888}
889
890/*
891 * dbg_queue: prints a QUEUE event
892 * @addr: endpoint address
893 * @req: USB request
894 * @status: status
895 */
896static void dbg_queue(u8 addr, const struct usb_request *req, int status)
897{
898 char msg[DBG_DATA_MSG];
899
900 if (req != NULL) {
901 scnprintf(msg, sizeof(msg),
902 "%d %d", !req->no_interrupt, req->length);
903 dbg_print(addr, "QUEUE", status, msg);
904 }
905}
906
907/**
908 * dbg_setup: prints a SETUP event
909 * @addr: endpoint address
910 * @req: setup request
911 */
912static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
913{
914 char msg[DBG_DATA_MSG];
915
916 if (req != NULL) {
917 scnprintf(msg, sizeof(msg),
918 "%02X %02X %04X %04X %d", req->bRequestType,
919 req->bRequest, le16_to_cpu(req->wValue),
920 le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
921 dbg_print(addr, "SETUP", 0, msg);
922 }
923}
924
925/**
926 * show_events: displays the event buffer
927 *
928 * Check "device.h" for details
929 */
930static ssize_t show_events(struct device *dev, struct device_attribute *attr,
931 char *buf)
932{
933 unsigned long flags;
934 unsigned i, j, n = 0;
935
936 dbg_trace("[%s] %p\n", __func__, buf);
937 if (attr == NULL || buf == NULL) {
938 dev_err(dev, "[%s] EINVAL\n", __func__);
939 return 0;
940 }
941
942 read_lock_irqsave(&dbg_data.lck, flags);
943
944 i = dbg_data.idx;
945 for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
946 n += strlen(dbg_data.buf[i]);
947 if (n >= PAGE_SIZE) {
948 n -= strlen(dbg_data.buf[i]);
949 break;
950 }
951 }
952 for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
953 j += scnprintf(buf + j, PAGE_SIZE - j,
954 "%s", dbg_data.buf[i]);
955
956 read_unlock_irqrestore(&dbg_data.lck, flags);
957
958 return n;
959}
960
961/**
962 * store_events: configure if events are going to be also printed to console
963 *
964 * Check "device.h" for details
965 */
966static ssize_t store_events(struct device *dev, struct device_attribute *attr,
967 const char *buf, size_t count)
968{
969 unsigned tty;
970
971 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
972 if (attr == NULL || buf == NULL) {
973 dev_err(dev, "[%s] EINVAL\n", __func__);
974 goto done;
975 }
976
977 if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
978 dev_err(dev, "<1|0>: enable|disable console log\n");
979 goto done;
980 }
981
982 dbg_data.tty = tty;
983 dev_info(dev, "tty = %u", dbg_data.tty);
984
985 done:
986 return count;
987}
988static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
989
990/**
991 * show_inters: interrupt status, enable status and historic
992 *
993 * Check "device.h" for details
994 */
995static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
996 char *buf)
997{
998 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
999 unsigned long flags;
1000 u32 intr;
1001 unsigned i, j, n = 0;
1002
1003 dbg_trace("[%s] %p\n", __func__, buf);
1004 if (attr == NULL || buf == NULL) {
1005 dev_err(dev, "[%s] EINVAL\n", __func__);
1006 return 0;
1007 }
1008
1009 spin_lock_irqsave(udc->lock, flags);
1010
1011 n += scnprintf(buf + n, PAGE_SIZE - n,
1012 "status = %08x\n", hw_read_intr_status());
1013 n += scnprintf(buf + n, PAGE_SIZE - n,
1014 "enable = %08x\n", hw_read_intr_enable());
1015
1016 n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
1017 isr_statistics.test);
1018 n += scnprintf(buf + n, PAGE_SIZE - n, "» ui = %d\n",
1019 isr_statistics.ui);
1020 n += scnprintf(buf + n, PAGE_SIZE - n, "» uei = %d\n",
1021 isr_statistics.uei);
1022 n += scnprintf(buf + n, PAGE_SIZE - n, "» pci = %d\n",
1023 isr_statistics.pci);
1024 n += scnprintf(buf + n, PAGE_SIZE - n, "» uri = %d\n",
1025 isr_statistics.uri);
1026 n += scnprintf(buf + n, PAGE_SIZE - n, "» sli = %d\n",
1027 isr_statistics.sli);
1028 n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
1029 isr_statistics.none);
1030 n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
1031 isr_statistics.hndl.cnt);
1032
1033 for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
1034 i &= ISR_MASK;
1035 intr = isr_statistics.hndl.buf[i];
1036
1037 if (USBi_UI & intr)
1038 n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
1039 intr &= ~USBi_UI;
1040 if (USBi_UEI & intr)
1041 n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
1042 intr &= ~USBi_UEI;
1043 if (USBi_PCI & intr)
1044 n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
1045 intr &= ~USBi_PCI;
1046 if (USBi_URI & intr)
1047 n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
1048 intr &= ~USBi_URI;
1049 if (USBi_SLI & intr)
1050 n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
1051 intr &= ~USBi_SLI;
1052 if (intr)
1053 n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
1054 if (isr_statistics.hndl.buf[i])
1055 n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
1056 }
1057
1058 spin_unlock_irqrestore(udc->lock, flags);
1059
1060 return n;
1061}
1062
1063/**
1064 * store_inters: enable & force or disable an individual interrutps
1065 * (to be used for test purposes only)
1066 *
1067 * Check "device.h" for details
1068 */
1069static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
1070 const char *buf, size_t count)
1071{
1072 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1073 unsigned long flags;
1074 unsigned en, bit;
1075
1076 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1077 if (attr == NULL || buf == NULL) {
1078 dev_err(dev, "[%s] EINVAL\n", __func__);
1079 goto done;
1080 }
1081
1082 if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
1083 dev_err(dev, "<1|0> <bit>: enable|disable interrupt");
1084 goto done;
1085 }
1086
1087 spin_lock_irqsave(udc->lock, flags);
1088 if (en) {
1089 if (hw_intr_force(bit))
1090 dev_err(dev, "invalid bit number\n");
1091 else
1092 isr_statistics.test++;
1093 } else {
1094 if (hw_intr_clear(bit))
1095 dev_err(dev, "invalid bit number\n");
1096 }
1097 spin_unlock_irqrestore(udc->lock, flags);
1098
1099 done:
1100 return count;
1101}
1102static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
1103
1104/**
1105 * show_port_test: reads port test mode
1106 *
1107 * Check "device.h" for details
1108 */
1109static ssize_t show_port_test(struct device *dev,
1110 struct device_attribute *attr, char *buf)
1111{
1112 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1113 unsigned long flags;
1114 unsigned mode;
1115
1116 dbg_trace("[%s] %p\n", __func__, buf);
1117 if (attr == NULL || buf == NULL) {
1118 dev_err(dev, "[%s] EINVAL\n", __func__);
1119 return 0;
1120 }
1121
1122 spin_lock_irqsave(udc->lock, flags);
1123 mode = hw_port_test_get();
1124 spin_unlock_irqrestore(udc->lock, flags);
1125
1126 return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
1127}
1128
1129/**
1130 * store_port_test: writes port test mode
1131 *
1132 * Check "device.h" for details
1133 */
1134static ssize_t store_port_test(struct device *dev,
1135 struct device_attribute *attr,
1136 const char *buf, size_t count)
1137{
1138 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1139 unsigned long flags;
1140 unsigned mode;
1141
1142 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1143 if (attr == NULL || buf == NULL) {
1144 dev_err(dev, "[%s] EINVAL\n", __func__);
1145 goto done;
1146 }
1147
1148 if (sscanf(buf, "%u", &mode) != 1) {
1149 dev_err(dev, "<mode>: set port test mode");
1150 goto done;
1151 }
1152
1153 spin_lock_irqsave(udc->lock, flags);
1154 if (hw_port_test_set(mode))
1155 dev_err(dev, "invalid mode\n");
1156 spin_unlock_irqrestore(udc->lock, flags);
1157
1158 done:
1159 return count;
1160}
1161static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
1162 show_port_test, store_port_test);
1163
1164/**
1165 * show_qheads: DMA contents of all queue heads
1166 *
1167 * Check "device.h" for details
1168 */
1169static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
1170 char *buf)
1171{
1172 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1173 unsigned long flags;
1174 unsigned i, j, n = 0;
1175
1176 dbg_trace("[%s] %p\n", __func__, buf);
1177 if (attr == NULL || buf == NULL) {
1178 dev_err(dev, "[%s] EINVAL\n", __func__);
1179 return 0;
1180 }
1181
1182 spin_lock_irqsave(udc->lock, flags);
1183 for (i = 0; i < hw_ep_max; i++) {
1184 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1185 n += scnprintf(buf + n, PAGE_SIZE - n,
1186 "EP=%02i: RX=%08X TX=%08X\n",
1187 i, (u32)mEp->qh[RX].dma, (u32)mEp->qh[TX].dma);
1188 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
1189 n += scnprintf(buf + n, PAGE_SIZE - n,
1190 " %04X: %08X %08X\n", j,
1191 *((u32 *)mEp->qh[RX].ptr + j),
1192 *((u32 *)mEp->qh[TX].ptr + j));
1193 }
1194 }
1195 spin_unlock_irqrestore(udc->lock, flags);
1196
1197 return n;
1198}
1199static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
1200
1201/**
1202 * show_registers: dumps all registers
1203 *
1204 * Check "device.h" for details
1205 */
1206static ssize_t show_registers(struct device *dev,
1207 struct device_attribute *attr, char *buf)
1208{
1209 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1210 unsigned long flags;
1211 u32 dump[512];
1212 unsigned i, k, n = 0;
1213
1214 dbg_trace("[%s] %p\n", __func__, buf);
1215 if (attr == NULL || buf == NULL) {
1216 dev_err(dev, "[%s] EINVAL\n", __func__);
1217 return 0;
1218 }
1219
1220 spin_lock_irqsave(udc->lock, flags);
1221 k = hw_register_read(dump, sizeof(dump)/sizeof(u32));
1222 spin_unlock_irqrestore(udc->lock, flags);
1223
1224 for (i = 0; i < k; i++) {
1225 n += scnprintf(buf + n, PAGE_SIZE - n,
1226 "reg[0x%04X] = 0x%08X\n",
1227 i * (unsigned)sizeof(u32), dump[i]);
1228 }
1229
1230 return n;
1231}
1232
1233/**
1234 * store_registers: writes value to register address
1235 *
1236 * Check "device.h" for details
1237 */
1238static ssize_t store_registers(struct device *dev,
1239 struct device_attribute *attr,
1240 const char *buf, size_t count)
1241{
1242 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1243 unsigned long addr, data, flags;
1244
1245 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1246 if (attr == NULL || buf == NULL) {
1247 dev_err(dev, "[%s] EINVAL\n", __func__);
1248 goto done;
1249 }
1250
1251 if (sscanf(buf, "%li %li", &addr, &data) != 2) {
1252 dev_err(dev, "<addr> <data>: write data to register address");
1253 goto done;
1254 }
1255
1256 spin_lock_irqsave(udc->lock, flags);
1257 if (hw_register_write(addr, data))
1258 dev_err(dev, "invalid address range\n");
1259 spin_unlock_irqrestore(udc->lock, flags);
1260
1261 done:
1262 return count;
1263}
1264static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
1265 show_registers, store_registers);
1266
1267/**
1268 * show_requests: DMA contents of all requests currently queued (all endpts)
1269 *
1270 * Check "device.h" for details
1271 */
1272static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
1273 char *buf)
1274{
1275 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1276 unsigned long flags;
1277 struct list_head *ptr = NULL;
1278 struct ci13xxx_req *req = NULL;
1279 unsigned i, j, k, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
1280
1281 dbg_trace("[%s] %p\n", __func__, buf);
1282 if (attr == NULL || buf == NULL) {
1283 dev_err(dev, "[%s] EINVAL\n", __func__);
1284 return 0;
1285 }
1286
1287 spin_lock_irqsave(udc->lock, flags);
1288 for (i = 0; i < hw_ep_max; i++)
1289 for (k = RX; k <= TX; k++)
1290 list_for_each(ptr, &udc->ci13xxx_ep[i].qh[k].queue)
1291 {
1292 req = list_entry(ptr,
1293 struct ci13xxx_req, queue);
1294
1295 n += scnprintf(buf + n, PAGE_SIZE - n,
1296 "EP=%02i: TD=%08X %s\n",
1297 i, (u32)req->dma,
1298 ((k == RX) ? "RX" : "TX"));
1299
1300 for (j = 0; j < qSize; j++)
1301 n += scnprintf(buf + n, PAGE_SIZE - n,
1302 " %04X: %08X\n", j,
1303 *((u32 *)req->ptr + j));
1304 }
1305 spin_unlock_irqrestore(udc->lock, flags);
1306
1307 return n;
1308}
1309static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
1310
1311/**
1312 * dbg_create_files: initializes the attribute interface
1313 * @dev: device
1314 *
1315 * This function returns an error code
1316 */
1317__maybe_unused static int dbg_create_files(struct device *dev)
1318{
1319 int retval = 0;
1320
1321 if (dev == NULL)
1322 return -EINVAL;
1323 retval = device_create_file(dev, &dev_attr_device);
1324 if (retval)
1325 goto done;
1326 retval = device_create_file(dev, &dev_attr_driver);
1327 if (retval)
1328 goto rm_device;
1329 retval = device_create_file(dev, &dev_attr_events);
1330 if (retval)
1331 goto rm_driver;
1332 retval = device_create_file(dev, &dev_attr_inters);
1333 if (retval)
1334 goto rm_events;
1335 retval = device_create_file(dev, &dev_attr_port_test);
1336 if (retval)
1337 goto rm_inters;
1338 retval = device_create_file(dev, &dev_attr_qheads);
1339 if (retval)
1340 goto rm_port_test;
1341 retval = device_create_file(dev, &dev_attr_registers);
1342 if (retval)
1343 goto rm_qheads;
1344 retval = device_create_file(dev, &dev_attr_requests);
1345 if (retval)
1346 goto rm_registers;
1347 return 0;
1348
1349 rm_registers:
1350 device_remove_file(dev, &dev_attr_registers);
1351 rm_qheads:
1352 device_remove_file(dev, &dev_attr_qheads);
1353 rm_port_test:
1354 device_remove_file(dev, &dev_attr_port_test);
1355 rm_inters:
1356 device_remove_file(dev, &dev_attr_inters);
1357 rm_events:
1358 device_remove_file(dev, &dev_attr_events);
1359 rm_driver:
1360 device_remove_file(dev, &dev_attr_driver);
1361 rm_device:
1362 device_remove_file(dev, &dev_attr_device);
1363 done:
1364 return retval;
1365}
1366
1367/**
1368 * dbg_remove_files: destroys the attribute interface
1369 * @dev: device
1370 *
1371 * This function returns an error code
1372 */
1373__maybe_unused static int dbg_remove_files(struct device *dev)
1374{
1375 if (dev == NULL)
1376 return -EINVAL;
1377 device_remove_file(dev, &dev_attr_requests);
1378 device_remove_file(dev, &dev_attr_registers);
1379 device_remove_file(dev, &dev_attr_qheads);
1380 device_remove_file(dev, &dev_attr_port_test);
1381 device_remove_file(dev, &dev_attr_inters);
1382 device_remove_file(dev, &dev_attr_events);
1383 device_remove_file(dev, &dev_attr_driver);
1384 device_remove_file(dev, &dev_attr_device);
1385 return 0;
1386}
1387
1388/******************************************************************************
1389 * UTIL block
1390 *****************************************************************************/
1391/**
1392 * _usb_addr: calculates endpoint address from direction & number
1393 * @ep: endpoint
1394 */
1395static inline u8 _usb_addr(struct ci13xxx_ep *ep)
1396{
1397 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
1398}
1399
1400/**
1401 * _hardware_queue: configures a request at hardware level
1402 * @gadget: gadget
1403 * @mEp: endpoint
1404 *
1405 * This function returns an error code
1406 */
1407static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1408{
1409 unsigned i;
1410
1411 trace("%p, %p", mEp, mReq);
1412
1413 /* don't queue twice */
1414 if (mReq->req.status == -EALREADY)
1415 return -EALREADY;
1416
1417 if (hw_ep_is_primed(mEp->num, mEp->dir))
1418 return -EBUSY;
1419
1420 mReq->req.status = -EALREADY;
1421
1422 if (mReq->req.length && !mReq->req.dma) {
1423 mReq->req.dma = \
1424 dma_map_single(mEp->device, mReq->req.buf,
1425 mReq->req.length, mEp->dir ?
1426 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1427 if (mReq->req.dma == 0)
1428 return -ENOMEM;
1429
1430 mReq->map = 1;
1431 }
1432
1433 /*
1434 * TD configuration
1435 * TODO - handle requests which spawns into several TDs
1436 */
1437 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
1438 mReq->ptr->next |= TD_TERMINATE;
1439 mReq->ptr->token = mReq->req.length << ffs_nr(TD_TOTAL_BYTES);
1440 mReq->ptr->token &= TD_TOTAL_BYTES;
1441 mReq->ptr->token |= TD_IOC;
1442 mReq->ptr->token |= TD_STATUS_ACTIVE;
1443 mReq->ptr->page[0] = mReq->req.dma;
1444 for (i = 1; i < 5; i++)
1445 mReq->ptr->page[i] =
1446 (mReq->req.dma + i * PAGE_SIZE) & ~TD_RESERVED_MASK;
1447
1448 /*
1449 * QH configuration
1450 * At this point it's guaranteed exclusive access to qhead
1451 * (endpt is not primed) so it's no need to use tripwire
1452 */
1453 mEp->qh[mEp->dir].ptr->td.next = mReq->dma; /* TERMINATE = 0 */
1454 mEp->qh[mEp->dir].ptr->td.token &= ~TD_STATUS; /* clear status */
1455 if (mReq->req.zero == 0)
1456 mEp->qh[mEp->dir].ptr->cap |= QH_ZLT;
1457 else
1458 mEp->qh[mEp->dir].ptr->cap &= ~QH_ZLT;
1459
1460 wmb(); /* synchronize before ep prime */
1461
1462 return hw_ep_prime(mEp->num, mEp->dir,
1463 mEp->type == USB_ENDPOINT_XFER_CONTROL);
1464}
1465
1466/**
1467 * _hardware_dequeue: handles a request at hardware level
1468 * @gadget: gadget
1469 * @mEp: endpoint
1470 *
1471 * This function returns an error code
1472 */
1473static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1474{
1475 trace("%p, %p", mEp, mReq);
1476
1477 if (mReq->req.status != -EALREADY)
1478 return -EINVAL;
1479
1480 if (hw_ep_is_primed(mEp->num, mEp->dir))
1481 hw_ep_flush(mEp->num, mEp->dir);
1482
1483 mReq->req.status = 0;
1484
1485 if (mReq->map) {
1486 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
1487 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1488 mReq->req.dma = 0;
1489 mReq->map = 0;
1490 }
1491
1492 mReq->req.status = mReq->ptr->token & TD_STATUS;
1493 if ((TD_STATUS_ACTIVE & mReq->req.status) != 0)
1494 mReq->req.status = -ECONNRESET;
1495 else if ((TD_STATUS_HALTED & mReq->req.status) != 0)
1496 mReq->req.status = -1;
1497 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
1498 mReq->req.status = -1;
1499 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
1500 mReq->req.status = -1;
1501
1502 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
1503 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
1504 mReq->req.actual = mReq->req.length - mReq->req.actual;
1505 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
1506
1507 return mReq->req.actual;
1508}
1509
1510/**
1511 * _ep_nuke: dequeues all endpoint requests
1512 * @mEp: endpoint
1513 *
1514 * This function returns an error code
1515 * Caller must hold lock
1516 */
1517static int _ep_nuke(struct ci13xxx_ep *mEp)
1518__releases(mEp->lock)
1519__acquires(mEp->lock)
1520{
1521 trace("%p", mEp);
1522
1523 if (mEp == NULL)
1524 return -EINVAL;
1525
1526 hw_ep_flush(mEp->num, mEp->dir);
1527
1528 while (!list_empty(&mEp->qh[mEp->dir].queue)) {
1529
1530 /* pop oldest request */
1531 struct ci13xxx_req *mReq = \
1532 list_entry(mEp->qh[mEp->dir].queue.next,
1533 struct ci13xxx_req, queue);
1534 list_del_init(&mReq->queue);
1535 mReq->req.status = -ESHUTDOWN;
1536
1537 if (!mReq->req.no_interrupt && mReq->req.complete != NULL) {
1538 spin_unlock(mEp->lock);
1539 mReq->req.complete(&mEp->ep, &mReq->req);
1540 spin_lock(mEp->lock);
1541 }
1542 }
1543 return 0;
1544}
1545
1546/**
1547 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
1548 * @gadget: gadget
1549 *
1550 * This function returns an error code
1551 * Caller must hold lock
1552 */
1553static int _gadget_stop_activity(struct usb_gadget *gadget)
1554__releases(udc->lock)
1555__acquires(udc->lock)
1556{
1557 struct usb_ep *ep;
1558 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1559 struct ci13xxx_ep *mEp = container_of(gadget->ep0,
1560 struct ci13xxx_ep, ep);
1561
1562 trace("%p", gadget);
1563
1564 if (gadget == NULL)
1565 return -EINVAL;
1566
1567 spin_unlock(udc->lock);
1568
1569 /* flush all endpoints */
1570 gadget_for_each_ep(ep, gadget) {
1571 usb_ep_fifo_flush(ep);
1572 }
1573 usb_ep_fifo_flush(gadget->ep0);
1574
1575 udc->driver->disconnect(gadget);
1576
1577 /* make sure to disable all endpoints */
1578 gadget_for_each_ep(ep, gadget) {
1579 usb_ep_disable(ep);
1580 }
1581 usb_ep_disable(gadget->ep0);
1582
1583 if (mEp->status != NULL) {
1584 usb_ep_free_request(gadget->ep0, mEp->status);
1585 mEp->status = NULL;
1586 }
1587
1588 spin_lock(udc->lock);
1589
1590 return 0;
1591}
1592
1593/******************************************************************************
1594 * ISR block
1595 *****************************************************************************/
1596/**
1597 * isr_reset_handler: USB reset interrupt handler
1598 * @udc: UDC device
1599 *
1600 * This function resets USB engine after a bus reset occurred
1601 */
1602static void isr_reset_handler(struct ci13xxx *udc)
1603__releases(udc->lock)
1604__acquires(udc->lock)
1605{
1606 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[0];
1607 int retval;
1608
1609 trace("%p", udc);
1610
1611 if (udc == NULL) {
1612 err("EINVAL");
1613 return;
1614 }
1615
1616 dbg_event(0xFF, "BUS RST", 0);
1617
1618 retval = _gadget_stop_activity(&udc->gadget);
1619 if (retval)
1620 goto done;
1621
1622 retval = hw_usb_reset();
1623 if (retval)
1624 goto done;
1625
1626 spin_unlock(udc->lock);
1627 retval = usb_ep_enable(&mEp->ep, &ctrl_endpt_desc);
1628 if (!retval) {
Pavankumar Kondeti0a91efa2010-12-07 17:54:00 +05301629 mEp->status = usb_ep_alloc_request(&mEp->ep, GFP_ATOMIC);
David Lopoaa69a802008-11-17 14:14:51 -08001630 if (mEp->status == NULL) {
1631 usb_ep_disable(&mEp->ep);
1632 retval = -ENOMEM;
1633 }
1634 }
1635 spin_lock(udc->lock);
1636
1637 done:
1638 if (retval)
1639 err("error: %i", retval);
1640}
1641
1642/**
1643 * isr_get_status_complete: get_status request complete function
1644 * @ep: endpoint
1645 * @req: request handled
1646 *
1647 * Caller must release lock
1648 */
1649static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
1650{
1651 trace("%p, %p", ep, req);
1652
1653 if (ep == NULL || req == NULL) {
1654 err("EINVAL");
1655 return;
1656 }
1657
1658 kfree(req->buf);
1659 usb_ep_free_request(ep, req);
1660}
1661
1662/**
1663 * isr_get_status_response: get_status request response
1664 * @ep: endpoint
1665 * @setup: setup request packet
1666 *
1667 * This function returns an error code
1668 */
1669static int isr_get_status_response(struct ci13xxx_ep *mEp,
1670 struct usb_ctrlrequest *setup)
1671__releases(mEp->lock)
1672__acquires(mEp->lock)
1673{
1674 struct usb_request *req = NULL;
1675 gfp_t gfp_flags = GFP_ATOMIC;
1676 int dir, num, retval;
1677
1678 trace("%p, %p", mEp, setup);
1679
1680 if (mEp == NULL || setup == NULL)
1681 return -EINVAL;
1682
1683 spin_unlock(mEp->lock);
1684 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
1685 spin_lock(mEp->lock);
1686 if (req == NULL)
1687 return -ENOMEM;
1688
1689 req->complete = isr_get_status_complete;
1690 req->length = 2;
1691 req->buf = kzalloc(req->length, gfp_flags);
1692 if (req->buf == NULL) {
1693 retval = -ENOMEM;
1694 goto err_free_req;
1695 }
1696
1697 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1698 /* TODO: D1 - Remote Wakeup; D0 - Self Powered */
1699 retval = 0;
1700 } else if ((setup->bRequestType & USB_RECIP_MASK) \
1701 == USB_RECIP_ENDPOINT) {
1702 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
1703 TX : RX;
1704 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1705 *((u16 *)req->buf) = hw_ep_get_halt(num, dir);
1706 }
1707 /* else do nothing; reserved for future use */
1708
1709 spin_unlock(mEp->lock);
1710 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
1711 spin_lock(mEp->lock);
1712 if (retval)
1713 goto err_free_buf;
1714
1715 return 0;
1716
1717 err_free_buf:
1718 kfree(req->buf);
1719 err_free_req:
1720 spin_unlock(mEp->lock);
1721 usb_ep_free_request(&mEp->ep, req);
1722 spin_lock(mEp->lock);
1723 return retval;
1724}
1725
1726/**
1727 * isr_setup_status_phase: queues the status phase of a setup transation
1728 * @mEp: endpoint
1729 *
1730 * This function returns an error code
1731 */
1732static int isr_setup_status_phase(struct ci13xxx_ep *mEp)
1733__releases(mEp->lock)
1734__acquires(mEp->lock)
1735{
1736 int retval;
1737
1738 trace("%p", mEp);
1739
1740 /* mEp is always valid & configured */
1741
1742 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1743 mEp->dir = (mEp->dir == TX) ? RX : TX;
1744
1745 mEp->status->no_interrupt = 1;
1746
1747 spin_unlock(mEp->lock);
1748 retval = usb_ep_queue(&mEp->ep, mEp->status, GFP_ATOMIC);
1749 spin_lock(mEp->lock);
1750
1751 return retval;
1752}
1753
1754/**
1755 * isr_tr_complete_low: transaction complete low level handler
1756 * @mEp: endpoint
1757 *
1758 * This function returns an error code
1759 * Caller must hold lock
1760 */
1761static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
1762__releases(mEp->lock)
1763__acquires(mEp->lock)
1764{
1765 struct ci13xxx_req *mReq;
1766 int retval;
1767
1768 trace("%p", mEp);
1769
1770 if (list_empty(&mEp->qh[mEp->dir].queue))
1771 return -EINVAL;
1772
1773 /* pop oldest request */
1774 mReq = list_entry(mEp->qh[mEp->dir].queue.next,
1775 struct ci13xxx_req, queue);
1776 list_del_init(&mReq->queue);
1777
1778 retval = _hardware_dequeue(mEp, mReq);
1779 if (retval < 0) {
1780 dbg_event(_usb_addr(mEp), "DONE", retval);
1781 goto done;
1782 }
1783
1784 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
1785
1786 if (!mReq->req.no_interrupt && mReq->req.complete != NULL) {
1787 spin_unlock(mEp->lock);
1788 mReq->req.complete(&mEp->ep, &mReq->req);
1789 spin_lock(mEp->lock);
1790 }
1791
1792 if (!list_empty(&mEp->qh[mEp->dir].queue)) {
1793 mReq = list_entry(mEp->qh[mEp->dir].queue.next,
1794 struct ci13xxx_req, queue);
1795 _hardware_enqueue(mEp, mReq);
1796 }
1797
1798 done:
1799 return retval;
1800}
1801
1802/**
1803 * isr_tr_complete_handler: transaction complete interrupt handler
1804 * @udc: UDC descriptor
1805 *
1806 * This function handles traffic events
1807 */
1808static void isr_tr_complete_handler(struct ci13xxx *udc)
1809__releases(udc->lock)
1810__acquires(udc->lock)
1811{
1812 unsigned i;
1813
1814 trace("%p", udc);
1815
1816 if (udc == NULL) {
1817 err("EINVAL");
1818 return;
1819 }
1820
1821 for (i = 0; i < hw_ep_max; i++) {
1822 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1823 int type, num, err = -EINVAL;
1824 struct usb_ctrlrequest req;
1825
1826
1827 if (mEp->desc == NULL)
1828 continue; /* not configured */
1829
1830 if ((mEp->dir == RX && hw_test_and_clear_complete(i)) ||
1831 (mEp->dir == TX && hw_test_and_clear_complete(i + 16))) {
1832 err = isr_tr_complete_low(mEp);
1833 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1834 if (err > 0) /* needs status phase */
1835 err = isr_setup_status_phase(mEp);
1836 if (err < 0) {
1837 dbg_event(_usb_addr(mEp),
1838 "ERROR", err);
1839 spin_unlock(udc->lock);
1840 if (usb_ep_set_halt(&mEp->ep))
1841 err("error: ep_set_halt");
1842 spin_lock(udc->lock);
1843 }
1844 }
1845 }
1846
1847 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
1848 !hw_test_and_clear_setup_status(i))
1849 continue;
1850
1851 if (i != 0) {
1852 warn("ctrl traffic received at endpoint");
1853 continue;
1854 }
1855
1856 /* read_setup_packet */
1857 do {
1858 hw_test_and_set_setup_guard();
1859 memcpy(&req, &mEp->qh[RX].ptr->setup, sizeof(req));
1860 } while (!hw_test_and_clear_setup_guard());
1861
1862 type = req.bRequestType;
1863
1864 mEp->dir = (type & USB_DIR_IN) ? TX : RX;
1865
1866 dbg_setup(_usb_addr(mEp), &req);
1867
1868 switch (req.bRequest) {
1869 case USB_REQ_CLEAR_FEATURE:
1870 if (type != (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1871 le16_to_cpu(req.wValue) != USB_ENDPOINT_HALT)
1872 goto delegate;
1873 if (req.wLength != 0)
1874 break;
1875 num = le16_to_cpu(req.wIndex);
1876 num &= USB_ENDPOINT_NUMBER_MASK;
1877 if (!udc->ci13xxx_ep[num].wedge) {
1878 spin_unlock(udc->lock);
1879 err = usb_ep_clear_halt(
1880 &udc->ci13xxx_ep[num].ep);
1881 spin_lock(udc->lock);
1882 if (err)
1883 break;
1884 }
1885 err = isr_setup_status_phase(mEp);
1886 break;
1887 case USB_REQ_GET_STATUS:
1888 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1889 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1890 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1891 goto delegate;
1892 if (le16_to_cpu(req.wLength) != 2 ||
1893 le16_to_cpu(req.wValue) != 0)
1894 break;
1895 err = isr_get_status_response(mEp, &req);
1896 break;
1897 case USB_REQ_SET_ADDRESS:
1898 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1899 goto delegate;
1900 if (le16_to_cpu(req.wLength) != 0 ||
1901 le16_to_cpu(req.wIndex) != 0)
1902 break;
1903 err = hw_usb_set_address((u8)le16_to_cpu(req.wValue));
1904 if (err)
1905 break;
1906 err = isr_setup_status_phase(mEp);
1907 break;
1908 case USB_REQ_SET_FEATURE:
1909 if (type != (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1910 le16_to_cpu(req.wValue) != USB_ENDPOINT_HALT)
1911 goto delegate;
1912 if (req.wLength != 0)
1913 break;
1914 num = le16_to_cpu(req.wIndex);
1915 num &= USB_ENDPOINT_NUMBER_MASK;
1916
1917 spin_unlock(udc->lock);
1918 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
1919 spin_lock(udc->lock);
1920 if (err)
1921 break;
1922 err = isr_setup_status_phase(mEp);
1923 break;
1924 default:
1925delegate:
1926 if (req.wLength == 0) /* no data phase */
1927 mEp->dir = TX;
1928
1929 spin_unlock(udc->lock);
1930 err = udc->driver->setup(&udc->gadget, &req);
1931 spin_lock(udc->lock);
1932 break;
1933 }
1934
1935 if (err < 0) {
1936 dbg_event(_usb_addr(mEp), "ERROR", err);
1937
1938 spin_unlock(udc->lock);
1939 if (usb_ep_set_halt(&mEp->ep))
1940 err("error: ep_set_halt");
1941 spin_lock(udc->lock);
1942 }
1943 }
1944}
1945
1946/******************************************************************************
1947 * ENDPT block
1948 *****************************************************************************/
1949/**
1950 * ep_enable: configure endpoint, making it usable
1951 *
1952 * Check usb_ep_enable() at "usb_gadget.h" for details
1953 */
1954static int ep_enable(struct usb_ep *ep,
1955 const struct usb_endpoint_descriptor *desc)
1956{
1957 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
1958 int direction, retval = 0;
1959 unsigned long flags;
1960
1961 trace("%p, %p", ep, desc);
1962
1963 if (ep == NULL || desc == NULL)
1964 return -EINVAL;
1965
1966 spin_lock_irqsave(mEp->lock, flags);
1967
1968 /* only internal SW should enable ctrl endpts */
1969
1970 mEp->desc = desc;
1971
1972 if (!list_empty(&mEp->qh[mEp->dir].queue))
1973 warn("enabling a non-empty endpoint!");
1974
Matthias Kaehlcke15739bb2009-04-15 22:28:41 +02001975 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1976 mEp->num = usb_endpoint_num(desc);
1977 mEp->type = usb_endpoint_type(desc);
David Lopoaa69a802008-11-17 14:14:51 -08001978
1979 mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize);
1980
1981 direction = mEp->dir;
1982 do {
1983 dbg_event(_usb_addr(mEp), "ENABLE", 0);
1984
David Lopof23e6492009-04-16 14:35:24 -07001985 mEp->qh[mEp->dir].ptr->cap = 0;
1986
David Lopoaa69a802008-11-17 14:14:51 -08001987 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
1988 mEp->qh[mEp->dir].ptr->cap |= QH_IOS;
1989 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
1990 mEp->qh[mEp->dir].ptr->cap &= ~QH_MULT;
1991 else
1992 mEp->qh[mEp->dir].ptr->cap &= ~QH_ZLT;
1993
1994 mEp->qh[mEp->dir].ptr->cap |=
1995 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
1996 mEp->qh[mEp->dir].ptr->td.next |= TD_TERMINATE; /* needed? */
1997
1998 retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type);
1999
2000 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2001 mEp->dir = (mEp->dir == TX) ? RX : TX;
2002
2003 } while (mEp->dir != direction);
2004
2005 spin_unlock_irqrestore(mEp->lock, flags);
2006 return retval;
2007}
2008
2009/**
2010 * ep_disable: endpoint is no longer usable
2011 *
2012 * Check usb_ep_disable() at "usb_gadget.h" for details
2013 */
2014static int ep_disable(struct usb_ep *ep)
2015{
2016 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2017 int direction, retval = 0;
2018 unsigned long flags;
2019
2020 trace("%p", ep);
2021
2022 if (ep == NULL)
2023 return -EINVAL;
2024 else if (mEp->desc == NULL)
2025 return -EBUSY;
2026
2027 spin_lock_irqsave(mEp->lock, flags);
2028
2029 /* only internal SW should disable ctrl endpts */
2030
2031 direction = mEp->dir;
2032 do {
2033 dbg_event(_usb_addr(mEp), "DISABLE", 0);
2034
2035 retval |= _ep_nuke(mEp);
2036 retval |= hw_ep_disable(mEp->num, mEp->dir);
2037
2038 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2039 mEp->dir = (mEp->dir == TX) ? RX : TX;
2040
2041 } while (mEp->dir != direction);
2042
2043 mEp->desc = NULL;
2044
2045 spin_unlock_irqrestore(mEp->lock, flags);
2046 return retval;
2047}
2048
2049/**
2050 * ep_alloc_request: allocate a request object to use with this endpoint
2051 *
2052 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
2053 */
2054static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2055{
2056 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2057 struct ci13xxx_req *mReq = NULL;
David Lopoaa69a802008-11-17 14:14:51 -08002058
2059 trace("%p, %i", ep, gfp_flags);
2060
2061 if (ep == NULL) {
2062 err("EINVAL");
2063 return NULL;
2064 }
2065
David Lopoaa69a802008-11-17 14:14:51 -08002066 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2067 if (mReq != NULL) {
2068 INIT_LIST_HEAD(&mReq->queue);
2069
2070 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
2071 &mReq->dma);
2072 if (mReq->ptr == NULL) {
2073 kfree(mReq);
2074 mReq = NULL;
2075 }
2076 }
2077
2078 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
2079
David Lopoaa69a802008-11-17 14:14:51 -08002080 return (mReq == NULL) ? NULL : &mReq->req;
2081}
2082
2083/**
2084 * ep_free_request: frees a request object
2085 *
2086 * Check usb_ep_free_request() at "usb_gadget.h" for details
2087 */
2088static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
2089{
2090 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2091 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2092 unsigned long flags;
2093
2094 trace("%p, %p", ep, req);
2095
2096 if (ep == NULL || req == NULL) {
2097 err("EINVAL");
2098 return;
2099 } else if (!list_empty(&mReq->queue)) {
2100 err("EBUSY");
2101 return;
2102 }
2103
2104 spin_lock_irqsave(mEp->lock, flags);
2105
2106 if (mReq->ptr)
2107 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
2108 kfree(mReq);
2109
2110 dbg_event(_usb_addr(mEp), "FREE", 0);
2111
2112 spin_unlock_irqrestore(mEp->lock, flags);
2113}
2114
2115/**
2116 * ep_queue: queues (submits) an I/O request to an endpoint
2117 *
2118 * Check usb_ep_queue()* at usb_gadget.h" for details
2119 */
2120static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2121 gfp_t __maybe_unused gfp_flags)
2122{
2123 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2124 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2125 int retval = 0;
2126 unsigned long flags;
2127
2128 trace("%p, %p, %X", ep, req, gfp_flags);
2129
2130 if (ep == NULL || req == NULL || mEp->desc == NULL)
2131 return -EINVAL;
2132
2133 spin_lock_irqsave(mEp->lock, flags);
2134
2135 if (mEp->type == USB_ENDPOINT_XFER_CONTROL &&
2136 !list_empty(&mEp->qh[mEp->dir].queue)) {
2137 _ep_nuke(mEp);
2138 retval = -EOVERFLOW;
2139 warn("endpoint ctrl %X nuked", _usb_addr(mEp));
2140 }
2141
2142 /* first nuke then test link, e.g. previous status has not sent */
2143 if (!list_empty(&mReq->queue)) {
2144 retval = -EBUSY;
2145 err("request already in queue");
2146 goto done;
2147 }
2148
2149 if (req->length > (4 * PAGE_SIZE)) {
2150 req->length = (4 * PAGE_SIZE);
2151 retval = -EMSGSIZE;
2152 warn("request length truncated");
2153 }
2154
2155 dbg_queue(_usb_addr(mEp), req, retval);
2156
2157 /* push request */
2158 mReq->req.status = -EINPROGRESS;
2159 mReq->req.actual = 0;
2160 list_add_tail(&mReq->queue, &mEp->qh[mEp->dir].queue);
2161
2162 retval = _hardware_enqueue(mEp, mReq);
2163 if (retval == -EALREADY || retval == -EBUSY) {
2164 dbg_event(_usb_addr(mEp), "QUEUE", retval);
2165 retval = 0;
2166 }
2167
2168 done:
2169 spin_unlock_irqrestore(mEp->lock, flags);
2170 return retval;
2171}
2172
2173/**
2174 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
2175 *
2176 * Check usb_ep_dequeue() at "usb_gadget.h" for details
2177 */
2178static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2179{
2180 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2181 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2182 unsigned long flags;
2183
2184 trace("%p, %p", ep, req);
2185
2186 if (ep == NULL || req == NULL || mEp->desc == NULL ||
2187 list_empty(&mReq->queue) || list_empty(&mEp->qh[mEp->dir].queue))
2188 return -EINVAL;
2189
2190 spin_lock_irqsave(mEp->lock, flags);
2191
2192 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
2193
2194 if (mReq->req.status == -EALREADY)
2195 _hardware_dequeue(mEp, mReq);
2196
2197 /* pop request */
2198 list_del_init(&mReq->queue);
2199 req->status = -ECONNRESET;
2200
2201 if (!mReq->req.no_interrupt && mReq->req.complete != NULL) {
2202 spin_unlock(mEp->lock);
2203 mReq->req.complete(&mEp->ep, &mReq->req);
2204 spin_lock(mEp->lock);
2205 }
2206
2207 spin_unlock_irqrestore(mEp->lock, flags);
2208 return 0;
2209}
2210
2211/**
2212 * ep_set_halt: sets the endpoint halt feature
2213 *
2214 * Check usb_ep_set_halt() at "usb_gadget.h" for details
2215 */
2216static int ep_set_halt(struct usb_ep *ep, int value)
2217{
2218 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2219 int direction, retval = 0;
2220 unsigned long flags;
2221
2222 trace("%p, %i", ep, value);
2223
2224 if (ep == NULL || mEp->desc == NULL)
2225 return -EINVAL;
2226
2227 spin_lock_irqsave(mEp->lock, flags);
2228
2229#ifndef STALL_IN
2230 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
2231 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
2232 !list_empty(&mEp->qh[mEp->dir].queue)) {
2233 spin_unlock_irqrestore(mEp->lock, flags);
2234 return -EAGAIN;
2235 }
2236#endif
2237
2238 direction = mEp->dir;
2239 do {
2240 dbg_event(_usb_addr(mEp), "HALT", value);
2241 retval |= hw_ep_set_halt(mEp->num, mEp->dir, value);
2242
2243 if (!value)
2244 mEp->wedge = 0;
2245
2246 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2247 mEp->dir = (mEp->dir == TX) ? RX : TX;
2248
2249 } while (mEp->dir != direction);
2250
2251 spin_unlock_irqrestore(mEp->lock, flags);
2252 return retval;
2253}
2254
2255/**
2256 * ep_set_wedge: sets the halt feature and ignores clear requests
2257 *
2258 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
2259 */
2260static int ep_set_wedge(struct usb_ep *ep)
2261{
2262 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2263 unsigned long flags;
2264
2265 trace("%p", ep);
2266
2267 if (ep == NULL || mEp->desc == NULL)
2268 return -EINVAL;
2269
2270 spin_lock_irqsave(mEp->lock, flags);
2271
2272 dbg_event(_usb_addr(mEp), "WEDGE", 0);
2273 mEp->wedge = 1;
2274
2275 spin_unlock_irqrestore(mEp->lock, flags);
2276
2277 return usb_ep_set_halt(ep);
2278}
2279
2280/**
2281 * ep_fifo_flush: flushes contents of a fifo
2282 *
2283 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
2284 */
2285static void ep_fifo_flush(struct usb_ep *ep)
2286{
2287 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2288 unsigned long flags;
2289
2290 trace("%p", ep);
2291
2292 if (ep == NULL) {
2293 err("%02X: -EINVAL", _usb_addr(mEp));
2294 return;
2295 }
2296
2297 spin_lock_irqsave(mEp->lock, flags);
2298
2299 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
2300 hw_ep_flush(mEp->num, mEp->dir);
2301
2302 spin_unlock_irqrestore(mEp->lock, flags);
2303}
2304
2305/**
2306 * Endpoint-specific part of the API to the USB controller hardware
2307 * Check "usb_gadget.h" for details
2308 */
2309static const struct usb_ep_ops usb_ep_ops = {
2310 .enable = ep_enable,
2311 .disable = ep_disable,
2312 .alloc_request = ep_alloc_request,
2313 .free_request = ep_free_request,
2314 .queue = ep_queue,
2315 .dequeue = ep_dequeue,
2316 .set_halt = ep_set_halt,
2317 .set_wedge = ep_set_wedge,
2318 .fifo_flush = ep_fifo_flush,
2319};
2320
2321/******************************************************************************
2322 * GADGET block
2323 *****************************************************************************/
2324/**
2325 * Device operations part of the API to the USB controller hardware,
2326 * which don't involve endpoints (or i/o)
2327 * Check "usb_gadget.h" for details
2328 */
2329static const struct usb_gadget_ops usb_gadget_ops;
2330
2331/**
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002332 * usb_gadget_probe_driver: register a gadget driver
2333 * @driver: the driver being registered
2334 * @bind: the driver's bind callback
David Lopoaa69a802008-11-17 14:14:51 -08002335 *
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002336 * Check usb_gadget_probe_driver() at <linux/usb/gadget.h> for details.
2337 * Interrupts are enabled here.
David Lopoaa69a802008-11-17 14:14:51 -08002338 */
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002339int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
2340 int (*bind)(struct usb_gadget *))
David Lopoaa69a802008-11-17 14:14:51 -08002341{
2342 struct ci13xxx *udc = _udc;
2343 unsigned long i, k, flags;
2344 int retval = -ENOMEM;
2345
2346 trace("%p", driver);
2347
2348 if (driver == NULL ||
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002349 bind == NULL ||
David Lopoaa69a802008-11-17 14:14:51 -08002350 driver->unbind == NULL ||
2351 driver->setup == NULL ||
2352 driver->disconnect == NULL ||
2353 driver->suspend == NULL ||
2354 driver->resume == NULL)
2355 return -EINVAL;
2356 else if (udc == NULL)
2357 return -ENODEV;
2358 else if (udc->driver != NULL)
2359 return -EBUSY;
2360
2361 /* alloc resources */
2362 udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
2363 sizeof(struct ci13xxx_qh),
2364 64, PAGE_SIZE);
2365 if (udc->qh_pool == NULL)
2366 return -ENOMEM;
2367
2368 udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
2369 sizeof(struct ci13xxx_td),
2370 64, PAGE_SIZE);
2371 if (udc->td_pool == NULL) {
2372 dma_pool_destroy(udc->qh_pool);
2373 udc->qh_pool = NULL;
2374 return -ENOMEM;
2375 }
2376
2377 spin_lock_irqsave(udc->lock, flags);
2378
2379 info("hw_ep_max = %d", hw_ep_max);
2380
2381 udc->driver = driver;
2382 udc->gadget.ops = NULL;
2383 udc->gadget.dev.driver = NULL;
2384
2385 retval = 0;
2386 for (i = 0; i < hw_ep_max; i++) {
2387 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
2388
2389 scnprintf(mEp->name, sizeof(mEp->name), "ep%i", (int)i);
2390
2391 mEp->lock = udc->lock;
2392 mEp->device = &udc->gadget.dev;
2393 mEp->td_pool = udc->td_pool;
2394
2395 mEp->ep.name = mEp->name;
2396 mEp->ep.ops = &usb_ep_ops;
2397 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
2398
2399 /* this allocation cannot be random */
2400 for (k = RX; k <= TX; k++) {
2401 INIT_LIST_HEAD(&mEp->qh[k].queue);
Pavankumar Kondeti0a91efa2010-12-07 17:54:00 +05302402 spin_unlock_irqrestore(udc->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08002403 mEp->qh[k].ptr = dma_pool_alloc(udc->qh_pool,
2404 GFP_KERNEL,
2405 &mEp->qh[k].dma);
Pavankumar Kondeti0a91efa2010-12-07 17:54:00 +05302406 spin_lock_irqsave(udc->lock, flags);
David Lopoaa69a802008-11-17 14:14:51 -08002407 if (mEp->qh[k].ptr == NULL)
2408 retval = -ENOMEM;
2409 else
2410 memset(mEp->qh[k].ptr, 0,
2411 sizeof(*mEp->qh[k].ptr));
2412 }
2413 if (i == 0)
2414 udc->gadget.ep0 = &mEp->ep;
2415 else
2416 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
2417 }
2418 if (retval)
2419 goto done;
2420
2421 /* bind gadget */
2422 driver->driver.bus = NULL;
2423 udc->gadget.ops = &usb_gadget_ops;
2424 udc->gadget.dev.driver = &driver->driver;
2425
2426 spin_unlock_irqrestore(udc->lock, flags);
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002427 retval = bind(&udc->gadget); /* MAY SLEEP */
David Lopoaa69a802008-11-17 14:14:51 -08002428 spin_lock_irqsave(udc->lock, flags);
2429
2430 if (retval) {
2431 udc->gadget.ops = NULL;
2432 udc->gadget.dev.driver = NULL;
2433 goto done;
2434 }
2435
2436 retval = hw_device_state(udc->ci13xxx_ep[0].qh[RX].dma);
2437
2438 done:
2439 spin_unlock_irqrestore(udc->lock, flags);
2440 if (retval)
2441 usb_gadget_unregister_driver(driver);
2442 return retval;
2443}
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002444EXPORT_SYMBOL(usb_gadget_probe_driver);
David Lopoaa69a802008-11-17 14:14:51 -08002445
2446/**
2447 * usb_gadget_unregister_driver: unregister a gadget driver
2448 *
2449 * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
2450 */
2451int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2452{
2453 struct ci13xxx *udc = _udc;
2454 unsigned long i, k, flags;
2455
2456 trace("%p", driver);
2457
2458 if (driver == NULL ||
David Lopoaa69a802008-11-17 14:14:51 -08002459 driver->unbind == NULL ||
2460 driver->setup == NULL ||
2461 driver->disconnect == NULL ||
2462 driver->suspend == NULL ||
2463 driver->resume == NULL ||
2464 driver != udc->driver)
2465 return -EINVAL;
2466
2467 spin_lock_irqsave(udc->lock, flags);
2468
2469 hw_device_state(0);
2470
2471 /* unbind gadget */
2472 if (udc->gadget.ops != NULL) {
2473 _gadget_stop_activity(&udc->gadget);
2474
2475 spin_unlock_irqrestore(udc->lock, flags);
2476 driver->unbind(&udc->gadget); /* MAY SLEEP */
2477 spin_lock_irqsave(udc->lock, flags);
2478
2479 udc->gadget.ops = NULL;
2480 udc->gadget.dev.driver = NULL;
2481 }
2482
2483 /* free resources */
2484 for (i = 0; i < hw_ep_max; i++) {
2485 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
2486
2487 if (i == 0)
2488 udc->gadget.ep0 = NULL;
2489 else if (!list_empty(&mEp->ep.ep_list))
2490 list_del_init(&mEp->ep.ep_list);
2491
2492 for (k = RX; k <= TX; k++)
2493 if (mEp->qh[k].ptr != NULL)
2494 dma_pool_free(udc->qh_pool,
2495 mEp->qh[k].ptr, mEp->qh[k].dma);
2496 }
2497
2498 udc->driver = NULL;
2499
2500 spin_unlock_irqrestore(udc->lock, flags);
2501
2502 if (udc->td_pool != NULL) {
2503 dma_pool_destroy(udc->td_pool);
2504 udc->td_pool = NULL;
2505 }
2506 if (udc->qh_pool != NULL) {
2507 dma_pool_destroy(udc->qh_pool);
2508 udc->qh_pool = NULL;
2509 }
2510
2511 return 0;
2512}
2513EXPORT_SYMBOL(usb_gadget_unregister_driver);
2514
2515/******************************************************************************
2516 * BUS block
2517 *****************************************************************************/
2518/**
2519 * udc_irq: global interrupt handler
2520 *
2521 * This function returns IRQ_HANDLED if the IRQ has been handled
2522 * It locks access to registers
2523 */
2524static irqreturn_t udc_irq(void)
2525{
2526 struct ci13xxx *udc = _udc;
2527 irqreturn_t retval;
2528 u32 intr;
2529
2530 trace();
2531
2532 if (udc == NULL) {
2533 err("ENODEV");
2534 return IRQ_HANDLED;
2535 }
2536
2537 spin_lock(udc->lock);
2538 intr = hw_test_and_clear_intr_active();
2539 if (intr) {
2540 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
2541 isr_statistics.hndl.idx &= ISR_MASK;
2542 isr_statistics.hndl.cnt++;
2543
2544 /* order defines priority - do NOT change it */
2545 if (USBi_URI & intr) {
2546 isr_statistics.uri++;
2547 isr_reset_handler(udc);
2548 }
2549 if (USBi_PCI & intr) {
2550 isr_statistics.pci++;
2551 udc->gadget.speed = hw_port_is_high_speed() ?
2552 USB_SPEED_HIGH : USB_SPEED_FULL;
2553 }
2554 if (USBi_UEI & intr)
2555 isr_statistics.uei++;
2556 if (USBi_UI & intr) {
2557 isr_statistics.ui++;
2558 isr_tr_complete_handler(udc);
2559 }
2560 if (USBi_SLI & intr)
2561 isr_statistics.sli++;
2562 retval = IRQ_HANDLED;
2563 } else {
2564 isr_statistics.none++;
2565 retval = IRQ_NONE;
2566 }
2567 spin_unlock(udc->lock);
2568
2569 return retval;
2570}
2571
2572/**
2573 * udc_release: driver release function
2574 * @dev: device
2575 *
2576 * Currently does nothing
2577 */
2578static void udc_release(struct device *dev)
2579{
2580 trace("%p", dev);
2581
2582 if (dev == NULL)
2583 err("EINVAL");
2584}
2585
2586/**
2587 * udc_probe: parent probe must call this to initialize UDC
2588 * @dev: parent device
2589 * @regs: registers base address
2590 * @name: driver name
2591 *
2592 * This function returns an error code
2593 * No interrupts active, the IRQ has not been requested yet
2594 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2595 */
2596static int udc_probe(struct device *dev, void __iomem *regs, const char *name)
2597{
2598 struct ci13xxx *udc;
2599 int retval = 0;
2600
2601 trace("%p, %p, %p", dev, regs, name);
2602
2603 if (dev == NULL || regs == NULL || name == NULL)
2604 return -EINVAL;
2605
2606 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
2607 if (udc == NULL)
2608 return -ENOMEM;
2609
2610 udc->lock = &udc_lock;
2611
2612 retval = hw_device_reset(regs);
2613 if (retval)
2614 goto done;
2615
2616 udc->gadget.ops = NULL;
2617 udc->gadget.speed = USB_SPEED_UNKNOWN;
2618 udc->gadget.is_dualspeed = 1;
2619 udc->gadget.is_otg = 0;
2620 udc->gadget.name = name;
2621
2622 INIT_LIST_HEAD(&udc->gadget.ep_list);
2623 udc->gadget.ep0 = NULL;
2624
Kay Sievers5df58522009-03-24 16:38:23 -07002625 dev_set_name(&udc->gadget.dev, "gadget");
David Lopoaa69a802008-11-17 14:14:51 -08002626 udc->gadget.dev.dma_mask = dev->dma_mask;
2627 udc->gadget.dev.parent = dev;
2628 udc->gadget.dev.release = udc_release;
2629
2630 retval = device_register(&udc->gadget.dev);
2631 if (retval)
2632 goto done;
2633
2634#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2635 retval = dbg_create_files(&udc->gadget.dev);
2636#endif
2637 if (retval) {
2638 device_unregister(&udc->gadget.dev);
2639 goto done;
2640 }
2641
2642 _udc = udc;
2643 return retval;
2644
2645 done:
2646 err("error = %i", retval);
2647 kfree(udc);
2648 _udc = NULL;
2649 return retval;
2650}
2651
2652/**
2653 * udc_remove: parent remove must call this to remove UDC
2654 *
2655 * No interrupts active, the IRQ has been released
2656 */
2657static void udc_remove(void)
2658{
2659 struct ci13xxx *udc = _udc;
2660
2661 if (udc == NULL) {
2662 err("EINVAL");
2663 return;
2664 }
2665
2666#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2667 dbg_remove_files(&udc->gadget.dev);
2668#endif
2669 device_unregister(&udc->gadget.dev);
2670
2671 kfree(udc);
2672 _udc = NULL;
2673}