]> Pileus Git - ~andy/linux/blob - drivers/staging/octeon-usb/octeon-hcd.c
staging: octeon-usb: delete cvmx_usb_isochronous_flags
[~andy/linux] / drivers / staging / octeon-usb / octeon-hcd.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2008 Cavium Networks
7  */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/pci.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/usb.h>
15
16 #include <linux/time.h>
17 #include <linux/delay.h>
18
19 #include <asm/octeon/cvmx.h>
20 #include "cvmx-usb.h"
21 #include <asm/octeon/cvmx-iob-defs.h>
22
23 #include <linux/usb/hcd.h>
24
25 #include <linux/err.h>
26
27 struct octeon_hcd {
28         spinlock_t lock;
29         struct cvmx_usb_state usb;
30         struct tasklet_struct dequeue_tasklet;
31         struct list_head dequeue_list;
32 };
33
34 /* convert between an HCD pointer and the corresponding struct octeon_hcd */
35 static inline struct octeon_hcd *hcd_to_octeon(struct usb_hcd *hcd)
36 {
37         return (struct octeon_hcd *)(hcd->hcd_priv);
38 }
39
40 static inline struct usb_hcd *octeon_to_hcd(struct octeon_hcd *p)
41 {
42         return container_of((void *)p, struct usb_hcd, hcd_priv);
43 }
44
45 static inline struct octeon_hcd *cvmx_usb_to_octeon(struct cvmx_usb_state *p)
46 {
47         return container_of(p, struct octeon_hcd, usb);
48 }
49
50 static irqreturn_t octeon_usb_irq(struct usb_hcd *hcd)
51 {
52         struct octeon_hcd *priv = hcd_to_octeon(hcd);
53         unsigned long flags;
54
55         spin_lock_irqsave(&priv->lock, flags);
56         cvmx_usb_poll(&priv->usb);
57         spin_unlock_irqrestore(&priv->lock, flags);
58         return IRQ_HANDLED;
59 }
60
61 static void octeon_usb_port_callback(struct cvmx_usb_state *usb,
62                                      enum cvmx_usb_callback reason,
63                                      enum cvmx_usb_complete status,
64                                      int pipe_handle,
65                                      int submit_handle,
66                                      int bytes_transferred,
67                                      void *user_data)
68 {
69         struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
70
71         spin_unlock(&priv->lock);
72         usb_hcd_poll_rh_status(octeon_to_hcd(priv));
73         spin_lock(&priv->lock);
74 }
75
76 static int octeon_usb_start(struct usb_hcd *hcd)
77 {
78         struct octeon_hcd *priv = hcd_to_octeon(hcd);
79         unsigned long flags;
80
81         hcd->state = HC_STATE_RUNNING;
82         spin_lock_irqsave(&priv->lock, flags);
83         cvmx_usb_register_callback(&priv->usb, CVMX_USB_CALLBACK_PORT_CHANGED,
84                                    octeon_usb_port_callback, NULL);
85         spin_unlock_irqrestore(&priv->lock, flags);
86         return 0;
87 }
88
89 static void octeon_usb_stop(struct usb_hcd *hcd)
90 {
91         struct octeon_hcd *priv = hcd_to_octeon(hcd);
92         unsigned long flags;
93
94         spin_lock_irqsave(&priv->lock, flags);
95         cvmx_usb_register_callback(&priv->usb, CVMX_USB_CALLBACK_PORT_CHANGED,
96                                    NULL, NULL);
97         spin_unlock_irqrestore(&priv->lock, flags);
98         hcd->state = HC_STATE_HALT;
99 }
100
101 static int octeon_usb_get_frame_number(struct usb_hcd *hcd)
102 {
103         struct octeon_hcd *priv = hcd_to_octeon(hcd);
104
105         return cvmx_usb_get_frame_number(&priv->usb);
106 }
107
108 static void octeon_usb_urb_complete_callback(struct cvmx_usb_state *usb,
109                                              enum cvmx_usb_callback reason,
110                                              enum cvmx_usb_complete status,
111                                              int pipe_handle,
112                                              int submit_handle,
113                                              int bytes_transferred,
114                                              void *user_data)
115 {
116         struct octeon_hcd *priv = cvmx_usb_to_octeon(usb);
117         struct usb_hcd *hcd = octeon_to_hcd(priv);
118         struct device *dev = hcd->self.controller;
119         struct urb *urb = user_data;
120
121         urb->actual_length = bytes_transferred;
122         urb->hcpriv = NULL;
123
124         if (!list_empty(&urb->urb_list)) {
125                 /*
126                  * It is on the dequeue_list, but we are going to call
127                  * usb_hcd_giveback_urb(), so we must clear it from
128                  * the list.  We got to it before the
129                  * octeon_usb_urb_dequeue_work() tasklet did.
130                  */
131                 list_del(&urb->urb_list);
132                 /* No longer on the dequeue_list. */
133                 INIT_LIST_HEAD(&urb->urb_list);
134         }
135
136         /* For Isochronous transactions we need to update the URB packet status
137            list from data in our private copy */
138         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
139                 int i;
140                 /*
141                  * The pointer to the private list is stored in the setup_packet
142                  * field.
143                  */
144                 struct cvmx_usb_iso_packet *iso_packet =
145                         (struct cvmx_usb_iso_packet *) urb->setup_packet;
146                 /* Recalculate the transfer size by adding up each packet */
147                 urb->actual_length = 0;
148                 for (i = 0; i < urb->number_of_packets; i++) {
149                         if (iso_packet[i].status == CVMX_USB_COMPLETE_SUCCESS) {
150                                 urb->iso_frame_desc[i].status = 0;
151                                 urb->iso_frame_desc[i].actual_length = iso_packet[i].length;
152                                 urb->actual_length += urb->iso_frame_desc[i].actual_length;
153                         } else {
154                                 dev_dbg(dev, "ISOCHRONOUS packet=%d of %d status=%d pipe=%d submit=%d size=%d\n",
155                                         i, urb->number_of_packets,
156                                         iso_packet[i].status, pipe_handle,
157                                         submit_handle, iso_packet[i].length);
158                                 urb->iso_frame_desc[i].status = -EREMOTEIO;
159                         }
160                 }
161                 /* Free the private list now that we don't need it anymore */
162                 kfree(iso_packet);
163                 urb->setup_packet = NULL;
164         }
165
166         switch (status) {
167         case CVMX_USB_COMPLETE_SUCCESS:
168                 urb->status = 0;
169                 break;
170         case CVMX_USB_COMPLETE_CANCEL:
171                 if (urb->status == 0)
172                         urb->status = -ENOENT;
173                 break;
174         case CVMX_USB_COMPLETE_STALL:
175                 dev_dbg(dev, "status=stall pipe=%d submit=%d size=%d\n",
176                         pipe_handle, submit_handle, bytes_transferred);
177                 urb->status = -EPIPE;
178                 break;
179         case CVMX_USB_COMPLETE_BABBLEERR:
180                 dev_dbg(dev, "status=babble pipe=%d submit=%d size=%d\n",
181                         pipe_handle, submit_handle, bytes_transferred);
182                 urb->status = -EPIPE;
183                 break;
184         case CVMX_USB_COMPLETE_SHORT:
185                 dev_dbg(dev, "status=short pipe=%d submit=%d size=%d\n",
186                         pipe_handle, submit_handle, bytes_transferred);
187                 urb->status = -EREMOTEIO;
188                 break;
189         case CVMX_USB_COMPLETE_ERROR:
190         case CVMX_USB_COMPLETE_XACTERR:
191         case CVMX_USB_COMPLETE_DATATGLERR:
192         case CVMX_USB_COMPLETE_FRAMEERR:
193                 dev_dbg(dev, "status=%d pipe=%d submit=%d size=%d\n",
194                         status, pipe_handle, submit_handle, bytes_transferred);
195                 urb->status = -EPROTO;
196                 break;
197         }
198         spin_unlock(&priv->lock);
199         usb_hcd_giveback_urb(octeon_to_hcd(priv), urb, urb->status);
200         spin_lock(&priv->lock);
201 }
202
203 static int octeon_usb_urb_enqueue(struct usb_hcd *hcd,
204                                   struct urb *urb,
205                                   gfp_t mem_flags)
206 {
207         struct octeon_hcd *priv = hcd_to_octeon(hcd);
208         struct device *dev = hcd->self.controller;
209         int submit_handle = -1;
210         int pipe_handle;
211         unsigned long flags;
212         struct cvmx_usb_iso_packet *iso_packet;
213         struct usb_host_endpoint *ep = urb->ep;
214
215         urb->status = 0;
216         INIT_LIST_HEAD(&urb->urb_list); /* not enqueued on dequeue_list */
217         spin_lock_irqsave(&priv->lock, flags);
218
219         if (!ep->hcpriv) {
220                 enum cvmx_usb_transfer transfer_type;
221                 enum cvmx_usb_speed speed;
222                 int split_device = 0;
223                 int split_port = 0;
224                 switch (usb_pipetype(urb->pipe)) {
225                 case PIPE_ISOCHRONOUS:
226                         transfer_type = CVMX_USB_TRANSFER_ISOCHRONOUS;
227                         break;
228                 case PIPE_INTERRUPT:
229                         transfer_type = CVMX_USB_TRANSFER_INTERRUPT;
230                         break;
231                 case PIPE_CONTROL:
232                         transfer_type = CVMX_USB_TRANSFER_CONTROL;
233                         break;
234                 default:
235                         transfer_type = CVMX_USB_TRANSFER_BULK;
236                         break;
237                 }
238                 switch (urb->dev->speed) {
239                 case USB_SPEED_LOW:
240                         speed = CVMX_USB_SPEED_LOW;
241                         break;
242                 case USB_SPEED_FULL:
243                         speed = CVMX_USB_SPEED_FULL;
244                         break;
245                 default:
246                         speed = CVMX_USB_SPEED_HIGH;
247                         break;
248                 }
249                 /*
250                  * For slow devices on high speed ports we need to find the hub
251                  * that does the speed translation so we know where to send the
252                  * split transactions.
253                  */
254                 if (speed != CVMX_USB_SPEED_HIGH) {
255                         /*
256                          * Start at this device and work our way up the usb
257                          * tree.
258                          */
259                         struct usb_device *dev = urb->dev;
260                         while (dev->parent) {
261                                 /*
262                                  * If our parent is high speed then he'll
263                                  * receive the splits.
264                                  */
265                                 if (dev->parent->speed == USB_SPEED_HIGH) {
266                                         split_device = dev->parent->devnum;
267                                         split_port = dev->portnum;
268                                         break;
269                                 }
270                                 /*
271                                  * Move up the tree one level. If we make it all
272                                  * the way up the tree, then the port must not
273                                  * be in high speed mode and we don't need a
274                                  * split.
275                                  */
276                                 dev = dev->parent;
277                         }
278                 }
279                 pipe_handle = cvmx_usb_open_pipe(&priv->usb,
280                                                  0,
281                                                  usb_pipedevice(urb->pipe),
282                                                  usb_pipeendpoint(urb->pipe),
283                                                  speed,
284                                                  le16_to_cpu(ep->desc.wMaxPacketSize) & 0x7ff,
285                                                  transfer_type,
286                                                  usb_pipein(urb->pipe) ? CVMX_USB_DIRECTION_IN : CVMX_USB_DIRECTION_OUT,
287                                                  urb->interval,
288                                                  (le16_to_cpu(ep->desc.wMaxPacketSize) >> 11) & 0x3,
289                                                  split_device,
290                                                  split_port);
291                 if (pipe_handle < 0) {
292                         spin_unlock_irqrestore(&priv->lock, flags);
293                         dev_dbg(dev, "Failed to create pipe\n");
294                         return -ENOMEM;
295                 }
296                 ep->hcpriv = (void *)(0x10000L + pipe_handle);
297         } else {
298                 pipe_handle = 0xffff & (long)ep->hcpriv;
299         }
300
301         switch (usb_pipetype(urb->pipe)) {
302         case PIPE_ISOCHRONOUS:
303                 dev_dbg(dev, "Submit isochronous to %d.%d\n",
304                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
305                 /*
306                  * Allocate a structure to use for our private list of
307                  * isochronous packets.
308                  */
309                 iso_packet = kmalloc(urb->number_of_packets *
310                                      sizeof(struct cvmx_usb_iso_packet),
311                                      GFP_ATOMIC);
312                 if (iso_packet) {
313                         int i;
314                         /* Fill the list with the data from the URB */
315                         for (i = 0; i < urb->number_of_packets; i++) {
316                                 iso_packet[i].offset = urb->iso_frame_desc[i].offset;
317                                 iso_packet[i].length = urb->iso_frame_desc[i].length;
318                                 iso_packet[i].status = CVMX_USB_COMPLETE_ERROR;
319                         }
320                         /*
321                          * Store a pointer to the list in the URB setup_packet
322                          * field. We know this currently isn't being used and
323                          * this saves us a bunch of logic.
324                          */
325                         urb->setup_packet = (char *)iso_packet;
326                         submit_handle = cvmx_usb_submit_isochronous(&priv->usb, pipe_handle,
327                                                         urb->start_frame,
328                                                         urb->number_of_packets,
329                                                         iso_packet,
330                                                         urb->transfer_dma,
331                                                         urb->transfer_buffer_length,
332                                                         octeon_usb_urb_complete_callback,
333                                                         urb);
334                         /*
335                          * If submit failed we need to free our private packet
336                          * list.
337                          */
338                         if (submit_handle < 0) {
339                                 urb->setup_packet = NULL;
340                                 kfree(iso_packet);
341                         }
342                 }
343                 break;
344         case PIPE_INTERRUPT:
345                 dev_dbg(dev, "Submit interrupt to %d.%d\n",
346                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
347                 submit_handle = cvmx_usb_submit_interrupt(&priv->usb, pipe_handle,
348                                               urb->transfer_dma,
349                                               urb->transfer_buffer_length,
350                                               octeon_usb_urb_complete_callback,
351                                               urb);
352                 break;
353         case PIPE_CONTROL:
354                 dev_dbg(dev, "Submit control to %d.%d\n",
355                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
356                 submit_handle = cvmx_usb_submit_control(&priv->usb, pipe_handle,
357                                             urb->setup_dma,
358                                             urb->transfer_dma,
359                                             urb->transfer_buffer_length,
360                                             octeon_usb_urb_complete_callback,
361                                             urb);
362                 break;
363         case PIPE_BULK:
364                 dev_dbg(dev, "Submit bulk to %d.%d\n",
365                         usb_pipedevice(urb->pipe), usb_pipeendpoint(urb->pipe));
366                 submit_handle = cvmx_usb_submit_bulk(&priv->usb, pipe_handle,
367                                          urb->transfer_dma,
368                                          urb->transfer_buffer_length,
369                                          octeon_usb_urb_complete_callback,
370                                          urb);
371                 break;
372         }
373         if (submit_handle < 0) {
374                 spin_unlock_irqrestore(&priv->lock, flags);
375                 dev_dbg(dev, "Failed to submit\n");
376                 return -ENOMEM;
377         }
378         urb->hcpriv = (void *)(long)(((submit_handle & 0xffff) << 16) | pipe_handle);
379         spin_unlock_irqrestore(&priv->lock, flags);
380         return 0;
381 }
382
383 static void octeon_usb_urb_dequeue_work(unsigned long arg)
384 {
385         unsigned long flags;
386         struct octeon_hcd *priv = (struct octeon_hcd *)arg;
387
388         spin_lock_irqsave(&priv->lock, flags);
389
390         while (!list_empty(&priv->dequeue_list)) {
391                 int pipe_handle;
392                 int submit_handle;
393                 struct urb *urb = container_of(priv->dequeue_list.next, struct urb, urb_list);
394                 list_del(&urb->urb_list);
395                 /* not enqueued on dequeue_list */
396                 INIT_LIST_HEAD(&urb->urb_list);
397                 pipe_handle = 0xffff & (long)urb->hcpriv;
398                 submit_handle = ((long)urb->hcpriv) >> 16;
399                 cvmx_usb_cancel(&priv->usb, pipe_handle, submit_handle);
400         }
401
402         spin_unlock_irqrestore(&priv->lock, flags);
403 }
404
405 static int octeon_usb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
406 {
407         struct octeon_hcd *priv = hcd_to_octeon(hcd);
408         unsigned long flags;
409
410         if (!urb->dev)
411                 return -EINVAL;
412
413         spin_lock_irqsave(&priv->lock, flags);
414
415         urb->status = status;
416         list_add_tail(&urb->urb_list, &priv->dequeue_list);
417
418         spin_unlock_irqrestore(&priv->lock, flags);
419
420         tasklet_schedule(&priv->dequeue_tasklet);
421
422         return 0;
423 }
424
425 static void octeon_usb_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
426 {
427         struct device *dev = hcd->self.controller;
428
429         if (ep->hcpriv) {
430                 struct octeon_hcd *priv = hcd_to_octeon(hcd);
431                 int pipe_handle = 0xffff & (long)ep->hcpriv;
432                 unsigned long flags;
433                 spin_lock_irqsave(&priv->lock, flags);
434                 cvmx_usb_cancel_all(&priv->usb, pipe_handle);
435                 if (cvmx_usb_close_pipe(&priv->usb, pipe_handle))
436                         dev_dbg(dev, "Closing pipe %d failed\n", pipe_handle);
437                 spin_unlock_irqrestore(&priv->lock, flags);
438                 ep->hcpriv = NULL;
439         }
440 }
441
442 static int octeon_usb_hub_status_data(struct usb_hcd *hcd, char *buf)
443 {
444         struct octeon_hcd *priv = hcd_to_octeon(hcd);
445         struct cvmx_usb_port_status port_status;
446         unsigned long flags;
447
448         spin_lock_irqsave(&priv->lock, flags);
449         port_status = cvmx_usb_get_status(&priv->usb);
450         spin_unlock_irqrestore(&priv->lock, flags);
451         buf[0] = 0;
452         buf[0] = port_status.connect_change << 1;
453
454         return (buf[0] != 0);
455 }
456
457 static int octeon_usb_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
458 {
459         struct octeon_hcd *priv = hcd_to_octeon(hcd);
460         struct device *dev = hcd->self.controller;
461         struct cvmx_usb_port_status usb_port_status;
462         int port_status;
463         struct usb_hub_descriptor *desc;
464         unsigned long flags;
465
466         switch (typeReq) {
467         case ClearHubFeature:
468                 dev_dbg(dev, "ClearHubFeature\n");
469                 switch (wValue) {
470                 case C_HUB_LOCAL_POWER:
471                 case C_HUB_OVER_CURRENT:
472                         /* Nothing required here */
473                         break;
474                 default:
475                         return -EINVAL;
476                 }
477                 break;
478         case ClearPortFeature:
479                 dev_dbg(dev, "ClearPortFeature\n");
480                 if (wIndex != 1) {
481                         dev_dbg(dev, " INVALID\n");
482                         return -EINVAL;
483                 }
484
485                 switch (wValue) {
486                 case USB_PORT_FEAT_ENABLE:
487                         dev_dbg(dev, " ENABLE\n");
488                         spin_lock_irqsave(&priv->lock, flags);
489                         cvmx_usb_disable(&priv->usb);
490                         spin_unlock_irqrestore(&priv->lock, flags);
491                         break;
492                 case USB_PORT_FEAT_SUSPEND:
493                         dev_dbg(dev, " SUSPEND\n");
494                         /* Not supported on Octeon */
495                         break;
496                 case USB_PORT_FEAT_POWER:
497                         dev_dbg(dev, " POWER\n");
498                         /* Not supported on Octeon */
499                         break;
500                 case USB_PORT_FEAT_INDICATOR:
501                         dev_dbg(dev, " INDICATOR\n");
502                         /* Port inidicator not supported */
503                         break;
504                 case USB_PORT_FEAT_C_CONNECTION:
505                         dev_dbg(dev, " C_CONNECTION\n");
506                         /* Clears drivers internal connect status change flag */
507                         spin_lock_irqsave(&priv->lock, flags);
508                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
509                         spin_unlock_irqrestore(&priv->lock, flags);
510                         break;
511                 case USB_PORT_FEAT_C_RESET:
512                         dev_dbg(dev, " C_RESET\n");
513                         /*
514                          * Clears the driver's internal Port Reset Change flag.
515                          */
516                         spin_lock_irqsave(&priv->lock, flags);
517                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
518                         spin_unlock_irqrestore(&priv->lock, flags);
519                         break;
520                 case USB_PORT_FEAT_C_ENABLE:
521                         dev_dbg(dev, " C_ENABLE\n");
522                         /*
523                          * Clears the driver's internal Port Enable/Disable
524                          * Change flag.
525                          */
526                         spin_lock_irqsave(&priv->lock, flags);
527                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
528                         spin_unlock_irqrestore(&priv->lock, flags);
529                         break;
530                 case USB_PORT_FEAT_C_SUSPEND:
531                         dev_dbg(dev, " C_SUSPEND\n");
532                         /*
533                          * Clears the driver's internal Port Suspend Change
534                          * flag, which is set when resume signaling on the host
535                          * port is complete.
536                          */
537                         break;
538                 case USB_PORT_FEAT_C_OVER_CURRENT:
539                         dev_dbg(dev, " C_OVER_CURRENT\n");
540                         /* Clears the driver's overcurrent Change flag */
541                         spin_lock_irqsave(&priv->lock, flags);
542                         cvmx_usb_set_status(&priv->usb, cvmx_usb_get_status(&priv->usb));
543                         spin_unlock_irqrestore(&priv->lock, flags);
544                         break;
545                 default:
546                         dev_dbg(dev, " UNKNOWN\n");
547                         return -EINVAL;
548                 }
549                 break;
550         case GetHubDescriptor:
551                 dev_dbg(dev, "GetHubDescriptor\n");
552                 desc = (struct usb_hub_descriptor *)buf;
553                 desc->bDescLength = 9;
554                 desc->bDescriptorType = 0x29;
555                 desc->bNbrPorts = 1;
556                 desc->wHubCharacteristics = 0x08;
557                 desc->bPwrOn2PwrGood = 1;
558                 desc->bHubContrCurrent = 0;
559                 desc->u.hs.DeviceRemovable[0] = 0;
560                 desc->u.hs.DeviceRemovable[1] = 0xff;
561                 break;
562         case GetHubStatus:
563                 dev_dbg(dev, "GetHubStatus\n");
564                 *(__le32 *) buf = 0;
565                 break;
566         case GetPortStatus:
567                 dev_dbg(dev, "GetPortStatus\n");
568                 if (wIndex != 1) {
569                         dev_dbg(dev, " INVALID\n");
570                         return -EINVAL;
571                 }
572
573                 spin_lock_irqsave(&priv->lock, flags);
574                 usb_port_status = cvmx_usb_get_status(&priv->usb);
575                 spin_unlock_irqrestore(&priv->lock, flags);
576                 port_status = 0;
577
578                 if (usb_port_status.connect_change) {
579                         port_status |= (1 << USB_PORT_FEAT_C_CONNECTION);
580                         dev_dbg(dev, " C_CONNECTION\n");
581                 }
582
583                 if (usb_port_status.port_enabled) {
584                         port_status |= (1 << USB_PORT_FEAT_C_ENABLE);
585                         dev_dbg(dev, " C_ENABLE\n");
586                 }
587
588                 if (usb_port_status.connected) {
589                         port_status |= (1 << USB_PORT_FEAT_CONNECTION);
590                         dev_dbg(dev, " CONNECTION\n");
591                 }
592
593                 if (usb_port_status.port_enabled) {
594                         port_status |= (1 << USB_PORT_FEAT_ENABLE);
595                         dev_dbg(dev, " ENABLE\n");
596                 }
597
598                 if (usb_port_status.port_over_current) {
599                         port_status |= (1 << USB_PORT_FEAT_OVER_CURRENT);
600                         dev_dbg(dev, " OVER_CURRENT\n");
601                 }
602
603                 if (usb_port_status.port_powered) {
604                         port_status |= (1 << USB_PORT_FEAT_POWER);
605                         dev_dbg(dev, " POWER\n");
606                 }
607
608                 if (usb_port_status.port_speed == CVMX_USB_SPEED_HIGH) {
609                         port_status |= USB_PORT_STAT_HIGH_SPEED;
610                         dev_dbg(dev, " HIGHSPEED\n");
611                 } else if (usb_port_status.port_speed == CVMX_USB_SPEED_LOW) {
612                         port_status |= (1 << USB_PORT_FEAT_LOWSPEED);
613                         dev_dbg(dev, " LOWSPEED\n");
614                 }
615
616                 *((__le32 *) buf) = cpu_to_le32(port_status);
617                 break;
618         case SetHubFeature:
619                 dev_dbg(dev, "SetHubFeature\n");
620                 /* No HUB features supported */
621                 break;
622         case SetPortFeature:
623                 dev_dbg(dev, "SetPortFeature\n");
624                 if (wIndex != 1) {
625                         dev_dbg(dev, " INVALID\n");
626                         return -EINVAL;
627                 }
628
629                 switch (wValue) {
630                 case USB_PORT_FEAT_SUSPEND:
631                         dev_dbg(dev, " SUSPEND\n");
632                         return -EINVAL;
633                 case USB_PORT_FEAT_POWER:
634                         dev_dbg(dev, " POWER\n");
635                         return -EINVAL;
636                 case USB_PORT_FEAT_RESET:
637                         dev_dbg(dev, " RESET\n");
638                         spin_lock_irqsave(&priv->lock, flags);
639                         cvmx_usb_disable(&priv->usb);
640                         if (cvmx_usb_enable(&priv->usb))
641                                 dev_dbg(dev, "Failed to enable the port\n");
642                         spin_unlock_irqrestore(&priv->lock, flags);
643                         return 0;
644                 case USB_PORT_FEAT_INDICATOR:
645                         dev_dbg(dev, " INDICATOR\n");
646                         /* Not supported */
647                         break;
648                 default:
649                         dev_dbg(dev, " UNKNOWN\n");
650                         return -EINVAL;
651                 }
652                 break;
653         default:
654                 dev_dbg(dev, "Unknown root hub request\n");
655                 return -EINVAL;
656         }
657         return 0;
658 }
659
660
661 static const struct hc_driver octeon_hc_driver = {
662         .description            = "Octeon USB",
663         .product_desc           = "Octeon Host Controller",
664         .hcd_priv_size          = sizeof(struct octeon_hcd),
665         .irq                    = octeon_usb_irq,
666         .flags                  = HCD_MEMORY | HCD_USB2,
667         .start                  = octeon_usb_start,
668         .stop                   = octeon_usb_stop,
669         .urb_enqueue            = octeon_usb_urb_enqueue,
670         .urb_dequeue            = octeon_usb_urb_dequeue,
671         .endpoint_disable       = octeon_usb_endpoint_disable,
672         .get_frame_number       = octeon_usb_get_frame_number,
673         .hub_status_data        = octeon_usb_hub_status_data,
674         .hub_control            = octeon_usb_hub_control,
675 };
676
677
678 static int octeon_usb_driver_probe(struct device *dev)
679 {
680         int status;
681         int usb_num = to_platform_device(dev)->id;
682         int irq = platform_get_irq(to_platform_device(dev), 0);
683         struct octeon_hcd *priv;
684         struct usb_hcd *hcd;
685         unsigned long flags;
686
687         /*
688          * Set the DMA mask to 64bits so we get buffers already translated for
689          * DMA.
690          */
691         dev->coherent_dma_mask = ~0;
692         dev->dma_mask = &dev->coherent_dma_mask;
693
694         hcd = usb_create_hcd(&octeon_hc_driver, dev, dev_name(dev));
695         if (!hcd) {
696                 dev_dbg(dev, "Failed to allocate memory for HCD\n");
697                 return -1;
698         }
699         hcd->uses_new_polling = 1;
700         priv = (struct octeon_hcd *)hcd->hcd_priv;
701
702         spin_lock_init(&priv->lock);
703
704         tasklet_init(&priv->dequeue_tasklet, octeon_usb_urb_dequeue_work, (unsigned long)priv);
705         INIT_LIST_HEAD(&priv->dequeue_list);
706
707         status = cvmx_usb_initialize(&priv->usb, usb_num);
708         if (status) {
709                 dev_dbg(dev, "USB initialization failed with %d\n", status);
710                 kfree(hcd);
711                 return -1;
712         }
713
714         /* This delay is needed for CN3010, but I don't know why... */
715         mdelay(10);
716
717         spin_lock_irqsave(&priv->lock, flags);
718         cvmx_usb_poll(&priv->usb);
719         spin_unlock_irqrestore(&priv->lock, flags);
720
721         status = usb_add_hcd(hcd, irq, IRQF_SHARED);
722         if (status) {
723                 dev_dbg(dev, "USB add HCD failed with %d\n", status);
724                 kfree(hcd);
725                 return -1;
726         }
727
728         dev_dbg(dev, "Registered HCD for port %d on irq %d\n", usb_num, irq);
729
730         return 0;
731 }
732
733 static int octeon_usb_driver_remove(struct device *dev)
734 {
735         int status;
736         struct usb_hcd *hcd = dev_get_drvdata(dev);
737         struct octeon_hcd *priv = hcd_to_octeon(hcd);
738         unsigned long flags;
739
740         usb_remove_hcd(hcd);
741         tasklet_kill(&priv->dequeue_tasklet);
742         spin_lock_irqsave(&priv->lock, flags);
743         status = cvmx_usb_shutdown(&priv->usb);
744         spin_unlock_irqrestore(&priv->lock, flags);
745         if (status)
746                 dev_dbg(dev, "USB shutdown failed with %d\n", status);
747
748         kfree(hcd);
749
750         return 0;
751 }
752
753 static struct device_driver octeon_usb_driver = {
754         .name   = "OcteonUSB",
755         .bus    = &platform_bus_type,
756         .probe  = octeon_usb_driver_probe,
757         .remove = octeon_usb_driver_remove,
758 };
759
760
761 #define MAX_USB_PORTS   10
762 static struct platform_device *pdev_glob[MAX_USB_PORTS];
763 static int octeon_usb_registered;
764 static int __init octeon_usb_module_init(void)
765 {
766         int num_devices = cvmx_usb_get_num_ports();
767         int device;
768
769         if (usb_disabled() || num_devices == 0)
770                 return -ENODEV;
771
772         if (driver_register(&octeon_usb_driver))
773                 return -ENOMEM;
774
775         octeon_usb_registered = 1;
776
777         /*
778          * Only cn52XX and cn56XX have DWC_OTG USB hardware and the
779          * IOB priority registers.  Under heavy network load USB
780          * hardware can be starved by the IOB causing a crash.  Give
781          * it a priority boost if it has been waiting more than 400
782          * cycles to avoid this situation.
783          *
784          * Testing indicates that a cnt_val of 8192 is not sufficient,
785          * but no failures are seen with 4096.  We choose a value of
786          * 400 to give a safety factor of 10.
787          */
788         if (OCTEON_IS_MODEL(OCTEON_CN52XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
789                 union cvmx_iob_n2c_l2c_pri_cnt pri_cnt;
790
791                 pri_cnt.u64 = 0;
792                 pri_cnt.s.cnt_enb = 1;
793                 pri_cnt.s.cnt_val = 400;
794                 cvmx_write_csr(CVMX_IOB_N2C_L2C_PRI_CNT, pri_cnt.u64);
795         }
796
797         for (device = 0; device < num_devices; device++) {
798                 struct resource irq_resource;
799                 struct platform_device *pdev;
800                 memset(&irq_resource, 0, sizeof(irq_resource));
801                 irq_resource.start = (device == 0) ? OCTEON_IRQ_USB0 : OCTEON_IRQ_USB1;
802                 irq_resource.end = irq_resource.start;
803                 irq_resource.flags = IORESOURCE_IRQ;
804                 pdev = platform_device_register_simple((char *)octeon_usb_driver.  name, device, &irq_resource, 1);
805                 if (IS_ERR(pdev)) {
806                         driver_unregister(&octeon_usb_driver);
807                         octeon_usb_registered = 0;
808                         return PTR_ERR(pdev);
809                 }
810                 if (device < MAX_USB_PORTS)
811                         pdev_glob[device] = pdev;
812
813         }
814         return 0;
815 }
816
817 static void __exit octeon_usb_module_cleanup(void)
818 {
819         int i;
820
821         for (i = 0; i < MAX_USB_PORTS; i++)
822                 if (pdev_glob[i]) {
823                         platform_device_unregister(pdev_glob[i]);
824                         pdev_glob[i] = NULL;
825                 }
826         if (octeon_usb_registered)
827                 driver_unregister(&octeon_usb_driver);
828 }
829
830 MODULE_LICENSE("GPL");
831 MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
832 MODULE_DESCRIPTION("Cavium Networks Octeon USB Host driver.");
833 module_init(octeon_usb_module_init);
834 module_exit(octeon_usb_module_cleanup);