blob: 8543bb29a138c232748c0bd13f2811a5ba494e99 [file] [log] [blame]
Chris Kellyae926052012-02-20 21:11:53 +00001/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 *
5 * This file provides the implementation of a USB host controller device that
6 * does not have any associated hardware. Instead the virtual device is
7 * connected to the WiFi network and emulates the operation of a USB hcd by
8 * receiving and sending network frames.
9 * Note:
10 * We take great pains to reduce the amount of code where interrupts need to be
11 * disabled and in this respect we are different from standard HCD's. In
12 * particular we don't want in_irq() code bleeding over to the protocol side of
13 * the driver.
14 * The troublesome functions are the urb enqueue and dequeue functions both of
15 * which can be called in_irq(). So for these functions we put the urbs into a
16 * queue and request a tasklet to process them. This means that a spinlock with
17 * interrupts disabled must be held for insertion and removal but most code is
18 * is in tasklet or soft irq context. The lock that protects this list is called
19 * the tasklet lock and serves the purpose of the 'HCD lock' which must be held
20 * when calling the following functions.
21 * usb_hcd_link_urb_to_ep()
22 * usb_hcd_unlink_urb_from_ep()
23 * usb_hcd_flush_endpoint()
24 * usb_hcd_check_unlink_urb()
25 * -----------------------------------------------------------------------------
26 */
27#include <linux/platform_device.h>
28#include <linux/usb.h>
Chris Kellyae926052012-02-20 21:11:53 +000029#include <linux/slab.h>
30#include <linux/export.h>
31#include "linux/usb/hcd.h"
32#include <asm/unaligned.h>
Joe Perchesf724b582013-07-23 13:45:00 +010033#include "ozdbg.h"
Chris Kellyae926052012-02-20 21:11:53 +000034#include "ozusbif.h"
Chris Kellyae926052012-02-20 21:11:53 +000035#include "ozurbparanoia.h"
Peter Huewe4f9d5b22013-02-15 21:17:21 +010036#include "ozhcd.h"
Rupesh Gujare6e244a82013-08-13 18:24:22 +010037
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010038/*
Chris Kellyae926052012-02-20 21:11:53 +000039 * Number of units of buffering to capture for an isochronous IN endpoint before
40 * allowing data to be indicated up.
41 */
Rupesh Gujarebe5e5922013-08-28 12:43:14 +010042#define OZ_IN_BUFFERING_UNITS 100
Rupesh Gujare6e244a82013-08-13 18:24:22 +010043
Chris Kellyae926052012-02-20 21:11:53 +000044/* Name of our platform device.
45 */
46#define OZ_PLAT_DEV_NAME "ozwpan"
Rupesh Gujare6e244a82013-08-13 18:24:22 +010047
Rupesh Gujare8fd07002013-07-30 13:31:50 +010048/*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
49 */
50#define EP0_TIMEOUT_COUNTER 13
Rupesh Gujare6e244a82013-08-13 18:24:22 +010051
Rupesh Gujare0140eb22013-08-27 16:53:42 +010052/* Debounce time HCD driver should wait before unregistering.
53 */
54#define OZ_HUB_DEBOUNCE_TIMEOUT 1500
55
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010056/*
Chris Kellyae926052012-02-20 21:11:53 +000057 * Used to link urbs together and also store some status information for each
58 * urb.
59 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
60 */
61struct oz_urb_link {
62 struct list_head link;
63 struct urb *urb;
64 struct oz_port *port;
65 u8 req_id;
66 u8 ep_num;
Rupesh Gujare8fd07002013-07-30 13:31:50 +010067 unsigned submit_counter;
Chris Kellyae926052012-02-20 21:11:53 +000068};
69
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +020070static struct kmem_cache *oz_urb_link_cache;
71
Chris Kellyae926052012-02-20 21:11:53 +000072/* Holds state information about a USB endpoint.
73 */
Rupesh Gujare0f750be2013-08-23 18:33:29 +010074#define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
Rupesh Gujare00d2a462013-08-23 18:33:30 +010075#define OZ_EP_BUFFER_SIZE_INT 512
Chris Kellyae926052012-02-20 21:11:53 +000076struct oz_endpoint {
77 struct list_head urb_list; /* List of oz_urb_link items. */
78 struct list_head link; /* For isoc ep, links in to isoc
79 lists of oz_port. */
Rupesh Gujare8fd07002013-07-30 13:31:50 +010080 struct timespec timestamp;
Chris Kellyae926052012-02-20 21:11:53 +000081 int credit;
82 int credit_ceiling;
83 u8 ep_num;
84 u8 attrib;
85 u8 *buffer;
86 int buffer_size;
87 int in_ix;
88 int out_ix;
89 int buffered_units;
90 unsigned flags;
91 int start_frame;
92};
Rupesh Gujare6e244a82013-08-13 18:24:22 +010093
Chris Kellyae926052012-02-20 21:11:53 +000094/* Bits in the flags field. */
95#define OZ_F_EP_BUFFERING 0x1
96#define OZ_F_EP_HAVE_STREAM 0x2
97
98/* Holds state information about a USB interface.
99 */
100struct oz_interface {
101 unsigned ep_mask;
102 u8 alt;
103};
104
105/* Holds state information about an hcd port.
106 */
107#define OZ_NB_ENDPOINTS 16
108struct oz_port {
109 unsigned flags;
110 unsigned status;
111 void *hpd;
112 struct oz_hcd *ozhcd;
113 spinlock_t port_lock;
114 u8 bus_addr;
115 u8 next_req_id;
116 u8 config_num;
117 int num_iface;
118 struct oz_interface *iface;
119 struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
120 struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
121 struct list_head isoc_out_ep;
122 struct list_head isoc_in_ep;
123};
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100124
Chris Kellyae926052012-02-20 21:11:53 +0000125#define OZ_PORT_F_PRESENT 0x1
126#define OZ_PORT_F_CHANGED 0x2
127#define OZ_PORT_F_DYING 0x4
128
129/* Data structure in the private context area of struct usb_hcd.
130 */
131#define OZ_NB_PORTS 8
132struct oz_hcd {
133 spinlock_t hcd_lock;
134 struct list_head urb_pending_list;
135 struct list_head urb_cancel_list;
136 struct list_head orphanage;
137 int conn_port; /* Port that is currently connecting, -1 if none.*/
138 struct oz_port ports[OZ_NB_PORTS];
139 uint flags;
140 struct usb_hcd *hcd;
141};
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100142
Chris Kellyae926052012-02-20 21:11:53 +0000143/* Bits in flags field.
144 */
145#define OZ_HDC_F_SUSPENDED 0x1
146
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100147/*
Chris Kellyae926052012-02-20 21:11:53 +0000148 * Static function prototypes.
149 */
150static int oz_hcd_start(struct usb_hcd *hcd);
151static void oz_hcd_stop(struct usb_hcd *hcd);
152static void oz_hcd_shutdown(struct usb_hcd *hcd);
153static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
154 gfp_t mem_flags);
155static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
156static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
157 struct usb_host_endpoint *ep);
158static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
159 struct usb_host_endpoint *ep);
160static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
161static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
162static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
163 u16 windex, char *buf, u16 wlength);
164static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
165static int oz_hcd_bus_resume(struct usb_hcd *hcd);
166static int oz_plat_probe(struct platform_device *dev);
167static int oz_plat_remove(struct platform_device *dev);
168static void oz_plat_shutdown(struct platform_device *dev);
169static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
170static int oz_plat_resume(struct platform_device *dev);
171static void oz_urb_process_tasklet(unsigned long unused);
172static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
173 struct oz_port *port, struct usb_host_config *config,
174 gfp_t mem_flags);
175static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
176 struct oz_port *port);
177static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
178 struct oz_port *port,
179 struct usb_host_interface *intf, gfp_t mem_flags);
180static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
181 struct oz_port *port, int if_ix);
182static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
183 gfp_t mem_flags);
184static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
185 struct urb *urb);
186static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100187
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100188/*
Chris Kellyae926052012-02-20 21:11:53 +0000189 * Static external variables.
190 */
191static struct platform_device *g_plat_dev;
192static struct oz_hcd *g_ozhcd;
193static DEFINE_SPINLOCK(g_hcdlock); /* Guards g_ozhcd. */
194static const char g_hcd_name[] = "Ozmo WPAN";
Chris Kellyae926052012-02-20 21:11:53 +0000195static DEFINE_SPINLOCK(g_tasklet_lock);
196static struct tasklet_struct g_urb_process_tasklet;
197static struct tasklet_struct g_urb_cancel_tasklet;
198static atomic_t g_pending_urbs = ATOMIC_INIT(0);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100199static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
Chris Kellyae926052012-02-20 21:11:53 +0000200static const struct hc_driver g_oz_hc_drv = {
201 .description = g_hcd_name,
202 .product_desc = "Ozmo Devices WPAN",
203 .hcd_priv_size = sizeof(struct oz_hcd),
204 .flags = HCD_USB11,
205 .start = oz_hcd_start,
206 .stop = oz_hcd_stop,
207 .shutdown = oz_hcd_shutdown,
208 .urb_enqueue = oz_hcd_urb_enqueue,
209 .urb_dequeue = oz_hcd_urb_dequeue,
210 .endpoint_disable = oz_hcd_endpoint_disable,
211 .endpoint_reset = oz_hcd_endpoint_reset,
212 .get_frame_number = oz_hcd_get_frame_number,
213 .hub_status_data = oz_hcd_hub_status_data,
214 .hub_control = oz_hcd_hub_control,
215 .bus_suspend = oz_hcd_bus_suspend,
216 .bus_resume = oz_hcd_bus_resume,
217};
218
219static struct platform_driver g_oz_plat_drv = {
220 .probe = oz_plat_probe,
221 .remove = oz_plat_remove,
222 .shutdown = oz_plat_shutdown,
223 .suspend = oz_plat_suspend,
224 .resume = oz_plat_resume,
225 .driver = {
226 .name = OZ_PLAT_DEV_NAME,
Chris Kellyae926052012-02-20 21:11:53 +0000227 },
228};
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100229
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100230/*
Chris Kellyae926052012-02-20 21:11:53 +0000231 * Gets our private context area (which is of type struct oz_hcd) from the
232 * usb_hcd structure.
233 * Context: any
234 */
235static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
236{
237 return (struct oz_hcd *)hcd->hcd_priv;
238}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100239
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100240/*
Chris Kellyae926052012-02-20 21:11:53 +0000241 * Searches list of ports to find the index of the one with a specified USB
242 * bus address. If none of the ports has the bus address then the connection
243 * port is returned, if there is one or -1 otherwise.
244 * Context: any
245 */
246static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
247{
248 int i;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100249
Chris Kellyae926052012-02-20 21:11:53 +0000250 for (i = 0; i < OZ_NB_PORTS; i++) {
251 if (ozhcd->ports[i].bus_addr == bus_addr)
252 return i;
253 }
254 return ozhcd->conn_port;
255}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100256
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100257/*
Chris Kellyae926052012-02-20 21:11:53 +0000258 * Context: any
259 */
260static struct oz_urb_link *oz_alloc_urb_link(void)
261{
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +0200262 return kmem_cache_alloc(oz_urb_link_cache, GFP_ATOMIC);
Chris Kellyae926052012-02-20 21:11:53 +0000263}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100264
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100265/*
Chris Kellyae926052012-02-20 21:11:53 +0000266 * Context: any
267 */
268static void oz_free_urb_link(struct oz_urb_link *urbl)
269{
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +0200270 if (!urbl)
271 return;
James A Shacklefordba910802014-05-21 14:50:26 -0400272
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +0200273 kmem_cache_free(oz_urb_link_cache, urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000274}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100275
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100276/*
Chris Kellyae926052012-02-20 21:11:53 +0000277 * Allocates endpoint structure and optionally a buffer. If a buffer is
278 * allocated it immediately follows the endpoint structure.
279 * Context: softirq
280 */
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +0100281static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
Chris Kellyae926052012-02-20 21:11:53 +0000282{
283 struct oz_endpoint *ep =
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800284 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
Chris Kellyae926052012-02-20 21:11:53 +0000285 if (ep) {
Chris Kellyae926052012-02-20 21:11:53 +0000286 INIT_LIST_HEAD(&ep->urb_list);
287 INIT_LIST_HEAD(&ep->link);
288 ep->credit = -1;
289 if (buffer_size) {
290 ep->buffer_size = buffer_size;
291 ep->buffer = (u8 *)(ep+1);
292 }
293 }
294 return ep;
295}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100296
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100297/*
Chris Kellyae926052012-02-20 21:11:53 +0000298 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
299 * disabled.
300 * Context: softirq or process
301 */
Jérôme Pinot46374d32014-03-13 10:17:17 +0900302static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
303 struct urb *urb)
Chris Kellyae926052012-02-20 21:11:53 +0000304{
305 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100306
Christoph Jaegera87c3802014-08-04 14:54:56 +0200307 list_for_each_entry(urbl, &ozhcd->urb_cancel_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +0000308 if (urb == urbl->urb) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200309 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +0000310 return urbl;
311 }
312 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100313 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000314}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100315
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100316/*
Chris Kellyae926052012-02-20 21:11:53 +0000317 * This is called when we have finished processing an urb. It unlinks it from
318 * the ep and returns it to the core.
319 * Context: softirq or process
320 */
321static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100322 int status)
Chris Kellyae926052012-02-20 21:11:53 +0000323{
324 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
325 unsigned long irq_state;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100326 struct oz_urb_link *cancel_urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100327
Chris Kellyae926052012-02-20 21:11:53 +0000328 spin_lock_irqsave(&g_tasklet_lock, irq_state);
329 usb_hcd_unlink_urb_from_ep(hcd, urb);
330 /* Clear hcpriv which will prevent it being put in the cancel list
331 * in the event that an attempt is made to cancel it.
332 */
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100333 urb->hcpriv = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000334 /* Walk the cancel list in case the urb is already sitting there.
335 * Since we process the cancel list in a tasklet rather than in
336 * the dequeue function this could happen.
337 */
338 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
339 /* Note: we release lock but do not enable local irqs.
340 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
341 * or at least other host controllers disable interrupts at this point
342 * so we do the same. We must, however, release the lock otherwise a
343 * deadlock will occur if an urb is submitted to our driver in the urb
344 * completion function. Because we disable interrupts it is possible
345 * that the urb_enqueue function can be called with them disabled.
346 */
347 spin_unlock(&g_tasklet_lock);
348 if (oz_forget_urb(urb)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100349 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +0000350 } else {
Chris Kellyae926052012-02-20 21:11:53 +0000351 atomic_dec(&g_pending_urbs);
Chris Kellyae926052012-02-20 21:11:53 +0000352 usb_hcd_giveback_urb(hcd, urb, status);
353 }
354 spin_lock(&g_tasklet_lock);
355 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
Markus Elfring968bf0c2014-11-25 16:51:08 +0100356 oz_free_urb_link(cancel_urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000357}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100358
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100359/*
Chris Kellyae926052012-02-20 21:11:53 +0000360 * Deallocates an endpoint including deallocating any associated stream and
361 * returning any queued urbs to the core.
362 * Context: softirq
363 */
364static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
365{
Chris Kellyae926052012-02-20 21:11:53 +0000366 if (port) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200367 LIST_HEAD(list);
Chris Kellyae926052012-02-20 21:11:53 +0000368 struct oz_hcd *ozhcd = port->ozhcd;
James A Shacklefordba910802014-05-21 14:50:26 -0400369
Chris Kellyae926052012-02-20 21:11:53 +0000370 if (ep->flags & OZ_F_EP_HAVE_STREAM)
371 oz_usb_stream_delete(port->hpd, ep->ep_num);
372 /* Transfer URBs to the orphanage while we hold the lock. */
373 spin_lock_bh(&ozhcd->hcd_lock);
374 /* Note: this works even if ep->urb_list is empty.*/
375 list_replace_init(&ep->urb_list, &list);
376 /* Put the URBs in the orphanage. */
377 list_splice_tail(&list, &ozhcd->orphanage);
378 spin_unlock_bh(&ozhcd->hcd_lock);
379 }
Joe Perchesf724b582013-07-23 13:45:00 +0100380 oz_dbg(ON, "Freeing endpoint memory\n");
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800381 kfree(ep);
Chris Kellyae926052012-02-20 21:11:53 +0000382}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100383
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100384/*
Chris Kellyae926052012-02-20 21:11:53 +0000385 * Context: softirq
386 */
Peter Huewea7f74c32013-02-15 21:17:22 +0100387static void oz_complete_buffered_urb(struct oz_port *port,
388 struct oz_endpoint *ep,
Rupesh Gujare28a72292012-07-23 18:49:43 +0100389 struct urb *urb)
390{
Rupesh Gujare4882ad952013-08-23 18:33:31 +0100391 int data_len, available_space, copy_len;
Rupesh Gujare28a72292012-07-23 18:49:43 +0100392
Rupesh Gujare4882ad952013-08-23 18:33:31 +0100393 data_len = ep->buffer[ep->out_ix];
Rupesh Gujare28a72292012-07-23 18:49:43 +0100394 if (data_len <= urb->transfer_buffer_length)
395 available_space = data_len;
396 else
397 available_space = urb->transfer_buffer_length;
398
399 if (++ep->out_ix == ep->buffer_size)
400 ep->out_ix = 0;
401 copy_len = ep->buffer_size - ep->out_ix;
402 if (copy_len >= available_space)
403 copy_len = available_space;
404 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
405
406 if (copy_len < available_space) {
407 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
408 (available_space - copy_len));
409 ep->out_ix = available_space - copy_len;
410 } else {
411 ep->out_ix += copy_len;
412 }
413 urb->actual_length = available_space;
414 if (ep->out_ix == ep->buffer_size)
415 ep->out_ix = 0;
416
417 ep->buffered_units--;
Joe Perchesf724b582013-07-23 13:45:00 +0100418 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
419 available_space);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100420 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Rupesh Gujare28a72292012-07-23 18:49:43 +0100421}
422
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100423/*
Rupesh Gujare28a72292012-07-23 18:49:43 +0100424 * Context: softirq
425 */
Chris Kellyae926052012-02-20 21:11:53 +0000426static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
427 struct urb *urb, u8 req_id)
428{
429 struct oz_urb_link *urbl;
Rupesh Gujare2c663352013-08-13 18:24:23 +0100430 struct oz_endpoint *ep = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000431 int err = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100432
Chris Kellyae926052012-02-20 21:11:53 +0000433 if (ep_addr >= OZ_NB_ENDPOINTS) {
Joe Perchesf724b582013-07-23 13:45:00 +0100434 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
Chris Kellyae926052012-02-20 21:11:53 +0000435 return -EINVAL;
436 }
437 urbl = oz_alloc_urb_link();
438 if (!urbl)
439 return -ENOMEM;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100440 urbl->submit_counter = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000441 urbl->urb = urb;
442 urbl->req_id = req_id;
443 urbl->ep_num = ep_addr;
444 /* Hold lock while we insert the URB into the list within the
445 * endpoint structure.
446 */
447 spin_lock_bh(&port->ozhcd->hcd_lock);
448 /* If the urb has been unlinked while out of any list then
449 * complete it now.
450 */
451 if (urb->unlinked) {
452 spin_unlock_bh(&port->ozhcd->hcd_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100453 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100454 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000455 oz_free_urb_link(urbl);
456 return 0;
457 }
Rupesh Gujare2c663352013-08-13 18:24:23 +0100458
459 if (in_dir)
Chris Kellyae926052012-02-20 21:11:53 +0000460 ep = port->in_ep[ep_addr];
Rupesh Gujare2c663352013-08-13 18:24:23 +0100461 else
Chris Kellyae926052012-02-20 21:11:53 +0000462 ep = port->out_ep[ep_addr];
Rupesh Gujare2c663352013-08-13 18:24:23 +0100463 if (!ep) {
Rupesh Gujarebc9aece2013-08-05 18:40:12 +0100464 err = -ENOMEM;
465 goto out;
466 }
Rupesh Gujare28a72292012-07-23 18:49:43 +0100467
468 /*For interrupt endpoint check for buffered data
469 * & complete urb
470 */
471 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
472 && ep->buffered_units > 0) {
473 oz_free_urb_link(urbl);
474 spin_unlock_bh(&port->ozhcd->hcd_lock);
475 oz_complete_buffered_urb(port, ep, urb);
476 return 0;
477 }
478
Rupesh Gujarec45905a2013-08-13 18:29:21 +0100479 if (port->hpd) {
Chris Kellyae926052012-02-20 21:11:53 +0000480 list_add_tail(&urbl->link, &ep->urb_list);
481 if (!in_dir && ep_addr && (ep->credit < 0)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100482 getrawmonotonic(&ep->timestamp);
Chris Kellyae926052012-02-20 21:11:53 +0000483 ep->credit = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000484 }
485 } else {
486 err = -EPIPE;
487 }
Rupesh Gujarebc9aece2013-08-05 18:40:12 +0100488out:
Chris Kellyae926052012-02-20 21:11:53 +0000489 spin_unlock_bh(&port->ozhcd->hcd_lock);
490 if (err)
491 oz_free_urb_link(urbl);
492 return err;
493}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100494
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100495/*
Chris Kellyae926052012-02-20 21:11:53 +0000496 * Removes an urb from the queue in the endpoint.
497 * Returns 0 if it is found and -EIDRM otherwise.
498 * Context: softirq
499 */
500static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
501 struct urb *urb)
502{
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100503 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000504 struct oz_endpoint *ep;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100505
Chris Kellyae926052012-02-20 21:11:53 +0000506 spin_lock_bh(&port->ozhcd->hcd_lock);
507 if (in_dir)
508 ep = port->in_ep[ep_addr];
509 else
510 ep = port->out_ep[ep_addr];
511 if (ep) {
512 struct list_head *e;
James A Shacklefordba910802014-05-21 14:50:26 -0400513
Chris Kellyae926052012-02-20 21:11:53 +0000514 list_for_each(e, &ep->urb_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200515 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +0000516 if (urbl->urb == urb) {
517 list_del_init(e);
518 break;
519 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100520 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000521 }
522 }
523 spin_unlock_bh(&port->ozhcd->hcd_lock);
Markus Elfring968bf0c2014-11-25 16:51:08 +0100524 oz_free_urb_link(urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000525 return urbl ? 0 : -EIDRM;
526}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100527
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100528/*
Chris Kellyae926052012-02-20 21:11:53 +0000529 * Finds an urb given its request id.
530 * Context: softirq
531 */
532static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
533 u8 req_id)
534{
535 struct oz_hcd *ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100536 struct urb *urb = NULL;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100537 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +0000538 struct oz_endpoint *ep;
539
540 spin_lock_bh(&ozhcd->hcd_lock);
541 ep = port->out_ep[ep_ix];
542 if (ep) {
543 struct list_head *e;
James A Shacklefordba910802014-05-21 14:50:26 -0400544
Chris Kellyae926052012-02-20 21:11:53 +0000545 list_for_each(e, &ep->urb_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200546 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +0000547 if (urbl->req_id == req_id) {
548 urb = urbl->urb;
549 list_del_init(e);
550 break;
551 }
552 }
553 }
554 spin_unlock_bh(&ozhcd->hcd_lock);
555 /* If urb is non-zero then we we must have an urb link to delete.
556 */
557 if (urb)
558 oz_free_urb_link(urbl);
559 return urb;
560}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100561
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100562/*
Chris Kellyae926052012-02-20 21:11:53 +0000563 * Pre-condition: Port lock must be held.
564 * Context: softirq
565 */
566static void oz_acquire_port(struct oz_port *port, void *hpd)
567{
568 INIT_LIST_HEAD(&port->isoc_out_ep);
569 INIT_LIST_HEAD(&port->isoc_in_ep);
570 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
571 port->status |= USB_PORT_STAT_CONNECTION |
572 (USB_PORT_STAT_C_CONNECTION << 16);
573 oz_usb_get(hpd);
574 port->hpd = hpd;
575}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100576
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100577/*
Chris Kellyae926052012-02-20 21:11:53 +0000578 * Context: softirq
579 */
580static struct oz_hcd *oz_hcd_claim(void)
581{
582 struct oz_hcd *ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100583
Chris Kellyae926052012-02-20 21:11:53 +0000584 spin_lock_bh(&g_hcdlock);
585 ozhcd = g_ozhcd;
586 if (ozhcd)
587 usb_get_hcd(ozhcd->hcd);
588 spin_unlock_bh(&g_hcdlock);
589 return ozhcd;
590}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100591
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100592/*
Chris Kellyae926052012-02-20 21:11:53 +0000593 * Context: softirq
594 */
595static inline void oz_hcd_put(struct oz_hcd *ozhcd)
596{
597 if (ozhcd)
598 usb_put_hcd(ozhcd->hcd);
599}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100600
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100601/*
Chris Kellyae926052012-02-20 21:11:53 +0000602 * This is called by the protocol handler to notify that a PD has arrived.
603 * We allocate a port to associate with the PD and create a structure for
604 * endpoint 0. This port is made the connection port.
605 * In the event that one of the other port is already a connection port then
606 * we fail.
607 * TODO We should be able to do better than fail and should be able remember
608 * that this port needs configuring and make it the connection port once the
609 * current connection port has been assigned an address. Collisions here are
610 * probably very rare indeed.
611 * Context: softirq
612 */
Rupesh Gujare0503d202013-08-13 18:29:22 +0100613struct oz_port *oz_hcd_pd_arrived(void *hpd)
Chris Kellyae926052012-02-20 21:11:53 +0000614{
615 int i;
Rupesh Gujare3bc0d882013-08-15 12:00:21 +0100616 struct oz_port *hport;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100617 struct oz_hcd *ozhcd;
Chris Kellyae926052012-02-20 21:11:53 +0000618 struct oz_endpoint *ep;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100619
Chris Kellyae926052012-02-20 21:11:53 +0000620 ozhcd = oz_hcd_claim();
Dan Carpenter95b20b82013-08-13 18:29:26 +0100621 if (!ozhcd)
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100622 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000623 /* Allocate an endpoint object in advance (before holding hcd lock) to
624 * use for out endpoint 0.
625 */
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +0100626 ep = oz_ep_alloc(0, GFP_ATOMIC);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100627 if (!ep)
628 goto err_put;
629
Chris Kellyae926052012-02-20 21:11:53 +0000630 spin_lock_bh(&ozhcd->hcd_lock);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100631 if (ozhcd->conn_port >= 0)
632 goto err_unlock;
633
Chris Kellyae926052012-02-20 21:11:53 +0000634 for (i = 0; i < OZ_NB_PORTS; i++) {
635 struct oz_port *port = &ozhcd->ports[i];
Dan Carpenter95b20b82013-08-13 18:29:26 +0100636
Chris Kellyae926052012-02-20 21:11:53 +0000637 spin_lock(&port->port_lock);
Rupesh Gujare0a7bfbf2013-08-22 17:38:48 +0100638 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
Chris Kellyae926052012-02-20 21:11:53 +0000639 oz_acquire_port(port, hpd);
640 spin_unlock(&port->port_lock);
641 break;
642 }
643 spin_unlock(&port->port_lock);
644 }
Dan Carpenter95b20b82013-08-13 18:29:26 +0100645 if (i == OZ_NB_PORTS)
646 goto err_unlock;
647
648 ozhcd->conn_port = i;
649 hport = &ozhcd->ports[i];
650 hport->out_ep[0] = ep;
651 spin_unlock_bh(&ozhcd->hcd_lock);
652 if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
653 usb_hcd_resume_root_hub(ozhcd->hcd);
654 usb_hcd_poll_rh_status(ozhcd->hcd);
Chris Kellyae926052012-02-20 21:11:53 +0000655 oz_hcd_put(ozhcd);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100656
Chris Kellyae926052012-02-20 21:11:53 +0000657 return hport;
Dan Carpenter95b20b82013-08-13 18:29:26 +0100658
659err_unlock:
660 spin_unlock_bh(&ozhcd->hcd_lock);
661 oz_ep_free(NULL, ep);
662err_put:
663 oz_hcd_put(ozhcd);
664 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000665}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100666
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100667/*
Chris Kellyae926052012-02-20 21:11:53 +0000668 * This is called by the protocol handler to notify that the PD has gone away.
669 * We need to deallocate all resources and then request that the root hub is
670 * polled. We release the reference we hold on the PD.
671 * Context: softirq
672 */
Rupesh Gujare83db51f2013-08-15 12:00:22 +0100673void oz_hcd_pd_departed(struct oz_port *port)
Chris Kellyae926052012-02-20 21:11:53 +0000674{
Chris Kellyae926052012-02-20 21:11:53 +0000675 struct oz_hcd *ozhcd;
676 void *hpd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100677 struct oz_endpoint *ep = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000678
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100679 if (port == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100680 oz_dbg(ON, "%s: port = 0\n", __func__);
Chris Kellyae926052012-02-20 21:11:53 +0000681 return;
682 }
683 ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100684 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +0000685 return;
686 /* Check if this is the connection port - if so clear it.
687 */
688 spin_lock_bh(&ozhcd->hcd_lock);
689 if ((ozhcd->conn_port >= 0) &&
690 (port == &ozhcd->ports[ozhcd->conn_port])) {
Joe Perchesf724b582013-07-23 13:45:00 +0100691 oz_dbg(ON, "Clearing conn_port\n");
Chris Kellyae926052012-02-20 21:11:53 +0000692 ozhcd->conn_port = -1;
693 }
694 spin_lock(&port->port_lock);
695 port->flags |= OZ_PORT_F_DYING;
696 spin_unlock(&port->port_lock);
697 spin_unlock_bh(&ozhcd->hcd_lock);
698
699 oz_clean_endpoints_for_config(ozhcd->hcd, port);
700 spin_lock_bh(&port->port_lock);
701 hpd = port->hpd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100702 port->hpd = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000703 port->bus_addr = 0xff;
Rupesh Gujared7729832013-08-05 18:40:14 +0100704 port->config_num = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000705 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
706 port->flags |= OZ_PORT_F_CHANGED;
Rupesh Gujare68501432013-08-27 16:53:41 +0100707 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
Chris Kellyae926052012-02-20 21:11:53 +0000708 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
709 /* If there is an endpont 0 then clear the pointer while we hold
710 * the spinlock be we deallocate it after releasing the lock.
711 */
712 if (port->out_ep[0]) {
713 ep = port->out_ep[0];
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100714 port->out_ep[0] = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000715 }
716 spin_unlock_bh(&port->port_lock);
717 if (ep)
718 oz_ep_free(port, ep);
719 usb_hcd_poll_rh_status(ozhcd->hcd);
720 oz_usb_put(hpd);
721}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100722
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100723/*
Chris Kellyae926052012-02-20 21:11:53 +0000724 * Context: softirq
725 */
726void oz_hcd_pd_reset(void *hpd, void *hport)
727{
728 /* Cleanup the current configuration and report reset to the core.
729 */
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +0530730 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +0000731 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100732
Joe Perchesf724b582013-07-23 13:45:00 +0100733 oz_dbg(ON, "PD Reset\n");
Chris Kellyae926052012-02-20 21:11:53 +0000734 spin_lock_bh(&port->port_lock);
735 port->flags |= OZ_PORT_F_CHANGED;
736 port->status |= USB_PORT_STAT_RESET;
737 port->status |= (USB_PORT_STAT_C_RESET << 16);
738 spin_unlock_bh(&port->port_lock);
739 oz_clean_endpoints_for_config(ozhcd->hcd, port);
740 usb_hcd_poll_rh_status(ozhcd->hcd);
741}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100742
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100743/*
Chris Kellyae926052012-02-20 21:11:53 +0000744 * Context: softirq
745 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100746void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
Chris Kellyae926052012-02-20 21:11:53 +0000747 int length, int offset, int total_size)
748{
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +0530749 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +0000750 struct urb *urb;
751 int err = 0;
752
Joe Perchesf724b582013-07-23 13:45:00 +0100753 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
754 length, offset, total_size);
Chris Kellyae926052012-02-20 21:11:53 +0000755 urb = oz_find_urb_by_id(port, 0, req_id);
756 if (!urb)
757 return;
758 if (status == 0) {
759 int copy_len;
760 int required_size = urb->transfer_buffer_length;
James A Shacklefordba910802014-05-21 14:50:26 -0400761
Chris Kellyae926052012-02-20 21:11:53 +0000762 if (required_size > total_size)
763 required_size = total_size;
764 copy_len = required_size-offset;
765 if (length <= copy_len)
766 copy_len = length;
767 memcpy(urb->transfer_buffer+offset, desc, copy_len);
768 offset += copy_len;
769 if (offset < required_size) {
770 struct usb_ctrlrequest *setup =
771 (struct usb_ctrlrequest *)urb->setup_packet;
772 unsigned wvalue = le16_to_cpu(setup->wValue);
James A Shacklefordba910802014-05-21 14:50:26 -0400773
Chris Kellyae926052012-02-20 21:11:53 +0000774 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
775 err = -ENOMEM;
776 else if (oz_usb_get_desc_req(port->hpd, req_id,
777 setup->bRequestType, (u8)(wvalue>>8),
778 (u8)wvalue, setup->wIndex, offset,
779 required_size-offset)) {
780 oz_dequeue_ep_urb(port, 0, 0, urb);
781 err = -ENOMEM;
782 }
783 if (err == 0)
784 return;
785 }
786 }
787 urb->actual_length = total_size;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100788 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000789}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100790
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100791/*
Chris Kellyae926052012-02-20 21:11:53 +0000792 * Context: softirq
793 */
Chris Kellyae926052012-02-20 21:11:53 +0000794static void oz_display_conf_type(u8 t)
795{
796 switch (t) {
797 case USB_REQ_GET_STATUS:
Joe Perchesf724b582013-07-23 13:45:00 +0100798 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000799 break;
800 case USB_REQ_CLEAR_FEATURE:
Joe Perchesf724b582013-07-23 13:45:00 +0100801 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000802 break;
803 case USB_REQ_SET_FEATURE:
Joe Perchesf724b582013-07-23 13:45:00 +0100804 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000805 break;
806 case USB_REQ_SET_ADDRESS:
Joe Perchesf724b582013-07-23 13:45:00 +0100807 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000808 break;
809 case USB_REQ_GET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +0100810 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000811 break;
812 case USB_REQ_SET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +0100813 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000814 break;
815 case USB_REQ_GET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +0100816 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000817 break;
818 case USB_REQ_SET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +0100819 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000820 break;
821 case USB_REQ_GET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +0100822 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000823 break;
824 case USB_REQ_SET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +0100825 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000826 break;
827 case USB_REQ_SYNCH_FRAME:
Joe Perchesf724b582013-07-23 13:45:00 +0100828 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000829 break;
830 }
831}
Joe Perches05f608f2013-07-23 13:45:01 +0100832
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100833/*
Chris Kellyae926052012-02-20 21:11:53 +0000834 * Context: softirq
835 */
836static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
837 u8 rcode, u8 config_num)
838{
839 int rc = 0;
840 struct usb_hcd *hcd = port->ozhcd->hcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100841
Chris Kellyae926052012-02-20 21:11:53 +0000842 if (rcode == 0) {
843 port->config_num = config_num;
844 oz_clean_endpoints_for_config(hcd, port);
845 if (oz_build_endpoints_for_config(hcd, port,
846 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
847 rc = -ENOMEM;
848 }
849 } else {
850 rc = -ENOMEM;
851 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100852 oz_complete_urb(hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +0000853}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100854
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100855/*
Chris Kellyae926052012-02-20 21:11:53 +0000856 * Context: softirq
857 */
858static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
859 u8 rcode, u8 if_num, u8 alt)
860{
861 struct usb_hcd *hcd = port->ozhcd->hcd;
862 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100863
Rupesh Gujare050596a2013-08-23 18:33:28 +0100864 if ((rcode == 0) && (port->config_num > 0)) {
Chris Kellyae926052012-02-20 21:11:53 +0000865 struct usb_host_config *config;
866 struct usb_host_interface *intf;
James A Shacklefordba910802014-05-21 14:50:26 -0400867
Joe Perchesf724b582013-07-23 13:45:00 +0100868 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
Chris Kellyae926052012-02-20 21:11:53 +0000869 oz_clean_endpoints_for_interface(hcd, port, if_num);
870 config = &urb->dev->config[port->config_num-1];
871 intf = &config->intf_cache[if_num]->altsetting[alt];
872 if (oz_build_endpoints_for_interface(hcd, port, intf,
873 GFP_ATOMIC))
874 rc = -ENOMEM;
875 else
876 port->iface[if_num].alt = alt;
877 } else {
878 rc = -ENOMEM;
879 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100880 oz_complete_urb(hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +0000881}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100882
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100883/*
Chris Kellyae926052012-02-20 21:11:53 +0000884 * Context: softirq
885 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100886void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
Chris Kellyae926052012-02-20 21:11:53 +0000887 int data_len)
888{
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +0530889 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +0000890 struct urb *urb;
891 struct usb_ctrlrequest *setup;
892 struct usb_hcd *hcd = port->ozhcd->hcd;
893 unsigned windex;
894 unsigned wvalue;
895
Joe Perchesf724b582013-07-23 13:45:00 +0100896 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
Chris Kellyae926052012-02-20 21:11:53 +0000897 urb = oz_find_urb_by_id(port, 0, req_id);
898 if (!urb) {
Joe Perchesf724b582013-07-23 13:45:00 +0100899 oz_dbg(ON, "URB not found\n");
Chris Kellyae926052012-02-20 21:11:53 +0000900 return;
901 }
902 setup = (struct usb_ctrlrequest *)urb->setup_packet;
903 windex = le16_to_cpu(setup->wIndex);
904 wvalue = le16_to_cpu(setup->wValue);
905 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
906 /* Standard requests */
907 oz_display_conf_type(setup->bRequest);
908 switch (setup->bRequest) {
909 case USB_REQ_SET_CONFIGURATION:
910 oz_hcd_complete_set_config(port, urb, rcode,
911 (u8)wvalue);
912 break;
913 case USB_REQ_SET_INTERFACE:
914 oz_hcd_complete_set_interface(port, urb, rcode,
915 (u8)windex, (u8)wvalue);
916 break;
917 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100918 oz_complete_urb(hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000919 }
920
921 } else {
922 int copy_len;
James A Shacklefordba910802014-05-21 14:50:26 -0400923
Joe Perchesf724b582013-07-23 13:45:00 +0100924 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
Rupesh Gujare5494ebd2012-07-23 18:49:45 +0100925 if (data_len) {
926 if (data_len <= urb->transfer_buffer_length)
927 copy_len = data_len;
928 else
929 copy_len = urb->transfer_buffer_length;
Chris Kellyae926052012-02-20 21:11:53 +0000930 memcpy(urb->transfer_buffer, data, copy_len);
Rupesh Gujare5494ebd2012-07-23 18:49:45 +0100931 urb->actual_length = copy_len;
932 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100933 oz_complete_urb(hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000934 }
935}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100936
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100937/*
Chris Kellyae926052012-02-20 21:11:53 +0000938 * Context: softirq-serialized
939 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100940static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
941 int data_len)
Chris Kellyae926052012-02-20 21:11:53 +0000942{
943 int space;
944 int copy_len;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100945
Chris Kellyae926052012-02-20 21:11:53 +0000946 if (!ep->buffer)
947 return -1;
948 space = ep->out_ix-ep->in_ix-1;
949 if (space < 0)
950 space += ep->buffer_size;
951 if (space < (data_len+1)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100952 oz_dbg(ON, "Buffer full\n");
Chris Kellyae926052012-02-20 21:11:53 +0000953 return -1;
954 }
955 ep->buffer[ep->in_ix] = (u8)data_len;
956 if (++ep->in_ix == ep->buffer_size)
957 ep->in_ix = 0;
958 copy_len = ep->buffer_size - ep->in_ix;
959 if (copy_len > data_len)
960 copy_len = data_len;
961 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
962
963 if (copy_len < data_len) {
964 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
965 ep->in_ix = data_len-copy_len;
966 } else {
967 ep->in_ix += copy_len;
968 }
969 if (ep->in_ix == ep->buffer_size)
970 ep->in_ix = 0;
971 ep->buffered_units++;
972 return 0;
973}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100974
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100975/*
Chris Kellyae926052012-02-20 21:11:53 +0000976 * Context: softirq-serialized
977 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100978void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
Chris Kellyae926052012-02-20 21:11:53 +0000979{
980 struct oz_port *port = (struct oz_port *)hport;
981 struct oz_endpoint *ep;
982 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100983
Chris Kellyae926052012-02-20 21:11:53 +0000984 spin_lock_bh(&ozhcd->hcd_lock);
985 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100986 if (ep == NULL)
Chris Kellyae926052012-02-20 21:11:53 +0000987 goto done;
988 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
989 case USB_ENDPOINT_XFER_INT:
990 case USB_ENDPOINT_XFER_BULK:
991 if (!list_empty(&ep->urb_list)) {
992 struct oz_urb_link *urbl =
993 list_first_entry(&ep->urb_list,
994 struct oz_urb_link, link);
995 struct urb *urb;
996 int copy_len;
James A Shacklefordba910802014-05-21 14:50:26 -0400997
Chris Kellyae926052012-02-20 21:11:53 +0000998 list_del_init(&urbl->link);
999 spin_unlock_bh(&ozhcd->hcd_lock);
1000 urb = urbl->urb;
1001 oz_free_urb_link(urbl);
1002 if (data_len <= urb->transfer_buffer_length)
1003 copy_len = data_len;
1004 else
1005 copy_len = urb->transfer_buffer_length;
1006 memcpy(urb->transfer_buffer, data, copy_len);
1007 urb->actual_length = copy_len;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001008 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001009 return;
1010 }
Adrian Nicoarad75b6c62014-09-08 14:41:48 -04001011 oz_dbg(ON, "buffering frame as URB is not available\n");
1012 oz_hcd_buffer_data(ep, data, data_len);
Chris Kellyae926052012-02-20 21:11:53 +00001013 break;
1014 case USB_ENDPOINT_XFER_ISOC:
1015 oz_hcd_buffer_data(ep, data, data_len);
1016 break;
1017 }
1018done:
1019 spin_unlock_bh(&ozhcd->hcd_lock);
1020}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001021
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001022/*
Chris Kellyae926052012-02-20 21:11:53 +00001023 * Context: unknown
1024 */
1025static inline int oz_usb_get_frame_number(void)
1026{
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001027 return atomic_inc_return(&g_usb_frame_number);
Chris Kellyae926052012-02-20 21:11:53 +00001028}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001029
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001030/*
Chris Kellyae926052012-02-20 21:11:53 +00001031 * Context: softirq
1032 */
1033int oz_hcd_heartbeat(void *hport)
1034{
1035 int rc = 0;
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +05301036 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +00001037 struct oz_hcd *ozhcd = port->ozhcd;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001038 struct oz_urb_link *urbl, *n;
1039 LIST_HEAD(xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001040 struct urb *urb;
1041 struct oz_endpoint *ep;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001042 struct timespec ts, delta;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001043
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001044 getrawmonotonic(&ts);
Chris Kellyae926052012-02-20 21:11:53 +00001045 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1046 */
1047 spin_lock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001048 list_for_each_entry(ep, &port->isoc_out_ep, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001049 if (ep->credit < 0)
1050 continue;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001051 delta = timespec_sub(ts, ep->timestamp);
Rupesh Gujare5cf9d252013-08-05 12:14:04 +01001052 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
Chris Kellyae926052012-02-20 21:11:53 +00001053 if (ep->credit > ep->credit_ceiling)
1054 ep->credit = ep->credit_ceiling;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001055 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001056 while (ep->credit && !list_empty(&ep->urb_list)) {
1057 urbl = list_first_entry(&ep->urb_list,
1058 struct oz_urb_link, link);
1059 urb = urbl->urb;
Rupesh Gujare24168912012-07-23 18:49:44 +01001060 if ((ep->credit + 1) < urb->number_of_packets)
Chris Kellyae926052012-02-20 21:11:53 +00001061 break;
1062 ep->credit -= urb->number_of_packets;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001063 if (ep->credit < 0)
1064 ep->credit = 0;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001065 list_move_tail(&urbl->link, &xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001066 }
1067 }
1068 spin_unlock_bh(&ozhcd->hcd_lock);
1069 /* Send to PD and complete URBs.
1070 */
Christoph Jaegera87c3802014-08-04 14:54:56 +02001071 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001072 urb = urbl->urb;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001073 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +00001074 urb->error_count = 0;
1075 urb->start_frame = oz_usb_get_frame_number();
1076 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1077 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001078 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001079 }
1080 /* Check the IN isoc endpoints to see if any URBs can be completed.
1081 */
1082 spin_lock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001083 list_for_each_entry(ep, &port->isoc_in_ep, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001084 if (ep->flags & OZ_F_EP_BUFFERING) {
Rupesh Gujareb360cb92013-01-29 17:00:55 +00001085 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
Chris Kellyae926052012-02-20 21:11:53 +00001086 ep->flags &= ~OZ_F_EP_BUFFERING;
1087 ep->credit = 0;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001088 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001089 ep->start_frame = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001090 }
1091 continue;
1092 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001093 delta = timespec_sub(ts, ep->timestamp);
Rupesh Gujare5cf9d252013-08-05 12:14:04 +01001094 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001095 ep->timestamp = ts;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001096 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001097 struct urb *urb = urbl->urb;
1098 int len = 0;
1099 int copy_len;
1100 int i;
James A Shacklefordba910802014-05-21 14:50:26 -04001101
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001102 if (ep->credit < urb->number_of_packets)
Chris Kellyae926052012-02-20 21:11:53 +00001103 break;
1104 if (ep->buffered_units < urb->number_of_packets)
1105 break;
1106 urb->actual_length = 0;
1107 for (i = 0; i < urb->number_of_packets; i++) {
1108 len = ep->buffer[ep->out_ix];
1109 if (++ep->out_ix == ep->buffer_size)
1110 ep->out_ix = 0;
1111 copy_len = ep->buffer_size - ep->out_ix;
1112 if (copy_len > len)
1113 copy_len = len;
1114 memcpy(urb->transfer_buffer,
1115 &ep->buffer[ep->out_ix], copy_len);
1116 if (copy_len < len) {
1117 memcpy(urb->transfer_buffer+copy_len,
1118 ep->buffer, len-copy_len);
1119 ep->out_ix = len-copy_len;
1120 } else
1121 ep->out_ix += copy_len;
1122 if (ep->out_ix == ep->buffer_size)
1123 ep->out_ix = 0;
1124 urb->iso_frame_desc[i].offset =
1125 urb->actual_length;
1126 urb->actual_length += len;
1127 urb->iso_frame_desc[i].actual_length = len;
1128 urb->iso_frame_desc[i].status = 0;
1129 }
1130 ep->buffered_units -= urb->number_of_packets;
1131 urb->error_count = 0;
1132 urb->start_frame = ep->start_frame;
1133 ep->start_frame += urb->number_of_packets;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001134 list_move_tail(&urbl->link, &xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001135 ep->credit -= urb->number_of_packets;
Chris Kellyae926052012-02-20 21:11:53 +00001136 }
1137 }
1138 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1139 rc = 1;
1140 spin_unlock_bh(&ozhcd->hcd_lock);
1141 /* Complete the filled URBs.
1142 */
Christoph Jaegera87c3802014-08-04 14:54:56 +02001143 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001144 urb = urbl->urb;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001145 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +00001146 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001147 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001148 }
1149 /* Check if there are any ep0 requests that have timed out.
1150 * If so resent to PD.
1151 */
1152 ep = port->out_ep[0];
1153 if (ep) {
Chris Kellyae926052012-02-20 21:11:53 +00001154 spin_lock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001155 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001156 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1157 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001158 list_move_tail(&urbl->link, &xfr_list);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001159 urbl->submit_counter = 0;
1160 } else {
1161 urbl->submit_counter++;
Chris Kellyae926052012-02-20 21:11:53 +00001162 }
1163 }
1164 if (!list_empty(&ep->urb_list))
1165 rc = 1;
1166 spin_unlock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001167 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
Joe Perchesf724b582013-07-23 13:45:00 +01001168 oz_dbg(ON, "Resending request to PD\n");
Chris Kellyae926052012-02-20 21:11:53 +00001169 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1170 oz_free_urb_link(urbl);
1171 }
1172 }
1173 return rc;
1174}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001175
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001176/*
Chris Kellyae926052012-02-20 21:11:53 +00001177 * Context: softirq
1178 */
1179static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1180 struct oz_port *port,
1181 struct usb_host_interface *intf, gfp_t mem_flags)
1182{
1183 struct oz_hcd *ozhcd = port->ozhcd;
1184 int i;
1185 int if_ix = intf->desc.bInterfaceNumber;
1186 int request_heartbeat = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001187
Joe Perchesf724b582013-07-23 13:45:00 +01001188 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
Rupesh Gujared236dc12013-08-22 17:38:49 +01001189 if (if_ix >= port->num_iface || port->iface == NULL)
1190 return -ENOMEM;
Chris Kellyae926052012-02-20 21:11:53 +00001191 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1192 struct usb_host_endpoint *hep = &intf->endpoint[i];
1193 u8 ep_addr = hep->desc.bEndpointAddress;
1194 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1195 struct oz_endpoint *ep;
1196 int buffer_size = 0;
1197
Joe Perchesf724b582013-07-23 13:45:00 +01001198 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
Rupesh Gujare28a72292012-07-23 18:49:43 +01001199 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1200 switch (hep->desc.bmAttributes &
1201 USB_ENDPOINT_XFERTYPE_MASK) {
1202 case USB_ENDPOINT_XFER_ISOC:
Rupesh Gujare0f750be2013-08-23 18:33:29 +01001203 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001204 break;
1205 case USB_ENDPOINT_XFER_INT:
Rupesh Gujare00d2a462013-08-23 18:33:30 +01001206 buffer_size = OZ_EP_BUFFER_SIZE_INT;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001207 break;
1208 }
Chris Kellyae926052012-02-20 21:11:53 +00001209 }
1210
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +01001211 ep = oz_ep_alloc(buffer_size, mem_flags);
Chris Kellyae926052012-02-20 21:11:53 +00001212 if (!ep) {
1213 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1214 return -ENOMEM;
1215 }
1216 ep->attrib = hep->desc.bmAttributes;
1217 ep->ep_num = ep_num;
1218 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1219 == USB_ENDPOINT_XFER_ISOC) {
Joe Perchesf724b582013-07-23 13:45:00 +01001220 oz_dbg(ON, "wMaxPacketSize = %d\n",
1221 usb_endpoint_maxp(&hep->desc));
Chris Kellyae926052012-02-20 21:11:53 +00001222 ep->credit_ceiling = 200;
1223 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1224 ep->flags |= OZ_F_EP_BUFFERING;
Chris Kellyae926052012-02-20 21:11:53 +00001225 } else {
1226 ep->flags |= OZ_F_EP_HAVE_STREAM;
1227 if (oz_usb_stream_create(port->hpd, ep_num))
1228 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1229 }
1230 }
1231 spin_lock_bh(&ozhcd->hcd_lock);
1232 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1233 port->in_ep[ep_num] = ep;
1234 port->iface[if_ix].ep_mask |=
1235 (1<<(ep_num+OZ_NB_ENDPOINTS));
1236 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1237 == USB_ENDPOINT_XFER_ISOC) {
1238 list_add_tail(&ep->link, &port->isoc_in_ep);
1239 request_heartbeat = 1;
1240 }
1241 } else {
1242 port->out_ep[ep_num] = ep;
1243 port->iface[if_ix].ep_mask |= (1<<ep_num);
1244 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1245 == USB_ENDPOINT_XFER_ISOC) {
1246 list_add_tail(&ep->link, &port->isoc_out_ep);
1247 request_heartbeat = 1;
1248 }
1249 }
1250 spin_unlock_bh(&ozhcd->hcd_lock);
1251 if (request_heartbeat && port->hpd)
1252 oz_usb_request_heartbeat(port->hpd);
1253 }
1254 return 0;
1255}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001256
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001257/*
Chris Kellyae926052012-02-20 21:11:53 +00001258 * Context: softirq
1259 */
1260static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1261 struct oz_port *port, int if_ix)
1262{
1263 struct oz_hcd *ozhcd = port->ozhcd;
1264 unsigned mask;
1265 int i;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001266 LIST_HEAD(ep_list);
1267 struct oz_endpoint *ep, *n;
Chris Kellyae926052012-02-20 21:11:53 +00001268
Joe Perchesf724b582013-07-23 13:45:00 +01001269 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
Chris Kellyae926052012-02-20 21:11:53 +00001270 if (if_ix >= port->num_iface)
1271 return;
Chris Kellyae926052012-02-20 21:11:53 +00001272 spin_lock_bh(&ozhcd->hcd_lock);
1273 mask = port->iface[if_ix].ep_mask;
1274 port->iface[if_ix].ep_mask = 0;
1275 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1276 struct list_head *e;
1277 /* Gather OUT endpoints.
1278 */
1279 if ((mask & (1<<i)) && port->out_ep[i]) {
1280 e = &port->out_ep[i]->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001281 port->out_ep[i] = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001282 /* Remove from isoc list if present.
1283 */
Wei Yongjun094e74c2012-09-05 14:48:48 +08001284 list_move_tail(e, &ep_list);
Chris Kellyae926052012-02-20 21:11:53 +00001285 }
1286 /* Gather IN endpoints.
1287 */
1288 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1289 e = &port->in_ep[i]->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001290 port->in_ep[i] = NULL;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001291 list_move_tail(e, &ep_list);
Chris Kellyae926052012-02-20 21:11:53 +00001292 }
1293 }
1294 spin_unlock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001295 list_for_each_entry_safe(ep, n, &ep_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001296 list_del_init(&ep->link);
1297 oz_ep_free(port, ep);
1298 }
1299}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001300
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001301/*
Chris Kellyae926052012-02-20 21:11:53 +00001302 * Context: softirq
1303 */
1304static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1305 struct oz_port *port, struct usb_host_config *config,
1306 gfp_t mem_flags)
1307{
1308 struct oz_hcd *ozhcd = port->ozhcd;
1309 int i;
1310 int num_iface = config->desc.bNumInterfaces;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001311
Chris Kellyae926052012-02-20 21:11:53 +00001312 if (num_iface) {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -08001313 struct oz_interface *iface;
1314
Adrian Nicoarae0301d02014-09-08 15:02:49 -04001315 iface = kmalloc_array(num_iface, sizeof(struct oz_interface),
1316 mem_flags | __GFP_ZERO);
Chris Kellyae926052012-02-20 21:11:53 +00001317 if (!iface)
1318 return -ENOMEM;
1319 spin_lock_bh(&ozhcd->hcd_lock);
1320 port->iface = iface;
1321 port->num_iface = num_iface;
1322 spin_unlock_bh(&ozhcd->hcd_lock);
1323 }
1324 for (i = 0; i < num_iface; i++) {
1325 struct usb_host_interface *intf =
1326 &config->intf_cache[i]->altsetting[0];
1327 if (oz_build_endpoints_for_interface(hcd, port, intf,
1328 mem_flags))
1329 goto fail;
1330 }
1331 return 0;
1332fail:
1333 oz_clean_endpoints_for_config(hcd, port);
1334 return -1;
1335}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001336
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001337/*
Chris Kellyae926052012-02-20 21:11:53 +00001338 * Context: softirq
1339 */
1340static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1341 struct oz_port *port)
1342{
1343 struct oz_hcd *ozhcd = port->ozhcd;
1344 int i;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001345
Joe Perchesf724b582013-07-23 13:45:00 +01001346 oz_dbg(ON, "Deleting endpoints for configuration\n");
Chris Kellyae926052012-02-20 21:11:53 +00001347 for (i = 0; i < port->num_iface; i++)
1348 oz_clean_endpoints_for_interface(hcd, port, i);
1349 spin_lock_bh(&ozhcd->hcd_lock);
1350 if (port->iface) {
Joe Perchesf724b582013-07-23 13:45:00 +01001351 oz_dbg(ON, "Freeing interfaces object\n");
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -08001352 kfree(port->iface);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001353 port->iface = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001354 }
1355 port->num_iface = 0;
1356 spin_unlock_bh(&ozhcd->hcd_lock);
1357}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001358
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001359/*
Chris Kellyae926052012-02-20 21:11:53 +00001360 * Context: tasklet
1361 */
1362static void *oz_claim_hpd(struct oz_port *port)
1363{
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001364 void *hpd;
Chris Kellyae926052012-02-20 21:11:53 +00001365 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001366
Chris Kellyae926052012-02-20 21:11:53 +00001367 spin_lock_bh(&ozhcd->hcd_lock);
1368 hpd = port->hpd;
1369 if (hpd)
1370 oz_usb_get(hpd);
1371 spin_unlock_bh(&ozhcd->hcd_lock);
1372 return hpd;
1373}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001374
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001375/*
Chris Kellyae926052012-02-20 21:11:53 +00001376 * Context: tasklet
1377 */
1378static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1379 gfp_t mem_flags)
1380{
1381 struct usb_ctrlrequest *setup;
1382 unsigned windex;
1383 unsigned wvalue;
1384 unsigned wlength;
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001385 void *hpd;
Chris Kellyae926052012-02-20 21:11:53 +00001386 u8 req_id;
1387 int rc = 0;
1388 unsigned complete = 0;
1389
1390 int port_ix = -1;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001391 struct oz_port *port = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001392
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001393 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001394 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1395 if (port_ix < 0) {
1396 rc = -EPIPE;
1397 goto out;
1398 }
1399 port = &ozhcd->ports[port_ix];
1400 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1401 || (port->flags & OZ_PORT_F_DYING)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001402 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1403 port_ix, urb->dev->devnum);
Chris Kellyae926052012-02-20 21:11:53 +00001404 rc = -EPIPE;
1405 goto out;
1406 }
1407 /* Store port in private context data.
1408 */
1409 urb->hcpriv = port;
1410 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1411 windex = le16_to_cpu(setup->wIndex);
1412 wvalue = le16_to_cpu(setup->wValue);
1413 wlength = le16_to_cpu(setup->wLength);
Joe Perchesf724b582013-07-23 13:45:00 +01001414 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1415 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1416 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1417 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1418 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
Chris Kellyae926052012-02-20 21:11:53 +00001419
1420 req_id = port->next_req_id++;
1421 hpd = oz_claim_hpd(port);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001422 if (hpd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001423 oz_dbg(ON, "Cannot claim port\n");
Chris Kellyae926052012-02-20 21:11:53 +00001424 rc = -EPIPE;
1425 goto out;
1426 }
1427
1428 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1429 /* Standard requests
1430 */
1431 switch (setup->bRequest) {
1432 case USB_REQ_GET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +01001433 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001434 break;
1435 case USB_REQ_SET_ADDRESS:
Joe Perchesf724b582013-07-23 13:45:00 +01001436 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1437 oz_dbg(ON, "Port %d address is 0x%x\n",
1438 ozhcd->conn_port,
1439 (u8)le16_to_cpu(setup->wValue));
Chris Kellyae926052012-02-20 21:11:53 +00001440 spin_lock_bh(&ozhcd->hcd_lock);
1441 if (ozhcd->conn_port >= 0) {
1442 ozhcd->ports[ozhcd->conn_port].bus_addr =
1443 (u8)le16_to_cpu(setup->wValue);
Joe Perchesf724b582013-07-23 13:45:00 +01001444 oz_dbg(ON, "Clearing conn_port\n");
Chris Kellyae926052012-02-20 21:11:53 +00001445 ozhcd->conn_port = -1;
1446 }
1447 spin_unlock_bh(&ozhcd->hcd_lock);
1448 complete = 1;
1449 break;
1450 case USB_REQ_SET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +01001451 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001452 break;
1453 case USB_REQ_GET_CONFIGURATION:
Masanari Iida8dc24592012-04-25 23:28:35 +09001454 /* We short circuit this case and reply directly since
Chris Kellyae926052012-02-20 21:11:53 +00001455 * we have the selected configuration number cached.
1456 */
Joe Perchesf724b582013-07-23 13:45:00 +01001457 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
Chris Kellyae926052012-02-20 21:11:53 +00001458 if (urb->transfer_buffer_length >= 1) {
1459 urb->actual_length = 1;
1460 *((u8 *)urb->transfer_buffer) =
1461 port->config_num;
1462 complete = 1;
1463 } else {
1464 rc = -EPIPE;
1465 }
1466 break;
1467 case USB_REQ_GET_INTERFACE:
Masanari Iida8dc24592012-04-25 23:28:35 +09001468 /* We short circuit this case and reply directly since
Chris Kellyae926052012-02-20 21:11:53 +00001469 * we have the selected interface alternative cached.
1470 */
Joe Perchesf724b582013-07-23 13:45:00 +01001471 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
Chris Kellyae926052012-02-20 21:11:53 +00001472 if (urb->transfer_buffer_length >= 1) {
1473 urb->actual_length = 1;
1474 *((u8 *)urb->transfer_buffer) =
1475 port->iface[(u8)windex].alt;
Joe Perchesf724b582013-07-23 13:45:00 +01001476 oz_dbg(ON, "interface = %d alt = %d\n",
1477 windex, port->iface[(u8)windex].alt);
Chris Kellyae926052012-02-20 21:11:53 +00001478 complete = 1;
1479 } else {
1480 rc = -EPIPE;
1481 }
1482 break;
1483 case USB_REQ_SET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +01001484 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001485 break;
1486 }
1487 }
1488 if (!rc && !complete) {
1489 int data_len = 0;
James A Shacklefordba910802014-05-21 14:50:26 -04001490
Chris Kellyae926052012-02-20 21:11:53 +00001491 if ((setup->bRequestType & USB_DIR_IN) == 0)
1492 data_len = wlength;
Rupesh Gujare5494ebd2012-07-23 18:49:45 +01001493 urb->actual_length = data_len;
Chris Kellyae926052012-02-20 21:11:53 +00001494 if (oz_usb_control_req(port->hpd, req_id, setup,
1495 urb->transfer_buffer, data_len)) {
1496 rc = -ENOMEM;
1497 } else {
1498 /* Note: we are queuing the request after we have
Justin P. Mattock595914f2012-03-19 08:17:50 -07001499 * submitted it to be transmitted. If the request were
Chris Kellyae926052012-02-20 21:11:53 +00001500 * to complete before we queued it then it would not
1501 * be found in the queue. It seems impossible for
1502 * this to happen but if it did the request would
1503 * be resubmitted so the problem would hopefully
1504 * resolve itself. Putting the request into the
1505 * queue before it has been sent is worse since the
1506 * urb could be cancelled while we are using it
1507 * to build the request.
1508 */
1509 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1510 rc = -ENOMEM;
1511 }
1512 }
1513 oz_usb_put(hpd);
1514out:
1515 if (rc || complete) {
Joe Perchesf724b582013-07-23 13:45:00 +01001516 oz_dbg(ON, "Completing request locally\n");
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001517 oz_complete_urb(ozhcd->hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +00001518 } else {
1519 oz_usb_request_heartbeat(port->hpd);
1520 }
1521}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001522
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001523/*
Chris Kellyae926052012-02-20 21:11:53 +00001524 * Context: tasklet
1525 */
1526static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1527{
1528 int rc = 0;
1529 struct oz_port *port = urb->hcpriv;
1530 u8 ep_addr;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001531
Chris Kellyae926052012-02-20 21:11:53 +00001532 /* When we are paranoid we keep a list of urbs which we check against
1533 * before handing one back. This is just for debugging during
1534 * development and should be turned off in the released driver.
1535 */
1536 oz_remember_urb(urb);
1537 /* Check buffer is valid.
1538 */
1539 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1540 return -EINVAL;
1541 /* Check if there is a device at the port - refuse if not.
1542 */
1543 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1544 return -EPIPE;
1545 ep_addr = usb_pipeendpoint(urb->pipe);
1546 if (ep_addr) {
1547 /* If the request is not for EP0 then queue it.
1548 */
1549 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1550 urb, 0))
1551 rc = -EPIPE;
1552 } else {
1553 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1554 }
1555 return rc;
1556}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001557
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001558/*
Chris Kellyae926052012-02-20 21:11:53 +00001559 * Context: tasklet
1560 */
1561static void oz_urb_process_tasklet(unsigned long unused)
1562{
1563 unsigned long irq_state;
1564 struct urb *urb;
1565 struct oz_hcd *ozhcd = oz_hcd_claim();
Christoph Jaegera87c3802014-08-04 14:54:56 +02001566 struct oz_urb_link *urbl, *n;
Chris Kellyae926052012-02-20 21:11:53 +00001567 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001568
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001569 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001570 return;
1571 /* This is called from a tasklet so is in softirq context but the urb
1572 * list is filled from any context so we need to lock
1573 * appropriately while removing urbs.
1574 */
1575 spin_lock_irqsave(&g_tasklet_lock, irq_state);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001576 list_for_each_entry_safe(urbl, n, &ozhcd->urb_pending_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001577 list_del_init(&urbl->link);
1578 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1579 urb = urbl->urb;
1580 oz_free_urb_link(urbl);
1581 rc = oz_urb_process(ozhcd, urb);
1582 if (rc)
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001583 oz_complete_urb(ozhcd->hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +00001584 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1585 }
1586 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1587 oz_hcd_put(ozhcd);
1588}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001589
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001590/*
Chris Kellyae926052012-02-20 21:11:53 +00001591 * This function searches for the urb in any of the lists it could be in.
1592 * If it is found it is removed from the list and completed. If the urb is
1593 * being processed then it won't be in a list so won't be found. However, the
1594 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1595 * to a non-zero value. When an attempt is made to put the urb back in a list
1596 * the unlinked field will be checked and the urb will then be completed.
1597 * Context: tasklet
1598 */
1599static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1600{
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001601 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001602 struct list_head *e;
1603 struct oz_hcd *ozhcd;
1604 unsigned long irq_state;
1605 u8 ix;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001606
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001607 if (port == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001608 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001609 return;
1610 }
1611 ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001612 if (ozhcd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001613 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001614 return;
1615 }
1616
1617 /* Look in the tasklet queue.
1618 */
1619 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1620 list_for_each(e, &ozhcd->urb_cancel_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001621 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +00001622 if (urb == urbl->urb) {
1623 list_del_init(e);
1624 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1625 goto out2;
1626 }
1627 }
1628 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001629 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001630
1631 /* Look in the orphanage.
1632 */
1633 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1634 list_for_each(e, &ozhcd->orphanage) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001635 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +00001636 if (urbl->urb == urb) {
1637 list_del(e);
Joe Perchesf724b582013-07-23 13:45:00 +01001638 oz_dbg(ON, "Found urb in orphanage\n");
Chris Kellyae926052012-02-20 21:11:53 +00001639 goto out;
1640 }
1641 }
1642 ix = (ep_num & 0xf);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001643 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001644 if ((ep_num & USB_DIR_IN) && ix)
1645 urbl = oz_remove_urb(port->in_ep[ix], urb);
1646 else
1647 urbl = oz_remove_urb(port->out_ep[ix], urb);
1648out:
1649 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1650out2:
1651 if (urbl) {
1652 urb->actual_length = 0;
1653 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001654 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
Chris Kellyae926052012-02-20 21:11:53 +00001655 }
1656}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001657
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001658/*
Chris Kellyae926052012-02-20 21:11:53 +00001659 * Context: tasklet
1660 */
1661static void oz_urb_cancel_tasklet(unsigned long unused)
1662{
1663 unsigned long irq_state;
1664 struct urb *urb;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001665 struct oz_urb_link *urbl, *n;
Chris Kellyae926052012-02-20 21:11:53 +00001666 struct oz_hcd *ozhcd = oz_hcd_claim();
Rupesh Gujare18f81912013-08-13 18:24:21 +01001667
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001668 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001669 return;
1670 spin_lock_irqsave(&g_tasklet_lock, irq_state);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001671 list_for_each_entry_safe(urbl, n, &ozhcd->urb_cancel_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001672 list_del_init(&urbl->link);
1673 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1674 urb = urbl->urb;
1675 if (urb->unlinked)
1676 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1677 oz_free_urb_link(urbl);
1678 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1679 }
1680 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1681 oz_hcd_put(ozhcd);
1682}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001683
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001684/*
Chris Kellyae926052012-02-20 21:11:53 +00001685 * Context: unknown
1686 */
1687static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1688{
1689 if (ozhcd) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001690 struct oz_urb_link *urbl, *n;
James A Shacklefordba910802014-05-21 14:50:26 -04001691
Christoph Jaegera87c3802014-08-04 14:54:56 +02001692 list_for_each_entry_safe(urbl, n, &ozhcd->orphanage, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001693 list_del(&urbl->link);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001694 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
Chris Kellyae926052012-02-20 21:11:53 +00001695 oz_free_urb_link(urbl);
1696 }
1697 }
1698}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001699
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001700/*
Chris Kellyae926052012-02-20 21:11:53 +00001701 * Context: unknown
1702 */
1703static int oz_hcd_start(struct usb_hcd *hcd)
1704{
Chris Kellyae926052012-02-20 21:11:53 +00001705 hcd->power_budget = 200;
1706 hcd->state = HC_STATE_RUNNING;
1707 hcd->uses_new_polling = 1;
1708 return 0;
1709}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001710
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001711/*
Chris Kellyae926052012-02-20 21:11:53 +00001712 * Context: unknown
1713 */
1714static void oz_hcd_stop(struct usb_hcd *hcd)
1715{
Chris Kellyae926052012-02-20 21:11:53 +00001716}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001717
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001718/*
Chris Kellyae926052012-02-20 21:11:53 +00001719 * Context: unknown
1720 */
1721static void oz_hcd_shutdown(struct usb_hcd *hcd)
1722{
Chris Kellyae926052012-02-20 21:11:53 +00001723}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001724
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001725/*
Chris Kellyae926052012-02-20 21:11:53 +00001726 * Called to queue an urb for the device.
1727 * This function should return a non-zero error code if it fails the urb but
1728 * should not call usb_hcd_giveback_urb().
1729 * Context: any
1730 */
1731static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1732 gfp_t mem_flags)
1733{
1734 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001735 int rc;
Chris Kellyae926052012-02-20 21:11:53 +00001736 int port_ix;
1737 struct oz_port *port;
1738 unsigned long irq_state;
1739 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001740
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001741 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001742 if (unlikely(ozhcd == NULL)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001743 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +00001744 return -EPIPE;
1745 }
1746 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001747 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +00001748 return -EPIPE;
1749 }
1750 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1751 if (port_ix < 0)
1752 return -EPIPE;
1753 port = &ozhcd->ports[port_ix];
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001754 if (port == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001755 return -EPIPE;
Rupesh Gujare0a7bfbf2013-08-22 17:38:48 +01001756 if (!(port->flags & OZ_PORT_F_PRESENT) ||
1757 (port->flags & OZ_PORT_F_CHANGED)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001758 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1759 port_ix, urb->dev->devnum);
Chris Kellyae926052012-02-20 21:11:53 +00001760 return -EPIPE;
1761 }
1762 urb->hcpriv = port;
1763 /* Put request in queue for processing by tasklet.
1764 */
1765 urbl = oz_alloc_urb_link();
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001766 if (unlikely(urbl == NULL))
Chris Kellyae926052012-02-20 21:11:53 +00001767 return -ENOMEM;
1768 urbl->urb = urb;
1769 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1770 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1771 if (unlikely(rc)) {
1772 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1773 oz_free_urb_link(urbl);
1774 return rc;
1775 }
1776 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1777 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1778 tasklet_schedule(&g_urb_process_tasklet);
1779 atomic_inc(&g_pending_urbs);
1780 return 0;
1781}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001782
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001783/*
Chris Kellyae926052012-02-20 21:11:53 +00001784 * Context: tasklet
1785 */
1786static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1787 struct urb *urb)
1788{
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001789 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001790
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001791 if (unlikely(ep == NULL))
1792 return NULL;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001793
1794 list_for_each_entry(urbl, &ep->urb_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001795 if (urbl->urb == urb) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001796 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +00001797 if (usb_pipeisoc(urb->pipe)) {
1798 ep->credit -= urb->number_of_packets;
1799 if (ep->credit < 0)
1800 ep->credit = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001801 }
1802 return urbl;
1803 }
1804 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001805 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001806}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001807
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001808/*
Chris Kellyae926052012-02-20 21:11:53 +00001809 * Called to dequeue a previously submitted urb for the device.
1810 * Context: any
1811 */
1812static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1813{
1814 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001815 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +00001816 int rc;
1817 unsigned long irq_state;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001818
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001819 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001820 urbl = oz_alloc_urb_link();
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001821 if (unlikely(urbl == NULL))
Chris Kellyae926052012-02-20 21:11:53 +00001822 return -ENOMEM;
1823 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1824 /* The following function checks the urb is still in the queue
1825 * maintained by the core and that the unlinked field is zero.
1826 * If both are true the function sets the unlinked field and returns
1827 * zero. Otherwise it returns an error.
1828 */
1829 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1830 /* We have to check we haven't completed the urb or are about
1831 * to complete it. When we do we set hcpriv to 0 so if this has
1832 * already happened we don't put the urb in the cancel queue.
1833 */
1834 if ((rc == 0) && urb->hcpriv) {
1835 urbl->urb = urb;
1836 urbl->port = (struct oz_port *)urb->hcpriv;
1837 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1838 if (usb_pipein(urb->pipe))
1839 urbl->ep_num |= USB_DIR_IN;
1840 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1841 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1842 tasklet_schedule(&g_urb_cancel_tasklet);
1843 } else {
1844 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1845 oz_free_urb_link(urbl);
1846 }
1847 return rc;
1848}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001849
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001850/*
Chris Kellyae926052012-02-20 21:11:53 +00001851 * Context: unknown
1852 */
1853static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1854 struct usb_host_endpoint *ep)
1855{
Chris Kellyae926052012-02-20 21:11:53 +00001856}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001857
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001858/*
Chris Kellyae926052012-02-20 21:11:53 +00001859 * Context: unknown
1860 */
1861static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1862 struct usb_host_endpoint *ep)
1863{
Chris Kellyae926052012-02-20 21:11:53 +00001864}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001865
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001866/*
Chris Kellyae926052012-02-20 21:11:53 +00001867 * Context: unknown
1868 */
1869static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1870{
Joe Perchesf724b582013-07-23 13:45:00 +01001871 oz_dbg(ON, "oz_hcd_get_frame_number\n");
Chris Kellyae926052012-02-20 21:11:53 +00001872 return oz_usb_get_frame_number();
1873}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001874
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001875/*
Chris Kellyae926052012-02-20 21:11:53 +00001876 * Context: softirq
1877 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1878 * always do that in softirq context.
1879 */
1880static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1881{
1882 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1883 int i;
1884
Chris Kellyae926052012-02-20 21:11:53 +00001885 buf[0] = 0;
Rupesh Gujarea6669812013-08-05 18:40:15 +01001886 buf[1] = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001887
1888 spin_lock_bh(&ozhcd->hcd_lock);
1889 for (i = 0; i < OZ_NB_PORTS; i++) {
1890 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
Joe Perchesf724b582013-07-23 13:45:00 +01001891 oz_dbg(HUB, "Port %d changed\n", i);
Chris Kellyae926052012-02-20 21:11:53 +00001892 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
Rupesh Gujarea6669812013-08-05 18:40:15 +01001893 if (i < 7)
Rupesh Gujare05352812013-08-13 18:24:24 +01001894 buf[0] |= 1 << (i + 1);
Rupesh Gujarea6669812013-08-05 18:40:15 +01001895 else
Rupesh Gujare05352812013-08-13 18:24:24 +01001896 buf[1] |= 1 << (i - 7);
Chris Kellyae926052012-02-20 21:11:53 +00001897 }
1898 }
1899 spin_unlock_bh(&ozhcd->hcd_lock);
Rupesh Gujarea6669812013-08-05 18:40:15 +01001900 if (buf[0] != 0 || buf[1] != 0)
1901 return 2;
Adrian Nicoarad75b6c62014-09-08 14:41:48 -04001902 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00001903}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001904
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001905/*
Chris Kellyae926052012-02-20 21:11:53 +00001906 * Context: process
1907 */
1908static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1909 struct usb_hub_descriptor *desc)
1910{
Chris Kellyae926052012-02-20 21:11:53 +00001911 memset(desc, 0, sizeof(*desc));
1912 desc->bDescriptorType = 0x29;
1913 desc->bDescLength = 9;
Melike Yurtoglu43f61da2014-10-26 23:55:22 +02001914 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Chris Kellyae926052012-02-20 21:11:53 +00001915 desc->bNbrPorts = OZ_NB_PORTS;
1916}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001917
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001918/*
Chris Kellyae926052012-02-20 21:11:53 +00001919 * Context: process
1920 */
1921static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1922{
1923 struct oz_port *port;
Chris Kellyae926052012-02-20 21:11:53 +00001924 u8 port_id = (u8)windex;
1925 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1926 unsigned set_bits = 0;
1927 unsigned clear_bits = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01001928
Chris Kellyae926052012-02-20 21:11:53 +00001929 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1930 return -EPIPE;
1931 port = &ozhcd->ports[port_id-1];
1932 switch (wvalue) {
1933 case USB_PORT_FEAT_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01001934 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00001935 break;
1936 case USB_PORT_FEAT_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01001937 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00001938 break;
1939 case USB_PORT_FEAT_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01001940 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00001941 break;
1942 case USB_PORT_FEAT_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01001943 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00001944 break;
1945 case USB_PORT_FEAT_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01001946 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00001947 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
1948 clear_bits = USB_PORT_STAT_RESET;
1949 ozhcd->ports[port_id-1].bus_addr = 0;
1950 break;
1951 case USB_PORT_FEAT_POWER:
Joe Perchesf724b582013-07-23 13:45:00 +01001952 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
Chris Kellyae926052012-02-20 21:11:53 +00001953 set_bits |= USB_PORT_STAT_POWER;
1954 break;
1955 case USB_PORT_FEAT_LOWSPEED:
Joe Perchesf724b582013-07-23 13:45:00 +01001956 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
Chris Kellyae926052012-02-20 21:11:53 +00001957 break;
1958 case USB_PORT_FEAT_C_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01001959 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00001960 break;
1961 case USB_PORT_FEAT_C_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01001962 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00001963 break;
1964 case USB_PORT_FEAT_C_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01001965 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00001966 break;
1967 case USB_PORT_FEAT_C_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01001968 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00001969 break;
1970 case USB_PORT_FEAT_C_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01001971 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00001972 break;
1973 case USB_PORT_FEAT_TEST:
Joe Perchesf724b582013-07-23 13:45:00 +01001974 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
Chris Kellyae926052012-02-20 21:11:53 +00001975 break;
1976 case USB_PORT_FEAT_INDICATOR:
Joe Perchesf724b582013-07-23 13:45:00 +01001977 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
Chris Kellyae926052012-02-20 21:11:53 +00001978 break;
1979 default:
Joe Perchesf724b582013-07-23 13:45:00 +01001980 oz_dbg(HUB, "Other %d\n", wvalue);
Chris Kellyae926052012-02-20 21:11:53 +00001981 break;
1982 }
1983 if (set_bits || clear_bits) {
1984 spin_lock_bh(&port->port_lock);
1985 port->status &= ~clear_bits;
1986 port->status |= set_bits;
1987 spin_unlock_bh(&port->port_lock);
1988 }
Joe Perchesf724b582013-07-23 13:45:00 +01001989 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
Peter Senna Tschudina3c97192014-05-20 12:33:47 +02001990 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00001991}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001992
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001993/*
Chris Kellyae926052012-02-20 21:11:53 +00001994 * Context: process
1995 */
1996static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1997{
1998 struct oz_port *port;
Chris Kellyae926052012-02-20 21:11:53 +00001999 u8 port_id = (u8)windex;
2000 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2001 unsigned clear_bits = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002002
Chris Kellyae926052012-02-20 21:11:53 +00002003 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2004 return -EPIPE;
2005 port = &ozhcd->ports[port_id-1];
2006 switch (wvalue) {
2007 case USB_PORT_FEAT_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002008 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00002009 break;
2010 case USB_PORT_FEAT_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002011 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00002012 clear_bits = USB_PORT_STAT_ENABLE;
2013 break;
2014 case USB_PORT_FEAT_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002015 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002016 break;
2017 case USB_PORT_FEAT_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002018 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002019 break;
2020 case USB_PORT_FEAT_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002021 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00002022 break;
2023 case USB_PORT_FEAT_POWER:
Joe Perchesf724b582013-07-23 13:45:00 +01002024 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
Chris Kellyae926052012-02-20 21:11:53 +00002025 clear_bits |= USB_PORT_STAT_POWER;
2026 break;
2027 case USB_PORT_FEAT_LOWSPEED:
Joe Perchesf724b582013-07-23 13:45:00 +01002028 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
Chris Kellyae926052012-02-20 21:11:53 +00002029 break;
2030 case USB_PORT_FEAT_C_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002031 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
Jiayi Ye1d06bb42014-10-25 10:58:28 +08002032 clear_bits = USB_PORT_STAT_C_CONNECTION << 16;
Chris Kellyae926052012-02-20 21:11:53 +00002033 break;
2034 case USB_PORT_FEAT_C_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002035 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
Jiayi Ye1d06bb42014-10-25 10:58:28 +08002036 clear_bits = USB_PORT_STAT_C_ENABLE << 16;
Chris Kellyae926052012-02-20 21:11:53 +00002037 break;
2038 case USB_PORT_FEAT_C_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002039 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002040 break;
2041 case USB_PORT_FEAT_C_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002042 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002043 break;
2044 case USB_PORT_FEAT_C_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002045 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
Jiayi Ye1d06bb42014-10-25 10:58:28 +08002046 clear_bits = USB_PORT_FEAT_C_RESET << 16;
Chris Kellyae926052012-02-20 21:11:53 +00002047 break;
2048 case USB_PORT_FEAT_TEST:
Joe Perchesf724b582013-07-23 13:45:00 +01002049 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
Chris Kellyae926052012-02-20 21:11:53 +00002050 break;
2051 case USB_PORT_FEAT_INDICATOR:
Joe Perchesf724b582013-07-23 13:45:00 +01002052 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
Chris Kellyae926052012-02-20 21:11:53 +00002053 break;
2054 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002055 oz_dbg(HUB, "Other %d\n", wvalue);
Chris Kellyae926052012-02-20 21:11:53 +00002056 break;
2057 }
2058 if (clear_bits) {
2059 spin_lock_bh(&port->port_lock);
2060 port->status &= ~clear_bits;
2061 spin_unlock_bh(&port->port_lock);
2062 }
Joe Perchesf724b582013-07-23 13:45:00 +01002063 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2064 port_id, ozhcd->ports[port_id-1].status);
Peter Senna Tschudina3c97192014-05-20 12:33:47 +02002065 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00002066}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002067
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002068/*
Chris Kellyae926052012-02-20 21:11:53 +00002069 * Context: process
2070 */
2071static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2072{
2073 struct oz_hcd *ozhcd;
Rupesh Gujarea15e0422013-08-13 18:29:24 +01002074 u32 status;
Rupesh Gujare18f81912013-08-13 18:24:21 +01002075
Chris Kellyae926052012-02-20 21:11:53 +00002076 if ((windex < 1) || (windex > OZ_NB_PORTS))
2077 return -EPIPE;
2078 ozhcd = oz_hcd_private(hcd);
Joe Perchesf724b582013-07-23 13:45:00 +01002079 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
Chris Kellyae926052012-02-20 21:11:53 +00002080 status = ozhcd->ports[windex-1].status;
2081 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
Joe Perchesf724b582013-07-23 13:45:00 +01002082 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
Chris Kellyae926052012-02-20 21:11:53 +00002083 return 0;
2084}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002085
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002086/*
Chris Kellyae926052012-02-20 21:11:53 +00002087 * Context: process
2088 */
2089static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2090 u16 windex, char *buf, u16 wlength)
2091{
2092 int err = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002093
Chris Kellyae926052012-02-20 21:11:53 +00002094 switch (req_type) {
2095 case ClearHubFeature:
Joe Perchesf724b582013-07-23 13:45:00 +01002096 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002097 break;
2098 case ClearPortFeature:
2099 err = oz_clear_port_feature(hcd, wvalue, windex);
2100 break;
2101 case GetHubDescriptor:
2102 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2103 break;
2104 case GetHubStatus:
Joe Perchesf724b582013-07-23 13:45:00 +01002105 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
Jérôme Pinotc6328242014-03-13 10:20:55 +09002106 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
Chris Kellyae926052012-02-20 21:11:53 +00002107 break;
2108 case GetPortStatus:
2109 err = oz_get_port_status(hcd, windex, buf);
2110 break;
2111 case SetHubFeature:
Joe Perchesf724b582013-07-23 13:45:00 +01002112 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002113 break;
2114 case SetPortFeature:
2115 err = oz_set_port_feature(hcd, wvalue, windex);
2116 break;
2117 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002118 oz_dbg(HUB, "Other: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002119 break;
2120 }
2121 return err;
2122}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002123
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002124/*
Chris Kellyae926052012-02-20 21:11:53 +00002125 * Context: process
2126 */
2127static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2128{
2129 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002130
Chris Kellyae926052012-02-20 21:11:53 +00002131 ozhcd = oz_hcd_private(hcd);
2132 spin_lock_bh(&ozhcd->hcd_lock);
2133 hcd->state = HC_STATE_SUSPENDED;
2134 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2135 spin_unlock_bh(&ozhcd->hcd_lock);
2136 return 0;
2137}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002138
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002139/*
Chris Kellyae926052012-02-20 21:11:53 +00002140 * Context: process
2141 */
2142static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2143{
2144 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002145
Chris Kellyae926052012-02-20 21:11:53 +00002146 ozhcd = oz_hcd_private(hcd);
2147 spin_lock_bh(&ozhcd->hcd_lock);
2148 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2149 hcd->state = HC_STATE_RUNNING;
2150 spin_unlock_bh(&ozhcd->hcd_lock);
2151 return 0;
2152}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002153
Chris Kellyae926052012-02-20 21:11:53 +00002154static void oz_plat_shutdown(struct platform_device *dev)
2155{
Chris Kellyae926052012-02-20 21:11:53 +00002156}
Joe Perches30f1e5a2013-07-23 13:44:59 +01002157
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002158/*
Chris Kellyae926052012-02-20 21:11:53 +00002159 * Context: process
2160 */
2161static int oz_plat_probe(struct platform_device *dev)
2162{
2163 int i;
2164 int err;
2165 struct usb_hcd *hcd;
2166 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002167
Chris Kellyae926052012-02-20 21:11:53 +00002168 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002169 if (hcd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01002170 oz_dbg(ON, "Failed to created hcd object OK\n");
Chris Kellyae926052012-02-20 21:11:53 +00002171 return -ENOMEM;
2172 }
2173 ozhcd = oz_hcd_private(hcd);
2174 memset(ozhcd, 0, sizeof(*ozhcd));
2175 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2176 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2177 INIT_LIST_HEAD(&ozhcd->orphanage);
2178 ozhcd->hcd = hcd;
2179 ozhcd->conn_port = -1;
2180 spin_lock_init(&ozhcd->hcd_lock);
2181 for (i = 0; i < OZ_NB_PORTS; i++) {
2182 struct oz_port *port = &ozhcd->ports[i];
James A Shacklefordba910802014-05-21 14:50:26 -04002183
Chris Kellyae926052012-02-20 21:11:53 +00002184 port->ozhcd = ozhcd;
2185 port->flags = 0;
2186 port->status = 0;
2187 port->bus_addr = 0xff;
2188 spin_lock_init(&port->port_lock);
2189 }
2190 err = usb_add_hcd(hcd, 0, 0);
2191 if (err) {
Joe Perchesf724b582013-07-23 13:45:00 +01002192 oz_dbg(ON, "Failed to add hcd object OK\n");
Chris Kellyae926052012-02-20 21:11:53 +00002193 usb_put_hcd(hcd);
2194 return -1;
2195 }
Peter Chen3c9740a2013-11-05 10:46:02 +08002196 device_wakeup_enable(hcd->self.controller);
2197
Chris Kellyae926052012-02-20 21:11:53 +00002198 spin_lock_bh(&g_hcdlock);
2199 g_ozhcd = ozhcd;
2200 spin_unlock_bh(&g_hcdlock);
2201 return 0;
2202}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002203
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002204/*
Chris Kellyae926052012-02-20 21:11:53 +00002205 * Context: unknown
2206 */
2207static int oz_plat_remove(struct platform_device *dev)
2208{
2209 struct usb_hcd *hcd = platform_get_drvdata(dev);
2210 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002211
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002212 if (hcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00002213 return -1;
2214 ozhcd = oz_hcd_private(hcd);
2215 spin_lock_bh(&g_hcdlock);
2216 if (ozhcd == g_ozhcd)
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002217 g_ozhcd = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00002218 spin_unlock_bh(&g_hcdlock);
Joe Perchesf724b582013-07-23 13:45:00 +01002219 oz_dbg(ON, "Clearing orphanage\n");
Chris Kellyae926052012-02-20 21:11:53 +00002220 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
Joe Perchesf724b582013-07-23 13:45:00 +01002221 oz_dbg(ON, "Removing hcd\n");
Chris Kellyae926052012-02-20 21:11:53 +00002222 usb_remove_hcd(hcd);
2223 usb_put_hcd(hcd);
Chris Kellyae926052012-02-20 21:11:53 +00002224 return 0;
2225}
Joe Perchesf724b582013-07-23 13:45:00 +01002226
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002227/*
Chris Kellyae926052012-02-20 21:11:53 +00002228 * Context: unknown
2229 */
2230static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2231{
Chris Kellyae926052012-02-20 21:11:53 +00002232 return 0;
2233}
Joe Perchesf724b582013-07-23 13:45:00 +01002234
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002235
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002236/*
Chris Kellyae926052012-02-20 21:11:53 +00002237 * Context: unknown
2238 */
2239static int oz_plat_resume(struct platform_device *dev)
2240{
Chris Kellyae926052012-02-20 21:11:53 +00002241 return 0;
2242}
Joe Perchesf724b582013-07-23 13:45:00 +01002243
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002244/*
Chris Kellyae926052012-02-20 21:11:53 +00002245 * Context: process
2246 */
2247int oz_hcd_init(void)
2248{
2249 int err;
Rupesh Gujare18f81912013-08-13 18:24:21 +01002250
Chris Kellyae926052012-02-20 21:11:53 +00002251 if (usb_disabled())
2252 return -ENODEV;
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +02002253
2254 oz_urb_link_cache = KMEM_CACHE(oz_urb_link, 0);
2255 if (!oz_urb_link_cache)
2256 return -ENOMEM;
2257
Chris Kellyae926052012-02-20 21:11:53 +00002258 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2259 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2260 err = platform_driver_register(&g_oz_plat_drv);
Joe Perchesf724b582013-07-23 13:45:00 +01002261 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
Chris Kellyae926052012-02-20 21:11:53 +00002262 if (err)
2263 goto error;
2264 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002265 if (g_plat_dev == NULL) {
Chris Kellyae926052012-02-20 21:11:53 +00002266 err = -ENOMEM;
2267 goto error1;
2268 }
Joe Perchesf724b582013-07-23 13:45:00 +01002269 oz_dbg(ON, "platform_device_alloc() succeeded\n");
Chris Kellyae926052012-02-20 21:11:53 +00002270 err = platform_device_add(g_plat_dev);
2271 if (err)
2272 goto error2;
Joe Perchesf724b582013-07-23 13:45:00 +01002273 oz_dbg(ON, "platform_device_add() succeeded\n");
Chris Kellyae926052012-02-20 21:11:53 +00002274 return 0;
2275error2:
2276 platform_device_put(g_plat_dev);
2277error1:
2278 platform_driver_unregister(&g_oz_plat_drv);
2279error:
2280 tasklet_disable(&g_urb_process_tasklet);
2281 tasklet_disable(&g_urb_cancel_tasklet);
Joe Perchesf724b582013-07-23 13:45:00 +01002282 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
Chris Kellyae926052012-02-20 21:11:53 +00002283 return err;
2284}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002285
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002286/*
Chris Kellyae926052012-02-20 21:11:53 +00002287 * Context: process
2288 */
2289void oz_hcd_term(void)
2290{
Rupesh Gujare0140eb22013-08-27 16:53:42 +01002291 msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
Xiaotian Feng984a4a02012-10-31 18:56:48 +08002292 tasklet_kill(&g_urb_process_tasklet);
2293 tasklet_kill(&g_urb_cancel_tasklet);
Chris Kellyae926052012-02-20 21:11:53 +00002294 platform_device_unregister(g_plat_dev);
2295 platform_driver_unregister(&g_oz_plat_drv);
Joe Perchesf724b582013-07-23 13:45:00 +01002296 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +02002297 kmem_cache_destroy(oz_urb_link_cache);
Chris Kellyae926052012-02-20 21:11:53 +00002298}