GNU Linux-libre 4.4.288-gnu1
[releases.git] / drivers / usb / renesas_usbhs / pipe.c
1 /*
2  * Renesas USB driver
3  *
4  * Copyright (C) 2011 Renesas Solutions Corp.
5  * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15  *
16  */
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include "common.h"
20 #include "pipe.h"
21
22 /*
23  *              macros
24  */
25 #define usbhsp_addr_offset(p)   ((usbhs_pipe_number(p) - 1) * 2)
26
27 #define usbhsp_flags_set(p, f)  ((p)->flags |=  USBHS_PIPE_FLAGS_##f)
28 #define usbhsp_flags_clr(p, f)  ((p)->flags &= ~USBHS_PIPE_FLAGS_##f)
29 #define usbhsp_flags_has(p, f)  ((p)->flags &   USBHS_PIPE_FLAGS_##f)
30 #define usbhsp_flags_init(p)    do {(p)->flags = 0; } while (0)
31
32 /*
33  * for debug
34  */
35 static char *usbhsp_pipe_name[] = {
36         [USB_ENDPOINT_XFER_CONTROL]     = "DCP",
37         [USB_ENDPOINT_XFER_BULK]        = "BULK",
38         [USB_ENDPOINT_XFER_INT]         = "INT",
39         [USB_ENDPOINT_XFER_ISOC]        = "ISO",
40 };
41
42 char *usbhs_pipe_name(struct usbhs_pipe *pipe)
43 {
44         return usbhsp_pipe_name[usbhs_pipe_type(pipe)];
45 }
46
47 /*
48  *              DCPCTR/PIPEnCTR functions
49  */
50 static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
51 {
52         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
53         int offset = usbhsp_addr_offset(pipe);
54
55         if (usbhs_pipe_is_dcp(pipe))
56                 usbhs_bset(priv, DCPCTR, mask, val);
57         else
58                 usbhs_bset(priv, PIPEnCTR + offset, mask, val);
59 }
60
61 static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
62 {
63         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
64         int offset = usbhsp_addr_offset(pipe);
65
66         if (usbhs_pipe_is_dcp(pipe))
67                 return usbhs_read(priv, DCPCTR);
68         else
69                 return usbhs_read(priv, PIPEnCTR + offset);
70 }
71
72 /*
73  *              DCP/PIPE functions
74  */
75 static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
76                                   u16 dcp_reg, u16 pipe_reg,
77                                   u16 mask, u16 val)
78 {
79         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
80
81         if (usbhs_pipe_is_dcp(pipe))
82                 usbhs_bset(priv, dcp_reg, mask, val);
83         else
84                 usbhs_bset(priv, pipe_reg, mask, val);
85 }
86
87 static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
88                                  u16 dcp_reg, u16 pipe_reg)
89 {
90         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
91
92         if (usbhs_pipe_is_dcp(pipe))
93                 return usbhs_read(priv, dcp_reg);
94         else
95                 return usbhs_read(priv, pipe_reg);
96 }
97
98 /*
99  *              DCPCFG/PIPECFG functions
100  */
101 static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
102 {
103         __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
104 }
105
106 static u16 usbhsp_pipe_cfg_get(struct usbhs_pipe *pipe)
107 {
108         return __usbhsp_pipe_xxx_get(pipe, DCPCFG, PIPECFG);
109 }
110
111 /*
112  *              PIPEnTRN/PIPEnTRE functions
113  */
114 static void usbhsp_pipe_trn_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
115 {
116         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
117         struct device *dev = usbhs_priv_to_dev(priv);
118         int num = usbhs_pipe_number(pipe);
119         u16 reg;
120
121         /*
122          * It is impossible to calculate address,
123          * since PIPEnTRN addresses were mapped randomly.
124          */
125 #define CASE_PIPExTRN(a)                \
126         case 0x ## a:                   \
127                 reg = PIPE ## a ## TRN; \
128                 break;
129
130         switch (num) {
131         CASE_PIPExTRN(1);
132         CASE_PIPExTRN(2);
133         CASE_PIPExTRN(3);
134         CASE_PIPExTRN(4);
135         CASE_PIPExTRN(5);
136         CASE_PIPExTRN(B);
137         CASE_PIPExTRN(C);
138         CASE_PIPExTRN(D);
139         CASE_PIPExTRN(E);
140         CASE_PIPExTRN(F);
141         CASE_PIPExTRN(9);
142         CASE_PIPExTRN(A);
143         default:
144                 dev_err(dev, "unknown pipe (%d)\n", num);
145                 return;
146         }
147         __usbhsp_pipe_xxx_set(pipe, 0, reg, mask, val);
148 }
149
150 static void usbhsp_pipe_tre_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
151 {
152         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
153         struct device *dev = usbhs_priv_to_dev(priv);
154         int num = usbhs_pipe_number(pipe);
155         u16 reg;
156
157         /*
158          * It is impossible to calculate address,
159          * since PIPEnTRE addresses were mapped randomly.
160          */
161 #define CASE_PIPExTRE(a)                        \
162         case 0x ## a:                           \
163                 reg = PIPE ## a ## TRE;         \
164                 break;
165
166         switch (num) {
167         CASE_PIPExTRE(1);
168         CASE_PIPExTRE(2);
169         CASE_PIPExTRE(3);
170         CASE_PIPExTRE(4);
171         CASE_PIPExTRE(5);
172         CASE_PIPExTRE(B);
173         CASE_PIPExTRE(C);
174         CASE_PIPExTRE(D);
175         CASE_PIPExTRE(E);
176         CASE_PIPExTRE(F);
177         CASE_PIPExTRE(9);
178         CASE_PIPExTRE(A);
179         default:
180                 dev_err(dev, "unknown pipe (%d)\n", num);
181                 return;
182         }
183
184         __usbhsp_pipe_xxx_set(pipe, 0, reg, mask, val);
185 }
186
187 /*
188  *              PIPEBUF
189  */
190 static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
191 {
192         if (usbhs_pipe_is_dcp(pipe))
193                 return;
194
195         __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
196 }
197
198 /*
199  *              DCPMAXP/PIPEMAXP
200  */
201 static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
202 {
203         __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
204 }
205
206 /*
207  *              pipe control functions
208  */
209 static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
210 {
211         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
212
213         /*
214          * On pipe, this is necessary before
215          * accesses to below registers.
216          *
217          * PIPESEL      : usbhsp_pipe_select
218          * PIPECFG      : usbhsp_pipe_cfg_xxx
219          * PIPEBUF      : usbhsp_pipe_buf_xxx
220          * PIPEMAXP     : usbhsp_pipe_maxp_xxx
221          * PIPEPERI
222          */
223
224         /*
225          * if pipe is dcp, no pipe is selected.
226          * it is no problem, because dcp have its register
227          */
228         usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
229 }
230
231 static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
232 {
233         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
234         int timeout = 1024;
235         u16 val;
236
237         /*
238          * make sure....
239          *
240          * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
241          * specified by the CURPIPE bits.
242          * When changing the setting of this bit after changing
243          * the PID bits for the selected pipe from BUF to NAK,
244          * check that CSSTS = 0 and PBUSY = 0.
245          */
246
247         /*
248          * CURPIPE bit = 0
249          *
250          * see also
251          *  "Operation"
252          *  - "Pipe Control"
253          *   - "Pipe Control Registers Switching Procedure"
254          */
255         usbhs_write(priv, CFIFOSEL, 0);
256         usbhs_pipe_disable(pipe);
257
258         do {
259                 val  = usbhsp_pipectrl_get(pipe);
260                 val &= CSSTS | PID_MASK;
261                 if (!val)
262                         return 0;
263
264                 udelay(10);
265
266         } while (timeout--);
267
268         return -EBUSY;
269 }
270
271 int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe)
272 {
273         u16 val;
274
275         val = usbhsp_pipectrl_get(pipe);
276         if (val & BSTS)
277                 return 0;
278
279         return -EBUSY;
280 }
281
282 bool usbhs_pipe_contains_transmittable_data(struct usbhs_pipe *pipe)
283 {
284         u16 val;
285
286         /* Do not support for DCP pipe */
287         if (usbhs_pipe_is_dcp(pipe))
288                 return false;
289
290         val = usbhsp_pipectrl_get(pipe);
291         if (val & INBUFM)
292                 return true;
293
294         return false;
295 }
296
297 /*
298  *              PID ctrl
299  */
300 static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
301 {
302         u16 pid = usbhsp_pipectrl_get(pipe);
303
304         pid &= PID_MASK;
305
306         /*
307          * see
308          * "Pipe n Control Register" - "PID"
309          */
310         switch (pid) {
311         case PID_STALL11:
312                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
313                 /* fall-through */
314         case PID_STALL10:
315                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
316         }
317 }
318
319 void usbhs_pipe_disable(struct usbhs_pipe *pipe)
320 {
321         int timeout = 1024;
322         u16 val;
323
324         /* see "Pipe n Control Register" - "PID" */
325         __usbhsp_pid_try_nak_if_stall(pipe);
326
327         usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
328
329         do {
330                 val  = usbhsp_pipectrl_get(pipe);
331                 val &= PBUSY;
332                 if (!val)
333                         break;
334
335                 udelay(10);
336         } while (timeout--);
337 }
338
339 void usbhs_pipe_enable(struct usbhs_pipe *pipe)
340 {
341         /* see "Pipe n Control Register" - "PID" */
342         __usbhsp_pid_try_nak_if_stall(pipe);
343
344         usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
345 }
346
347 void usbhs_pipe_stall(struct usbhs_pipe *pipe)
348 {
349         u16 pid = usbhsp_pipectrl_get(pipe);
350
351         pid &= PID_MASK;
352
353         /*
354          * see
355          * "Pipe n Control Register" - "PID"
356          */
357         switch (pid) {
358         case PID_NAK:
359                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
360                 break;
361         case PID_BUF:
362                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
363                 break;
364         }
365 }
366
367 int usbhs_pipe_is_stall(struct usbhs_pipe *pipe)
368 {
369         u16 pid = usbhsp_pipectrl_get(pipe) & PID_MASK;
370
371         return (int)(pid == PID_STALL10 || pid == PID_STALL11);
372 }
373
374 void usbhs_pipe_set_trans_count_if_bulk(struct usbhs_pipe *pipe, int len)
375 {
376         if (!usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
377                 return;
378
379         /*
380          * clear and disable transfer counter for IN/OUT pipe
381          */
382         usbhsp_pipe_tre_set(pipe, TRCLR | TRENB, TRCLR);
383
384         /*
385          * Only IN direction bulk pipe can use transfer count.
386          * Without using this function,
387          * received data will break if it was large data size.
388          * see PIPEnTRN/PIPEnTRE for detail
389          */
390         if (usbhs_pipe_is_dir_in(pipe)) {
391                 int maxp = usbhs_pipe_get_maxpacket(pipe);
392
393                 usbhsp_pipe_trn_set(pipe, 0xffff, DIV_ROUND_UP(len, maxp));
394                 usbhsp_pipe_tre_set(pipe, TRENB, TRENB); /* enable */
395         }
396 }
397
398
399 /*
400  *              pipe setup
401  */
402 static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
403 {
404         /*
405          * only ISO / BULK pipe can use double buffer
406          */
407         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
408             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
409                 return 1;
410
411         return 0;
412 }
413
414 static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
415                                 int is_host,
416                                 int dir_in)
417 {
418         u16 type = 0;
419         u16 bfre = 0;
420         u16 dblb = 0;
421         u16 cntmd = 0;
422         u16 dir = 0;
423         u16 epnum = 0;
424         u16 shtnak = 0;
425         u16 type_array[] = {
426                 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
427                 [USB_ENDPOINT_XFER_INT]  = TYPE_INT,
428                 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
429         };
430         int is_double = usbhsp_possible_double_buffer(pipe);
431
432         if (usbhs_pipe_is_dcp(pipe))
433                 return -EINVAL;
434
435         /*
436          * PIPECFG
437          *
438          * see
439          *  - "Register Descriptions" - "PIPECFG" register
440          *  - "Features"  - "Pipe configuration"
441          *  - "Operation" - "Pipe Control"
442          */
443
444         /* TYPE */
445         type = type_array[usbhs_pipe_type(pipe)];
446
447         /* BFRE */
448         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
449             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
450                 bfre = 0; /* FIXME */
451
452         /* DBLB */
453         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
454             usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
455                 dblb = (is_double) ? DBLB : 0;
456
457         /* CNTMD */
458         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK))
459                 cntmd = 0; /* FIXME */
460
461         /* DIR */
462         if (dir_in)
463                 usbhsp_flags_set(pipe, IS_DIR_HOST);
464
465         if (!!is_host ^ !!dir_in)
466                 dir |= DIR_OUT;
467
468         if (!dir)
469                 usbhsp_flags_set(pipe, IS_DIR_IN);
470
471         /* SHTNAK */
472         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
473             !dir)
474                 shtnak = SHTNAK;
475
476         /* EPNUM */
477         epnum = 0; /* see usbhs_pipe_config_update() */
478
479         return  type    |
480                 bfre    |
481                 dblb    |
482                 cntmd   |
483                 dir     |
484                 shtnak  |
485                 epnum;
486 }
487
488 static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe)
489 {
490         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
491         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
492         struct device *dev = usbhs_priv_to_dev(priv);
493         int pipe_num = usbhs_pipe_number(pipe);
494         int is_double = usbhsp_possible_double_buffer(pipe);
495         u16 buff_size;
496         u16 bufnmb;
497         u16 bufnmb_cnt;
498
499         /*
500          * PIPEBUF
501          *
502          * see
503          *  - "Register Descriptions" - "PIPEBUF" register
504          *  - "Features"  - "Pipe configuration"
505          *  - "Operation" - "FIFO Buffer Memory"
506          *  - "Operation" - "Pipe Control"
507          *
508          * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
509          *
510          * BUFNMB:      PIPE
511          * 0:           pipe0 (DCP 256byte)
512          * 1:           -
513          * 2:           -
514          * 3:           -
515          * 4:           pipe6 (INT 64byte)
516          * 5:           pipe7 (INT 64byte)
517          * 6:           pipe8 (INT 64byte)
518          * 7:           pipe9 (INT 64byte)
519          * 8 - xx:      free (for BULK, ISOC)
520          */
521
522         /*
523          * FIXME
524          *
525          * it doesn't have good buffer allocator
526          *
527          * DCP : 256 byte
528          * BULK: 512 byte
529          * INT :  64 byte
530          * ISOC: 512 byte
531          */
532         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
533                 buff_size = 256;
534         else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
535                 buff_size = 64;
536         else
537                 buff_size = 512;
538
539         /* change buff_size to register value */
540         bufnmb_cnt = (buff_size / 64) - 1;
541
542         /* BUFNMB has been reserved for INT pipe
543          * see above */
544         if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
545                 bufnmb = pipe_num - 2;
546         } else {
547                 bufnmb = info->bufnmb_last;
548                 info->bufnmb_last += bufnmb_cnt + 1;
549
550                 /*
551                  * double buffer
552                  */
553                 if (is_double)
554                         info->bufnmb_last += bufnmb_cnt + 1;
555         }
556
557         dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
558                 pipe_num, buff_size, bufnmb);
559
560         return  (0x1f & bufnmb_cnt)     << 10 |
561                 (0xff & bufnmb)         <<  0;
562 }
563
564 void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel,
565                               u16 epnum, u16 maxp)
566 {
567         if (devsel > 0xA) {
568                 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
569                 struct device *dev = usbhs_priv_to_dev(priv);
570
571                 dev_err(dev, "devsel error %d\n", devsel);
572
573                 devsel = 0;
574         }
575
576         usbhsp_pipe_barrier(pipe);
577
578         pipe->maxp = maxp;
579
580         usbhsp_pipe_select(pipe);
581         usbhsp_pipe_maxp_set(pipe, 0xFFFF,
582                              (devsel << 12) |
583                              maxp);
584
585         if (!usbhs_pipe_is_dcp(pipe))
586                 usbhsp_pipe_cfg_set(pipe,  0x000F, epnum);
587 }
588
589 /*
590  *              pipe control
591  */
592 int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
593 {
594         /*
595          * see
596          *      usbhs_pipe_config_update()
597          *      usbhs_dcp_malloc()
598          */
599         return pipe->maxp;
600 }
601
602 int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
603 {
604         return usbhsp_flags_has(pipe, IS_DIR_IN);
605 }
606
607 int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
608 {
609         return usbhsp_flags_has(pipe, IS_DIR_HOST);
610 }
611
612 int usbhs_pipe_is_running(struct usbhs_pipe *pipe)
613 {
614         return usbhsp_flags_has(pipe, IS_RUNNING);
615 }
616
617 void usbhs_pipe_running(struct usbhs_pipe *pipe, int running)
618 {
619         if (running)
620                 usbhsp_flags_set(pipe, IS_RUNNING);
621         else
622                 usbhsp_flags_clr(pipe, IS_RUNNING);
623 }
624
625 void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int sequence)
626 {
627         u16 mask = (SQCLR | SQSET);
628         u16 val;
629
630         /*
631          * sequence
632          *  0  : data0
633          *  1  : data1
634          *  -1 : no change
635          */
636         switch (sequence) {
637         case 0:
638                 val = SQCLR;
639                 break;
640         case 1:
641                 val = SQSET;
642                 break;
643         default:
644                 return;
645         }
646
647         usbhsp_pipectrl_set(pipe, mask, val);
648 }
649
650 static int usbhs_pipe_get_data_sequence(struct usbhs_pipe *pipe)
651 {
652         return !!(usbhsp_pipectrl_get(pipe) & SQMON);
653 }
654
655 void usbhs_pipe_clear(struct usbhs_pipe *pipe)
656 {
657         if (usbhs_pipe_is_dcp(pipe)) {
658                 usbhs_fifo_clear_dcp(pipe);
659         } else {
660                 usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
661                 usbhsp_pipectrl_set(pipe, ACLRM, 0);
662         }
663 }
664
665 void usbhs_pipe_config_change_bfre(struct usbhs_pipe *pipe, int enable)
666 {
667         int sequence;
668
669         if (usbhs_pipe_is_dcp(pipe))
670                 return;
671
672         usbhsp_pipe_select(pipe);
673         /* check if the driver needs to change the BFRE value */
674         if (!(enable ^ !!(usbhsp_pipe_cfg_get(pipe) & BFRE)))
675                 return;
676
677         sequence = usbhs_pipe_get_data_sequence(pipe);
678         usbhsp_pipe_cfg_set(pipe, BFRE, enable ? BFRE : 0);
679         usbhs_pipe_clear(pipe);
680         usbhs_pipe_data_sequence(pipe, sequence);
681 }
682
683 static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
684 {
685         struct usbhs_pipe *pos, *pipe;
686         int i;
687
688         /*
689          * find target pipe
690          */
691         pipe = NULL;
692         usbhs_for_each_pipe_with_dcp(pos, priv, i) {
693                 if (!usbhs_pipe_type_is(pos, type))
694                         continue;
695                 if (usbhsp_flags_has(pos, IS_USED))
696                         continue;
697
698                 pipe = pos;
699                 break;
700         }
701
702         if (!pipe)
703                 return NULL;
704
705         /*
706          * initialize pipe flags
707          */
708         usbhsp_flags_init(pipe);
709         usbhsp_flags_set(pipe, IS_USED);
710
711         return pipe;
712 }
713
714 static void usbhsp_put_pipe(struct usbhs_pipe *pipe)
715 {
716         usbhsp_flags_init(pipe);
717 }
718
719 void usbhs_pipe_init(struct usbhs_priv *priv,
720                      int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map))
721 {
722         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
723         struct usbhs_pipe *pipe;
724         int i;
725
726         /*
727          * FIXME
728          *
729          * driver needs good allocator.
730          *
731          * find first free buffer area (BULK, ISOC)
732          * (DCP, INT area is fixed)
733          *
734          * buffer number 0 - 3 have been reserved for DCP
735          * see
736          *      usbhsp_to_bufnmb
737          */
738         info->bufnmb_last = 4;
739         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
740                 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT))
741                         info->bufnmb_last++;
742
743                 usbhsp_flags_init(pipe);
744                 pipe->fifo = NULL;
745                 pipe->mod_private = NULL;
746                 INIT_LIST_HEAD(&pipe->list);
747
748                 /* pipe force init */
749                 usbhs_pipe_clear(pipe);
750         }
751
752         info->dma_map_ctrl = dma_map_ctrl;
753 }
754
755 struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
756                                      int endpoint_type,
757                                      int dir_in)
758 {
759         struct device *dev = usbhs_priv_to_dev(priv);
760         struct usbhs_pipe *pipe;
761         int is_host = usbhs_mod_is_host(priv);
762         int ret;
763         u16 pipecfg, pipebuf;
764
765         pipe = usbhsp_get_pipe(priv, endpoint_type);
766         if (!pipe) {
767                 dev_err(dev, "can't get pipe (%s)\n",
768                         usbhsp_pipe_name[endpoint_type]);
769                 return NULL;
770         }
771
772         INIT_LIST_HEAD(&pipe->list);
773
774         usbhs_pipe_disable(pipe);
775
776         /* make sure pipe is not busy */
777         ret = usbhsp_pipe_barrier(pipe);
778         if (ret < 0) {
779                 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
780                 return NULL;
781         }
782
783         pipecfg  = usbhsp_setup_pipecfg(pipe, is_host, dir_in);
784         pipebuf  = usbhsp_setup_pipebuff(pipe);
785
786         usbhsp_pipe_select(pipe);
787         usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
788         usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
789         usbhs_pipe_clear(pipe);
790
791         usbhs_pipe_sequence_data0(pipe);
792
793         dev_dbg(dev, "enable pipe %d : %s (%s)\n",
794                 usbhs_pipe_number(pipe),
795                 usbhs_pipe_name(pipe),
796                 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
797
798         /*
799          * epnum / maxp are still not set to this pipe.
800          * call usbhs_pipe_config_update() after this function !!
801          */
802
803         return pipe;
804 }
805
806 void usbhs_pipe_free(struct usbhs_pipe *pipe)
807 {
808         usbhsp_pipe_select(pipe);
809         usbhsp_pipe_cfg_set(pipe, 0xFFFF, 0);
810         usbhsp_put_pipe(pipe);
811 }
812
813 void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo)
814 {
815         if (pipe->fifo)
816                 pipe->fifo->pipe = NULL;
817
818         pipe->fifo = fifo;
819
820         if (fifo)
821                 fifo->pipe = pipe;
822 }
823
824
825 /*
826  *              dcp control
827  */
828 struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
829 {
830         struct usbhs_pipe *pipe;
831
832         pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
833         if (!pipe)
834                 return NULL;
835
836         INIT_LIST_HEAD(&pipe->list);
837
838         /*
839          * call usbhs_pipe_config_update() after this function !!
840          */
841
842         return pipe;
843 }
844
845 void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
846 {
847         struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe);
848
849         WARN_ON(!usbhs_pipe_is_dcp(pipe));
850
851         usbhs_pipe_enable(pipe);
852
853         if (!usbhs_mod_is_host(priv)) /* funconly */
854                 usbhsp_pipectrl_set(pipe, CCPL, CCPL);
855 }
856
857 void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out)
858 {
859         usbhsp_pipe_cfg_set(pipe, DIR_OUT,
860                             dir_out ? DIR_OUT : 0);
861 }
862
863 /*
864  *              pipe module function
865  */
866 int usbhs_pipe_probe(struct usbhs_priv *priv)
867 {
868         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
869         struct usbhs_pipe *pipe;
870         struct device *dev = usbhs_priv_to_dev(priv);
871         u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
872         int pipe_size = usbhs_get_dparam(priv, pipe_size);
873         int i;
874
875         /* This driver expects 1st pipe is DCP */
876         if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
877                 dev_err(dev, "1st PIPE is not DCP\n");
878                 return -EINVAL;
879         }
880
881         info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
882         if (!info->pipe) {
883                 dev_err(dev, "Could not allocate pipe\n");
884                 return -ENOMEM;
885         }
886
887         info->size = pipe_size;
888
889         /*
890          * init pipe
891          */
892         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
893                 pipe->priv = priv;
894
895                 usbhs_pipe_type(pipe) =
896                         pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
897
898                 dev_dbg(dev, "pipe %x\t: %s\n",
899                         i, usbhsp_pipe_name[pipe_type[i]]);
900         }
901
902         return 0;
903 }
904
905 void usbhs_pipe_remove(struct usbhs_priv *priv)
906 {
907         struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv);
908
909         kfree(info->pipe);
910 }