Merge tag 'usb-for-v3.19' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
[deliverable/linux.git] / drivers / usb / gadget / udc / bdc / bdc_ep.c
1 /*
2 * bdc_ep.c - BRCM BDC USB3.0 device controller endpoint related functions
3 *
4 * Copyright (C) 2014 Broadcom Corporation
5 *
6 * Author: Ashwini Pahuja
7 *
8 * Based on drivers under drivers/usb/
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/dmapool.h>
22 #include <linux/ioport.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/timer.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
30 #include <linux/moduleparam.h>
31 #include <linux/device.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/otg.h>
35 #include <linux/pm.h>
36 #include <linux/io.h>
37 #include <linux/irq.h>
38 #include <asm/unaligned.h>
39 #include <linux/platform_device.h>
40 #include <linux/usb/composite.h>
41
42 #include "bdc.h"
43 #include "bdc_ep.h"
44 #include "bdc_cmd.h"
45 #include "bdc_dbg.h"
46
47 static const char * const ep0_state_string[] = {
48 "WAIT_FOR_SETUP",
49 "WAIT_FOR_DATA_START",
50 "WAIT_FOR_DATA_XMIT",
51 "WAIT_FOR_STATUS_START",
52 "WAIT_FOR_STATUS_XMIT",
53 "STATUS_PENDING"
54 };
55
56 /* Free the bdl during ep disable */
57 static void ep_bd_list_free(struct bdc_ep *ep, u32 num_tabs)
58 {
59 struct bd_list *bd_list = &ep->bd_list;
60 struct bdc *bdc = ep->bdc;
61 struct bd_table *bd_table;
62 int index;
63
64 dev_dbg(bdc->dev, "%s ep:%s num_tabs:%d\n",
65 __func__, ep->name, num_tabs);
66
67 if (!bd_list->bd_table_array) {
68 dev_dbg(bdc->dev, "%s already freed\n", ep->name);
69 return;
70 }
71 for (index = 0; index < num_tabs; index++) {
72 /*
73 * check if the bd_table struct is allocated ?
74 * if yes, then check if bd memory has been allocated, then
75 * free the dma_pool and also the bd_table struct memory
76 */
77 bd_table = bd_list->bd_table_array[index];
78 dev_dbg(bdc->dev, "bd_table:%p index:%d\n", bd_table, index);
79 if (!bd_table) {
80 dev_dbg(bdc->dev, "bd_table not allocated\n");
81 continue;
82 }
83 if (!bd_table->start_bd) {
84 dev_dbg(bdc->dev, "bd dma pool not allocted\n");
85 continue;
86 }
87
88 dev_dbg(bdc->dev,
89 "Free dma pool start_bd:%p dma:%llx\n",
90 bd_table->start_bd,
91 (unsigned long long)bd_table->dma);
92
93 dma_pool_free(bdc->bd_table_pool,
94 bd_table->start_bd,
95 bd_table->dma);
96 /* Free the bd_table structure */
97 kfree(bd_table);
98 }
99 /* Free the bd table array */
100 kfree(ep->bd_list.bd_table_array);
101 }
102
103 /*
104 * chain the tables, by insteting a chain bd at the end of prev_table, pointing
105 * to next_table
106 */
107 static inline void chain_table(struct bd_table *prev_table,
108 struct bd_table *next_table,
109 u32 bd_p_tab)
110 {
111 /* Chain the prev table to next table */
112 prev_table->start_bd[bd_p_tab-1].offset[0] =
113 cpu_to_le32(lower_32_bits(next_table->dma));
114
115 prev_table->start_bd[bd_p_tab-1].offset[1] =
116 cpu_to_le32(upper_32_bits(next_table->dma));
117
118 prev_table->start_bd[bd_p_tab-1].offset[2] =
119 0x0;
120
121 prev_table->start_bd[bd_p_tab-1].offset[3] =
122 cpu_to_le32(MARK_CHAIN_BD);
123 }
124
125 /* Allocate the bdl for ep, during config ep */
126 static int ep_bd_list_alloc(struct bdc_ep *ep)
127 {
128 struct bd_table *prev_table = NULL;
129 int index, num_tabs, bd_p_tab;
130 struct bdc *bdc = ep->bdc;
131 struct bd_table *bd_table;
132 dma_addr_t dma;
133
134 if (usb_endpoint_xfer_isoc(ep->desc))
135 num_tabs = NUM_TABLES_ISOCH;
136 else
137 num_tabs = NUM_TABLES;
138
139 bd_p_tab = NUM_BDS_PER_TABLE;
140 /* if there is only 1 table in bd list then loop chain to self */
141 dev_dbg(bdc->dev,
142 "%s ep:%p num_tabs:%d\n",
143 __func__, ep, num_tabs);
144
145 /* Allocate memory for table array */
146 ep->bd_list.bd_table_array = kzalloc(
147 num_tabs * sizeof(struct bd_table *),
148 GFP_ATOMIC);
149 if (!ep->bd_list.bd_table_array)
150 return -ENOMEM;
151
152 /* Allocate memory for each table */
153 for (index = 0; index < num_tabs; index++) {
154 /* Allocate memory for bd_table structure */
155 bd_table = kzalloc(sizeof(struct bd_table), GFP_ATOMIC);
156 if (!bd_table)
157 goto fail;
158
159 bd_table->start_bd = dma_pool_alloc(bdc->bd_table_pool,
160 GFP_ATOMIC,
161 &dma);
162 if (!bd_table->start_bd)
163 goto fail;
164
165 bd_table->dma = dma;
166
167 dev_dbg(bdc->dev,
168 "index:%d start_bd:%p dma=%08llx prev_table:%p\n",
169 index, bd_table->start_bd,
170 (unsigned long long)bd_table->dma, prev_table);
171
172 ep->bd_list.bd_table_array[index] = bd_table;
173 memset(bd_table->start_bd, 0, bd_p_tab * sizeof(struct bdc_bd));
174 if (prev_table)
175 chain_table(prev_table, bd_table, bd_p_tab);
176
177 prev_table = bd_table;
178 }
179 chain_table(prev_table, ep->bd_list.bd_table_array[0], bd_p_tab);
180 /* Memory allocation is successful, now init the internal fields */
181 ep->bd_list.num_tabs = num_tabs;
182 ep->bd_list.max_bdi = (num_tabs * bd_p_tab) - 1;
183 ep->bd_list.num_tabs = num_tabs;
184 ep->bd_list.num_bds_table = bd_p_tab;
185 ep->bd_list.eqp_bdi = 0;
186 ep->bd_list.hwd_bdi = 0;
187
188 return 0;
189 fail:
190 /* Free the bd_table_array, bd_table struct, bd's */
191 ep_bd_list_free(ep, num_tabs);
192
193 return -ENOMEM;
194 }
195
196 /* returns how many bd's are need for this transfer */
197 static inline int bd_needed_req(struct bdc_req *req)
198 {
199 int bd_needed = 0;
200 int remaining;
201
202 /* 1 bd needed for 0 byte transfer */
203 if (req->usb_req.length == 0)
204 return 1;
205
206 /* remaining bytes after tranfering all max BD size BD's */
207 remaining = req->usb_req.length % BD_MAX_BUFF_SIZE;
208 if (remaining)
209 bd_needed++;
210
211 /* How many maximum BUFF size BD's ? */
212 remaining = req->usb_req.length / BD_MAX_BUFF_SIZE;
213 bd_needed += remaining;
214
215 return bd_needed;
216 }
217
218 /* returns the bd index(bdi) corresponding to bd dma address */
219 static int bd_add_to_bdi(struct bdc_ep *ep, dma_addr_t bd_dma_addr)
220 {
221 struct bd_list *bd_list = &ep->bd_list;
222 dma_addr_t dma_first_bd, dma_last_bd;
223 struct bdc *bdc = ep->bdc;
224 struct bd_table *bd_table;
225 bool found = false;
226 int tbi, bdi;
227
228 dma_first_bd = dma_last_bd = 0;
229 dev_dbg(bdc->dev, "%s %llx\n",
230 __func__, (unsigned long long)bd_dma_addr);
231 /*
232 * Find in which table this bd_dma_addr belongs?, go through the table
233 * array and compare addresses of first and last address of bd of each
234 * table
235 */
236 for (tbi = 0; tbi < bd_list->num_tabs; tbi++) {
237 bd_table = bd_list->bd_table_array[tbi];
238 dma_first_bd = bd_table->dma;
239 dma_last_bd = bd_table->dma +
240 (sizeof(struct bdc_bd) *
241 (bd_list->num_bds_table - 1));
242 dev_dbg(bdc->dev, "dma_first_bd:%llx dma_last_bd:%llx\n",
243 (unsigned long long)dma_first_bd,
244 (unsigned long long)dma_last_bd);
245 if (bd_dma_addr >= dma_first_bd && bd_dma_addr <= dma_last_bd) {
246 found = true;
247 break;
248 }
249 }
250 if (unlikely(!found)) {
251 dev_err(bdc->dev, "%s FATAL err, bd not found\n", __func__);
252 return -EINVAL;
253 }
254 /* Now we know the table, find the bdi */
255 bdi = (bd_dma_addr - dma_first_bd) / sizeof(struct bdc_bd);
256
257 /* return the global bdi, to compare with ep eqp_bdi */
258 return (bdi + (tbi * bd_list->num_bds_table));
259 }
260
261 /* returns the table index(tbi) of the given bdi */
262 static int bdi_to_tbi(struct bdc_ep *ep, int bdi)
263 {
264 int tbi;
265
266 tbi = bdi / ep->bd_list.num_bds_table;
267 dev_vdbg(ep->bdc->dev,
268 "bdi:%d num_bds_table:%d tbi:%d\n",
269 bdi, ep->bd_list.num_bds_table, tbi);
270
271 return tbi;
272 }
273
274 /* Find the bdi last bd in the transfer */
275 static inline int find_end_bdi(struct bdc_ep *ep, int next_hwd_bdi)
276 {
277 int end_bdi;
278
279 end_bdi = next_hwd_bdi - 1;
280 if (end_bdi < 0)
281 end_bdi = ep->bd_list.max_bdi - 1;
282 else if ((end_bdi % (ep->bd_list.num_bds_table-1)) == 0)
283 end_bdi--;
284
285 return end_bdi;
286 }
287
288 /*
289 * How many transfer bd's are available on this ep bdl, chain bds are not
290 * counted in available bds
291 */
292 static int bd_available_ep(struct bdc_ep *ep)
293 {
294 struct bd_list *bd_list = &ep->bd_list;
295 int available1, available2;
296 struct bdc *bdc = ep->bdc;
297 int chain_bd1, chain_bd2;
298 int available_bd = 0;
299
300 available1 = available2 = chain_bd1 = chain_bd2 = 0;
301 /* if empty then we have all bd's available - number of chain bd's */
302 if (bd_list->eqp_bdi == bd_list->hwd_bdi)
303 return bd_list->max_bdi - bd_list->num_tabs;
304
305 /*
306 * Depending upon where eqp and dqp pointers are, caculate number
307 * of avaialble bd's
308 */
309 if (bd_list->hwd_bdi < bd_list->eqp_bdi) {
310 /* available bd's are from eqp..max_bds + 0..dqp - chain_bds */
311 available1 = bd_list->max_bdi - bd_list->eqp_bdi;
312 available2 = bd_list->hwd_bdi;
313 chain_bd1 = available1 / bd_list->num_bds_table;
314 chain_bd2 = available2 / bd_list->num_bds_table;
315 dev_vdbg(bdc->dev, "chain_bd1:%d chain_bd2:%d\n",
316 chain_bd1, chain_bd2);
317 available_bd = available1 + available2 - chain_bd1 - chain_bd2;
318 } else {
319 /* available bd's are from eqp..dqp - number of chain bd's */
320 available1 = bd_list->hwd_bdi - bd_list->eqp_bdi;
321 /* if gap between eqp and dqp is less than NUM_BDS_PER_TABLE */
322 if ((bd_list->hwd_bdi - bd_list->eqp_bdi)
323 <= bd_list->num_bds_table) {
324 /* If there any chain bd in between */
325 if (!(bdi_to_tbi(ep, bd_list->hwd_bdi)
326 == bdi_to_tbi(ep, bd_list->eqp_bdi))) {
327 available_bd = available1 - 1;
328 }
329 } else {
330 chain_bd1 = available1 / bd_list->num_bds_table;
331 available_bd = available1 - chain_bd1;
332 }
333 }
334 /*
335 * we need to keep one extra bd to check if ring is full or empty so
336 * reduce by 1
337 */
338 available_bd--;
339 dev_vdbg(bdc->dev, "available_bd:%d\n", available_bd);
340
341 return available_bd;
342 }
343
344 /* Notify the hardware after queueing the bd to bdl */
345 void bdc_notify_xfr(struct bdc *bdc, u32 epnum)
346 {
347 struct bdc_ep *ep = bdc->bdc_ep_array[epnum];
348
349 dev_vdbg(bdc->dev, "%s epnum:%d\n", __func__, epnum);
350 /*
351 * We don't have anyway to check if ep state is running,
352 * except the software flags.
353 */
354 if (unlikely(ep->flags & BDC_EP_STOP))
355 ep->flags &= ~BDC_EP_STOP;
356
357 bdc_writel(bdc->regs, BDC_XSFNTF, epnum);
358 }
359
360 /* returns the bd corresponding to bdi */
361 static struct bdc_bd *bdi_to_bd(struct bdc_ep *ep, int bdi)
362 {
363 int tbi = bdi_to_tbi(ep, bdi);
364 int local_bdi = 0;
365
366 local_bdi = bdi - (tbi * ep->bd_list.num_bds_table);
367 dev_vdbg(ep->bdc->dev,
368 "%s bdi:%d local_bdi:%d\n",
369 __func__, bdi, local_bdi);
370
371 return (ep->bd_list.bd_table_array[tbi]->start_bd + local_bdi);
372 }
373
374 /* Advance the enqueue pointer */
375 static void ep_bdlist_eqp_adv(struct bdc_ep *ep)
376 {
377 ep->bd_list.eqp_bdi++;
378 /* if it's chain bd, then move to next */
379 if (((ep->bd_list.eqp_bdi + 1) % ep->bd_list.num_bds_table) == 0)
380 ep->bd_list.eqp_bdi++;
381
382 /* if the eqp is pointing to last + 1 then move back to 0 */
383 if (ep->bd_list.eqp_bdi == (ep->bd_list.max_bdi + 1))
384 ep->bd_list.eqp_bdi = 0;
385 }
386
387 /* Setup the first bd for ep0 transfer */
388 static int setup_first_bd_ep0(struct bdc *bdc, struct bdc_req *req, u32 *dword3)
389 {
390 u16 wValue;
391 u32 req_len;
392
393 req->ep->dir = 0;
394 req_len = req->usb_req.length;
395 switch (bdc->ep0_state) {
396 case WAIT_FOR_DATA_START:
397 *dword3 |= BD_TYPE_DS;
398 if (bdc->setup_pkt.bRequestType & USB_DIR_IN)
399 *dword3 |= BD_DIR_IN;
400
401 /* check if zlp will be needed */
402 wValue = le16_to_cpu(bdc->setup_pkt.wValue);
403 if ((wValue > req_len) &&
404 (req_len % bdc->gadget.ep0->maxpacket == 0)) {
405 dev_dbg(bdc->dev, "ZLP needed wVal:%d len:%d MaxP:%d\n",
406 wValue, req_len,
407 bdc->gadget.ep0->maxpacket);
408 bdc->zlp_needed = true;
409 }
410 break;
411
412 case WAIT_FOR_STATUS_START:
413 *dword3 |= BD_TYPE_SS;
414 if (!le16_to_cpu(bdc->setup_pkt.wLength) ||
415 !(bdc->setup_pkt.bRequestType & USB_DIR_IN))
416 *dword3 |= BD_DIR_IN;
417 break;
418 default:
419 dev_err(bdc->dev,
420 "Unknown ep0 state for queueing bd ep0_state:%s\n",
421 ep0_state_string[bdc->ep0_state]);
422 return -EINVAL;
423 }
424
425 return 0;
426 }
427
428 /* Setup the bd dma descriptor for a given request */
429 static int setup_bd_list_xfr(struct bdc *bdc, struct bdc_req *req, int num_bds)
430 {
431 dma_addr_t buf_add = req->usb_req.dma;
432 u32 maxp, tfs, dword2, dword3;
433 struct bd_transfer *bd_xfr;
434 struct bd_list *bd_list;
435 struct bdc_ep *ep;
436 struct bdc_bd *bd;
437 int ret, bdnum;
438 u32 req_len;
439
440 ep = req->ep;
441 bd_list = &ep->bd_list;
442 bd_xfr = &req->bd_xfr;
443 bd_xfr->req = req;
444 bd_xfr->start_bdi = bd_list->eqp_bdi;
445 bd = bdi_to_bd(ep, bd_list->eqp_bdi);
446 req_len = req->usb_req.length;
447 maxp = usb_endpoint_maxp(ep->desc) & 0x7ff;
448 tfs = roundup(req->usb_req.length, maxp);
449 tfs = tfs/maxp;
450 dev_vdbg(bdc->dev, "%s ep:%s num_bds:%d tfs:%d r_len:%d bd:%p\n",
451 __func__, ep->name, num_bds, tfs, req_len, bd);
452
453 for (bdnum = 0; bdnum < num_bds; bdnum++) {
454 dword2 = dword3 = 0;
455 /* First bd */
456 if (!bdnum) {
457 dword3 |= BD_SOT|BD_SBF|(tfs<<BD_TFS_SHIFT);
458 dword2 |= BD_LTF;
459 /* format of first bd for ep0 is different than other */
460 if (ep->ep_num == 1)
461 ret = setup_first_bd_ep0(bdc, req, &dword3);
462 if (ret)
463 return ret;
464 }
465 if (!req->ep->dir)
466 dword3 |= BD_ISP;
467
468 if (req_len > BD_MAX_BUFF_SIZE) {
469 dword2 |= BD_MAX_BUFF_SIZE;
470 req_len -= BD_MAX_BUFF_SIZE;
471 } else {
472 /* this should be the last bd */
473 dword2 |= req_len;
474 dword3 |= BD_IOC;
475 dword3 |= BD_EOT;
476 }
477 /* Currently only 1 INT target is supported */
478 dword2 |= BD_INTR_TARGET(0);
479 bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
480 if (unlikely(!bd)) {
481 dev_err(bdc->dev, "Err bd pointing to wrong addr\n");
482 return -EINVAL;
483 }
484 /* write bd */
485 bd->offset[0] = cpu_to_le32(lower_32_bits(buf_add));
486 bd->offset[1] = cpu_to_le32(upper_32_bits(buf_add));
487 bd->offset[2] = cpu_to_le32(dword2);
488 bd->offset[3] = cpu_to_le32(dword3);
489 /* advance eqp pointer */
490 ep_bdlist_eqp_adv(ep);
491 /* advance the buff pointer */
492 buf_add += BD_MAX_BUFF_SIZE;
493 dev_vdbg(bdc->dev, "buf_add:%08llx req_len:%d bd:%p eqp:%d\n",
494 (unsigned long long)buf_add, req_len, bd,
495 ep->bd_list.eqp_bdi);
496 bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
497 bd->offset[3] = cpu_to_le32(BD_SBF);
498 }
499 /* clear the STOP BD fetch bit from the first bd of this xfr */
500 bd = bdi_to_bd(ep, bd_xfr->start_bdi);
501 bd->offset[3] &= cpu_to_le32(~BD_SBF);
502 /* the new eqp will be next hw dqp */
503 bd_xfr->num_bds = num_bds;
504 bd_xfr->next_hwd_bdi = ep->bd_list.eqp_bdi;
505 /* everything is written correctly before notifying the HW */
506 wmb();
507
508 return 0;
509 }
510
511 /* Queue the xfr */
512 static int bdc_queue_xfr(struct bdc *bdc, struct bdc_req *req)
513 {
514 int num_bds, bd_available;
515 struct bdc_ep *ep;
516 int ret;
517
518 ep = req->ep;
519 dev_dbg(bdc->dev, "%s req:%p\n", __func__, req);
520 dev_dbg(bdc->dev, "eqp_bdi:%d hwd_bdi:%d\n",
521 ep->bd_list.eqp_bdi, ep->bd_list.hwd_bdi);
522
523 num_bds = bd_needed_req(req);
524 bd_available = bd_available_ep(ep);
525
526 /* how many bd's are avaialble on ep */
527 if (num_bds > bd_available)
528 return -ENOMEM;
529
530 ret = setup_bd_list_xfr(bdc, req, num_bds);
531 if (ret)
532 return ret;
533 list_add_tail(&req->queue, &ep->queue);
534 bdc_dbg_bd_list(bdc, ep);
535 bdc_notify_xfr(bdc, ep->ep_num);
536
537 return 0;
538 }
539
540 /* callback to gadget layer when xfr completes */
541 static void bdc_req_complete(struct bdc_ep *ep, struct bdc_req *req,
542 int status)
543 {
544 struct bdc *bdc = ep->bdc;
545
546 if (req == NULL || &req->queue == NULL || &req->usb_req == NULL)
547 return;
548
549 dev_dbg(bdc->dev, "%s ep:%s status:%d\n", __func__, ep->name, status);
550 list_del(&req->queue);
551 req->usb_req.status = status;
552 usb_gadget_unmap_request(&bdc->gadget, &req->usb_req, ep->dir);
553 if (req->usb_req.complete) {
554 spin_unlock(&bdc->lock);
555 usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
556 spin_lock(&bdc->lock);
557 }
558 }
559
560 /* Disable the endpoint */
561 int bdc_ep_disable(struct bdc_ep *ep)
562 {
563 struct bdc_req *req;
564 struct bdc *bdc;
565 int ret;
566
567 ret = 0;
568 bdc = ep->bdc;
569 dev_dbg(bdc->dev, "%s() ep->ep_num=%d\n", __func__, ep->ep_num);
570 /* Stop the endpoint */
571 ret = bdc_stop_ep(bdc, ep->ep_num);
572
573 /*
574 * Intentionally don't check the ret value of stop, it can fail in
575 * disconnect scenarios, continue with dconfig
576 */
577 /* de-queue any pending requests */
578 while (!list_empty(&ep->queue)) {
579 req = list_entry(ep->queue.next, struct bdc_req,
580 queue);
581 bdc_req_complete(ep, req, -ESHUTDOWN);
582 }
583 /* deconfigure the endpoint */
584 ret = bdc_dconfig_ep(bdc, ep);
585 if (ret)
586 dev_warn(bdc->dev,
587 "dconfig fail but continue with memory free");
588
589 ep->flags = 0;
590 /* ep0 memory is not freed, but reused on next connect sr */
591 if (ep->ep_num == 1)
592 return 0;
593
594 /* Free the bdl memory */
595 ep_bd_list_free(ep, ep->bd_list.num_tabs);
596 ep->desc = NULL;
597 ep->comp_desc = NULL;
598 ep->usb_ep.desc = NULL;
599 ep->ep_type = 0;
600
601 return ret;
602 }
603
604 /* Enable the ep */
605 int bdc_ep_enable(struct bdc_ep *ep)
606 {
607 struct bdc *bdc;
608 int ret = 0;
609
610 bdc = ep->bdc;
611 dev_dbg(bdc->dev, "%s NUM_TABLES:%d %d\n",
612 __func__, NUM_TABLES, NUM_TABLES_ISOCH);
613
614 ret = ep_bd_list_alloc(ep);
615 if (ret) {
616 dev_err(bdc->dev, "ep bd list allocation failed:%d\n", ret);
617 return -ENOMEM;
618 }
619 bdc_dbg_bd_list(bdc, ep);
620 /* only for ep0: config ep is called for ep0 from connect event */
621 ep->flags |= BDC_EP_ENABLED;
622 if (ep->ep_num == 1)
623 return ret;
624
625 /* Issue a configure endpoint command */
626 ret = bdc_config_ep(bdc, ep);
627 if (ret)
628 return ret;
629
630 ep->usb_ep.maxpacket = usb_endpoint_maxp(ep->desc);
631 ep->usb_ep.desc = ep->desc;
632 ep->usb_ep.comp_desc = ep->comp_desc;
633 ep->ep_type = usb_endpoint_type(ep->desc);
634 ep->flags |= BDC_EP_ENABLED;
635
636 return 0;
637 }
638
639 /* EP0 related code */
640
641 /* Queue a status stage BD */
642 static int ep0_queue_status_stage(struct bdc *bdc)
643 {
644 struct bdc_req *status_req;
645 struct bdc_ep *ep;
646
647 status_req = &bdc->status_req;
648 ep = bdc->bdc_ep_array[1];
649 status_req->ep = ep;
650 status_req->usb_req.length = 0;
651 status_req->usb_req.status = -EINPROGRESS;
652 status_req->usb_req.actual = 0;
653 status_req->usb_req.complete = NULL;
654 bdc_queue_xfr(bdc, status_req);
655
656 return 0;
657 }
658
659 /* Queue xfr on ep0 */
660 static int ep0_queue(struct bdc_ep *ep, struct bdc_req *req)
661 {
662 struct bdc *bdc;
663 int ret;
664
665 bdc = ep->bdc;
666 dev_dbg(bdc->dev, "%s()\n", __func__);
667 req->usb_req.actual = 0;
668 req->usb_req.status = -EINPROGRESS;
669 req->epnum = ep->ep_num;
670
671 if (bdc->delayed_status) {
672 bdc->delayed_status = false;
673 /* if status stage was delayed? */
674 if (bdc->ep0_state == WAIT_FOR_STATUS_START) {
675 /* Queue a status stage BD */
676 ep0_queue_status_stage(bdc);
677 bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
678 return 0;
679 }
680 } else {
681 /*
682 * if delayed status is false and 0 length transfer is requested
683 * i.e. for status stage of some setup request, then just
684 * return from here the status stage is queued independently
685 */
686 if (req->usb_req.length == 0)
687 return 0;
688
689 }
690 ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
691 if (ret) {
692 dev_err(bdc->dev, "dma mapping failed %s\n", ep->name);
693 return ret;
694 }
695
696 return bdc_queue_xfr(bdc, req);
697 }
698
699 /* Queue data stage */
700 static int ep0_queue_data_stage(struct bdc *bdc)
701 {
702 struct usb_request *ep0_usb_req;
703 struct bdc_ep *ep;
704
705 dev_dbg(bdc->dev, "%s\n", __func__);
706 ep0_usb_req = &bdc->ep0_req.usb_req;
707 ep = bdc->bdc_ep_array[1];
708 bdc->ep0_req.ep = ep;
709 bdc->ep0_req.usb_req.complete = NULL;
710
711 return ep0_queue(ep, &bdc->ep0_req);
712 }
713
714 /* Queue req on ep */
715 static int ep_queue(struct bdc_ep *ep, struct bdc_req *req)
716 {
717 struct bdc *bdc;
718 int ret = 0;
719
720 bdc = ep->bdc;
721 if (!req || !ep || !ep->usb_ep.desc)
722 return -EINVAL;
723
724 req->usb_req.actual = 0;
725 req->usb_req.status = -EINPROGRESS;
726 req->epnum = ep->ep_num;
727
728 ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
729 if (ret) {
730 dev_err(bdc->dev, "dma mapping failed\n");
731 return ret;
732 }
733
734 return bdc_queue_xfr(bdc, req);
735 }
736
737 /* Dequeue a request from ep */
738 static int ep_dequeue(struct bdc_ep *ep, struct bdc_req *req)
739 {
740 int start_bdi, end_bdi, tbi, eqp_bdi, curr_hw_dqpi;
741 bool start_pending, end_pending;
742 bool first_remove = false;
743 struct bdc_req *first_req;
744 struct bdc_bd *bd_start;
745 struct bd_table *table;
746 dma_addr_t next_bd_dma;
747 u64 deq_ptr_64 = 0;
748 struct bdc *bdc;
749 u32 tmp_32;
750 int ret;
751
752 bdc = ep->bdc;
753 start_pending = end_pending = false;
754 eqp_bdi = ep->bd_list.eqp_bdi - 1;
755
756 if (eqp_bdi < 0)
757 eqp_bdi = ep->bd_list.max_bdi;
758
759 start_bdi = req->bd_xfr.start_bdi;
760 end_bdi = find_end_bdi(ep, req->bd_xfr.next_hwd_bdi);
761
762 dev_dbg(bdc->dev, "%s ep:%s start:%d end:%d\n",
763 __func__, ep->name, start_bdi, end_bdi);
764 dev_dbg(bdc->dev, "ep_dequeue ep=%p ep->desc=%p\n",
765 ep, (void *)ep->usb_ep.desc);
766 /* Stop the ep to see where the HW is ? */
767 ret = bdc_stop_ep(bdc, ep->ep_num);
768 /* if there is an issue with stopping ep, then no need to go further */
769 if (ret)
770 return 0;
771
772 /*
773 * After endpoint is stopped, there can be 3 cases, the request
774 * is processed, pending or in the middle of processing
775 */
776
777 /* The current hw dequeue pointer */
778 tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS0(0));
779 deq_ptr_64 = tmp_32;
780 tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS0(1));
781 deq_ptr_64 |= ((u64)tmp_32 << 32);
782
783 /* we have the dma addr of next bd that will be fetched by hardware */
784 curr_hw_dqpi = bd_add_to_bdi(ep, deq_ptr_64);
785 if (curr_hw_dqpi < 0)
786 return curr_hw_dqpi;
787
788 /*
789 * curr_hw_dqpi points to actual dqp of HW and HW owns bd's from
790 * curr_hw_dqbdi..eqp_bdi.
791 */
792
793 /* Check if start_bdi and end_bdi are in range of HW owned BD's */
794 if (curr_hw_dqpi > eqp_bdi) {
795 /* there is a wrap from last to 0 */
796 if (start_bdi >= curr_hw_dqpi || start_bdi <= eqp_bdi) {
797 start_pending = true;
798 end_pending = true;
799 } else if (end_bdi >= curr_hw_dqpi || end_bdi <= eqp_bdi) {
800 end_pending = true;
801 }
802 } else {
803 if (start_bdi >= curr_hw_dqpi) {
804 start_pending = true;
805 end_pending = true;
806 } else if (end_bdi >= curr_hw_dqpi) {
807 end_pending = true;
808 }
809 }
810 dev_dbg(bdc->dev,
811 "start_pending:%d end_pending:%d speed:%d\n",
812 start_pending, end_pending, bdc->gadget.speed);
813
814 /* If both start till end are processes, we cannot deq req */
815 if (!start_pending && !end_pending)
816 return -EINVAL;
817
818 /*
819 * if ep_dequeue is called after disconnect then just return
820 * success from here
821 */
822 if (bdc->gadget.speed == USB_SPEED_UNKNOWN)
823 return 0;
824 tbi = bdi_to_tbi(ep, req->bd_xfr.next_hwd_bdi);
825 table = ep->bd_list.bd_table_array[tbi];
826 next_bd_dma = table->dma +
827 sizeof(struct bdc_bd)*(req->bd_xfr.next_hwd_bdi -
828 tbi * ep->bd_list.num_bds_table);
829
830 first_req = list_first_entry(&ep->queue, struct bdc_req,
831 queue);
832
833 if (req == first_req)
834 first_remove = true;
835
836 /*
837 * Due to HW limitation we need to bypadd chain bd's and issue ep_bla,
838 * incase if start is pending this is the first request in the list
839 * then issue ep_bla instead of marking as chain bd
840 */
841 if (start_pending && !first_remove) {
842 /*
843 * Mark the start bd as Chain bd, and point the chain
844 * bd to next_bd_dma
845 */
846 bd_start = bdi_to_bd(ep, start_bdi);
847 bd_start->offset[0] = cpu_to_le32(lower_32_bits(next_bd_dma));
848 bd_start->offset[1] = cpu_to_le32(upper_32_bits(next_bd_dma));
849 bd_start->offset[2] = 0x0;
850 bd_start->offset[3] = cpu_to_le32(MARK_CHAIN_BD);
851 bdc_dbg_bd_list(bdc, ep);
852 } else if (end_pending) {
853 /*
854 * The transfer is stopped in the middle, move the
855 * HW deq pointer to next_bd_dma
856 */
857 ret = bdc_ep_bla(bdc, ep, next_bd_dma);
858 if (ret) {
859 dev_err(bdc->dev, "error in ep_bla:%d\n", ret);
860 return ret;
861 }
862 }
863
864 return 0;
865 }
866
867 /* Halt/Clear the ep based on value */
868 static int ep_set_halt(struct bdc_ep *ep, u32 value)
869 {
870 struct bdc *bdc;
871 int ret;
872
873 bdc = ep->bdc;
874 dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
875
876 if (value) {
877 dev_dbg(bdc->dev, "Halt\n");
878 if (ep->ep_num == 1)
879 bdc->ep0_state = WAIT_FOR_SETUP;
880
881 ret = bdc_ep_set_stall(bdc, ep->ep_num);
882 if (ret)
883 dev_err(bdc->dev, "failed to %s STALL on %s\n",
884 value ? "set" : "clear", ep->name);
885 else
886 ep->flags |= BDC_EP_STALL;
887 } else {
888 /* Clear */
889 dev_dbg(bdc->dev, "Before Clear\n");
890 ret = bdc_ep_clear_stall(bdc, ep->ep_num);
891 if (ret)
892 dev_err(bdc->dev, "failed to %s STALL on %s\n",
893 value ? "set" : "clear", ep->name);
894 else
895 ep->flags &= ~BDC_EP_STALL;
896 dev_dbg(bdc->dev, "After Clear\n");
897 }
898
899 return ret;
900 }
901
902 /* Free all the ep */
903 void bdc_free_ep(struct bdc *bdc)
904 {
905 struct bdc_ep *ep;
906 u8 epnum;
907
908 dev_dbg(bdc->dev, "%s\n", __func__);
909 for (epnum = 1; epnum < bdc->num_eps; epnum++) {
910 ep = bdc->bdc_ep_array[epnum];
911 if (!ep)
912 continue;
913
914 if (ep->flags & BDC_EP_ENABLED)
915 ep_bd_list_free(ep, ep->bd_list.num_tabs);
916
917 /* ep0 is not in this gadget list */
918 if (epnum != 1)
919 list_del(&ep->usb_ep.ep_list);
920
921 kfree(ep);
922 }
923 }
924
925 /* USB2 spec, section 7.1.20 */
926 static int bdc_set_test_mode(struct bdc *bdc)
927 {
928 u32 usb2_pm;
929
930 usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
931 usb2_pm &= ~BDC_PTC_MASK;
932 dev_dbg(bdc->dev, "%s\n", __func__);
933 switch (bdc->test_mode) {
934 case TEST_J:
935 case TEST_K:
936 case TEST_SE0_NAK:
937 case TEST_PACKET:
938 case TEST_FORCE_EN:
939 usb2_pm |= bdc->test_mode << 28;
940 break;
941 default:
942 return -EINVAL;
943 }
944 dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
945 bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
946
947 return 0;
948 }
949
950 /*
951 * Helper function to handle Transfer status report with status as either
952 * success or short
953 */
954 static void handle_xsr_succ_status(struct bdc *bdc, struct bdc_ep *ep,
955 struct bdc_sr *sreport)
956 {
957 int short_bdi, start_bdi, end_bdi, max_len_bds, chain_bds;
958 struct bd_list *bd_list = &ep->bd_list;
959 int actual_length, length_short;
960 struct bd_transfer *bd_xfr;
961 struct bdc_bd *short_bd;
962 struct bdc_req *req;
963 u64 deq_ptr_64 = 0;
964 int status = 0;
965 int sr_status;
966 u32 tmp_32;
967
968 dev_dbg(bdc->dev, "%s ep:%p\n", __func__, ep);
969 bdc_dbg_srr(bdc, 0);
970 /* do not process thie sr if ignore flag is set */
971 if (ep->ignore_next_sr) {
972 ep->ignore_next_sr = false;
973 return;
974 }
975
976 if (unlikely(list_empty(&ep->queue))) {
977 dev_warn(bdc->dev, "xfr srr with no BD's queued\n");
978 return;
979 }
980 req = list_entry(ep->queue.next, struct bdc_req,
981 queue);
982
983 bd_xfr = &req->bd_xfr;
984 sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
985
986 /*
987 * sr_status is short and this transfer has more than 1 bd then it needs
988 * special handling, this is only applicable for bulk and ctrl
989 */
990 if (sr_status == XSF_SHORT && bd_xfr->num_bds > 1) {
991 /*
992 * This is multi bd xfr, lets see which bd
993 * caused short transfer and how many bytes have been
994 * transferred so far.
995 */
996 tmp_32 = le32_to_cpu(sreport->offset[0]);
997 deq_ptr_64 = tmp_32;
998 tmp_32 = le32_to_cpu(sreport->offset[1]);
999 deq_ptr_64 |= ((u64)tmp_32 << 32);
1000 short_bdi = bd_add_to_bdi(ep, deq_ptr_64);
1001 if (unlikely(short_bdi < 0))
1002 dev_warn(bdc->dev, "bd doesn't exist?\n");
1003
1004 start_bdi = bd_xfr->start_bdi;
1005 /*
1006 * We know the start_bdi and short_bdi, how many xfr
1007 * bds in between
1008 */
1009 if (start_bdi <= short_bdi) {
1010 max_len_bds = short_bdi - start_bdi;
1011 if (max_len_bds <= bd_list->num_bds_table) {
1012 if (!(bdi_to_tbi(ep, start_bdi) ==
1013 bdi_to_tbi(ep, short_bdi)))
1014 max_len_bds--;
1015 } else {
1016 chain_bds = max_len_bds/bd_list->num_bds_table;
1017 max_len_bds -= chain_bds;
1018 }
1019 } else {
1020 /* there is a wrap in the ring within a xfr */
1021 chain_bds = (bd_list->max_bdi - start_bdi)/
1022 bd_list->num_bds_table;
1023 chain_bds += short_bdi/bd_list->num_bds_table;
1024 max_len_bds = bd_list->max_bdi - start_bdi;
1025 max_len_bds += short_bdi;
1026 max_len_bds -= chain_bds;
1027 }
1028 /* max_len_bds is the number of full length bds */
1029 end_bdi = find_end_bdi(ep, bd_xfr->next_hwd_bdi);
1030 if (!(end_bdi == short_bdi))
1031 ep->ignore_next_sr = true;
1032
1033 actual_length = max_len_bds * BD_MAX_BUFF_SIZE;
1034 short_bd = bdi_to_bd(ep, short_bdi);
1035 /* length queued */
1036 length_short = le32_to_cpu(short_bd->offset[2]) & 0x1FFFFF;
1037 /* actual length trensfered */
1038 length_short -= SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
1039 actual_length += length_short;
1040 req->usb_req.actual = actual_length;
1041 } else {
1042 req->usb_req.actual = req->usb_req.length -
1043 SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
1044 dev_dbg(bdc->dev,
1045 "len=%d actual=%d bd_xfr->next_hwd_bdi:%d\n",
1046 req->usb_req.length, req->usb_req.actual,
1047 bd_xfr->next_hwd_bdi);
1048 }
1049
1050 /* Update the dequeue pointer */
1051 ep->bd_list.hwd_bdi = bd_xfr->next_hwd_bdi;
1052 if (req->usb_req.actual < req->usb_req.length) {
1053 dev_dbg(bdc->dev, "short xfr on %d\n", ep->ep_num);
1054 if (req->usb_req.short_not_ok)
1055 status = -EREMOTEIO;
1056 }
1057 bdc_req_complete(ep, bd_xfr->req, status);
1058 }
1059
1060 /* EP0 setup related packet handlers */
1061
1062 /*
1063 * Setup packet received, just store the packet and process on next DS or SS
1064 * started SR
1065 */
1066 void bdc_xsf_ep0_setup_recv(struct bdc *bdc, struct bdc_sr *sreport)
1067 {
1068 struct usb_ctrlrequest *setup_pkt;
1069 u32 len;
1070
1071 dev_dbg(bdc->dev,
1072 "%s ep0_state:%s\n",
1073 __func__, ep0_state_string[bdc->ep0_state]);
1074 /* Store received setup packet */
1075 setup_pkt = &bdc->setup_pkt;
1076 memcpy(setup_pkt, &sreport->offset[0], sizeof(*setup_pkt));
1077 len = le16_to_cpu(setup_pkt->wLength);
1078 if (!len)
1079 bdc->ep0_state = WAIT_FOR_STATUS_START;
1080 else
1081 bdc->ep0_state = WAIT_FOR_DATA_START;
1082
1083
1084 dev_dbg(bdc->dev,
1085 "%s exit ep0_state:%s\n",
1086 __func__, ep0_state_string[bdc->ep0_state]);
1087 }
1088
1089 /* Stall ep0 */
1090 static void ep0_stall(struct bdc *bdc)
1091 {
1092 struct bdc_ep *ep = bdc->bdc_ep_array[1];
1093 struct bdc_req *req;
1094
1095 dev_dbg(bdc->dev, "%s\n", __func__);
1096 bdc->delayed_status = false;
1097 ep_set_halt(ep, 1);
1098
1099 /* de-queue any pendig requests */
1100 while (!list_empty(&ep->queue)) {
1101 req = list_entry(ep->queue.next, struct bdc_req,
1102 queue);
1103 bdc_req_complete(ep, req, -ESHUTDOWN);
1104 }
1105 }
1106
1107 /* SET_ADD handlers */
1108 static int ep0_set_address(struct bdc *bdc, struct usb_ctrlrequest *ctrl)
1109 {
1110 enum usb_device_state state = bdc->gadget.state;
1111 int ret = 0;
1112 u32 addr;
1113
1114 addr = le16_to_cpu(ctrl->wValue);
1115 dev_dbg(bdc->dev,
1116 "%s addr:%d dev state:%d\n",
1117 __func__, addr, state);
1118
1119 if (addr > 127)
1120 return -EINVAL;
1121
1122 switch (state) {
1123 case USB_STATE_DEFAULT:
1124 case USB_STATE_ADDRESS:
1125 /* Issue Address device command */
1126 ret = bdc_address_device(bdc, addr);
1127 if (ret)
1128 return ret;
1129
1130 if (addr)
1131 usb_gadget_set_state(&bdc->gadget, USB_STATE_ADDRESS);
1132 else
1133 usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT);
1134
1135 bdc->dev_addr = addr;
1136 break;
1137 default:
1138 dev_warn(bdc->dev,
1139 "SET Address in wrong device state %d\n",
1140 state);
1141 ret = -EINVAL;
1142 }
1143
1144 return ret;
1145 }
1146
1147 /* Handler for SET/CLEAR FEATURE requests for device */
1148 static int ep0_handle_feature_dev(struct bdc *bdc, u16 wValue,
1149 u16 wIndex, bool set)
1150 {
1151 enum usb_device_state state = bdc->gadget.state;
1152 u32 usppms = 0;
1153
1154 dev_dbg(bdc->dev, "%s set:%d dev state:%d\n",
1155 __func__, set, state);
1156 switch (wValue) {
1157 case USB_DEVICE_REMOTE_WAKEUP:
1158 dev_dbg(bdc->dev, "USB_DEVICE_REMOTE_WAKEUP\n");
1159 if (set)
1160 bdc->devstatus |= REMOTE_WAKE_ENABLE;
1161 else
1162 bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
1163 break;
1164
1165 case USB_DEVICE_TEST_MODE:
1166 dev_dbg(bdc->dev, "USB_DEVICE_TEST_MODE\n");
1167 if ((wIndex & 0xFF) ||
1168 (bdc->gadget.speed != USB_SPEED_HIGH) || !set)
1169 return -EINVAL;
1170
1171 bdc->test_mode = wIndex >> 8;
1172 break;
1173
1174 case USB_DEVICE_U1_ENABLE:
1175 dev_dbg(bdc->dev, "USB_DEVICE_U1_ENABLE\n");
1176
1177 if (bdc->gadget.speed != USB_SPEED_SUPER ||
1178 state != USB_STATE_CONFIGURED)
1179 return -EINVAL;
1180
1181 usppms = bdc_readl(bdc->regs, BDC_USPPMS);
1182 if (set) {
1183 /* clear previous u1t */
1184 usppms &= ~BDC_U1T(BDC_U1T_MASK);
1185 usppms |= BDC_U1T(U1_TIMEOUT);
1186 usppms |= BDC_U1E | BDC_PORT_W1S;
1187 bdc->devstatus |= (1 << USB_DEV_STAT_U1_ENABLED);
1188 } else {
1189 usppms &= ~BDC_U1E;
1190 usppms |= BDC_PORT_W1S;
1191 bdc->devstatus &= ~(1 << USB_DEV_STAT_U1_ENABLED);
1192 }
1193 bdc_writel(bdc->regs, BDC_USPPMS, usppms);
1194 break;
1195
1196 case USB_DEVICE_U2_ENABLE:
1197 dev_dbg(bdc->dev, "USB_DEVICE_U2_ENABLE\n");
1198
1199 if (bdc->gadget.speed != USB_SPEED_SUPER ||
1200 state != USB_STATE_CONFIGURED)
1201 return -EINVAL;
1202
1203 usppms = bdc_readl(bdc->regs, BDC_USPPMS);
1204 if (set) {
1205 usppms |= BDC_U2E;
1206 usppms |= BDC_U2A;
1207 bdc->devstatus |= (1 << USB_DEV_STAT_U2_ENABLED);
1208 } else {
1209 usppms &= ~BDC_U2E;
1210 usppms &= ~BDC_U2A;
1211 bdc->devstatus &= ~(1 << USB_DEV_STAT_U2_ENABLED);
1212 }
1213 bdc_writel(bdc->regs, BDC_USPPMS, usppms);
1214 break;
1215
1216 case USB_DEVICE_LTM_ENABLE:
1217 dev_dbg(bdc->dev, "USB_DEVICE_LTM_ENABLE?\n");
1218 if (bdc->gadget.speed != USB_SPEED_SUPER ||
1219 state != USB_STATE_CONFIGURED)
1220 return -EINVAL;
1221 break;
1222 default:
1223 dev_err(bdc->dev, "Unknown wValue:%d\n", wValue);
1224 return -EOPNOTSUPP;
1225 } /* USB_RECIP_DEVICE end */
1226
1227 return 0;
1228 }
1229
1230 /* SET/CLEAR FEATURE handler */
1231 static int ep0_handle_feature(struct bdc *bdc,
1232 struct usb_ctrlrequest *setup_pkt, bool set)
1233 {
1234 enum usb_device_state state = bdc->gadget.state;
1235 struct bdc_ep *ep;
1236 u16 wValue;
1237 u16 wIndex;
1238 int epnum;
1239
1240 wValue = le16_to_cpu(setup_pkt->wValue);
1241 wIndex = le16_to_cpu(setup_pkt->wIndex);
1242
1243 dev_dbg(bdc->dev,
1244 "%s wValue=%d wIndex=%d devstate=%08x speed=%d set=%d",
1245 __func__, wValue, wIndex, state,
1246 bdc->gadget.speed, set);
1247
1248 switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
1249 case USB_RECIP_DEVICE:
1250 return ep0_handle_feature_dev(bdc, wValue, wIndex, set);
1251 case USB_RECIP_INTERFACE:
1252 dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
1253 /* USB3 spec, sec 9.4.9 */
1254 if (wValue != USB_INTRF_FUNC_SUSPEND)
1255 return -EINVAL;
1256 /* USB3 spec, Table 9-8 */
1257 if (set) {
1258 if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) {
1259 dev_dbg(bdc->dev, "SET REMOTE_WAKEUP\n");
1260 bdc->devstatus |= REMOTE_WAKE_ENABLE;
1261 } else {
1262 dev_dbg(bdc->dev, "CLEAR REMOTE_WAKEUP\n");
1263 bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
1264 }
1265 }
1266 break;
1267
1268 case USB_RECIP_ENDPOINT:
1269 dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
1270 if (wValue != USB_ENDPOINT_HALT)
1271 return -EINVAL;
1272
1273 epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
1274 if (epnum) {
1275 if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1276 epnum = epnum * 2 + 1;
1277 else
1278 epnum *= 2;
1279 } else {
1280 epnum = 1; /*EP0*/
1281 }
1282 /*
1283 * If CLEAR_FEATURE on ep0 then don't do anything as the stall
1284 * condition on ep0 has already been cleared when SETUP packet
1285 * was received.
1286 */
1287 if (epnum == 1 && !set) {
1288 dev_dbg(bdc->dev, "ep0 stall already cleared\n");
1289 return 0;
1290 }
1291 dev_dbg(bdc->dev, "epnum=%d\n", epnum);
1292 ep = bdc->bdc_ep_array[epnum];
1293 if (!ep)
1294 return -EINVAL;
1295
1296 return ep_set_halt(ep, set);
1297 default:
1298 dev_err(bdc->dev, "Unknown recipient\n");
1299 return -EINVAL;
1300 }
1301
1302 return 0;
1303 }
1304
1305 /* GET_STATUS request handler */
1306 static int ep0_handle_status(struct bdc *bdc,
1307 struct usb_ctrlrequest *setup_pkt)
1308 {
1309 enum usb_device_state state = bdc->gadget.state;
1310 struct bdc_ep *ep;
1311 u16 usb_status = 0;
1312 u32 epnum;
1313 u16 wIndex;
1314
1315 /* USB2.0 spec sec 9.4.5 */
1316 if (state == USB_STATE_DEFAULT)
1317 return -EINVAL;
1318 wIndex = le16_to_cpu(setup_pkt->wIndex);
1319 dev_dbg(bdc->dev, "%s\n", __func__);
1320 usb_status = bdc->devstatus;
1321 switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
1322 case USB_RECIP_DEVICE:
1323 dev_dbg(bdc->dev,
1324 "USB_RECIP_DEVICE devstatus:%08x\n",
1325 bdc->devstatus);
1326 /* USB3 spec, sec 9.4.5 */
1327 if (bdc->gadget.speed == USB_SPEED_SUPER)
1328 usb_status &= ~REMOTE_WAKE_ENABLE;
1329 break;
1330
1331 case USB_RECIP_INTERFACE:
1332 dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
1333 if (bdc->gadget.speed == USB_SPEED_SUPER) {
1334 /*
1335 * This should come from func for Func remote wkup
1336 * usb_status |=1;
1337 */
1338 if (bdc->devstatus & REMOTE_WAKE_ENABLE)
1339 usb_status |= REMOTE_WAKE_ENABLE;
1340 } else {
1341 usb_status = 0;
1342 }
1343
1344 break;
1345
1346 case USB_RECIP_ENDPOINT:
1347 dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
1348 epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
1349 if (epnum) {
1350 if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1351 epnum = epnum*2 + 1;
1352 else
1353 epnum *= 2;
1354 } else {
1355 epnum = 1; /* EP0 */
1356 }
1357
1358 ep = bdc->bdc_ep_array[epnum];
1359 if (!ep) {
1360 dev_err(bdc->dev, "ISSUE, GET_STATUS for invalid EP ?");
1361 return -EINVAL;
1362 }
1363 if (ep->flags & BDC_EP_STALL)
1364 usb_status |= 1 << USB_ENDPOINT_HALT;
1365
1366 break;
1367 default:
1368 dev_err(bdc->dev, "Unknown recipient for get_status\n");
1369 return -EINVAL;
1370 }
1371 /* prepare a data stage for GET_STATUS */
1372 dev_dbg(bdc->dev, "usb_status=%08x\n", usb_status);
1373 *(__le16 *)bdc->ep0_response_buff = cpu_to_le16(usb_status);
1374 bdc->ep0_req.usb_req.length = 2;
1375 bdc->ep0_req.usb_req.buf = &bdc->ep0_response_buff;
1376 ep0_queue_data_stage(bdc);
1377
1378 return 0;
1379 }
1380
1381 static void ep0_set_sel_cmpl(struct usb_ep *_ep, struct usb_request *_req)
1382 {
1383 /* ep0_set_sel_cmpl */
1384 }
1385
1386 /* Queue data stage to handle 6 byte SET_SEL request */
1387 static int ep0_set_sel(struct bdc *bdc,
1388 struct usb_ctrlrequest *setup_pkt)
1389 {
1390 struct bdc_ep *ep;
1391 u16 wLength;
1392 u16 wValue;
1393
1394 dev_dbg(bdc->dev, "%s\n", __func__);
1395 wValue = le16_to_cpu(setup_pkt->wValue);
1396 wLength = le16_to_cpu(setup_pkt->wLength);
1397 if (unlikely(wLength != 6)) {
1398 dev_err(bdc->dev, "%s Wrong wLength:%d\n", __func__, wLength);
1399 return -EINVAL;
1400 }
1401 ep = bdc->bdc_ep_array[1];
1402 bdc->ep0_req.ep = ep;
1403 bdc->ep0_req.usb_req.length = 6;
1404 bdc->ep0_req.usb_req.buf = bdc->ep0_response_buff;
1405 bdc->ep0_req.usb_req.complete = ep0_set_sel_cmpl;
1406 ep0_queue_data_stage(bdc);
1407
1408 return 0;
1409 }
1410
1411 /*
1412 * Queue a 0 byte bd only if wLength is more than the length and and length is
1413 * a multiple of MaxPacket then queue 0 byte BD
1414 */
1415 static int ep0_queue_zlp(struct bdc *bdc)
1416 {
1417 int ret;
1418
1419 dev_dbg(bdc->dev, "%s\n", __func__);
1420 bdc->ep0_req.ep = bdc->bdc_ep_array[1];
1421 bdc->ep0_req.usb_req.length = 0;
1422 bdc->ep0_req.usb_req.complete = NULL;
1423 bdc->ep0_state = WAIT_FOR_DATA_START;
1424 ret = bdc_queue_xfr(bdc, &bdc->ep0_req);
1425 if (ret) {
1426 dev_err(bdc->dev, "err queueing zlp :%d\n", ret);
1427 return ret;
1428 }
1429 bdc->ep0_state = WAIT_FOR_DATA_XMIT;
1430
1431 return 0;
1432 }
1433
1434 /* Control request handler */
1435 static int handle_control_request(struct bdc *bdc)
1436 {
1437 enum usb_device_state state = bdc->gadget.state;
1438 struct usb_ctrlrequest *setup_pkt;
1439 int delegate_setup = 0;
1440 int ret = 0;
1441 int config = 0;
1442
1443 setup_pkt = &bdc->setup_pkt;
1444 dev_dbg(bdc->dev, "%s\n", __func__);
1445 if ((setup_pkt->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1446 switch (setup_pkt->bRequest) {
1447 case USB_REQ_SET_ADDRESS:
1448 dev_dbg(bdc->dev, "USB_REQ_SET_ADDRESS\n");
1449 ret = ep0_set_address(bdc, setup_pkt);
1450 bdc->devstatus &= DEVSTATUS_CLEAR;
1451 break;
1452
1453 case USB_REQ_SET_CONFIGURATION:
1454 dev_dbg(bdc->dev, "USB_REQ_SET_CONFIGURATION\n");
1455 if (state == USB_STATE_ADDRESS) {
1456 usb_gadget_set_state(&bdc->gadget,
1457 USB_STATE_CONFIGURED);
1458 } else if (state == USB_STATE_CONFIGURED) {
1459 /*
1460 * USB2 spec sec 9.4.7, if wValue is 0 then dev
1461 * is moved to addressed state
1462 */
1463 config = le16_to_cpu(setup_pkt->wValue);
1464 if (!config)
1465 usb_gadget_set_state(
1466 &bdc->gadget,
1467 USB_STATE_ADDRESS);
1468 }
1469 delegate_setup = 1;
1470 break;
1471
1472 case USB_REQ_SET_FEATURE:
1473 dev_dbg(bdc->dev, "USB_REQ_SET_FEATURE\n");
1474 ret = ep0_handle_feature(bdc, setup_pkt, 1);
1475 break;
1476
1477 case USB_REQ_CLEAR_FEATURE:
1478 dev_dbg(bdc->dev, "USB_REQ_CLEAR_FEATURE\n");
1479 ret = ep0_handle_feature(bdc, setup_pkt, 0);
1480 break;
1481
1482 case USB_REQ_GET_STATUS:
1483 dev_dbg(bdc->dev, "USB_REQ_GET_STATUS\n");
1484 ret = ep0_handle_status(bdc, setup_pkt);
1485 break;
1486
1487 case USB_REQ_SET_SEL:
1488 dev_dbg(bdc->dev, "USB_REQ_SET_SEL\n");
1489 ret = ep0_set_sel(bdc, setup_pkt);
1490 break;
1491
1492 case USB_REQ_SET_ISOCH_DELAY:
1493 dev_warn(bdc->dev,
1494 "USB_REQ_SET_ISOCH_DELAY not handled\n");
1495 ret = 0;
1496 break;
1497 default:
1498 delegate_setup = 1;
1499 }
1500 } else {
1501 delegate_setup = 1;
1502 }
1503
1504 if (delegate_setup) {
1505 spin_unlock(&bdc->lock);
1506 ret = bdc->gadget_driver->setup(&bdc->gadget, setup_pkt);
1507 spin_lock(&bdc->lock);
1508 }
1509
1510 return ret;
1511 }
1512
1513 /* EP0: Data stage started */
1514 void bdc_xsf_ep0_data_start(struct bdc *bdc, struct bdc_sr *sreport)
1515 {
1516 struct bdc_ep *ep;
1517 int ret = 0;
1518
1519 dev_dbg(bdc->dev, "%s\n", __func__);
1520 ep = bdc->bdc_ep_array[1];
1521 /* If ep0 was stalled, the clear it first */
1522 if (ep->flags & BDC_EP_STALL) {
1523 ret = ep_set_halt(ep, 0);
1524 if (ret)
1525 goto err;
1526 }
1527 if (bdc->ep0_state != WAIT_FOR_DATA_START)
1528 dev_warn(bdc->dev,
1529 "Data stage not expected ep0_state:%s\n",
1530 ep0_state_string[bdc->ep0_state]);
1531
1532 ret = handle_control_request(bdc);
1533 if (ret == USB_GADGET_DELAYED_STATUS) {
1534 /*
1535 * The ep0 state will remain WAIT_FOR_DATA_START till
1536 * we received ep_queue on ep0
1537 */
1538 bdc->delayed_status = true;
1539 return;
1540 }
1541 if (!ret) {
1542 bdc->ep0_state = WAIT_FOR_DATA_XMIT;
1543 dev_dbg(bdc->dev,
1544 "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
1545 return;
1546 }
1547 err:
1548 ep0_stall(bdc);
1549 }
1550
1551 /* EP0: status stage started */
1552 void bdc_xsf_ep0_status_start(struct bdc *bdc, struct bdc_sr *sreport)
1553 {
1554 struct usb_ctrlrequest *setup_pkt;
1555 struct bdc_ep *ep;
1556 int ret = 0;
1557
1558 dev_dbg(bdc->dev,
1559 "%s ep0_state:%s",
1560 __func__, ep0_state_string[bdc->ep0_state]);
1561 ep = bdc->bdc_ep_array[1];
1562
1563 /* check if ZLP was queued? */
1564 if (bdc->zlp_needed)
1565 bdc->zlp_needed = false;
1566
1567 if (ep->flags & BDC_EP_STALL) {
1568 ret = ep_set_halt(ep, 0);
1569 if (ret)
1570 goto err;
1571 }
1572
1573 if ((bdc->ep0_state != WAIT_FOR_STATUS_START) &&
1574 (bdc->ep0_state != WAIT_FOR_DATA_XMIT))
1575 dev_err(bdc->dev,
1576 "Status stage recv but ep0_state:%s\n",
1577 ep0_state_string[bdc->ep0_state]);
1578
1579 /* check if data stage is in progress ? */
1580 if (bdc->ep0_state == WAIT_FOR_DATA_XMIT) {
1581 bdc->ep0_state = STATUS_PENDING;
1582 /* Status stage will be queued upon Data stage transmit event */
1583 dev_dbg(bdc->dev,
1584 "status started but data not transmitted yet\n");
1585 return;
1586 }
1587 setup_pkt = &bdc->setup_pkt;
1588
1589 /*
1590 * 2 stage setup then only process the setup, for 3 stage setup the date
1591 * stage is already handled
1592 */
1593 if (!le16_to_cpu(setup_pkt->wLength)) {
1594 ret = handle_control_request(bdc);
1595 if (ret == USB_GADGET_DELAYED_STATUS) {
1596 bdc->delayed_status = true;
1597 /* ep0_state will remain WAIT_FOR_STATUS_START */
1598 return;
1599 }
1600 }
1601 if (!ret) {
1602 /* Queue a status stage BD */
1603 ep0_queue_status_stage(bdc);
1604 bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
1605 dev_dbg(bdc->dev,
1606 "ep0_state:%s", ep0_state_string[bdc->ep0_state]);
1607 return;
1608 }
1609 err:
1610 ep0_stall(bdc);
1611 }
1612
1613 /* Helper function to update ep0 upon SR with xsf_succ or xsf_short */
1614 static void ep0_xsf_complete(struct bdc *bdc, struct bdc_sr *sreport)
1615 {
1616 dev_dbg(bdc->dev, "%s\n", __func__);
1617 switch (bdc->ep0_state) {
1618 case WAIT_FOR_DATA_XMIT:
1619 bdc->ep0_state = WAIT_FOR_STATUS_START;
1620 break;
1621 case WAIT_FOR_STATUS_XMIT:
1622 bdc->ep0_state = WAIT_FOR_SETUP;
1623 if (bdc->test_mode) {
1624 int ret;
1625
1626 dev_dbg(bdc->dev, "test_mode:%d\n", bdc->test_mode);
1627 ret = bdc_set_test_mode(bdc);
1628 if (ret < 0) {
1629 dev_err(bdc->dev, "Err in setting Test mode\n");
1630 return;
1631 }
1632 bdc->test_mode = 0;
1633 }
1634 break;
1635 case STATUS_PENDING:
1636 bdc_xsf_ep0_status_start(bdc, sreport);
1637 break;
1638
1639 default:
1640 dev_err(bdc->dev,
1641 "Unknown ep0_state:%s\n",
1642 ep0_state_string[bdc->ep0_state]);
1643
1644 }
1645 }
1646
1647 /* xfr completion status report handler */
1648 void bdc_sr_xsf(struct bdc *bdc, struct bdc_sr *sreport)
1649 {
1650 struct bdc_ep *ep;
1651 u32 sr_status;
1652 u8 ep_num;
1653
1654 ep_num = (le32_to_cpu(sreport->offset[3])>>4) & 0x1f;
1655 ep = bdc->bdc_ep_array[ep_num];
1656 if (!ep || !(ep->flags & BDC_EP_ENABLED)) {
1657 dev_err(bdc->dev, "xsf for ep not enabled\n");
1658 return;
1659 }
1660 /*
1661 * check if this transfer is after link went from U3->U0 due
1662 * to remote wakeup
1663 */
1664 if (bdc->devstatus & FUNC_WAKE_ISSUED) {
1665 bdc->devstatus &= ~(FUNC_WAKE_ISSUED);
1666 dev_dbg(bdc->dev, "%s clearing FUNC_WAKE_ISSUED flag\n",
1667 __func__);
1668 }
1669 sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
1670 dev_dbg_ratelimited(bdc->dev, "%s sr_status=%d ep:%s\n",
1671 __func__, sr_status, ep->name);
1672
1673 switch (sr_status) {
1674 case XSF_SUCC:
1675 case XSF_SHORT:
1676 handle_xsr_succ_status(bdc, ep, sreport);
1677 if (ep_num == 1)
1678 ep0_xsf_complete(bdc, sreport);
1679 break;
1680
1681 case XSF_SETUP_RECV:
1682 case XSF_DATA_START:
1683 case XSF_STATUS_START:
1684 if (ep_num != 1) {
1685 dev_err(bdc->dev,
1686 "ep0 related packets on non ep0 endpoint");
1687 return;
1688 }
1689 bdc->sr_xsf_ep0[sr_status - XSF_SETUP_RECV](bdc, sreport);
1690 break;
1691
1692 case XSF_BABB:
1693 if (ep_num == 1) {
1694 dev_dbg(bdc->dev, "Babble on ep0 zlp_need:%d\n",
1695 bdc->zlp_needed);
1696 /*
1697 * If the last completed transfer had wLength >Data Len,
1698 * and Len is multiple of MaxPacket,then queue ZLP
1699 */
1700 if (bdc->zlp_needed) {
1701 /* queue 0 length bd */
1702 ep0_queue_zlp(bdc);
1703 return;
1704 }
1705 }
1706 dev_warn(bdc->dev, "Babble on ep not handled\n");
1707 break;
1708 default:
1709 dev_warn(bdc->dev, "sr status not handled:%x\n", sr_status);
1710 break;
1711 }
1712 }
1713
1714 static int bdc_gadget_ep_queue(struct usb_ep *_ep,
1715 struct usb_request *_req, gfp_t gfp_flags)
1716 {
1717 struct bdc_req *req;
1718 unsigned long flags;
1719 struct bdc_ep *ep;
1720 struct bdc *bdc;
1721 int ret;
1722
1723 if (!_ep || !_ep->desc)
1724 return -ESHUTDOWN;
1725
1726 if (!_req || !_req->complete || !_req->buf)
1727 return -EINVAL;
1728
1729 ep = to_bdc_ep(_ep);
1730 req = to_bdc_req(_req);
1731 bdc = ep->bdc;
1732 dev_dbg(bdc->dev, "%s ep:%p req:%p\n", __func__, ep, req);
1733 dev_dbg(bdc->dev, "queuing request %p to %s length %d zero:%d\n",
1734 _req, ep->name, _req->length, _req->zero);
1735
1736 if (!ep->usb_ep.desc) {
1737 dev_warn(bdc->dev,
1738 "trying to queue req %p to disabled %s\n",
1739 _req, ep->name);
1740 return -ESHUTDOWN;
1741 }
1742
1743 if (_req->length > MAX_XFR_LEN) {
1744 dev_warn(bdc->dev,
1745 "req length > supported MAX:%d requested:%d\n",
1746 MAX_XFR_LEN, _req->length);
1747 return -EOPNOTSUPP;
1748 }
1749 spin_lock_irqsave(&bdc->lock, flags);
1750 if (ep == bdc->bdc_ep_array[1])
1751 ret = ep0_queue(ep, req);
1752 else
1753 ret = ep_queue(ep, req);
1754
1755 spin_unlock_irqrestore(&bdc->lock, flags);
1756
1757 return ret;
1758 }
1759
1760 static int bdc_gadget_ep_dequeue(struct usb_ep *_ep,
1761 struct usb_request *_req)
1762 {
1763 struct bdc_req *req;
1764 unsigned long flags;
1765 struct bdc_ep *ep;
1766 struct bdc *bdc;
1767 int ret;
1768
1769 if (!_ep || !_req)
1770 return -EINVAL;
1771
1772 ep = to_bdc_ep(_ep);
1773 req = to_bdc_req(_req);
1774 bdc = ep->bdc;
1775 dev_dbg(bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
1776 bdc_dbg_bd_list(bdc, ep);
1777 spin_lock_irqsave(&bdc->lock, flags);
1778 /* make sure it's still queued on this endpoint */
1779 list_for_each_entry(req, &ep->queue, queue) {
1780 if (&req->usb_req == _req)
1781 break;
1782 }
1783 if (&req->usb_req != _req) {
1784 spin_unlock_irqrestore(&bdc->lock, flags);
1785 dev_err(bdc->dev, "usb_req !=req n");
1786 return -EINVAL;
1787 }
1788 ret = ep_dequeue(ep, req);
1789 if (ret) {
1790 ret = -EOPNOTSUPP;
1791 goto err;
1792 }
1793 bdc_req_complete(ep, req, -ECONNRESET);
1794
1795 err:
1796 bdc_dbg_bd_list(bdc, ep);
1797 spin_unlock_irqrestore(&bdc->lock, flags);
1798
1799 return ret;
1800 }
1801
1802 static int bdc_gadget_ep_set_halt(struct usb_ep *_ep, int value)
1803 {
1804 unsigned long flags;
1805 struct bdc_ep *ep;
1806 struct bdc *bdc;
1807 int ret;
1808
1809 ep = to_bdc_ep(_ep);
1810 bdc = ep->bdc;
1811 dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
1812 spin_lock_irqsave(&bdc->lock, flags);
1813 if (usb_endpoint_xfer_isoc(ep->usb_ep.desc))
1814 ret = -EINVAL;
1815 else if (!list_empty(&ep->queue))
1816 ret = -EAGAIN;
1817 else
1818 ret = ep_set_halt(ep, value);
1819
1820 spin_unlock_irqrestore(&bdc->lock, flags);
1821
1822 return ret;
1823 }
1824
1825 static struct usb_request *bdc_gadget_alloc_request(struct usb_ep *_ep,
1826 gfp_t gfp_flags)
1827 {
1828 struct bdc_req *req;
1829 struct bdc_ep *ep;
1830
1831 req = kzalloc(sizeof(*req), gfp_flags);
1832 if (!req)
1833 return NULL;
1834
1835 ep = to_bdc_ep(_ep);
1836 req->ep = ep;
1837 req->epnum = ep->ep_num;
1838 req->usb_req.dma = DMA_ADDR_INVALID;
1839 dev_dbg(ep->bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
1840
1841 return &req->usb_req;
1842 }
1843
1844 static void bdc_gadget_free_request(struct usb_ep *_ep,
1845 struct usb_request *_req)
1846 {
1847 struct bdc_req *req;
1848
1849 req = to_bdc_req(_req);
1850 kfree(req);
1851 }
1852
1853 /* endpoint operations */
1854
1855 /* configure endpoint and also allocate resources */
1856 static int bdc_gadget_ep_enable(struct usb_ep *_ep,
1857 const struct usb_endpoint_descriptor *desc)
1858 {
1859 unsigned long flags;
1860 struct bdc_ep *ep;
1861 struct bdc *bdc;
1862 int ret;
1863
1864 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
1865 pr_debug("bdc_gadget_ep_enable invalid parameters\n");
1866 return -EINVAL;
1867 }
1868
1869 if (!desc->wMaxPacketSize) {
1870 pr_debug("bdc_gadget_ep_enable missing wMaxPacketSize\n");
1871 return -EINVAL;
1872 }
1873
1874 ep = to_bdc_ep(_ep);
1875 bdc = ep->bdc;
1876
1877 /* Sanity check, upper layer will not send enable for ep0 */
1878 if (ep == bdc->bdc_ep_array[1])
1879 return -EINVAL;
1880
1881 if (!bdc->gadget_driver
1882 || bdc->gadget.speed == USB_SPEED_UNKNOWN) {
1883 return -ESHUTDOWN;
1884 }
1885
1886 dev_dbg(bdc->dev, "%s Enabling %s\n", __func__, ep->name);
1887 spin_lock_irqsave(&bdc->lock, flags);
1888 ep->desc = desc;
1889 ep->comp_desc = _ep->comp_desc;
1890 ret = bdc_ep_enable(ep);
1891 spin_unlock_irqrestore(&bdc->lock, flags);
1892
1893 return ret;
1894 }
1895
1896 static int bdc_gadget_ep_disable(struct usb_ep *_ep)
1897 {
1898 unsigned long flags;
1899 struct bdc_ep *ep;
1900 struct bdc *bdc;
1901 int ret;
1902
1903 if (!_ep) {
1904 pr_debug("bdc: invalid parameters\n");
1905 return -EINVAL;
1906 }
1907 ep = to_bdc_ep(_ep);
1908 bdc = ep->bdc;
1909
1910 /* Upper layer will not call this for ep0, but do a sanity check */
1911 if (ep == bdc->bdc_ep_array[1]) {
1912 dev_warn(bdc->dev, "%s called for ep0\n", __func__);
1913 return -EINVAL;
1914 }
1915 dev_dbg(bdc->dev,
1916 "%s() ep:%s ep->flags:%08x\n",
1917 __func__, ep->name, ep->flags);
1918
1919 if (!(ep->flags & BDC_EP_ENABLED)) {
1920 dev_warn(bdc->dev, "%s is already disabled\n", ep->name);
1921 return 0;
1922 }
1923 spin_lock_irqsave(&bdc->lock, flags);
1924 ret = bdc_ep_disable(ep);
1925 spin_unlock_irqrestore(&bdc->lock, flags);
1926
1927 return ret;
1928 }
1929
1930 static const struct usb_ep_ops bdc_gadget_ep_ops = {
1931 .enable = bdc_gadget_ep_enable,
1932 .disable = bdc_gadget_ep_disable,
1933 .alloc_request = bdc_gadget_alloc_request,
1934 .free_request = bdc_gadget_free_request,
1935 .queue = bdc_gadget_ep_queue,
1936 .dequeue = bdc_gadget_ep_dequeue,
1937 .set_halt = bdc_gadget_ep_set_halt
1938 };
1939
1940 /* dir = 1 is IN */
1941 static int init_ep(struct bdc *bdc, u32 epnum, u32 dir)
1942 {
1943 struct bdc_ep *ep;
1944
1945 dev_dbg(bdc->dev, "%s epnum=%d dir=%d\n", __func__, epnum, dir);
1946 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1947 if (!ep)
1948 return -ENOMEM;
1949
1950 ep->bdc = bdc;
1951 ep->dir = dir;
1952
1953 /* ep->ep_num is the index inside bdc_ep */
1954 if (epnum == 1) {
1955 ep->ep_num = 1;
1956 bdc->bdc_ep_array[ep->ep_num] = ep;
1957 snprintf(ep->name, sizeof(ep->name), "ep%d", epnum - 1);
1958 usb_ep_set_maxpacket_limit(&ep->usb_ep, EP0_MAX_PKT_SIZE);
1959 ep->comp_desc = NULL;
1960 bdc->gadget.ep0 = &ep->usb_ep;
1961 } else {
1962 if (dir)
1963 ep->ep_num = epnum * 2 - 1;
1964 else
1965 ep->ep_num = epnum * 2 - 2;
1966
1967 bdc->bdc_ep_array[ep->ep_num] = ep;
1968 snprintf(ep->name, sizeof(ep->name), "ep%d%s", epnum - 1,
1969 dir & 1 ? "in" : "out");
1970
1971 usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
1972 ep->usb_ep.max_streams = 0;
1973 list_add_tail(&ep->usb_ep.ep_list, &bdc->gadget.ep_list);
1974 }
1975 ep->usb_ep.ops = &bdc_gadget_ep_ops;
1976 ep->usb_ep.name = ep->name;
1977 ep->flags = 0;
1978 ep->ignore_next_sr = false;
1979 dev_dbg(bdc->dev, "ep=%p ep->usb_ep.name=%s epnum=%d ep->epnum=%d\n",
1980 ep, ep->usb_ep.name, epnum, ep->ep_num);
1981
1982 INIT_LIST_HEAD(&ep->queue);
1983
1984 return 0;
1985 }
1986
1987 /* Init all ep */
1988 int bdc_init_ep(struct bdc *bdc)
1989 {
1990 u8 epnum;
1991 int ret;
1992
1993 dev_dbg(bdc->dev, "%s()\n", __func__);
1994 INIT_LIST_HEAD(&bdc->gadget.ep_list);
1995 /* init ep0 */
1996 ret = init_ep(bdc, 1, 0);
1997 if (ret) {
1998 dev_err(bdc->dev, "init ep ep0 fail %d\n", ret);
1999 return ret;
2000 }
2001
2002 for (epnum = 2; epnum <= bdc->num_eps / 2; epnum++) {
2003 /* OUT */
2004 ret = init_ep(bdc, epnum, 0);
2005 if (ret) {
2006 dev_err(bdc->dev,
2007 "init ep failed for:%d error: %d\n",
2008 epnum, ret);
2009 return ret;
2010 }
2011
2012 /* IN */
2013 ret = init_ep(bdc, epnum, 1);
2014 if (ret) {
2015 dev_err(bdc->dev,
2016 "init ep failed for:%d error: %d\n",
2017 epnum, ret);
2018 return ret;
2019 }
2020 }
2021
2022 return 0;
2023 }
This page took 0.107488 seconds and 5 git commands to generate.