1 #include <linux/delay.h>
2 #include <linux/device.h>
3 #include <linux/dmapool.h>
4 #include <linux/dma-mapping.h>
5 #include <linux/init.h>
6 #include <linux/platform_device.h>
7 #include <linux/module.h>
8 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/gadget.h>
16 #include <linux/usb/otg.h>
17 #include <linux/usb/chipidea.h>
24 /* Interrupt statistics */
26 static struct isr_statistics {
41 void dbg_interrupt(u32 intmask)
44 isr_statistics.none++;
48 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intmask;
49 isr_statistics.hndl.idx &= ISR_MASK;
50 isr_statistics.hndl.cnt++;
52 if (USBi_URI & intmask)
54 if (USBi_PCI & intmask)
56 if (USBi_UEI & intmask)
58 if (USBi_UI & intmask)
60 if (USBi_SLI & intmask)
65 * hw_register_read: reads all device registers (execute without interruption)
66 * @buf: destination buffer
69 * This function returns number of registers read
71 static size_t hw_register_read(struct ci13xxx *udc, u32 *buf, size_t size)
75 if (size > udc->hw_bank.size)
76 size = udc->hw_bank.size;
78 for (i = 0; i < size; i++)
79 buf[i] = hw_read(udc, i * sizeof(u32), ~0);
85 * hw_register_write: writes to register
86 * @addr: register address
87 * @data: register value
89 * This function returns an error code
91 static int hw_register_write(struct ci13xxx *udc, u16 addr, u32 data)
96 if (addr >= udc->hw_bank.size)
102 hw_write(udc, addr, ~0, data);
107 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
111 * This function returns an error code
113 static int hw_intr_clear(struct ci13xxx *udc, int n)
118 hw_write(udc, OP_USBINTR, BIT(n), 0);
119 hw_write(udc, OP_USBSTS, BIT(n), BIT(n));
124 * hw_intr_force: enables interrupt & forces interrupt status (execute without
128 * This function returns an error code
130 static int hw_intr_force(struct ci13xxx *udc, int n)
135 hw_write(udc, CAP_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
136 hw_write(udc, OP_USBINTR, BIT(n), BIT(n));
137 hw_write(udc, OP_USBSTS, BIT(n), BIT(n));
138 hw_write(udc, CAP_TESTMODE, TESTMODE_FORCE, 0);
143 * show_device: prints information about device capabilities and status
145 * Check "device.h" for details
147 static ssize_t show_device(struct device *dev, struct device_attribute *attr,
150 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
151 struct usb_gadget *gadget = &udc->gadget;
154 if (attr == NULL || buf == NULL) {
155 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
159 n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
161 n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed = %d\n",
163 /* TODO: Scheduled for removal in 3.8. */
164 n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
165 gadget_is_dualspeed(gadget));
166 n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
168 n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
169 gadget->is_a_peripheral);
170 n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
171 gadget->b_hnp_enable);
172 n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
173 gadget->a_hnp_support);
174 n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
175 gadget->a_alt_hnp_support);
176 n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
177 (gadget->name ? gadget->name : ""));
181 static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
184 * show_driver: prints information about attached gadget (if any)
186 * Check "device.h" for details
188 static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
191 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
192 struct usb_gadget_driver *driver = udc->driver;
195 if (attr == NULL || buf == NULL) {
196 dev_err(dev, "[%s] EINVAL\n", __func__);
201 return scnprintf(buf, PAGE_SIZE,
202 "There is no gadget attached!\n");
204 n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
205 (driver->function ? driver->function : ""));
206 n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
211 static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
213 /* Maximum event message length */
214 #define DBG_DATA_MSG 64UL
216 /* Maximum event messages */
217 #define DBG_DATA_MAX 128UL
219 /* Event buffer descriptor */
221 char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
222 unsigned idx; /* index */
223 unsigned tty; /* print to console? */
224 rwlock_t lck; /* lock */
228 .lck = __RW_LOCK_UNLOCKED(lck)
232 * dbg_dec: decrements debug event index
235 static void dbg_dec(unsigned *idx)
237 *idx = (*idx - 1) & (DBG_DATA_MAX-1);
241 * dbg_inc: increments debug event index
244 static void dbg_inc(unsigned *idx)
246 *idx = (*idx + 1) & (DBG_DATA_MAX-1);
250 * dbg_print: prints the common part of the event
251 * @addr: endpoint address
254 * @extra: extra information
256 static void dbg_print(u8 addr, const char *name, int status, const char *extra)
262 write_lock_irqsave(&dbg_data.lck, flags);
264 do_gettimeofday(&tval);
265 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
266 stamp = stamp * 1000000 + tval.tv_usec;
268 scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
269 "%04X\t? %02X %-7.7s %4i ?\t%s\n",
270 stamp, addr, name, status, extra);
272 dbg_inc(&dbg_data.idx);
274 write_unlock_irqrestore(&dbg_data.lck, flags);
276 if (dbg_data.tty != 0)
277 pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
278 stamp, addr, name, status, extra);
282 * dbg_done: prints a DONE event
283 * @addr: endpoint address
284 * @td: transfer descriptor
287 void dbg_done(u8 addr, const u32 token, int status)
289 char msg[DBG_DATA_MSG];
291 scnprintf(msg, sizeof(msg), "%d %02X",
292 (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
293 (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
294 dbg_print(addr, "DONE", status, msg);
298 * dbg_event: prints a generic event
299 * @addr: endpoint address
303 void dbg_event(u8 addr, const char *name, int status)
306 dbg_print(addr, name, status, "");
310 * dbg_queue: prints a QUEUE event
311 * @addr: endpoint address
315 void dbg_queue(u8 addr, const struct usb_request *req, int status)
317 char msg[DBG_DATA_MSG];
320 scnprintf(msg, sizeof(msg),
321 "%d %d", !req->no_interrupt, req->length);
322 dbg_print(addr, "QUEUE", status, msg);
327 * dbg_setup: prints a SETUP event
328 * @addr: endpoint address
329 * @req: setup request
331 void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
333 char msg[DBG_DATA_MSG];
336 scnprintf(msg, sizeof(msg),
337 "%02X %02X %04X %04X %d", req->bRequestType,
338 req->bRequest, le16_to_cpu(req->wValue),
339 le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
340 dbg_print(addr, "SETUP", 0, msg);
345 * show_events: displays the event buffer
347 * Check "device.h" for details
349 static ssize_t show_events(struct device *dev, struct device_attribute *attr,
353 unsigned i, j, n = 0;
355 if (attr == NULL || buf == NULL) {
356 dev_err(dev->parent, "[%s] EINVAL\n", __func__);
360 read_lock_irqsave(&dbg_data.lck, flags);
363 for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
364 n += strlen(dbg_data.buf[i]);
365 if (n >= PAGE_SIZE) {
366 n -= strlen(dbg_data.buf[i]);
370 for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
371 j += scnprintf(buf + j, PAGE_SIZE - j,
372 "%s", dbg_data.buf[i]);
374 read_unlock_irqrestore(&dbg_data.lck, flags);
380 * store_events: configure if events are going to be also printed to console
382 * Check "device.h" for details
384 static ssize_t store_events(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
389 if (attr == NULL || buf == NULL) {
390 dev_err(dev, "[%s] EINVAL\n", __func__);
394 if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
395 dev_err(dev, "<1|0>: enable|disable console log\n");
400 dev_info(dev, "tty = %u", dbg_data.tty);
405 static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
408 * show_inters: interrupt status, enable status and historic
410 * Check "device.h" for details
412 static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
415 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
418 unsigned i, j, n = 0;
420 if (attr == NULL || buf == NULL) {
421 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
425 spin_lock_irqsave(&udc->lock, flags);
427 /*n += scnprintf(buf + n, PAGE_SIZE - n,
428 "status = %08x\n", hw_read_intr_status(udc));
429 n += scnprintf(buf + n, PAGE_SIZE - n,
430 "enable = %08x\n", hw_read_intr_enable(udc));*/
432 n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
433 isr_statistics.test);
434 n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n",
436 n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
438 n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
440 n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
442 n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
444 n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
445 isr_statistics.none);
446 n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
447 isr_statistics.hndl.cnt);
449 for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
451 intr = isr_statistics.hndl.buf[i];
454 n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
457 n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
460 n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
463 n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
466 n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
469 n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
470 if (isr_statistics.hndl.buf[i])
471 n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
474 spin_unlock_irqrestore(&udc->lock, flags);
480 * store_inters: enable & force or disable an individual interrutps
481 * (to be used for test purposes only)
483 * Check "device.h" for details
485 static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
486 const char *buf, size_t count)
488 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
492 if (attr == NULL || buf == NULL) {
493 dev_err(udc->dev, "EINVAL\n");
497 if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
498 dev_err(udc->dev, "<1|0> <bit>: enable|disable interrupt\n");
502 spin_lock_irqsave(&udc->lock, flags);
504 if (hw_intr_force(udc, bit))
505 dev_err(dev, "invalid bit number\n");
507 isr_statistics.test++;
509 if (hw_intr_clear(udc, bit))
510 dev_err(dev, "invalid bit number\n");
512 spin_unlock_irqrestore(&udc->lock, flags);
517 static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
520 * show_port_test: reads port test mode
522 * Check "device.h" for details
524 static ssize_t show_port_test(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
531 if (attr == NULL || buf == NULL) {
532 dev_err(udc->dev, "EINVAL\n");
536 spin_lock_irqsave(&udc->lock, flags);
537 mode = hw_port_test_get(udc);
538 spin_unlock_irqrestore(&udc->lock, flags);
540 return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
544 * store_port_test: writes port test mode
546 * Check "device.h" for details
548 static ssize_t store_port_test(struct device *dev,
549 struct device_attribute *attr,
550 const char *buf, size_t count)
552 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
556 if (attr == NULL || buf == NULL) {
557 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
561 if (sscanf(buf, "%u", &mode) != 1) {
562 dev_err(udc->dev, "<mode>: set port test mode");
566 spin_lock_irqsave(&udc->lock, flags);
567 if (hw_port_test_set(udc, mode))
568 dev_err(udc->dev, "invalid mode\n");
569 spin_unlock_irqrestore(&udc->lock, flags);
574 static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
575 show_port_test, store_port_test);
578 * show_qheads: DMA contents of all queue heads
580 * Check "device.h" for details
582 static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
585 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
587 unsigned i, j, n = 0;
589 if (attr == NULL || buf == NULL) {
590 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
594 spin_lock_irqsave(&udc->lock, flags);
595 for (i = 0; i < udc->hw_ep_max/2; i++) {
596 struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
597 struct ci13xxx_ep *mEpTx =
598 &udc->ci13xxx_ep[i + udc->hw_ep_max/2];
599 n += scnprintf(buf + n, PAGE_SIZE - n,
600 "EP=%02i: RX=%08X TX=%08X\n",
601 i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
602 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
603 n += scnprintf(buf + n, PAGE_SIZE - n,
604 " %04X: %08X %08X\n", j,
605 *((u32 *)mEpRx->qh.ptr + j),
606 *((u32 *)mEpTx->qh.ptr + j));
609 spin_unlock_irqrestore(&udc->lock, flags);
613 static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
616 * show_registers: dumps all registers
618 * Check "device.h" for details
620 #define DUMP_ENTRIES 512
621 static ssize_t show_registers(struct device *dev,
622 struct device_attribute *attr, char *buf)
624 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
627 unsigned i, k, n = 0;
629 if (attr == NULL || buf == NULL) {
630 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
634 dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
636 dev_err(udc->dev, "%s: out of memory\n", __func__);
640 spin_lock_irqsave(&udc->lock, flags);
641 k = hw_register_read(udc, dump, DUMP_ENTRIES);
642 spin_unlock_irqrestore(&udc->lock, flags);
644 for (i = 0; i < k; i++) {
645 n += scnprintf(buf + n, PAGE_SIZE - n,
646 "reg[0x%04X] = 0x%08X\n",
647 i * (unsigned)sizeof(u32), dump[i]);
655 * store_registers: writes value to register address
657 * Check "device.h" for details
659 static ssize_t store_registers(struct device *dev,
660 struct device_attribute *attr,
661 const char *buf, size_t count)
663 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
664 unsigned long addr, data, flags;
666 if (attr == NULL || buf == NULL) {
667 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
671 if (sscanf(buf, "%li %li", &addr, &data) != 2) {
673 "<addr> <data>: write data to register address\n");
677 spin_lock_irqsave(&udc->lock, flags);
678 if (hw_register_write(udc, addr, data))
679 dev_err(udc->dev, "invalid address range\n");
680 spin_unlock_irqrestore(&udc->lock, flags);
685 static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
686 show_registers, store_registers);
689 * show_requests: DMA contents of all requests currently queued (all endpts)
691 * Check "device.h" for details
693 static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
696 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
698 struct list_head *ptr = NULL;
699 struct ci13xxx_req *req = NULL;
700 unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
702 if (attr == NULL || buf == NULL) {
703 dev_err(udc->dev, "[%s] EINVAL\n", __func__);
707 spin_lock_irqsave(&udc->lock, flags);
708 for (i = 0; i < udc->hw_ep_max; i++)
709 list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
711 req = list_entry(ptr, struct ci13xxx_req, queue);
713 n += scnprintf(buf + n, PAGE_SIZE - n,
714 "EP=%02i: TD=%08X %s\n",
715 i % udc->hw_ep_max/2, (u32)req->dma,
716 ((i < udc->hw_ep_max/2) ? "RX" : "TX"));
718 for (j = 0; j < qSize; j++)
719 n += scnprintf(buf + n, PAGE_SIZE - n,
721 *((u32 *)req->ptr + j));
723 spin_unlock_irqrestore(&udc->lock, flags);
727 static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
730 * dbg_create_files: initializes the attribute interface
733 * This function returns an error code
735 int dbg_create_files(struct device *dev)
741 retval = device_create_file(dev, &dev_attr_device);
744 retval = device_create_file(dev, &dev_attr_driver);
747 retval = device_create_file(dev, &dev_attr_events);
750 retval = device_create_file(dev, &dev_attr_inters);
753 retval = device_create_file(dev, &dev_attr_port_test);
756 retval = device_create_file(dev, &dev_attr_qheads);
759 retval = device_create_file(dev, &dev_attr_registers);
762 retval = device_create_file(dev, &dev_attr_requests);
768 device_remove_file(dev, &dev_attr_registers);
770 device_remove_file(dev, &dev_attr_qheads);
772 device_remove_file(dev, &dev_attr_port_test);
774 device_remove_file(dev, &dev_attr_inters);
776 device_remove_file(dev, &dev_attr_events);
778 device_remove_file(dev, &dev_attr_driver);
780 device_remove_file(dev, &dev_attr_device);
786 * dbg_remove_files: destroys the attribute interface
789 * This function returns an error code
791 int dbg_remove_files(struct device *dev)
795 device_remove_file(dev, &dev_attr_requests);
796 device_remove_file(dev, &dev_attr_registers);
797 device_remove_file(dev, &dev_attr_qheads);
798 device_remove_file(dev, &dev_attr_port_test);
799 device_remove_file(dev, &dev_attr_inters);
800 device_remove_file(dev, &dev_attr_events);
801 device_remove_file(dev, &dev_attr_driver);
802 device_remove_file(dev, &dev_attr_device);