blob: 784b5ecfa8493ba07d8ba90cde1b11b2b6a4b6b7 [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{
Quentin Lamberte03b72972015-02-10 10:24:12 +0100283 struct oz_endpoint *ep;
284
285 ep = kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
Quentin Lambert64986132015-02-10 11:42:08 +0100286 if (!ep)
287 return NULL;
288
289 INIT_LIST_HEAD(&ep->urb_list);
290 INIT_LIST_HEAD(&ep->link);
291 ep->credit = -1;
292 if (buffer_size) {
293 ep->buffer_size = buffer_size;
294 ep->buffer = (u8 *)(ep+1);
Chris Kellyae926052012-02-20 21:11:53 +0000295 }
Quentin Lambert64986132015-02-10 11:42:08 +0100296
Chris Kellyae926052012-02-20 21:11:53 +0000297 return ep;
298}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100299
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100300/*
Chris Kellyae926052012-02-20 21:11:53 +0000301 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
302 * disabled.
303 * Context: softirq or process
304 */
Jérôme Pinot46374d32014-03-13 10:17:17 +0900305static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
306 struct urb *urb)
Chris Kellyae926052012-02-20 21:11:53 +0000307{
308 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100309
Christoph Jaegera87c3802014-08-04 14:54:56 +0200310 list_for_each_entry(urbl, &ozhcd->urb_cancel_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +0000311 if (urb == urbl->urb) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200312 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +0000313 return urbl;
314 }
315 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100316 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000317}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100318
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100319/*
Chris Kellyae926052012-02-20 21:11:53 +0000320 * This is called when we have finished processing an urb. It unlinks it from
321 * the ep and returns it to the core.
322 * Context: softirq or process
323 */
324static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100325 int status)
Chris Kellyae926052012-02-20 21:11:53 +0000326{
327 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
328 unsigned long irq_state;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100329 struct oz_urb_link *cancel_urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100330
Chris Kellyae926052012-02-20 21:11:53 +0000331 spin_lock_irqsave(&g_tasklet_lock, irq_state);
332 usb_hcd_unlink_urb_from_ep(hcd, urb);
333 /* Clear hcpriv which will prevent it being put in the cancel list
334 * in the event that an attempt is made to cancel it.
335 */
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100336 urb->hcpriv = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000337 /* Walk the cancel list in case the urb is already sitting there.
338 * Since we process the cancel list in a tasklet rather than in
339 * the dequeue function this could happen.
340 */
341 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
342 /* Note: we release lock but do not enable local irqs.
343 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
344 * or at least other host controllers disable interrupts at this point
345 * so we do the same. We must, however, release the lock otherwise a
346 * deadlock will occur if an urb is submitted to our driver in the urb
347 * completion function. Because we disable interrupts it is possible
348 * that the urb_enqueue function can be called with them disabled.
349 */
350 spin_unlock(&g_tasklet_lock);
351 if (oz_forget_urb(urb)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100352 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +0000353 } else {
Chris Kellyae926052012-02-20 21:11:53 +0000354 atomic_dec(&g_pending_urbs);
Chris Kellyae926052012-02-20 21:11:53 +0000355 usb_hcd_giveback_urb(hcd, urb, status);
356 }
357 spin_lock(&g_tasklet_lock);
358 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
Markus Elfring968bf0c2014-11-25 16:51:08 +0100359 oz_free_urb_link(cancel_urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000360}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100361
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100362/*
Chris Kellyae926052012-02-20 21:11:53 +0000363 * Deallocates an endpoint including deallocating any associated stream and
364 * returning any queued urbs to the core.
365 * Context: softirq
366 */
367static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
368{
Chris Kellyae926052012-02-20 21:11:53 +0000369 if (port) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200370 LIST_HEAD(list);
Chris Kellyae926052012-02-20 21:11:53 +0000371 struct oz_hcd *ozhcd = port->ozhcd;
James A Shacklefordba910802014-05-21 14:50:26 -0400372
Chris Kellyae926052012-02-20 21:11:53 +0000373 if (ep->flags & OZ_F_EP_HAVE_STREAM)
374 oz_usb_stream_delete(port->hpd, ep->ep_num);
375 /* Transfer URBs to the orphanage while we hold the lock. */
376 spin_lock_bh(&ozhcd->hcd_lock);
377 /* Note: this works even if ep->urb_list is empty.*/
378 list_replace_init(&ep->urb_list, &list);
379 /* Put the URBs in the orphanage. */
380 list_splice_tail(&list, &ozhcd->orphanage);
381 spin_unlock_bh(&ozhcd->hcd_lock);
382 }
Joe Perchesf724b582013-07-23 13:45:00 +0100383 oz_dbg(ON, "Freeing endpoint memory\n");
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800384 kfree(ep);
Chris Kellyae926052012-02-20 21:11:53 +0000385}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100386
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100387/*
Chris Kellyae926052012-02-20 21:11:53 +0000388 * Context: softirq
389 */
Peter Huewea7f74c32013-02-15 21:17:22 +0100390static void oz_complete_buffered_urb(struct oz_port *port,
391 struct oz_endpoint *ep,
Rupesh Gujare28a72292012-07-23 18:49:43 +0100392 struct urb *urb)
393{
Rupesh Gujare4882ad952013-08-23 18:33:31 +0100394 int data_len, available_space, copy_len;
Rupesh Gujare28a72292012-07-23 18:49:43 +0100395
Rupesh Gujare4882ad952013-08-23 18:33:31 +0100396 data_len = ep->buffer[ep->out_ix];
Rupesh Gujare28a72292012-07-23 18:49:43 +0100397 if (data_len <= urb->transfer_buffer_length)
398 available_space = data_len;
399 else
400 available_space = urb->transfer_buffer_length;
401
402 if (++ep->out_ix == ep->buffer_size)
403 ep->out_ix = 0;
404 copy_len = ep->buffer_size - ep->out_ix;
405 if (copy_len >= available_space)
406 copy_len = available_space;
407 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
408
409 if (copy_len < available_space) {
410 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
411 (available_space - copy_len));
412 ep->out_ix = available_space - copy_len;
413 } else {
414 ep->out_ix += copy_len;
415 }
416 urb->actual_length = available_space;
417 if (ep->out_ix == ep->buffer_size)
418 ep->out_ix = 0;
419
420 ep->buffered_units--;
Joe Perchesf724b582013-07-23 13:45:00 +0100421 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
422 available_space);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100423 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Rupesh Gujare28a72292012-07-23 18:49:43 +0100424}
425
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100426/*
Rupesh Gujare28a72292012-07-23 18:49:43 +0100427 * Context: softirq
428 */
Chris Kellyae926052012-02-20 21:11:53 +0000429static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
430 struct urb *urb, u8 req_id)
431{
432 struct oz_urb_link *urbl;
Rupesh Gujare2c663352013-08-13 18:24:23 +0100433 struct oz_endpoint *ep = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000434 int err = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100435
Chris Kellyae926052012-02-20 21:11:53 +0000436 if (ep_addr >= OZ_NB_ENDPOINTS) {
Joe Perchesf724b582013-07-23 13:45:00 +0100437 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
Chris Kellyae926052012-02-20 21:11:53 +0000438 return -EINVAL;
439 }
440 urbl = oz_alloc_urb_link();
441 if (!urbl)
442 return -ENOMEM;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100443 urbl->submit_counter = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000444 urbl->urb = urb;
445 urbl->req_id = req_id;
446 urbl->ep_num = ep_addr;
447 /* Hold lock while we insert the URB into the list within the
448 * endpoint structure.
449 */
450 spin_lock_bh(&port->ozhcd->hcd_lock);
451 /* If the urb has been unlinked while out of any list then
452 * complete it now.
453 */
454 if (urb->unlinked) {
455 spin_unlock_bh(&port->ozhcd->hcd_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100456 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100457 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000458 oz_free_urb_link(urbl);
459 return 0;
460 }
Rupesh Gujare2c663352013-08-13 18:24:23 +0100461
462 if (in_dir)
Chris Kellyae926052012-02-20 21:11:53 +0000463 ep = port->in_ep[ep_addr];
Rupesh Gujare2c663352013-08-13 18:24:23 +0100464 else
Chris Kellyae926052012-02-20 21:11:53 +0000465 ep = port->out_ep[ep_addr];
Rupesh Gujare2c663352013-08-13 18:24:23 +0100466 if (!ep) {
Rupesh Gujarebc9aece2013-08-05 18:40:12 +0100467 err = -ENOMEM;
468 goto out;
469 }
Rupesh Gujare28a72292012-07-23 18:49:43 +0100470
471 /*For interrupt endpoint check for buffered data
472 * & complete urb
473 */
474 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
475 && ep->buffered_units > 0) {
476 oz_free_urb_link(urbl);
477 spin_unlock_bh(&port->ozhcd->hcd_lock);
478 oz_complete_buffered_urb(port, ep, urb);
479 return 0;
480 }
481
Rupesh Gujarec45905a2013-08-13 18:29:21 +0100482 if (port->hpd) {
Chris Kellyae926052012-02-20 21:11:53 +0000483 list_add_tail(&urbl->link, &ep->urb_list);
484 if (!in_dir && ep_addr && (ep->credit < 0)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100485 getrawmonotonic(&ep->timestamp);
Chris Kellyae926052012-02-20 21:11:53 +0000486 ep->credit = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000487 }
488 } else {
489 err = -EPIPE;
490 }
Rupesh Gujarebc9aece2013-08-05 18:40:12 +0100491out:
Chris Kellyae926052012-02-20 21:11:53 +0000492 spin_unlock_bh(&port->ozhcd->hcd_lock);
493 if (err)
494 oz_free_urb_link(urbl);
495 return err;
496}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100497
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100498/*
Chris Kellyae926052012-02-20 21:11:53 +0000499 * Removes an urb from the queue in the endpoint.
500 * Returns 0 if it is found and -EIDRM otherwise.
501 * Context: softirq
502 */
503static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
504 struct urb *urb)
505{
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100506 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000507 struct oz_endpoint *ep;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100508
Chris Kellyae926052012-02-20 21:11:53 +0000509 spin_lock_bh(&port->ozhcd->hcd_lock);
510 if (in_dir)
511 ep = port->in_ep[ep_addr];
512 else
513 ep = port->out_ep[ep_addr];
514 if (ep) {
515 struct list_head *e;
James A Shacklefordba910802014-05-21 14:50:26 -0400516
Chris Kellyae926052012-02-20 21:11:53 +0000517 list_for_each(e, &ep->urb_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200518 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +0000519 if (urbl->urb == urb) {
520 list_del_init(e);
521 break;
522 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100523 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000524 }
525 }
526 spin_unlock_bh(&port->ozhcd->hcd_lock);
Markus Elfring968bf0c2014-11-25 16:51:08 +0100527 oz_free_urb_link(urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000528 return urbl ? 0 : -EIDRM;
529}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100530
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100531/*
Chris Kellyae926052012-02-20 21:11:53 +0000532 * Finds an urb given its request id.
533 * Context: softirq
534 */
535static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
536 u8 req_id)
537{
538 struct oz_hcd *ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100539 struct urb *urb = NULL;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100540 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +0000541 struct oz_endpoint *ep;
542
543 spin_lock_bh(&ozhcd->hcd_lock);
544 ep = port->out_ep[ep_ix];
545 if (ep) {
546 struct list_head *e;
James A Shacklefordba910802014-05-21 14:50:26 -0400547
Chris Kellyae926052012-02-20 21:11:53 +0000548 list_for_each(e, &ep->urb_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200549 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +0000550 if (urbl->req_id == req_id) {
551 urb = urbl->urb;
552 list_del_init(e);
553 break;
554 }
555 }
556 }
557 spin_unlock_bh(&ozhcd->hcd_lock);
558 /* If urb is non-zero then we we must have an urb link to delete.
559 */
560 if (urb)
561 oz_free_urb_link(urbl);
562 return urb;
563}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100564
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100565/*
Chris Kellyae926052012-02-20 21:11:53 +0000566 * Pre-condition: Port lock must be held.
567 * Context: softirq
568 */
569static void oz_acquire_port(struct oz_port *port, void *hpd)
570{
571 INIT_LIST_HEAD(&port->isoc_out_ep);
572 INIT_LIST_HEAD(&port->isoc_in_ep);
573 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
574 port->status |= USB_PORT_STAT_CONNECTION |
575 (USB_PORT_STAT_C_CONNECTION << 16);
576 oz_usb_get(hpd);
577 port->hpd = hpd;
578}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100579
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100580/*
Chris Kellyae926052012-02-20 21:11:53 +0000581 * Context: softirq
582 */
583static struct oz_hcd *oz_hcd_claim(void)
584{
585 struct oz_hcd *ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100586
Chris Kellyae926052012-02-20 21:11:53 +0000587 spin_lock_bh(&g_hcdlock);
588 ozhcd = g_ozhcd;
589 if (ozhcd)
590 usb_get_hcd(ozhcd->hcd);
591 spin_unlock_bh(&g_hcdlock);
592 return ozhcd;
593}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100594
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100595/*
Chris Kellyae926052012-02-20 21:11:53 +0000596 * Context: softirq
597 */
598static inline void oz_hcd_put(struct oz_hcd *ozhcd)
599{
600 if (ozhcd)
601 usb_put_hcd(ozhcd->hcd);
602}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100603
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100604/*
Chris Kellyae926052012-02-20 21:11:53 +0000605 * This is called by the protocol handler to notify that a PD has arrived.
606 * We allocate a port to associate with the PD and create a structure for
607 * endpoint 0. This port is made the connection port.
608 * In the event that one of the other port is already a connection port then
609 * we fail.
610 * TODO We should be able to do better than fail and should be able remember
611 * that this port needs configuring and make it the connection port once the
612 * current connection port has been assigned an address. Collisions here are
613 * probably very rare indeed.
614 * Context: softirq
615 */
Rupesh Gujare0503d202013-08-13 18:29:22 +0100616struct oz_port *oz_hcd_pd_arrived(void *hpd)
Chris Kellyae926052012-02-20 21:11:53 +0000617{
618 int i;
Rupesh Gujare3bc0d882013-08-15 12:00:21 +0100619 struct oz_port *hport;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100620 struct oz_hcd *ozhcd;
Chris Kellyae926052012-02-20 21:11:53 +0000621 struct oz_endpoint *ep;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100622
Chris Kellyae926052012-02-20 21:11:53 +0000623 ozhcd = oz_hcd_claim();
Dan Carpenter95b20b82013-08-13 18:29:26 +0100624 if (!ozhcd)
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100625 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000626 /* Allocate an endpoint object in advance (before holding hcd lock) to
627 * use for out endpoint 0.
628 */
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +0100629 ep = oz_ep_alloc(0, GFP_ATOMIC);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100630 if (!ep)
631 goto err_put;
632
Chris Kellyae926052012-02-20 21:11:53 +0000633 spin_lock_bh(&ozhcd->hcd_lock);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100634 if (ozhcd->conn_port >= 0)
635 goto err_unlock;
636
Chris Kellyae926052012-02-20 21:11:53 +0000637 for (i = 0; i < OZ_NB_PORTS; i++) {
638 struct oz_port *port = &ozhcd->ports[i];
Dan Carpenter95b20b82013-08-13 18:29:26 +0100639
Chris Kellyae926052012-02-20 21:11:53 +0000640 spin_lock(&port->port_lock);
Rupesh Gujare0a7bfbf2013-08-22 17:38:48 +0100641 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
Chris Kellyae926052012-02-20 21:11:53 +0000642 oz_acquire_port(port, hpd);
643 spin_unlock(&port->port_lock);
644 break;
645 }
646 spin_unlock(&port->port_lock);
647 }
Dan Carpenter95b20b82013-08-13 18:29:26 +0100648 if (i == OZ_NB_PORTS)
649 goto err_unlock;
650
651 ozhcd->conn_port = i;
652 hport = &ozhcd->ports[i];
653 hport->out_ep[0] = ep;
654 spin_unlock_bh(&ozhcd->hcd_lock);
655 if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
656 usb_hcd_resume_root_hub(ozhcd->hcd);
657 usb_hcd_poll_rh_status(ozhcd->hcd);
Chris Kellyae926052012-02-20 21:11:53 +0000658 oz_hcd_put(ozhcd);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100659
Chris Kellyae926052012-02-20 21:11:53 +0000660 return hport;
Dan Carpenter95b20b82013-08-13 18:29:26 +0100661
662err_unlock:
663 spin_unlock_bh(&ozhcd->hcd_lock);
664 oz_ep_free(NULL, ep);
665err_put:
666 oz_hcd_put(ozhcd);
667 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000668}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100669
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100670/*
Chris Kellyae926052012-02-20 21:11:53 +0000671 * This is called by the protocol handler to notify that the PD has gone away.
672 * We need to deallocate all resources and then request that the root hub is
673 * polled. We release the reference we hold on the PD.
674 * Context: softirq
675 */
Rupesh Gujare83db51f2013-08-15 12:00:22 +0100676void oz_hcd_pd_departed(struct oz_port *port)
Chris Kellyae926052012-02-20 21:11:53 +0000677{
Chris Kellyae926052012-02-20 21:11:53 +0000678 struct oz_hcd *ozhcd;
679 void *hpd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100680 struct oz_endpoint *ep = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000681
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100682 if (port == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100683 oz_dbg(ON, "%s: port = 0\n", __func__);
Chris Kellyae926052012-02-20 21:11:53 +0000684 return;
685 }
686 ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100687 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +0000688 return;
689 /* Check if this is the connection port - if so clear it.
690 */
691 spin_lock_bh(&ozhcd->hcd_lock);
692 if ((ozhcd->conn_port >= 0) &&
693 (port == &ozhcd->ports[ozhcd->conn_port])) {
Joe Perchesf724b582013-07-23 13:45:00 +0100694 oz_dbg(ON, "Clearing conn_port\n");
Chris Kellyae926052012-02-20 21:11:53 +0000695 ozhcd->conn_port = -1;
696 }
697 spin_lock(&port->port_lock);
698 port->flags |= OZ_PORT_F_DYING;
699 spin_unlock(&port->port_lock);
700 spin_unlock_bh(&ozhcd->hcd_lock);
701
702 oz_clean_endpoints_for_config(ozhcd->hcd, port);
703 spin_lock_bh(&port->port_lock);
704 hpd = port->hpd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100705 port->hpd = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000706 port->bus_addr = 0xff;
Rupesh Gujared7729832013-08-05 18:40:14 +0100707 port->config_num = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000708 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
709 port->flags |= OZ_PORT_F_CHANGED;
Rupesh Gujare68501432013-08-27 16:53:41 +0100710 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
Chris Kellyae926052012-02-20 21:11:53 +0000711 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
712 /* If there is an endpont 0 then clear the pointer while we hold
713 * the spinlock be we deallocate it after releasing the lock.
714 */
715 if (port->out_ep[0]) {
716 ep = port->out_ep[0];
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100717 port->out_ep[0] = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000718 }
719 spin_unlock_bh(&port->port_lock);
720 if (ep)
721 oz_ep_free(port, ep);
722 usb_hcd_poll_rh_status(ozhcd->hcd);
723 oz_usb_put(hpd);
724}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100725
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100726/*
Chris Kellyae926052012-02-20 21:11:53 +0000727 * Context: softirq
728 */
729void oz_hcd_pd_reset(void *hpd, void *hport)
730{
731 /* Cleanup the current configuration and report reset to the core.
732 */
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +0530733 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +0000734 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100735
Joe Perchesf724b582013-07-23 13:45:00 +0100736 oz_dbg(ON, "PD Reset\n");
Chris Kellyae926052012-02-20 21:11:53 +0000737 spin_lock_bh(&port->port_lock);
738 port->flags |= OZ_PORT_F_CHANGED;
739 port->status |= USB_PORT_STAT_RESET;
740 port->status |= (USB_PORT_STAT_C_RESET << 16);
741 spin_unlock_bh(&port->port_lock);
742 oz_clean_endpoints_for_config(ozhcd->hcd, port);
743 usb_hcd_poll_rh_status(ozhcd->hcd);
744}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100745
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100746/*
Chris Kellyae926052012-02-20 21:11:53 +0000747 * Context: softirq
748 */
Jason A. Donenfeldb1bb5b42015-05-29 13:06:59 +0200749void oz_hcd_get_desc_cnf(void *hport, u8 req_id, u8 status, const u8 *desc,
750 u8 length, u16 offset, u16 total_size)
Chris Kellyae926052012-02-20 21:11:53 +0000751{
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +0530752 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +0000753 struct urb *urb;
754 int err = 0;
755
Joe Perchesf724b582013-07-23 13:45:00 +0100756 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
757 length, offset, total_size);
Chris Kellyae926052012-02-20 21:11:53 +0000758 urb = oz_find_urb_by_id(port, 0, req_id);
759 if (!urb)
760 return;
761 if (status == 0) {
Jason A. Donenfeldb1bb5b42015-05-29 13:06:59 +0200762 unsigned int copy_len;
763 unsigned int required_size = urb->transfer_buffer_length;
James A Shacklefordba910802014-05-21 14:50:26 -0400764
Chris Kellyae926052012-02-20 21:11:53 +0000765 if (required_size > total_size)
766 required_size = total_size;
767 copy_len = required_size-offset;
768 if (length <= copy_len)
769 copy_len = length;
770 memcpy(urb->transfer_buffer+offset, desc, copy_len);
771 offset += copy_len;
772 if (offset < required_size) {
773 struct usb_ctrlrequest *setup =
774 (struct usb_ctrlrequest *)urb->setup_packet;
775 unsigned wvalue = le16_to_cpu(setup->wValue);
James A Shacklefordba910802014-05-21 14:50:26 -0400776
Chris Kellyae926052012-02-20 21:11:53 +0000777 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
778 err = -ENOMEM;
779 else if (oz_usb_get_desc_req(port->hpd, req_id,
780 setup->bRequestType, (u8)(wvalue>>8),
781 (u8)wvalue, setup->wIndex, offset,
782 required_size-offset)) {
783 oz_dequeue_ep_urb(port, 0, 0, urb);
784 err = -ENOMEM;
785 }
786 if (err == 0)
787 return;
788 }
789 }
790 urb->actual_length = total_size;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100791 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000792}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100793
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100794/*
Chris Kellyae926052012-02-20 21:11:53 +0000795 * Context: softirq
796 */
Chris Kellyae926052012-02-20 21:11:53 +0000797static void oz_display_conf_type(u8 t)
798{
799 switch (t) {
800 case USB_REQ_GET_STATUS:
Joe Perchesf724b582013-07-23 13:45:00 +0100801 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000802 break;
803 case USB_REQ_CLEAR_FEATURE:
Joe Perchesf724b582013-07-23 13:45:00 +0100804 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000805 break;
806 case USB_REQ_SET_FEATURE:
Joe Perchesf724b582013-07-23 13:45:00 +0100807 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000808 break;
809 case USB_REQ_SET_ADDRESS:
Joe Perchesf724b582013-07-23 13:45:00 +0100810 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000811 break;
812 case USB_REQ_GET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +0100813 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000814 break;
815 case USB_REQ_SET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +0100816 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000817 break;
818 case USB_REQ_GET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +0100819 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000820 break;
821 case USB_REQ_SET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +0100822 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000823 break;
824 case USB_REQ_GET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +0100825 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000826 break;
827 case USB_REQ_SET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +0100828 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000829 break;
830 case USB_REQ_SYNCH_FRAME:
Joe Perchesf724b582013-07-23 13:45:00 +0100831 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000832 break;
833 }
834}
Joe Perches05f608f2013-07-23 13:45:01 +0100835
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100836/*
Chris Kellyae926052012-02-20 21:11:53 +0000837 * Context: softirq
838 */
839static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
840 u8 rcode, u8 config_num)
841{
842 int rc = 0;
843 struct usb_hcd *hcd = port->ozhcd->hcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100844
Chris Kellyae926052012-02-20 21:11:53 +0000845 if (rcode == 0) {
846 port->config_num = config_num;
847 oz_clean_endpoints_for_config(hcd, port);
848 if (oz_build_endpoints_for_config(hcd, port,
849 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
850 rc = -ENOMEM;
851 }
852 } else {
853 rc = -ENOMEM;
854 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100855 oz_complete_urb(hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +0000856}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100857
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100858/*
Chris Kellyae926052012-02-20 21:11:53 +0000859 * Context: softirq
860 */
861static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
862 u8 rcode, u8 if_num, u8 alt)
863{
864 struct usb_hcd *hcd = port->ozhcd->hcd;
865 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100866
Rupesh Gujare050596a2013-08-23 18:33:28 +0100867 if ((rcode == 0) && (port->config_num > 0)) {
Chris Kellyae926052012-02-20 21:11:53 +0000868 struct usb_host_config *config;
869 struct usb_host_interface *intf;
James A Shacklefordba910802014-05-21 14:50:26 -0400870
Joe Perchesf724b582013-07-23 13:45:00 +0100871 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
Chris Kellyae926052012-02-20 21:11:53 +0000872 oz_clean_endpoints_for_interface(hcd, port, if_num);
873 config = &urb->dev->config[port->config_num-1];
874 intf = &config->intf_cache[if_num]->altsetting[alt];
875 if (oz_build_endpoints_for_interface(hcd, port, intf,
876 GFP_ATOMIC))
877 rc = -ENOMEM;
878 else
879 port->iface[if_num].alt = alt;
880 } else {
881 rc = -ENOMEM;
882 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100883 oz_complete_urb(hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +0000884}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100885
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100886/*
Chris Kellyae926052012-02-20 21:11:53 +0000887 * Context: softirq
888 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100889void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
Chris Kellyae926052012-02-20 21:11:53 +0000890 int data_len)
891{
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +0530892 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +0000893 struct urb *urb;
894 struct usb_ctrlrequest *setup;
895 struct usb_hcd *hcd = port->ozhcd->hcd;
896 unsigned windex;
897 unsigned wvalue;
898
Joe Perchesf724b582013-07-23 13:45:00 +0100899 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
Chris Kellyae926052012-02-20 21:11:53 +0000900 urb = oz_find_urb_by_id(port, 0, req_id);
901 if (!urb) {
Joe Perchesf724b582013-07-23 13:45:00 +0100902 oz_dbg(ON, "URB not found\n");
Chris Kellyae926052012-02-20 21:11:53 +0000903 return;
904 }
905 setup = (struct usb_ctrlrequest *)urb->setup_packet;
906 windex = le16_to_cpu(setup->wIndex);
907 wvalue = le16_to_cpu(setup->wValue);
908 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
909 /* Standard requests */
910 oz_display_conf_type(setup->bRequest);
911 switch (setup->bRequest) {
912 case USB_REQ_SET_CONFIGURATION:
913 oz_hcd_complete_set_config(port, urb, rcode,
914 (u8)wvalue);
915 break;
916 case USB_REQ_SET_INTERFACE:
917 oz_hcd_complete_set_interface(port, urb, rcode,
918 (u8)windex, (u8)wvalue);
919 break;
920 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100921 oz_complete_urb(hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000922 }
923
924 } else {
925 int copy_len;
James A Shacklefordba910802014-05-21 14:50:26 -0400926
Joe Perchesf724b582013-07-23 13:45:00 +0100927 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
Rupesh Gujare5494ebd2012-07-23 18:49:45 +0100928 if (data_len) {
929 if (data_len <= urb->transfer_buffer_length)
930 copy_len = data_len;
931 else
932 copy_len = urb->transfer_buffer_length;
Chris Kellyae926052012-02-20 21:11:53 +0000933 memcpy(urb->transfer_buffer, data, copy_len);
Rupesh Gujare5494ebd2012-07-23 18:49:45 +0100934 urb->actual_length = copy_len;
935 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100936 oz_complete_urb(hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000937 }
938}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100939
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100940/*
Chris Kellyae926052012-02-20 21:11:53 +0000941 * Context: softirq-serialized
942 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100943static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
944 int data_len)
Chris Kellyae926052012-02-20 21:11:53 +0000945{
946 int space;
947 int copy_len;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100948
Chris Kellyae926052012-02-20 21:11:53 +0000949 if (!ep->buffer)
950 return -1;
951 space = ep->out_ix-ep->in_ix-1;
952 if (space < 0)
953 space += ep->buffer_size;
954 if (space < (data_len+1)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100955 oz_dbg(ON, "Buffer full\n");
Chris Kellyae926052012-02-20 21:11:53 +0000956 return -1;
957 }
958 ep->buffer[ep->in_ix] = (u8)data_len;
959 if (++ep->in_ix == ep->buffer_size)
960 ep->in_ix = 0;
961 copy_len = ep->buffer_size - ep->in_ix;
962 if (copy_len > data_len)
963 copy_len = data_len;
964 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
965
966 if (copy_len < data_len) {
967 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
968 ep->in_ix = data_len-copy_len;
969 } else {
970 ep->in_ix += copy_len;
971 }
972 if (ep->in_ix == ep->buffer_size)
973 ep->in_ix = 0;
974 ep->buffered_units++;
975 return 0;
976}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100977
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100978/*
Chris Kellyae926052012-02-20 21:11:53 +0000979 * Context: softirq-serialized
980 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100981void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
Chris Kellyae926052012-02-20 21:11:53 +0000982{
983 struct oz_port *port = (struct oz_port *)hport;
984 struct oz_endpoint *ep;
985 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100986
Chris Kellyae926052012-02-20 21:11:53 +0000987 spin_lock_bh(&ozhcd->hcd_lock);
988 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100989 if (ep == NULL)
Chris Kellyae926052012-02-20 21:11:53 +0000990 goto done;
991 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
992 case USB_ENDPOINT_XFER_INT:
993 case USB_ENDPOINT_XFER_BULK:
994 if (!list_empty(&ep->urb_list)) {
995 struct oz_urb_link *urbl =
996 list_first_entry(&ep->urb_list,
997 struct oz_urb_link, link);
998 struct urb *urb;
999 int copy_len;
James A Shacklefordba910802014-05-21 14:50:26 -04001000
Chris Kellyae926052012-02-20 21:11:53 +00001001 list_del_init(&urbl->link);
1002 spin_unlock_bh(&ozhcd->hcd_lock);
1003 urb = urbl->urb;
1004 oz_free_urb_link(urbl);
1005 if (data_len <= urb->transfer_buffer_length)
1006 copy_len = data_len;
1007 else
1008 copy_len = urb->transfer_buffer_length;
1009 memcpy(urb->transfer_buffer, data, copy_len);
1010 urb->actual_length = copy_len;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001011 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001012 return;
1013 }
Adrian Nicoarad75b6c62014-09-08 14:41:48 -04001014 oz_dbg(ON, "buffering frame as URB is not available\n");
1015 oz_hcd_buffer_data(ep, data, data_len);
Chris Kellyae926052012-02-20 21:11:53 +00001016 break;
1017 case USB_ENDPOINT_XFER_ISOC:
1018 oz_hcd_buffer_data(ep, data, data_len);
1019 break;
1020 }
1021done:
1022 spin_unlock_bh(&ozhcd->hcd_lock);
1023}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001024
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001025/*
Chris Kellyae926052012-02-20 21:11:53 +00001026 * Context: unknown
1027 */
1028static inline int oz_usb_get_frame_number(void)
1029{
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001030 return atomic_inc_return(&g_usb_frame_number);
Chris Kellyae926052012-02-20 21:11:53 +00001031}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001032
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001033/*
Chris Kellyae926052012-02-20 21:11:53 +00001034 * Context: softirq
1035 */
1036int oz_hcd_heartbeat(void *hport)
1037{
1038 int rc = 0;
Tapasweni Pathakb3d43a32014-10-30 17:02:57 +05301039 struct oz_port *port = hport;
Chris Kellyae926052012-02-20 21:11:53 +00001040 struct oz_hcd *ozhcd = port->ozhcd;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001041 struct oz_urb_link *urbl, *n;
1042 LIST_HEAD(xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001043 struct urb *urb;
1044 struct oz_endpoint *ep;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001045 struct timespec ts, delta;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001046
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001047 getrawmonotonic(&ts);
Chris Kellyae926052012-02-20 21:11:53 +00001048 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1049 */
1050 spin_lock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001051 list_for_each_entry(ep, &port->isoc_out_ep, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001052 if (ep->credit < 0)
1053 continue;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001054 delta = timespec_sub(ts, ep->timestamp);
Rupesh Gujare5cf9d252013-08-05 12:14:04 +01001055 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
Chris Kellyae926052012-02-20 21:11:53 +00001056 if (ep->credit > ep->credit_ceiling)
1057 ep->credit = ep->credit_ceiling;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001058 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001059 while (ep->credit && !list_empty(&ep->urb_list)) {
1060 urbl = list_first_entry(&ep->urb_list,
1061 struct oz_urb_link, link);
1062 urb = urbl->urb;
Rupesh Gujare24168912012-07-23 18:49:44 +01001063 if ((ep->credit + 1) < urb->number_of_packets)
Chris Kellyae926052012-02-20 21:11:53 +00001064 break;
1065 ep->credit -= urb->number_of_packets;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001066 if (ep->credit < 0)
1067 ep->credit = 0;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001068 list_move_tail(&urbl->link, &xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001069 }
1070 }
1071 spin_unlock_bh(&ozhcd->hcd_lock);
1072 /* Send to PD and complete URBs.
1073 */
Christoph Jaegera87c3802014-08-04 14:54:56 +02001074 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001075 urb = urbl->urb;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001076 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +00001077 urb->error_count = 0;
1078 urb->start_frame = oz_usb_get_frame_number();
1079 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1080 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001081 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001082 }
1083 /* Check the IN isoc endpoints to see if any URBs can be completed.
1084 */
1085 spin_lock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001086 list_for_each_entry(ep, &port->isoc_in_ep, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001087 if (ep->flags & OZ_F_EP_BUFFERING) {
Rupesh Gujareb360cb92013-01-29 17:00:55 +00001088 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
Chris Kellyae926052012-02-20 21:11:53 +00001089 ep->flags &= ~OZ_F_EP_BUFFERING;
1090 ep->credit = 0;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001091 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001092 ep->start_frame = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001093 }
1094 continue;
1095 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001096 delta = timespec_sub(ts, ep->timestamp);
Rupesh Gujare5cf9d252013-08-05 12:14:04 +01001097 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001098 ep->timestamp = ts;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001099 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001100 struct urb *urb = urbl->urb;
1101 int len = 0;
1102 int copy_len;
1103 int i;
James A Shacklefordba910802014-05-21 14:50:26 -04001104
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001105 if (ep->credit < urb->number_of_packets)
Chris Kellyae926052012-02-20 21:11:53 +00001106 break;
1107 if (ep->buffered_units < urb->number_of_packets)
1108 break;
1109 urb->actual_length = 0;
1110 for (i = 0; i < urb->number_of_packets; i++) {
1111 len = ep->buffer[ep->out_ix];
1112 if (++ep->out_ix == ep->buffer_size)
1113 ep->out_ix = 0;
1114 copy_len = ep->buffer_size - ep->out_ix;
1115 if (copy_len > len)
1116 copy_len = len;
1117 memcpy(urb->transfer_buffer,
1118 &ep->buffer[ep->out_ix], copy_len);
1119 if (copy_len < len) {
1120 memcpy(urb->transfer_buffer+copy_len,
1121 ep->buffer, len-copy_len);
1122 ep->out_ix = len-copy_len;
1123 } else
1124 ep->out_ix += copy_len;
1125 if (ep->out_ix == ep->buffer_size)
1126 ep->out_ix = 0;
1127 urb->iso_frame_desc[i].offset =
1128 urb->actual_length;
1129 urb->actual_length += len;
1130 urb->iso_frame_desc[i].actual_length = len;
1131 urb->iso_frame_desc[i].status = 0;
1132 }
1133 ep->buffered_units -= urb->number_of_packets;
1134 urb->error_count = 0;
1135 urb->start_frame = ep->start_frame;
1136 ep->start_frame += urb->number_of_packets;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001137 list_move_tail(&urbl->link, &xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001138 ep->credit -= urb->number_of_packets;
Chris Kellyae926052012-02-20 21:11:53 +00001139 }
1140 }
1141 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1142 rc = 1;
1143 spin_unlock_bh(&ozhcd->hcd_lock);
1144 /* Complete the filled URBs.
1145 */
Christoph Jaegera87c3802014-08-04 14:54:56 +02001146 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001147 urb = urbl->urb;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001148 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +00001149 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001150 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001151 }
1152 /* Check if there are any ep0 requests that have timed out.
1153 * If so resent to PD.
1154 */
1155 ep = port->out_ep[0];
1156 if (ep) {
Chris Kellyae926052012-02-20 21:11:53 +00001157 spin_lock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001158 list_for_each_entry_safe(urbl, n, &ep->urb_list, link) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001159 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1160 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001161 list_move_tail(&urbl->link, &xfr_list);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001162 urbl->submit_counter = 0;
1163 } else {
1164 urbl->submit_counter++;
Chris Kellyae926052012-02-20 21:11:53 +00001165 }
1166 }
1167 if (!list_empty(&ep->urb_list))
1168 rc = 1;
1169 spin_unlock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001170 list_for_each_entry_safe(urbl, n, &xfr_list, link) {
Joe Perchesf724b582013-07-23 13:45:00 +01001171 oz_dbg(ON, "Resending request to PD\n");
Chris Kellyae926052012-02-20 21:11:53 +00001172 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1173 oz_free_urb_link(urbl);
1174 }
1175 }
1176 return rc;
1177}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001178
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001179/*
Chris Kellyae926052012-02-20 21:11:53 +00001180 * Context: softirq
1181 */
1182static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1183 struct oz_port *port,
1184 struct usb_host_interface *intf, gfp_t mem_flags)
1185{
1186 struct oz_hcd *ozhcd = port->ozhcd;
1187 int i;
1188 int if_ix = intf->desc.bInterfaceNumber;
1189 int request_heartbeat = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001190
Joe Perchesf724b582013-07-23 13:45:00 +01001191 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
Rupesh Gujared236dc12013-08-22 17:38:49 +01001192 if (if_ix >= port->num_iface || port->iface == NULL)
1193 return -ENOMEM;
Chris Kellyae926052012-02-20 21:11:53 +00001194 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1195 struct usb_host_endpoint *hep = &intf->endpoint[i];
1196 u8 ep_addr = hep->desc.bEndpointAddress;
1197 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1198 struct oz_endpoint *ep;
1199 int buffer_size = 0;
1200
Joe Perchesf724b582013-07-23 13:45:00 +01001201 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
Rupesh Gujare28a72292012-07-23 18:49:43 +01001202 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1203 switch (hep->desc.bmAttributes &
1204 USB_ENDPOINT_XFERTYPE_MASK) {
1205 case USB_ENDPOINT_XFER_ISOC:
Rupesh Gujare0f750be2013-08-23 18:33:29 +01001206 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001207 break;
1208 case USB_ENDPOINT_XFER_INT:
Rupesh Gujare00d2a462013-08-23 18:33:30 +01001209 buffer_size = OZ_EP_BUFFER_SIZE_INT;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001210 break;
1211 }
Chris Kellyae926052012-02-20 21:11:53 +00001212 }
1213
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +01001214 ep = oz_ep_alloc(buffer_size, mem_flags);
Chris Kellyae926052012-02-20 21:11:53 +00001215 if (!ep) {
1216 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1217 return -ENOMEM;
1218 }
1219 ep->attrib = hep->desc.bmAttributes;
1220 ep->ep_num = ep_num;
1221 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1222 == USB_ENDPOINT_XFER_ISOC) {
Joe Perchesf724b582013-07-23 13:45:00 +01001223 oz_dbg(ON, "wMaxPacketSize = %d\n",
1224 usb_endpoint_maxp(&hep->desc));
Chris Kellyae926052012-02-20 21:11:53 +00001225 ep->credit_ceiling = 200;
1226 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1227 ep->flags |= OZ_F_EP_BUFFERING;
Chris Kellyae926052012-02-20 21:11:53 +00001228 } else {
1229 ep->flags |= OZ_F_EP_HAVE_STREAM;
1230 if (oz_usb_stream_create(port->hpd, ep_num))
1231 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1232 }
1233 }
1234 spin_lock_bh(&ozhcd->hcd_lock);
1235 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1236 port->in_ep[ep_num] = ep;
1237 port->iface[if_ix].ep_mask |=
1238 (1<<(ep_num+OZ_NB_ENDPOINTS));
1239 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1240 == USB_ENDPOINT_XFER_ISOC) {
1241 list_add_tail(&ep->link, &port->isoc_in_ep);
1242 request_heartbeat = 1;
1243 }
1244 } else {
1245 port->out_ep[ep_num] = ep;
1246 port->iface[if_ix].ep_mask |= (1<<ep_num);
1247 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1248 == USB_ENDPOINT_XFER_ISOC) {
1249 list_add_tail(&ep->link, &port->isoc_out_ep);
1250 request_heartbeat = 1;
1251 }
1252 }
1253 spin_unlock_bh(&ozhcd->hcd_lock);
1254 if (request_heartbeat && port->hpd)
1255 oz_usb_request_heartbeat(port->hpd);
1256 }
1257 return 0;
1258}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001259
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001260/*
Chris Kellyae926052012-02-20 21:11:53 +00001261 * Context: softirq
1262 */
1263static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1264 struct oz_port *port, int if_ix)
1265{
1266 struct oz_hcd *ozhcd = port->ozhcd;
1267 unsigned mask;
1268 int i;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001269 LIST_HEAD(ep_list);
1270 struct oz_endpoint *ep, *n;
Chris Kellyae926052012-02-20 21:11:53 +00001271
Joe Perchesf724b582013-07-23 13:45:00 +01001272 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
Chris Kellyae926052012-02-20 21:11:53 +00001273 if (if_ix >= port->num_iface)
1274 return;
Chris Kellyae926052012-02-20 21:11:53 +00001275 spin_lock_bh(&ozhcd->hcd_lock);
1276 mask = port->iface[if_ix].ep_mask;
1277 port->iface[if_ix].ep_mask = 0;
1278 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1279 struct list_head *e;
1280 /* Gather OUT endpoints.
1281 */
1282 if ((mask & (1<<i)) && port->out_ep[i]) {
1283 e = &port->out_ep[i]->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001284 port->out_ep[i] = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001285 /* Remove from isoc list if present.
1286 */
Wei Yongjun094e74c2012-09-05 14:48:48 +08001287 list_move_tail(e, &ep_list);
Chris Kellyae926052012-02-20 21:11:53 +00001288 }
1289 /* Gather IN endpoints.
1290 */
1291 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1292 e = &port->in_ep[i]->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001293 port->in_ep[i] = NULL;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001294 list_move_tail(e, &ep_list);
Chris Kellyae926052012-02-20 21:11:53 +00001295 }
1296 }
1297 spin_unlock_bh(&ozhcd->hcd_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001298 list_for_each_entry_safe(ep, n, &ep_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001299 list_del_init(&ep->link);
1300 oz_ep_free(port, ep);
1301 }
1302}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001303
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001304/*
Chris Kellyae926052012-02-20 21:11:53 +00001305 * Context: softirq
1306 */
1307static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1308 struct oz_port *port, struct usb_host_config *config,
1309 gfp_t mem_flags)
1310{
1311 struct oz_hcd *ozhcd = port->ozhcd;
1312 int i;
1313 int num_iface = config->desc.bNumInterfaces;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001314
Chris Kellyae926052012-02-20 21:11:53 +00001315 if (num_iface) {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -08001316 struct oz_interface *iface;
1317
Adrian Nicoarae0301d02014-09-08 15:02:49 -04001318 iface = kmalloc_array(num_iface, sizeof(struct oz_interface),
1319 mem_flags | __GFP_ZERO);
Chris Kellyae926052012-02-20 21:11:53 +00001320 if (!iface)
1321 return -ENOMEM;
1322 spin_lock_bh(&ozhcd->hcd_lock);
1323 port->iface = iface;
1324 port->num_iface = num_iface;
1325 spin_unlock_bh(&ozhcd->hcd_lock);
1326 }
1327 for (i = 0; i < num_iface; i++) {
1328 struct usb_host_interface *intf =
1329 &config->intf_cache[i]->altsetting[0];
1330 if (oz_build_endpoints_for_interface(hcd, port, intf,
1331 mem_flags))
1332 goto fail;
1333 }
1334 return 0;
1335fail:
1336 oz_clean_endpoints_for_config(hcd, port);
1337 return -1;
1338}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001339
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001340/*
Chris Kellyae926052012-02-20 21:11:53 +00001341 * Context: softirq
1342 */
1343static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1344 struct oz_port *port)
1345{
1346 struct oz_hcd *ozhcd = port->ozhcd;
1347 int i;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001348
Joe Perchesf724b582013-07-23 13:45:00 +01001349 oz_dbg(ON, "Deleting endpoints for configuration\n");
Chris Kellyae926052012-02-20 21:11:53 +00001350 for (i = 0; i < port->num_iface; i++)
1351 oz_clean_endpoints_for_interface(hcd, port, i);
1352 spin_lock_bh(&ozhcd->hcd_lock);
1353 if (port->iface) {
Joe Perchesf724b582013-07-23 13:45:00 +01001354 oz_dbg(ON, "Freeing interfaces object\n");
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -08001355 kfree(port->iface);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001356 port->iface = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001357 }
1358 port->num_iface = 0;
1359 spin_unlock_bh(&ozhcd->hcd_lock);
1360}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001361
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001362/*
Chris Kellyae926052012-02-20 21:11:53 +00001363 * Context: tasklet
1364 */
1365static void *oz_claim_hpd(struct oz_port *port)
1366{
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001367 void *hpd;
Chris Kellyae926052012-02-20 21:11:53 +00001368 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001369
Chris Kellyae926052012-02-20 21:11:53 +00001370 spin_lock_bh(&ozhcd->hcd_lock);
1371 hpd = port->hpd;
1372 if (hpd)
1373 oz_usb_get(hpd);
1374 spin_unlock_bh(&ozhcd->hcd_lock);
1375 return hpd;
1376}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001377
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001378/*
Chris Kellyae926052012-02-20 21:11:53 +00001379 * Context: tasklet
1380 */
1381static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1382 gfp_t mem_flags)
1383{
1384 struct usb_ctrlrequest *setup;
1385 unsigned windex;
1386 unsigned wvalue;
1387 unsigned wlength;
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001388 void *hpd;
Chris Kellyae926052012-02-20 21:11:53 +00001389 u8 req_id;
1390 int rc = 0;
1391 unsigned complete = 0;
1392
1393 int port_ix = -1;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001394 struct oz_port *port = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001395
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001396 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001397 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1398 if (port_ix < 0) {
1399 rc = -EPIPE;
1400 goto out;
1401 }
1402 port = &ozhcd->ports[port_ix];
1403 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1404 || (port->flags & OZ_PORT_F_DYING)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001405 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1406 port_ix, urb->dev->devnum);
Chris Kellyae926052012-02-20 21:11:53 +00001407 rc = -EPIPE;
1408 goto out;
1409 }
1410 /* Store port in private context data.
1411 */
1412 urb->hcpriv = port;
1413 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1414 windex = le16_to_cpu(setup->wIndex);
1415 wvalue = le16_to_cpu(setup->wValue);
1416 wlength = le16_to_cpu(setup->wLength);
Joe Perchesf724b582013-07-23 13:45:00 +01001417 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1418 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1419 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1420 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1421 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
Chris Kellyae926052012-02-20 21:11:53 +00001422
1423 req_id = port->next_req_id++;
1424 hpd = oz_claim_hpd(port);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001425 if (hpd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001426 oz_dbg(ON, "Cannot claim port\n");
Chris Kellyae926052012-02-20 21:11:53 +00001427 rc = -EPIPE;
1428 goto out;
1429 }
1430
1431 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1432 /* Standard requests
1433 */
1434 switch (setup->bRequest) {
1435 case USB_REQ_GET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +01001436 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001437 break;
1438 case USB_REQ_SET_ADDRESS:
Joe Perchesf724b582013-07-23 13:45:00 +01001439 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1440 oz_dbg(ON, "Port %d address is 0x%x\n",
1441 ozhcd->conn_port,
1442 (u8)le16_to_cpu(setup->wValue));
Chris Kellyae926052012-02-20 21:11:53 +00001443 spin_lock_bh(&ozhcd->hcd_lock);
1444 if (ozhcd->conn_port >= 0) {
1445 ozhcd->ports[ozhcd->conn_port].bus_addr =
1446 (u8)le16_to_cpu(setup->wValue);
Joe Perchesf724b582013-07-23 13:45:00 +01001447 oz_dbg(ON, "Clearing conn_port\n");
Chris Kellyae926052012-02-20 21:11:53 +00001448 ozhcd->conn_port = -1;
1449 }
1450 spin_unlock_bh(&ozhcd->hcd_lock);
1451 complete = 1;
1452 break;
1453 case USB_REQ_SET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +01001454 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001455 break;
1456 case USB_REQ_GET_CONFIGURATION:
Masanari Iida8dc24592012-04-25 23:28:35 +09001457 /* We short circuit this case and reply directly since
Chris Kellyae926052012-02-20 21:11:53 +00001458 * we have the selected configuration number cached.
1459 */
Joe Perchesf724b582013-07-23 13:45:00 +01001460 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
Chris Kellyae926052012-02-20 21:11:53 +00001461 if (urb->transfer_buffer_length >= 1) {
1462 urb->actual_length = 1;
1463 *((u8 *)urb->transfer_buffer) =
1464 port->config_num;
1465 complete = 1;
1466 } else {
1467 rc = -EPIPE;
1468 }
1469 break;
1470 case USB_REQ_GET_INTERFACE:
Masanari Iida8dc24592012-04-25 23:28:35 +09001471 /* We short circuit this case and reply directly since
Chris Kellyae926052012-02-20 21:11:53 +00001472 * we have the selected interface alternative cached.
1473 */
Joe Perchesf724b582013-07-23 13:45:00 +01001474 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
Chris Kellyae926052012-02-20 21:11:53 +00001475 if (urb->transfer_buffer_length >= 1) {
1476 urb->actual_length = 1;
1477 *((u8 *)urb->transfer_buffer) =
1478 port->iface[(u8)windex].alt;
Joe Perchesf724b582013-07-23 13:45:00 +01001479 oz_dbg(ON, "interface = %d alt = %d\n",
1480 windex, port->iface[(u8)windex].alt);
Chris Kellyae926052012-02-20 21:11:53 +00001481 complete = 1;
1482 } else {
1483 rc = -EPIPE;
1484 }
1485 break;
1486 case USB_REQ_SET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +01001487 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001488 break;
1489 }
1490 }
1491 if (!rc && !complete) {
1492 int data_len = 0;
James A Shacklefordba910802014-05-21 14:50:26 -04001493
Chris Kellyae926052012-02-20 21:11:53 +00001494 if ((setup->bRequestType & USB_DIR_IN) == 0)
1495 data_len = wlength;
Rupesh Gujare5494ebd2012-07-23 18:49:45 +01001496 urb->actual_length = data_len;
Chris Kellyae926052012-02-20 21:11:53 +00001497 if (oz_usb_control_req(port->hpd, req_id, setup,
1498 urb->transfer_buffer, data_len)) {
1499 rc = -ENOMEM;
1500 } else {
1501 /* Note: we are queuing the request after we have
Justin P. Mattock595914f2012-03-19 08:17:50 -07001502 * submitted it to be transmitted. If the request were
Chris Kellyae926052012-02-20 21:11:53 +00001503 * to complete before we queued it then it would not
1504 * be found in the queue. It seems impossible for
1505 * this to happen but if it did the request would
1506 * be resubmitted so the problem would hopefully
1507 * resolve itself. Putting the request into the
1508 * queue before it has been sent is worse since the
1509 * urb could be cancelled while we are using it
1510 * to build the request.
1511 */
1512 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1513 rc = -ENOMEM;
1514 }
1515 }
1516 oz_usb_put(hpd);
1517out:
1518 if (rc || complete) {
Joe Perchesf724b582013-07-23 13:45:00 +01001519 oz_dbg(ON, "Completing request locally\n");
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001520 oz_complete_urb(ozhcd->hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +00001521 } else {
1522 oz_usb_request_heartbeat(port->hpd);
1523 }
1524}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001525
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001526/*
Chris Kellyae926052012-02-20 21:11:53 +00001527 * Context: tasklet
1528 */
1529static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1530{
1531 int rc = 0;
1532 struct oz_port *port = urb->hcpriv;
1533 u8 ep_addr;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001534
Chris Kellyae926052012-02-20 21:11:53 +00001535 /* When we are paranoid we keep a list of urbs which we check against
1536 * before handing one back. This is just for debugging during
1537 * development and should be turned off in the released driver.
1538 */
1539 oz_remember_urb(urb);
1540 /* Check buffer is valid.
1541 */
1542 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1543 return -EINVAL;
1544 /* Check if there is a device at the port - refuse if not.
1545 */
1546 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1547 return -EPIPE;
1548 ep_addr = usb_pipeendpoint(urb->pipe);
1549 if (ep_addr) {
1550 /* If the request is not for EP0 then queue it.
1551 */
1552 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1553 urb, 0))
1554 rc = -EPIPE;
1555 } else {
1556 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1557 }
1558 return rc;
1559}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001560
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001561/*
Chris Kellyae926052012-02-20 21:11:53 +00001562 * Context: tasklet
1563 */
1564static void oz_urb_process_tasklet(unsigned long unused)
1565{
1566 unsigned long irq_state;
1567 struct urb *urb;
1568 struct oz_hcd *ozhcd = oz_hcd_claim();
Christoph Jaegera87c3802014-08-04 14:54:56 +02001569 struct oz_urb_link *urbl, *n;
Chris Kellyae926052012-02-20 21:11:53 +00001570 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001571
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001572 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001573 return;
1574 /* This is called from a tasklet so is in softirq context but the urb
1575 * list is filled from any context so we need to lock
1576 * appropriately while removing urbs.
1577 */
1578 spin_lock_irqsave(&g_tasklet_lock, irq_state);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001579 list_for_each_entry_safe(urbl, n, &ozhcd->urb_pending_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001580 list_del_init(&urbl->link);
1581 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1582 urb = urbl->urb;
1583 oz_free_urb_link(urbl);
1584 rc = oz_urb_process(ozhcd, urb);
1585 if (rc)
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001586 oz_complete_urb(ozhcd->hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +00001587 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1588 }
1589 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1590 oz_hcd_put(ozhcd);
1591}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001592
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001593/*
Chris Kellyae926052012-02-20 21:11:53 +00001594 * This function searches for the urb in any of the lists it could be in.
1595 * If it is found it is removed from the list and completed. If the urb is
1596 * being processed then it won't be in a list so won't be found. However, the
1597 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1598 * to a non-zero value. When an attempt is made to put the urb back in a list
1599 * the unlinked field will be checked and the urb will then be completed.
1600 * Context: tasklet
1601 */
1602static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1603{
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001604 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001605 struct list_head *e;
1606 struct oz_hcd *ozhcd;
1607 unsigned long irq_state;
1608 u8 ix;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001609
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001610 if (port == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001611 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001612 return;
1613 }
1614 ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001615 if (ozhcd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001616 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001617 return;
1618 }
1619
1620 /* Look in the tasklet queue.
1621 */
1622 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1623 list_for_each(e, &ozhcd->urb_cancel_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001624 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +00001625 if (urb == urbl->urb) {
1626 list_del_init(e);
1627 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1628 goto out2;
1629 }
1630 }
1631 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001632 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001633
1634 /* Look in the orphanage.
1635 */
1636 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1637 list_for_each(e, &ozhcd->orphanage) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001638 urbl = list_entry(e, struct oz_urb_link, link);
Chris Kellyae926052012-02-20 21:11:53 +00001639 if (urbl->urb == urb) {
1640 list_del(e);
Joe Perchesf724b582013-07-23 13:45:00 +01001641 oz_dbg(ON, "Found urb in orphanage\n");
Chris Kellyae926052012-02-20 21:11:53 +00001642 goto out;
1643 }
1644 }
1645 ix = (ep_num & 0xf);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001646 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001647 if ((ep_num & USB_DIR_IN) && ix)
1648 urbl = oz_remove_urb(port->in_ep[ix], urb);
1649 else
1650 urbl = oz_remove_urb(port->out_ep[ix], urb);
1651out:
1652 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1653out2:
1654 if (urbl) {
1655 urb->actual_length = 0;
1656 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001657 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
Chris Kellyae926052012-02-20 21:11:53 +00001658 }
1659}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001660
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001661/*
Chris Kellyae926052012-02-20 21:11:53 +00001662 * Context: tasklet
1663 */
1664static void oz_urb_cancel_tasklet(unsigned long unused)
1665{
1666 unsigned long irq_state;
1667 struct urb *urb;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001668 struct oz_urb_link *urbl, *n;
Chris Kellyae926052012-02-20 21:11:53 +00001669 struct oz_hcd *ozhcd = oz_hcd_claim();
Rupesh Gujare18f81912013-08-13 18:24:21 +01001670
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001671 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001672 return;
1673 spin_lock_irqsave(&g_tasklet_lock, irq_state);
Christoph Jaegera87c3802014-08-04 14:54:56 +02001674 list_for_each_entry_safe(urbl, n, &ozhcd->urb_cancel_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001675 list_del_init(&urbl->link);
1676 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1677 urb = urbl->urb;
1678 if (urb->unlinked)
1679 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1680 oz_free_urb_link(urbl);
1681 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1682 }
1683 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1684 oz_hcd_put(ozhcd);
1685}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001686
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001687/*
Chris Kellyae926052012-02-20 21:11:53 +00001688 * Context: unknown
1689 */
1690static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1691{
1692 if (ozhcd) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001693 struct oz_urb_link *urbl, *n;
James A Shacklefordba910802014-05-21 14:50:26 -04001694
Christoph Jaegera87c3802014-08-04 14:54:56 +02001695 list_for_each_entry_safe(urbl, n, &ozhcd->orphanage, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001696 list_del(&urbl->link);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001697 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
Chris Kellyae926052012-02-20 21:11:53 +00001698 oz_free_urb_link(urbl);
1699 }
1700 }
1701}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001702
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001703/*
Chris Kellyae926052012-02-20 21:11:53 +00001704 * Context: unknown
1705 */
1706static int oz_hcd_start(struct usb_hcd *hcd)
1707{
Chris Kellyae926052012-02-20 21:11:53 +00001708 hcd->power_budget = 200;
1709 hcd->state = HC_STATE_RUNNING;
1710 hcd->uses_new_polling = 1;
1711 return 0;
1712}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001713
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001714/*
Chris Kellyae926052012-02-20 21:11:53 +00001715 * Context: unknown
1716 */
1717static void oz_hcd_stop(struct usb_hcd *hcd)
1718{
Chris Kellyae926052012-02-20 21:11:53 +00001719}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001720
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001721/*
Chris Kellyae926052012-02-20 21:11:53 +00001722 * Context: unknown
1723 */
1724static void oz_hcd_shutdown(struct usb_hcd *hcd)
1725{
Chris Kellyae926052012-02-20 21:11:53 +00001726}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001727
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001728/*
Chris Kellyae926052012-02-20 21:11:53 +00001729 * Called to queue an urb for the device.
1730 * This function should return a non-zero error code if it fails the urb but
1731 * should not call usb_hcd_giveback_urb().
1732 * Context: any
1733 */
1734static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1735 gfp_t mem_flags)
1736{
1737 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001738 int rc;
Chris Kellyae926052012-02-20 21:11:53 +00001739 int port_ix;
1740 struct oz_port *port;
1741 unsigned long irq_state;
1742 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001743
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001744 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001745 if (unlikely(ozhcd == NULL)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001746 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +00001747 return -EPIPE;
1748 }
1749 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001750 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +00001751 return -EPIPE;
1752 }
1753 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1754 if (port_ix < 0)
1755 return -EPIPE;
1756 port = &ozhcd->ports[port_ix];
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001757 if (port == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001758 return -EPIPE;
Rupesh Gujare0a7bfbf2013-08-22 17:38:48 +01001759 if (!(port->flags & OZ_PORT_F_PRESENT) ||
1760 (port->flags & OZ_PORT_F_CHANGED)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001761 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1762 port_ix, urb->dev->devnum);
Chris Kellyae926052012-02-20 21:11:53 +00001763 return -EPIPE;
1764 }
1765 urb->hcpriv = port;
1766 /* Put request in queue for processing by tasklet.
1767 */
1768 urbl = oz_alloc_urb_link();
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001769 if (unlikely(urbl == NULL))
Chris Kellyae926052012-02-20 21:11:53 +00001770 return -ENOMEM;
1771 urbl->urb = urb;
1772 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1773 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1774 if (unlikely(rc)) {
1775 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1776 oz_free_urb_link(urbl);
1777 return rc;
1778 }
1779 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1780 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1781 tasklet_schedule(&g_urb_process_tasklet);
1782 atomic_inc(&g_pending_urbs);
1783 return 0;
1784}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001785
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001786/*
Chris Kellyae926052012-02-20 21:11:53 +00001787 * Context: tasklet
1788 */
1789static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1790 struct urb *urb)
1791{
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001792 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001793
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001794 if (unlikely(ep == NULL))
1795 return NULL;
Christoph Jaegera87c3802014-08-04 14:54:56 +02001796
1797 list_for_each_entry(urbl, &ep->urb_list, link) {
Chris Kellyae926052012-02-20 21:11:53 +00001798 if (urbl->urb == urb) {
Christoph Jaegera87c3802014-08-04 14:54:56 +02001799 list_del_init(&urbl->link);
Chris Kellyae926052012-02-20 21:11:53 +00001800 if (usb_pipeisoc(urb->pipe)) {
1801 ep->credit -= urb->number_of_packets;
1802 if (ep->credit < 0)
1803 ep->credit = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001804 }
1805 return urbl;
1806 }
1807 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001808 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001809}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001810
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001811/*
Chris Kellyae926052012-02-20 21:11:53 +00001812 * Called to dequeue a previously submitted urb for the device.
1813 * Context: any
1814 */
1815static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1816{
1817 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001818 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +00001819 int rc;
1820 unsigned long irq_state;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001821
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001822 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001823 urbl = oz_alloc_urb_link();
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001824 if (unlikely(urbl == NULL))
Chris Kellyae926052012-02-20 21:11:53 +00001825 return -ENOMEM;
1826 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1827 /* The following function checks the urb is still in the queue
1828 * maintained by the core and that the unlinked field is zero.
1829 * If both are true the function sets the unlinked field and returns
1830 * zero. Otherwise it returns an error.
1831 */
1832 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1833 /* We have to check we haven't completed the urb or are about
1834 * to complete it. When we do we set hcpriv to 0 so if this has
1835 * already happened we don't put the urb in the cancel queue.
1836 */
1837 if ((rc == 0) && urb->hcpriv) {
1838 urbl->urb = urb;
1839 urbl->port = (struct oz_port *)urb->hcpriv;
1840 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1841 if (usb_pipein(urb->pipe))
1842 urbl->ep_num |= USB_DIR_IN;
1843 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1844 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1845 tasklet_schedule(&g_urb_cancel_tasklet);
1846 } else {
1847 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1848 oz_free_urb_link(urbl);
1849 }
1850 return rc;
1851}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001852
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001853/*
Chris Kellyae926052012-02-20 21:11:53 +00001854 * Context: unknown
1855 */
1856static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1857 struct usb_host_endpoint *ep)
1858{
Chris Kellyae926052012-02-20 21:11:53 +00001859}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001860
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001861/*
Chris Kellyae926052012-02-20 21:11:53 +00001862 * Context: unknown
1863 */
1864static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1865 struct usb_host_endpoint *ep)
1866{
Chris Kellyae926052012-02-20 21:11:53 +00001867}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001868
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001869/*
Chris Kellyae926052012-02-20 21:11:53 +00001870 * Context: unknown
1871 */
1872static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1873{
Joe Perchesf724b582013-07-23 13:45:00 +01001874 oz_dbg(ON, "oz_hcd_get_frame_number\n");
Chris Kellyae926052012-02-20 21:11:53 +00001875 return oz_usb_get_frame_number();
1876}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001877
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001878/*
Chris Kellyae926052012-02-20 21:11:53 +00001879 * Context: softirq
1880 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1881 * always do that in softirq context.
1882 */
1883static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1884{
1885 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1886 int i;
1887
Chris Kellyae926052012-02-20 21:11:53 +00001888 buf[0] = 0;
Rupesh Gujarea6669812013-08-05 18:40:15 +01001889 buf[1] = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001890
1891 spin_lock_bh(&ozhcd->hcd_lock);
1892 for (i = 0; i < OZ_NB_PORTS; i++) {
1893 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
Joe Perchesf724b582013-07-23 13:45:00 +01001894 oz_dbg(HUB, "Port %d changed\n", i);
Chris Kellyae926052012-02-20 21:11:53 +00001895 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
Rupesh Gujarea6669812013-08-05 18:40:15 +01001896 if (i < 7)
Rupesh Gujare05352812013-08-13 18:24:24 +01001897 buf[0] |= 1 << (i + 1);
Rupesh Gujarea6669812013-08-05 18:40:15 +01001898 else
Rupesh Gujare05352812013-08-13 18:24:24 +01001899 buf[1] |= 1 << (i - 7);
Chris Kellyae926052012-02-20 21:11:53 +00001900 }
1901 }
1902 spin_unlock_bh(&ozhcd->hcd_lock);
Rupesh Gujarea6669812013-08-05 18:40:15 +01001903 if (buf[0] != 0 || buf[1] != 0)
1904 return 2;
Adrian Nicoarad75b6c62014-09-08 14:41:48 -04001905 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00001906}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001907
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001908/*
Chris Kellyae926052012-02-20 21:11:53 +00001909 * Context: process
1910 */
1911static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1912 struct usb_hub_descriptor *desc)
1913{
Chris Kellyae926052012-02-20 21:11:53 +00001914 memset(desc, 0, sizeof(*desc));
1915 desc->bDescriptorType = 0x29;
1916 desc->bDescLength = 9;
Melike Yurtoglu43f61da2014-10-26 23:55:22 +02001917 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Chris Kellyae926052012-02-20 21:11:53 +00001918 desc->bNbrPorts = OZ_NB_PORTS;
1919}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001920
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001921/*
Chris Kellyae926052012-02-20 21:11:53 +00001922 * Context: process
1923 */
1924static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1925{
1926 struct oz_port *port;
Chris Kellyae926052012-02-20 21:11:53 +00001927 u8 port_id = (u8)windex;
1928 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1929 unsigned set_bits = 0;
1930 unsigned clear_bits = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01001931
Chris Kellyae926052012-02-20 21:11:53 +00001932 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1933 return -EPIPE;
1934 port = &ozhcd->ports[port_id-1];
1935 switch (wvalue) {
1936 case USB_PORT_FEAT_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01001937 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00001938 break;
1939 case USB_PORT_FEAT_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01001940 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00001941 break;
1942 case USB_PORT_FEAT_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01001943 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00001944 break;
1945 case USB_PORT_FEAT_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01001946 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00001947 break;
1948 case USB_PORT_FEAT_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01001949 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00001950 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
1951 clear_bits = USB_PORT_STAT_RESET;
1952 ozhcd->ports[port_id-1].bus_addr = 0;
1953 break;
1954 case USB_PORT_FEAT_POWER:
Joe Perchesf724b582013-07-23 13:45:00 +01001955 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
Chris Kellyae926052012-02-20 21:11:53 +00001956 set_bits |= USB_PORT_STAT_POWER;
1957 break;
1958 case USB_PORT_FEAT_LOWSPEED:
Joe Perchesf724b582013-07-23 13:45:00 +01001959 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
Chris Kellyae926052012-02-20 21:11:53 +00001960 break;
1961 case USB_PORT_FEAT_C_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01001962 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00001963 break;
1964 case USB_PORT_FEAT_C_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01001965 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00001966 break;
1967 case USB_PORT_FEAT_C_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01001968 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00001969 break;
1970 case USB_PORT_FEAT_C_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01001971 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00001972 break;
1973 case USB_PORT_FEAT_C_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01001974 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00001975 break;
1976 case USB_PORT_FEAT_TEST:
Joe Perchesf724b582013-07-23 13:45:00 +01001977 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
Chris Kellyae926052012-02-20 21:11:53 +00001978 break;
1979 case USB_PORT_FEAT_INDICATOR:
Joe Perchesf724b582013-07-23 13:45:00 +01001980 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
Chris Kellyae926052012-02-20 21:11:53 +00001981 break;
1982 default:
Joe Perchesf724b582013-07-23 13:45:00 +01001983 oz_dbg(HUB, "Other %d\n", wvalue);
Chris Kellyae926052012-02-20 21:11:53 +00001984 break;
1985 }
1986 if (set_bits || clear_bits) {
1987 spin_lock_bh(&port->port_lock);
1988 port->status &= ~clear_bits;
1989 port->status |= set_bits;
1990 spin_unlock_bh(&port->port_lock);
1991 }
Joe Perchesf724b582013-07-23 13:45:00 +01001992 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
Peter Senna Tschudina3c97192014-05-20 12:33:47 +02001993 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00001994}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001995
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001996/*
Chris Kellyae926052012-02-20 21:11:53 +00001997 * Context: process
1998 */
1999static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2000{
2001 struct oz_port *port;
Chris Kellyae926052012-02-20 21:11:53 +00002002 u8 port_id = (u8)windex;
2003 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2004 unsigned clear_bits = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002005
Chris Kellyae926052012-02-20 21:11:53 +00002006 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2007 return -EPIPE;
2008 port = &ozhcd->ports[port_id-1];
2009 switch (wvalue) {
2010 case USB_PORT_FEAT_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002011 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00002012 break;
2013 case USB_PORT_FEAT_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002014 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00002015 clear_bits = USB_PORT_STAT_ENABLE;
2016 break;
2017 case USB_PORT_FEAT_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002018 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002019 break;
2020 case USB_PORT_FEAT_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002021 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002022 break;
2023 case USB_PORT_FEAT_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002024 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00002025 break;
2026 case USB_PORT_FEAT_POWER:
Joe Perchesf724b582013-07-23 13:45:00 +01002027 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
Chris Kellyae926052012-02-20 21:11:53 +00002028 clear_bits |= USB_PORT_STAT_POWER;
2029 break;
2030 case USB_PORT_FEAT_LOWSPEED:
Joe Perchesf724b582013-07-23 13:45:00 +01002031 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
Chris Kellyae926052012-02-20 21:11:53 +00002032 break;
2033 case USB_PORT_FEAT_C_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002034 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
Jiayi Ye1d06bb42014-10-25 10:58:28 +08002035 clear_bits = USB_PORT_STAT_C_CONNECTION << 16;
Chris Kellyae926052012-02-20 21:11:53 +00002036 break;
2037 case USB_PORT_FEAT_C_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002038 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
Jiayi Ye1d06bb42014-10-25 10:58:28 +08002039 clear_bits = USB_PORT_STAT_C_ENABLE << 16;
Chris Kellyae926052012-02-20 21:11:53 +00002040 break;
2041 case USB_PORT_FEAT_C_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002042 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002043 break;
2044 case USB_PORT_FEAT_C_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002045 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002046 break;
2047 case USB_PORT_FEAT_C_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002048 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
Jiayi Ye1d06bb42014-10-25 10:58:28 +08002049 clear_bits = USB_PORT_FEAT_C_RESET << 16;
Chris Kellyae926052012-02-20 21:11:53 +00002050 break;
2051 case USB_PORT_FEAT_TEST:
Joe Perchesf724b582013-07-23 13:45:00 +01002052 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
Chris Kellyae926052012-02-20 21:11:53 +00002053 break;
2054 case USB_PORT_FEAT_INDICATOR:
Joe Perchesf724b582013-07-23 13:45:00 +01002055 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
Chris Kellyae926052012-02-20 21:11:53 +00002056 break;
2057 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002058 oz_dbg(HUB, "Other %d\n", wvalue);
Chris Kellyae926052012-02-20 21:11:53 +00002059 break;
2060 }
2061 if (clear_bits) {
2062 spin_lock_bh(&port->port_lock);
2063 port->status &= ~clear_bits;
2064 spin_unlock_bh(&port->port_lock);
2065 }
Joe Perchesf724b582013-07-23 13:45:00 +01002066 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2067 port_id, ozhcd->ports[port_id-1].status);
Peter Senna Tschudina3c97192014-05-20 12:33:47 +02002068 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00002069}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002070
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002071/*
Chris Kellyae926052012-02-20 21:11:53 +00002072 * Context: process
2073 */
2074static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2075{
2076 struct oz_hcd *ozhcd;
Rupesh Gujarea15e0422013-08-13 18:29:24 +01002077 u32 status;
Rupesh Gujare18f81912013-08-13 18:24:21 +01002078
Chris Kellyae926052012-02-20 21:11:53 +00002079 if ((windex < 1) || (windex > OZ_NB_PORTS))
2080 return -EPIPE;
2081 ozhcd = oz_hcd_private(hcd);
Joe Perchesf724b582013-07-23 13:45:00 +01002082 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
Chris Kellyae926052012-02-20 21:11:53 +00002083 status = ozhcd->ports[windex-1].status;
2084 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
Joe Perchesf724b582013-07-23 13:45:00 +01002085 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
Chris Kellyae926052012-02-20 21:11:53 +00002086 return 0;
2087}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002088
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002089/*
Chris Kellyae926052012-02-20 21:11:53 +00002090 * Context: process
2091 */
2092static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2093 u16 windex, char *buf, u16 wlength)
2094{
2095 int err = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002096
Chris Kellyae926052012-02-20 21:11:53 +00002097 switch (req_type) {
2098 case ClearHubFeature:
Joe Perchesf724b582013-07-23 13:45:00 +01002099 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002100 break;
2101 case ClearPortFeature:
2102 err = oz_clear_port_feature(hcd, wvalue, windex);
2103 break;
2104 case GetHubDescriptor:
2105 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2106 break;
2107 case GetHubStatus:
Joe Perchesf724b582013-07-23 13:45:00 +01002108 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
Jérôme Pinotc6328242014-03-13 10:20:55 +09002109 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
Chris Kellyae926052012-02-20 21:11:53 +00002110 break;
2111 case GetPortStatus:
2112 err = oz_get_port_status(hcd, windex, buf);
2113 break;
2114 case SetHubFeature:
Joe Perchesf724b582013-07-23 13:45:00 +01002115 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002116 break;
2117 case SetPortFeature:
2118 err = oz_set_port_feature(hcd, wvalue, windex);
2119 break;
2120 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002121 oz_dbg(HUB, "Other: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002122 break;
2123 }
2124 return err;
2125}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002126
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002127/*
Chris Kellyae926052012-02-20 21:11:53 +00002128 * Context: process
2129 */
2130static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2131{
2132 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002133
Chris Kellyae926052012-02-20 21:11:53 +00002134 ozhcd = oz_hcd_private(hcd);
2135 spin_lock_bh(&ozhcd->hcd_lock);
2136 hcd->state = HC_STATE_SUSPENDED;
2137 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2138 spin_unlock_bh(&ozhcd->hcd_lock);
2139 return 0;
2140}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002141
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002142/*
Chris Kellyae926052012-02-20 21:11:53 +00002143 * Context: process
2144 */
2145static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2146{
2147 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002148
Chris Kellyae926052012-02-20 21:11:53 +00002149 ozhcd = oz_hcd_private(hcd);
2150 spin_lock_bh(&ozhcd->hcd_lock);
2151 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2152 hcd->state = HC_STATE_RUNNING;
2153 spin_unlock_bh(&ozhcd->hcd_lock);
2154 return 0;
2155}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002156
Chris Kellyae926052012-02-20 21:11:53 +00002157static void oz_plat_shutdown(struct platform_device *dev)
2158{
Chris Kellyae926052012-02-20 21:11:53 +00002159}
Joe Perches30f1e5a2013-07-23 13:44:59 +01002160
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002161/*
Chris Kellyae926052012-02-20 21:11:53 +00002162 * Context: process
2163 */
2164static int oz_plat_probe(struct platform_device *dev)
2165{
2166 int i;
2167 int err;
2168 struct usb_hcd *hcd;
2169 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002170
Chris Kellyae926052012-02-20 21:11:53 +00002171 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002172 if (hcd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01002173 oz_dbg(ON, "Failed to created hcd object OK\n");
Chris Kellyae926052012-02-20 21:11:53 +00002174 return -ENOMEM;
2175 }
2176 ozhcd = oz_hcd_private(hcd);
2177 memset(ozhcd, 0, sizeof(*ozhcd));
2178 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2179 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2180 INIT_LIST_HEAD(&ozhcd->orphanage);
2181 ozhcd->hcd = hcd;
2182 ozhcd->conn_port = -1;
2183 spin_lock_init(&ozhcd->hcd_lock);
2184 for (i = 0; i < OZ_NB_PORTS; i++) {
2185 struct oz_port *port = &ozhcd->ports[i];
James A Shacklefordba910802014-05-21 14:50:26 -04002186
Chris Kellyae926052012-02-20 21:11:53 +00002187 port->ozhcd = ozhcd;
2188 port->flags = 0;
2189 port->status = 0;
2190 port->bus_addr = 0xff;
2191 spin_lock_init(&port->port_lock);
2192 }
2193 err = usb_add_hcd(hcd, 0, 0);
2194 if (err) {
Joe Perchesf724b582013-07-23 13:45:00 +01002195 oz_dbg(ON, "Failed to add hcd object OK\n");
Chris Kellyae926052012-02-20 21:11:53 +00002196 usb_put_hcd(hcd);
2197 return -1;
2198 }
Peter Chen3c9740a2013-11-05 10:46:02 +08002199 device_wakeup_enable(hcd->self.controller);
2200
Chris Kellyae926052012-02-20 21:11:53 +00002201 spin_lock_bh(&g_hcdlock);
2202 g_ozhcd = ozhcd;
2203 spin_unlock_bh(&g_hcdlock);
2204 return 0;
2205}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002206
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002207/*
Chris Kellyae926052012-02-20 21:11:53 +00002208 * Context: unknown
2209 */
2210static int oz_plat_remove(struct platform_device *dev)
2211{
2212 struct usb_hcd *hcd = platform_get_drvdata(dev);
2213 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002214
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002215 if (hcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00002216 return -1;
2217 ozhcd = oz_hcd_private(hcd);
2218 spin_lock_bh(&g_hcdlock);
2219 if (ozhcd == g_ozhcd)
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002220 g_ozhcd = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00002221 spin_unlock_bh(&g_hcdlock);
Joe Perchesf724b582013-07-23 13:45:00 +01002222 oz_dbg(ON, "Clearing orphanage\n");
Chris Kellyae926052012-02-20 21:11:53 +00002223 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
Joe Perchesf724b582013-07-23 13:45:00 +01002224 oz_dbg(ON, "Removing hcd\n");
Chris Kellyae926052012-02-20 21:11:53 +00002225 usb_remove_hcd(hcd);
2226 usb_put_hcd(hcd);
Chris Kellyae926052012-02-20 21:11:53 +00002227 return 0;
2228}
Joe Perchesf724b582013-07-23 13:45:00 +01002229
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002230/*
Chris Kellyae926052012-02-20 21:11:53 +00002231 * Context: unknown
2232 */
2233static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2234{
Chris Kellyae926052012-02-20 21:11:53 +00002235 return 0;
2236}
Joe Perchesf724b582013-07-23 13:45:00 +01002237
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002238
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002239/*
Chris Kellyae926052012-02-20 21:11:53 +00002240 * Context: unknown
2241 */
2242static int oz_plat_resume(struct platform_device *dev)
2243{
Chris Kellyae926052012-02-20 21:11:53 +00002244 return 0;
2245}
Joe Perchesf724b582013-07-23 13:45:00 +01002246
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002247/*
Chris Kellyae926052012-02-20 21:11:53 +00002248 * Context: process
2249 */
2250int oz_hcd_init(void)
2251{
2252 int err;
Rupesh Gujare18f81912013-08-13 18:24:21 +01002253
Chris Kellyae926052012-02-20 21:11:53 +00002254 if (usb_disabled())
2255 return -ENODEV;
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +02002256
2257 oz_urb_link_cache = KMEM_CACHE(oz_urb_link, 0);
2258 if (!oz_urb_link_cache)
2259 return -ENOMEM;
2260
Chris Kellyae926052012-02-20 21:11:53 +00002261 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2262 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2263 err = platform_driver_register(&g_oz_plat_drv);
Joe Perchesf724b582013-07-23 13:45:00 +01002264 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
Chris Kellyae926052012-02-20 21:11:53 +00002265 if (err)
2266 goto error;
2267 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002268 if (g_plat_dev == NULL) {
Chris Kellyae926052012-02-20 21:11:53 +00002269 err = -ENOMEM;
2270 goto error1;
2271 }
Joe Perchesf724b582013-07-23 13:45:00 +01002272 oz_dbg(ON, "platform_device_alloc() succeeded\n");
Chris Kellyae926052012-02-20 21:11:53 +00002273 err = platform_device_add(g_plat_dev);
2274 if (err)
2275 goto error2;
Joe Perchesf724b582013-07-23 13:45:00 +01002276 oz_dbg(ON, "platform_device_add() succeeded\n");
Chris Kellyae926052012-02-20 21:11:53 +00002277 return 0;
2278error2:
2279 platform_device_put(g_plat_dev);
2280error1:
2281 platform_driver_unregister(&g_oz_plat_drv);
2282error:
2283 tasklet_disable(&g_urb_process_tasklet);
2284 tasklet_disable(&g_urb_cancel_tasklet);
Joe Perchesf724b582013-07-23 13:45:00 +01002285 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
Chris Kellyae926052012-02-20 21:11:53 +00002286 return err;
2287}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002288
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002289/*
Chris Kellyae926052012-02-20 21:11:53 +00002290 * Context: process
2291 */
2292void oz_hcd_term(void)
2293{
Rupesh Gujare0140eb22013-08-27 16:53:42 +01002294 msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
Xiaotian Feng984a4a02012-10-31 18:56:48 +08002295 tasklet_kill(&g_urb_process_tasklet);
2296 tasklet_kill(&g_urb_cancel_tasklet);
Chris Kellyae926052012-02-20 21:11:53 +00002297 platform_device_unregister(g_plat_dev);
2298 platform_driver_unregister(&g_oz_plat_drv);
Joe Perchesf724b582013-07-23 13:45:00 +01002299 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
Christoph Jaeger9e6fbdd2014-08-08 07:59:24 +02002300 kmem_cache_destroy(oz_urb_link_cache);
Chris Kellyae926052012-02-20 21:11:53 +00002301}