80fc4add6af220df3030e680de9d4737277769b9
[pandora-kernel.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/io.h>
19 #include <linux/slab.h>
20 #include "./common.h"
21 #include "./pipe.h"
22
23 /*
24  *              macros
25  */
26 #define usbhsp_priv_to_pipeinfo(pr)     (&(pr)->pipe_info)
27 #define usbhsp_pipe_to_priv(p)          ((p)->priv)
28
29 #define usbhsp_addr_offset(p)   ((usbhs_pipe_number(p) - 1) * 2)
30
31 #define usbhsp_is_dcp(p)        ((p)->priv->pipe_info.pipe == (p))
32
33 #define usbhsp_flags_set(p, f)  ((p)->flags |=  USBHS_PIPE_FLAGS_##f)
34 #define usbhsp_flags_clr(p, f)  ((p)->flags &= ~USBHS_PIPE_FLAGS_##f)
35 #define usbhsp_flags_has(p, f)  ((p)->flags &   USBHS_PIPE_FLAGS_##f)
36 #define usbhsp_flags_init(p)    do {(p)->flags = 0; } while (0)
37
38 #define usbhsp_type(p)          ((p)->pipe_type)
39 #define usbhsp_type_is(p, t)    ((p)->pipe_type == t)
40
41 /*
42  * for debug
43  */
44 static char *usbhsp_pipe_name[] = {
45         [USB_ENDPOINT_XFER_CONTROL]     = "DCP",
46         [USB_ENDPOINT_XFER_BULK]        = "BULK",
47         [USB_ENDPOINT_XFER_INT]         = "INT",
48         [USB_ENDPOINT_XFER_ISOC]        = "ISO",
49 };
50
51 /*
52  *              usb request functions
53  */
54 void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
55 {
56         u16 val;
57
58         val = usbhs_read(priv, USBREQ);
59         req->bRequest           = (val >> 8) & 0xFF;
60         req->bRequestType       = (val >> 0) & 0xFF;
61
62         req->wValue     = usbhs_read(priv, USBVAL);
63         req->wIndex     = usbhs_read(priv, USBINDX);
64         req->wLength    = usbhs_read(priv, USBLENG);
65 }
66
67 void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req)
68 {
69         usbhs_write(priv, USBREQ,  (req->bRequest << 8) | req->bRequestType);
70         usbhs_write(priv, USBVAL,  req->wValue);
71         usbhs_write(priv, USBINDX, req->wIndex);
72         usbhs_write(priv, USBLENG, req->wLength);
73 }
74
75 /*
76  *              DCPCTR/PIPEnCTR functions
77  */
78 static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
79 {
80         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
81         int offset = usbhsp_addr_offset(pipe);
82
83         if (usbhsp_is_dcp(pipe))
84                 usbhs_bset(priv, DCPCTR, mask, val);
85         else
86                 usbhs_bset(priv, PIPEnCTR + offset, mask, val);
87 }
88
89 static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe)
90 {
91         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
92         int offset = usbhsp_addr_offset(pipe);
93
94         if (usbhsp_is_dcp(pipe))
95                 return usbhs_read(priv, DCPCTR);
96         else
97                 return usbhs_read(priv, PIPEnCTR + offset);
98 }
99
100 /*
101  *              DCP/PIPE functions
102  */
103 static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe,
104                                   u16 dcp_reg, u16 pipe_reg,
105                                   u16 mask, u16 val)
106 {
107         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
108
109         if (usbhsp_is_dcp(pipe))
110                 usbhs_bset(priv, dcp_reg, mask, val);
111         else
112                 usbhs_bset(priv, pipe_reg, mask, val);
113 }
114
115 static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe,
116                                  u16 dcp_reg, u16 pipe_reg)
117 {
118         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
119
120         if (usbhsp_is_dcp(pipe))
121                 return usbhs_read(priv, dcp_reg);
122         else
123                 return usbhs_read(priv, pipe_reg);
124 }
125
126 /*
127  *              DCPCFG/PIPECFG functions
128  */
129 static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
130 {
131         __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val);
132 }
133
134 /*
135  *              PIPEBUF
136  */
137 static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
138 {
139         if (usbhsp_is_dcp(pipe))
140                 return;
141
142         __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val);
143 }
144
145 /*
146  *              DCPMAXP/PIPEMAXP
147  */
148 static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val)
149 {
150         __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val);
151 }
152
153 static u16 usbhsp_pipe_maxp_get(struct usbhs_pipe *pipe)
154 {
155         return __usbhsp_pipe_xxx_get(pipe, DCPMAXP, PIPEMAXP);
156 }
157
158 /*
159  *              pipe control functions
160  */
161 static void usbhsp_pipe_select(struct usbhs_pipe *pipe)
162 {
163         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
164
165         /*
166          * On pipe, this is necessary before
167          * accesses to below registers.
168          *
169          * PIPESEL      : usbhsp_pipe_select
170          * PIPECFG      : usbhsp_pipe_cfg_xxx
171          * PIPEBUF      : usbhsp_pipe_buf_xxx
172          * PIPEMAXP     : usbhsp_pipe_maxp_xxx
173          * PIPEPERI
174          */
175
176         /*
177          * if pipe is dcp, no pipe is selected.
178          * it is no problem, because dcp have its register
179          */
180         usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe));
181 }
182
183 static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe)
184 {
185         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
186         int timeout = 1024;
187         u16 val;
188
189         /*
190          * make sure....
191          *
192          * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is
193          * specified by the CURPIPE bits.
194          * When changing the setting of this bit after changing
195          * the PID bits for the selected pipe from BUF to NAK,
196          * check that CSSTS = 0 and PBUSY = 0.
197          */
198
199         /*
200          * CURPIPE bit = 0
201          *
202          * see also
203          *  "Operation"
204          *  - "Pipe Control"
205          *   - "Pipe Control Registers Switching Procedure"
206          */
207         usbhs_write(priv, CFIFOSEL, 0);
208         usbhs_fifo_disable(pipe);
209
210         do {
211                 val  = usbhsp_pipectrl_get(pipe);
212                 val &= CSSTS | PID_MASK;
213                 if (!val)
214                         return 0;
215
216                 udelay(10);
217
218         } while (timeout--);
219
220         return -EBUSY;
221 }
222
223 static int usbhsp_pipe_is_accessible(struct usbhs_pipe *pipe)
224 {
225         u16 val;
226
227         val = usbhsp_pipectrl_get(pipe);
228         if (val & BSTS)
229                 return 0;
230
231         return -EBUSY;
232 }
233
234 /*
235  *              PID ctrl
236  */
237 static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe)
238 {
239         u16 pid = usbhsp_pipectrl_get(pipe);
240
241         pid &= PID_MASK;
242
243         /*
244          * see
245          * "Pipe n Control Register" - "PID"
246          */
247         switch (pid) {
248         case PID_STALL11:
249                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
250                 /* fall-through */
251         case PID_STALL10:
252                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
253         }
254 }
255
256 void usbhs_fifo_disable(struct usbhs_pipe *pipe)
257 {
258         int timeout = 1024;
259         u16 val;
260
261         /* see "Pipe n Control Register" - "PID" */
262         __usbhsp_pid_try_nak_if_stall(pipe);
263
264         usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK);
265
266         do {
267                 val  = usbhsp_pipectrl_get(pipe);
268                 val &= PBUSY;
269                 if (!val)
270                         break;
271
272                 udelay(10);
273         } while (timeout--);
274 }
275
276 void usbhs_fifo_enable(struct usbhs_pipe *pipe)
277 {
278         /* see "Pipe n Control Register" - "PID" */
279         __usbhsp_pid_try_nak_if_stall(pipe);
280
281         usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF);
282 }
283
284 void usbhs_fifo_stall(struct usbhs_pipe *pipe)
285 {
286         u16 pid = usbhsp_pipectrl_get(pipe);
287
288         pid &= PID_MASK;
289
290         /*
291          * see
292          * "Pipe n Control Register" - "PID"
293          */
294         switch (pid) {
295         case PID_NAK:
296                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10);
297                 break;
298         case PID_BUF:
299                 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11);
300                 break;
301         }
302 }
303
304 /*
305  *              CFIFO ctrl
306  */
307 void usbhs_fifo_send_terminator(struct usbhs_pipe *pipe)
308 {
309         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
310
311         usbhs_bset(priv, CFIFOCTR, BVAL, BVAL);
312 }
313
314 static void usbhsp_fifo_clear(struct usbhs_pipe *pipe)
315 {
316         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
317
318         usbhs_write(priv, CFIFOCTR, BCLR);
319 }
320
321 static int usbhsp_fifo_barrier(struct usbhs_priv *priv)
322 {
323         int timeout = 1024;
324
325         do {
326                 /* The FIFO port is accessible */
327                 if (usbhs_read(priv, CFIFOCTR) & FRDY)
328                         return 0;
329
330                 udelay(10);
331         } while (timeout--);
332
333         return -EBUSY;
334 }
335
336 static int usbhsp_fifo_rcv_len(struct usbhs_priv *priv)
337 {
338         return usbhs_read(priv, CFIFOCTR) & DTLN_MASK;
339 }
340
341 static int usbhsp_fifo_select(struct usbhs_pipe *pipe, int write)
342 {
343         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
344         struct device *dev = usbhs_priv_to_dev(priv);
345         int timeout = 1024;
346         u16 mask = ((1 << 5) | 0xF);            /* mask of ISEL | CURPIPE */
347         u16 base = usbhs_pipe_number(pipe);     /* CURPIPE */
348
349         if (usbhsp_is_dcp(pipe))
350                 base |= (1 == write) << 5;      /* ISEL */
351
352         /* "base" will be used below  */
353         usbhs_write(priv, CFIFOSEL, base | MBW_32);
354
355         /* check ISEL and CURPIPE value */
356         while (timeout--) {
357                 if (base == (mask & usbhs_read(priv, CFIFOSEL)))
358                         return 0;
359                 udelay(10);
360         }
361
362         dev_err(dev, "fifo select error\n");
363
364         return -EIO;
365 }
366
367 int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe)
368 {
369         return usbhsp_fifo_select(pipe, 1);
370 }
371
372 int usbhs_fifo_write(struct usbhs_pipe *pipe, u8 *buf, int len)
373 {
374         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
375         void __iomem *addr = priv->base + CFIFO;
376         int maxp = usbhs_pipe_get_maxpacket(pipe);
377         int total_len;
378         int i, ret;
379
380         ret = usbhsp_pipe_is_accessible(pipe);
381         if (ret < 0)
382                 return ret;
383
384         ret = usbhsp_fifo_select(pipe, 1);
385         if (ret < 0)
386                 return ret;
387
388         ret = usbhsp_fifo_barrier(priv);
389         if (ret < 0)
390                 return ret;
391
392         len = min(len, maxp);
393         total_len = len;
394
395         /*
396          * FIXME
397          *
398          * 32-bit access only
399          */
400         if (len >= 4 &&
401             !((unsigned long)buf & 0x03)) {
402                 iowrite32_rep(addr, buf, len / 4);
403                 len %= 4;
404                 buf += total_len - len;
405         }
406
407         /* the rest operation */
408         for (i = 0; i < len; i++)
409                 iowrite8(buf[i], addr + (0x03 - (i & 0x03)));
410
411         if (total_len < maxp)
412                 usbhs_fifo_send_terminator(pipe);
413
414         return total_len;
415 }
416
417 int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe)
418 {
419         int ret;
420
421         /*
422          * select pipe and enable it to prepare packet receive
423          */
424         ret = usbhsp_fifo_select(pipe, 0);
425         if (ret < 0)
426                 return ret;
427
428         usbhs_fifo_enable(pipe);
429
430         return ret;
431 }
432
433 int usbhs_fifo_read(struct usbhs_pipe *pipe, u8 *buf, int len)
434 {
435         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
436         void __iomem *addr = priv->base + CFIFO;
437         int rcv_len;
438         int i, ret;
439         int total_len;
440         u32 data = 0;
441
442         ret = usbhsp_fifo_select(pipe, 0);
443         if (ret < 0)
444                 return ret;
445
446         ret = usbhsp_fifo_barrier(priv);
447         if (ret < 0)
448                 return ret;
449
450         rcv_len = usbhsp_fifo_rcv_len(priv);
451
452         /*
453          * Buffer clear if Zero-Length packet
454          *
455          * see
456          * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function"
457          */
458         if (0 == rcv_len) {
459                 usbhsp_fifo_clear(pipe);
460                 return 0;
461         }
462
463         len = min(rcv_len, len);
464         total_len = len;
465
466         /*
467          * FIXME
468          *
469          * 32-bit access only
470          */
471         if (len >= 4 &&
472             !((unsigned long)buf & 0x03)) {
473                 ioread32_rep(addr, buf, len / 4);
474                 len %= 4;
475                 buf += rcv_len - len;
476         }
477
478         /* the rest operation */
479         for (i = 0; i < len; i++) {
480                 if (!(i & 0x03))
481                         data = ioread32(addr);
482
483                 buf[i] = (data >> ((i & 0x03) * 8)) & 0xff;
484         }
485
486         return total_len;
487 }
488
489 /*
490  *              pipe setup
491  */
492 static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe)
493 {
494         /*
495          * only ISO / BULK pipe can use double buffer
496          */
497         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) ||
498             usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC))
499                 return 1;
500
501         return 0;
502 }
503
504 static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe,
505                                 const struct usb_endpoint_descriptor *desc,
506                                 int is_host)
507 {
508         u16 type = 0;
509         u16 bfre = 0;
510         u16 dblb = 0;
511         u16 cntmd = 0;
512         u16 dir = 0;
513         u16 epnum = 0;
514         u16 shtnak = 0;
515         u16 type_array[] = {
516                 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK,
517                 [USB_ENDPOINT_XFER_INT]  = TYPE_INT,
518                 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO,
519         };
520         int is_double = usbhsp_possible_double_buffer(pipe);
521
522         if (usbhsp_is_dcp(pipe))
523                 return -EINVAL;
524
525         /*
526          * PIPECFG
527          *
528          * see
529          *  - "Register Descriptions" - "PIPECFG" register
530          *  - "Features"  - "Pipe configuration"
531          *  - "Operation" - "Pipe Control"
532          */
533
534         /* TYPE */
535         type = type_array[usbhsp_type(pipe)];
536
537         /* BFRE */
538         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
539             usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
540                 bfre = 0; /* FIXME */
541
542         /* DBLB */
543         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) ||
544             usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
545                 dblb = (is_double) ? DBLB : 0;
546
547         /* CNTMD */
548         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK))
549                 cntmd = 0; /* FIXME */
550
551         /* DIR */
552         if (usb_endpoint_dir_in(desc))
553                 usbhsp_flags_set(pipe, IS_DIR_HOST);
554
555         if ((is_host  && usb_endpoint_dir_out(desc)) ||
556             (!is_host && usb_endpoint_dir_in(desc)))
557                 dir |= DIR_OUT;
558
559         if (!dir)
560                 usbhsp_flags_set(pipe, IS_DIR_IN);
561
562         /* SHTNAK */
563         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) &&
564             !dir)
565                 shtnak = SHTNAK;
566
567         /* EPNUM */
568         epnum = 0xF & usb_endpoint_num(desc);
569
570         return  type    |
571                 bfre    |
572                 dblb    |
573                 cntmd   |
574                 dir     |
575                 shtnak  |
576                 epnum;
577 }
578
579 static u16 usbhsp_setup_pipemaxp(struct usbhs_pipe *pipe,
580                                  const struct usb_endpoint_descriptor *desc,
581                                  int is_host)
582 {
583         /* host should set DEVSEL */
584
585         /* reutn MXPS */
586         return PIPE_MAXP_MASK & le16_to_cpu(desc->wMaxPacketSize);
587 }
588
589 static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe,
590                                  const struct usb_endpoint_descriptor *desc,
591                                  int is_host)
592 {
593         struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe);
594         struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
595         struct device *dev = usbhs_priv_to_dev(priv);
596         int pipe_num = usbhs_pipe_number(pipe);
597         int is_double = usbhsp_possible_double_buffer(pipe);
598         u16 buff_size;
599         u16 bufnmb;
600         u16 bufnmb_cnt;
601
602         /*
603          * PIPEBUF
604          *
605          * see
606          *  - "Register Descriptions" - "PIPEBUF" register
607          *  - "Features"  - "Pipe configuration"
608          *  - "Operation" - "FIFO Buffer Memory"
609          *  - "Operation" - "Pipe Control"
610          *
611          * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724)
612          *
613          * BUFNMB:      PIPE
614          * 0:           pipe0 (DCP 256byte)
615          * 1:           -
616          * 2:           -
617          * 3:           -
618          * 4:           pipe6 (INT 64byte)
619          * 5:           pipe7 (INT 64byte)
620          * 6:           pipe8 (INT 64byte)
621          * 7:           pipe9 (INT 64byte)
622          * 8 - xx:      free (for BULK, ISOC)
623          */
624
625         /*
626          * FIXME
627          *
628          * it doesn't have good buffer allocator
629          *
630          * DCP : 256 byte
631          * BULK: 512 byte
632          * INT :  64 byte
633          * ISOC: 512 byte
634          */
635         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_CONTROL))
636                 buff_size = 256;
637         else if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
638                 buff_size = 64;
639         else
640                 buff_size = 512;
641
642         /* change buff_size to register value */
643         bufnmb_cnt = (buff_size / 64) - 1;
644
645         /* BUFNMB has been reserved for INT pipe
646          * see above */
647         if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) {
648                 bufnmb = pipe_num - 2;
649         } else {
650                 bufnmb = info->bufnmb_last;
651                 info->bufnmb_last += bufnmb_cnt + 1;
652
653                 /*
654                  * double buffer
655                  */
656                 if (is_double)
657                         info->bufnmb_last += bufnmb_cnt + 1;
658         }
659
660         dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n",
661                 pipe_num, buff_size, bufnmb);
662
663         return  (0x1f & bufnmb_cnt)     << 10 |
664                 (0xff & bufnmb)         <<  0;
665 }
666
667 /*
668  *              pipe control
669  */
670 int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe)
671 {
672         u16 mask = usbhsp_is_dcp(pipe) ? DCP_MAXP_MASK : PIPE_MAXP_MASK;
673
674         usbhsp_pipe_select(pipe);
675
676         return (int)(usbhsp_pipe_maxp_get(pipe) & mask);
677 }
678
679 int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe)
680 {
681         return usbhsp_flags_has(pipe, IS_DIR_IN);
682 }
683
684 int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe)
685 {
686         return usbhsp_flags_has(pipe, IS_DIR_HOST);
687 }
688
689 void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe)
690 {
691         usbhsp_pipectrl_set(pipe, SQCLR, SQCLR);
692 }
693
694 static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type)
695 {
696         struct usbhs_pipe *pos, *pipe;
697         int i;
698
699         /*
700          * find target pipe
701          */
702         pipe = NULL;
703         usbhs_for_each_pipe_with_dcp(pos, priv, i) {
704                 if (!usbhsp_type_is(pos, type))
705                         continue;
706                 if (usbhsp_flags_has(pos, IS_USED))
707                         continue;
708
709                 pipe = pos;
710                 break;
711         }
712
713         if (!pipe)
714                 return NULL;
715
716         /*
717          * initialize pipe flags
718          */
719         usbhsp_flags_init(pipe);
720         usbhsp_flags_set(pipe, IS_USED);
721
722         return pipe;
723 }
724
725 void usbhs_pipe_init(struct usbhs_priv *priv)
726 {
727         struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
728         struct usbhs_pipe *pipe;
729         int i;
730
731         /*
732          * FIXME
733          *
734          * driver needs good allocator.
735          *
736          * find first free buffer area (BULK, ISOC)
737          * (DCP, INT area is fixed)
738          *
739          * buffer number 0 - 3 have been reserved for DCP
740          * see
741          *      usbhsp_to_bufnmb
742          */
743         info->bufnmb_last = 4;
744         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
745                 if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT))
746                         info->bufnmb_last++;
747
748                 usbhsp_flags_init(pipe);
749                 pipe->mod_private = NULL;
750
751                 usbhsp_fifo_clear(pipe);
752         }
753 }
754
755 struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv,
756                                      const struct usb_endpoint_descriptor *desc)
757 {
758         struct device *dev = usbhs_priv_to_dev(priv);
759         struct usbhs_mod *mod = usbhs_mod_get_current(priv);
760         struct usbhs_pipe *pipe;
761         int is_host = usbhs_mod_is_host(priv, mod);
762         int ret;
763         u16 pipecfg, pipebuf, pipemaxp;
764
765         pipe = usbhsp_get_pipe(priv, usb_endpoint_type(desc));
766         if (!pipe) {
767                 dev_err(dev, "can't get pipe (%s)\n",
768                         usbhsp_pipe_name[usb_endpoint_type(desc)]);
769                 return NULL;
770         }
771
772         usbhs_fifo_disable(pipe);
773
774         /* make sure pipe is not busy */
775         ret = usbhsp_pipe_barrier(pipe);
776         if (ret < 0) {
777                 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe));
778                 return NULL;
779         }
780
781         pipecfg  = usbhsp_setup_pipecfg(pipe,  desc, is_host);
782         pipebuf  = usbhsp_setup_pipebuff(pipe, desc, is_host);
783         pipemaxp = usbhsp_setup_pipemaxp(pipe, desc, is_host);
784
785         /* buffer clear
786          * see PIPECFG :: BFRE */
787         usbhsp_pipectrl_set(pipe, ACLRM, ACLRM);
788         usbhsp_pipectrl_set(pipe, ACLRM, 0);
789
790         usbhsp_pipe_select(pipe);
791         usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg);
792         usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf);
793         usbhsp_pipe_maxp_set(pipe, 0xFFFF, pipemaxp);
794
795         usbhs_pipe_clear_sequence(pipe);
796
797         dev_dbg(dev, "enable pipe %d : %s (%s)\n",
798                 usbhs_pipe_number(pipe),
799                 usbhsp_pipe_name[usb_endpoint_type(desc)],
800                 usbhs_pipe_is_dir_in(pipe) ? "in" : "out");
801
802         return pipe;
803 }
804
805 /*
806  *              dcp control
807  */
808 struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv)
809 {
810         struct usbhs_pipe *pipe;
811
812         pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL);
813         if (!pipe)
814                 return NULL;
815
816         /*
817          * dcpcfg  : default
818          * dcpmaxp : default
819          * pipebuf : nothing to do
820          */
821
822         usbhsp_pipe_select(pipe);
823         usbhs_pipe_clear_sequence(pipe);
824
825         return pipe;
826 }
827
828 void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe)
829 {
830         WARN_ON(!usbhsp_is_dcp(pipe));
831
832         usbhs_fifo_enable(pipe);
833         usbhsp_pipectrl_set(pipe, CCPL, CCPL);
834 }
835
836
837 /*
838  *              pipe module function
839  */
840 int usbhs_pipe_probe(struct usbhs_priv *priv)
841 {
842         struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
843         struct usbhs_pipe *pipe;
844         struct device *dev = usbhs_priv_to_dev(priv);
845         u32 *pipe_type = usbhs_get_dparam(priv, pipe_type);
846         int pipe_size = usbhs_get_dparam(priv, pipe_size);
847         int i;
848
849         /* This driver expects 1st pipe is DCP */
850         if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) {
851                 dev_err(dev, "1st PIPE is not DCP\n");
852                 return -EINVAL;
853         }
854
855         info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL);
856         if (!info->pipe) {
857                 dev_err(dev, "Could not allocate pipe\n");
858                 return -ENOMEM;
859         }
860
861         info->size = pipe_size;
862
863         /*
864          * init pipe
865          */
866         usbhs_for_each_pipe_with_dcp(pipe, priv, i) {
867                 pipe->priv = priv;
868                 usbhsp_type(pipe) = pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK;
869
870                 dev_dbg(dev, "pipe %x\t: %s\n",
871                         i, usbhsp_pipe_name[pipe_type[i]]);
872         }
873
874         return 0;
875 }
876
877 void usbhs_pipe_remove(struct usbhs_priv *priv)
878 {
879         struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv);
880
881         kfree(info->pipe);
882 }