blob: 30bd9286d47d20d556b1adf1424dbe9f85763c5f [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
Chris Kellyae926052012-02-20 21:11:53 +000048/* Maximum number of free urb links that can be kept in the pool.
49 */
50#define OZ_MAX_LINK_POOL_SIZE 16
Rupesh Gujare6e244a82013-08-13 18:24:22 +010051
Chris Kellyae926052012-02-20 21:11:53 +000052/* Get endpoint object from the containing link.
53 */
54#define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
Rupesh Gujare6e244a82013-08-13 18:24:22 +010055
Rupesh Gujare8fd07002013-07-30 13:31:50 +010056/*EP0 timeout before ep0 request is again added to TX queue. (13*8 = 98mSec)
57 */
58#define EP0_TIMEOUT_COUNTER 13
Rupesh Gujare6e244a82013-08-13 18:24:22 +010059
Rupesh Gujare0140eb22013-08-27 16:53:42 +010060/* Debounce time HCD driver should wait before unregistering.
61 */
62#define OZ_HUB_DEBOUNCE_TIMEOUT 1500
63
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010064/*
Chris Kellyae926052012-02-20 21:11:53 +000065 * Used to link urbs together and also store some status information for each
66 * urb.
67 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
68 */
69struct oz_urb_link {
70 struct list_head link;
71 struct urb *urb;
72 struct oz_port *port;
73 u8 req_id;
74 u8 ep_num;
Rupesh Gujare8fd07002013-07-30 13:31:50 +010075 unsigned submit_counter;
Chris Kellyae926052012-02-20 21:11:53 +000076};
77
78/* Holds state information about a USB endpoint.
79 */
Rupesh Gujare0f750be2013-08-23 18:33:29 +010080#define OZ_EP_BUFFER_SIZE_ISOC (1024 * 24)
Rupesh Gujare00d2a462013-08-23 18:33:30 +010081#define OZ_EP_BUFFER_SIZE_INT 512
Chris Kellyae926052012-02-20 21:11:53 +000082struct oz_endpoint {
83 struct list_head urb_list; /* List of oz_urb_link items. */
84 struct list_head link; /* For isoc ep, links in to isoc
85 lists of oz_port. */
Rupesh Gujare8fd07002013-07-30 13:31:50 +010086 struct timespec timestamp;
Chris Kellyae926052012-02-20 21:11:53 +000087 int credit;
88 int credit_ceiling;
89 u8 ep_num;
90 u8 attrib;
91 u8 *buffer;
92 int buffer_size;
93 int in_ix;
94 int out_ix;
95 int buffered_units;
96 unsigned flags;
97 int start_frame;
98};
Rupesh Gujare6e244a82013-08-13 18:24:22 +010099
Chris Kellyae926052012-02-20 21:11:53 +0000100/* Bits in the flags field. */
101#define OZ_F_EP_BUFFERING 0x1
102#define OZ_F_EP_HAVE_STREAM 0x2
103
104/* Holds state information about a USB interface.
105 */
106struct oz_interface {
107 unsigned ep_mask;
108 u8 alt;
109};
110
111/* Holds state information about an hcd port.
112 */
113#define OZ_NB_ENDPOINTS 16
114struct oz_port {
115 unsigned flags;
116 unsigned status;
117 void *hpd;
118 struct oz_hcd *ozhcd;
119 spinlock_t port_lock;
120 u8 bus_addr;
121 u8 next_req_id;
122 u8 config_num;
123 int num_iface;
124 struct oz_interface *iface;
125 struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
126 struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
127 struct list_head isoc_out_ep;
128 struct list_head isoc_in_ep;
129};
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100130
Chris Kellyae926052012-02-20 21:11:53 +0000131#define OZ_PORT_F_PRESENT 0x1
132#define OZ_PORT_F_CHANGED 0x2
133#define OZ_PORT_F_DYING 0x4
134
135/* Data structure in the private context area of struct usb_hcd.
136 */
137#define OZ_NB_PORTS 8
138struct oz_hcd {
139 spinlock_t hcd_lock;
140 struct list_head urb_pending_list;
141 struct list_head urb_cancel_list;
142 struct list_head orphanage;
143 int conn_port; /* Port that is currently connecting, -1 if none.*/
144 struct oz_port ports[OZ_NB_PORTS];
145 uint flags;
146 struct usb_hcd *hcd;
147};
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100148
Chris Kellyae926052012-02-20 21:11:53 +0000149/* Bits in flags field.
150 */
151#define OZ_HDC_F_SUSPENDED 0x1
152
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100153/*
Chris Kellyae926052012-02-20 21:11:53 +0000154 * Static function prototypes.
155 */
156static int oz_hcd_start(struct usb_hcd *hcd);
157static void oz_hcd_stop(struct usb_hcd *hcd);
158static void oz_hcd_shutdown(struct usb_hcd *hcd);
159static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
160 gfp_t mem_flags);
161static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
162static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
163 struct usb_host_endpoint *ep);
164static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
165 struct usb_host_endpoint *ep);
166static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
167static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
168static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
169 u16 windex, char *buf, u16 wlength);
170static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
171static int oz_hcd_bus_resume(struct usb_hcd *hcd);
172static int oz_plat_probe(struct platform_device *dev);
173static int oz_plat_remove(struct platform_device *dev);
174static void oz_plat_shutdown(struct platform_device *dev);
175static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
176static int oz_plat_resume(struct platform_device *dev);
177static void oz_urb_process_tasklet(unsigned long unused);
178static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
179 struct oz_port *port, struct usb_host_config *config,
180 gfp_t mem_flags);
181static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
182 struct oz_port *port);
183static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
184 struct oz_port *port,
185 struct usb_host_interface *intf, gfp_t mem_flags);
186static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
187 struct oz_port *port, int if_ix);
188static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
189 gfp_t mem_flags);
190static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
191 struct urb *urb);
192static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100193
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100194/*
Chris Kellyae926052012-02-20 21:11:53 +0000195 * Static external variables.
196 */
197static struct platform_device *g_plat_dev;
198static struct oz_hcd *g_ozhcd;
199static DEFINE_SPINLOCK(g_hcdlock); /* Guards g_ozhcd. */
200static const char g_hcd_name[] = "Ozmo WPAN";
201static struct list_head *g_link_pool;
202static int g_link_pool_size;
203static DEFINE_SPINLOCK(g_link_lock);
204static DEFINE_SPINLOCK(g_tasklet_lock);
205static struct tasklet_struct g_urb_process_tasklet;
206static struct tasklet_struct g_urb_cancel_tasklet;
207static atomic_t g_pending_urbs = ATOMIC_INIT(0);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100208static atomic_t g_usb_frame_number = ATOMIC_INIT(0);
Chris Kellyae926052012-02-20 21:11:53 +0000209static const struct hc_driver g_oz_hc_drv = {
210 .description = g_hcd_name,
211 .product_desc = "Ozmo Devices WPAN",
212 .hcd_priv_size = sizeof(struct oz_hcd),
213 .flags = HCD_USB11,
214 .start = oz_hcd_start,
215 .stop = oz_hcd_stop,
216 .shutdown = oz_hcd_shutdown,
217 .urb_enqueue = oz_hcd_urb_enqueue,
218 .urb_dequeue = oz_hcd_urb_dequeue,
219 .endpoint_disable = oz_hcd_endpoint_disable,
220 .endpoint_reset = oz_hcd_endpoint_reset,
221 .get_frame_number = oz_hcd_get_frame_number,
222 .hub_status_data = oz_hcd_hub_status_data,
223 .hub_control = oz_hcd_hub_control,
224 .bus_suspend = oz_hcd_bus_suspend,
225 .bus_resume = oz_hcd_bus_resume,
226};
227
228static struct platform_driver g_oz_plat_drv = {
229 .probe = oz_plat_probe,
230 .remove = oz_plat_remove,
231 .shutdown = oz_plat_shutdown,
232 .suspend = oz_plat_suspend,
233 .resume = oz_plat_resume,
234 .driver = {
235 .name = OZ_PLAT_DEV_NAME,
236 .owner = THIS_MODULE,
237 },
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 * Gets our private context area (which is of type struct oz_hcd) from the
242 * usb_hcd structure.
243 * Context: any
244 */
245static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
246{
247 return (struct oz_hcd *)hcd->hcd_priv;
248}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100249
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100250/*
Chris Kellyae926052012-02-20 21:11:53 +0000251 * Searches list of ports to find the index of the one with a specified USB
252 * bus address. If none of the ports has the bus address then the connection
253 * port is returned, if there is one or -1 otherwise.
254 * Context: any
255 */
256static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
257{
258 int i;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100259
Chris Kellyae926052012-02-20 21:11:53 +0000260 for (i = 0; i < OZ_NB_PORTS; i++) {
261 if (ozhcd->ports[i].bus_addr == bus_addr)
262 return i;
263 }
264 return ozhcd->conn_port;
265}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100266
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100267/*
Chris Kellyae926052012-02-20 21:11:53 +0000268 * Allocates an urb link, first trying the pool but going to heap if empty.
269 * Context: any
270 */
271static struct oz_urb_link *oz_alloc_urb_link(void)
272{
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100273 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000274 unsigned long irq_state;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100275
Chris Kellyae926052012-02-20 21:11:53 +0000276 spin_lock_irqsave(&g_link_lock, irq_state);
277 if (g_link_pool) {
278 urbl = container_of(g_link_pool, struct oz_urb_link, link);
279 g_link_pool = urbl->link.next;
280 --g_link_pool_size;
281 }
282 spin_unlock_irqrestore(&g_link_lock, irq_state);
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100283 if (urbl == NULL)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800284 urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
Chris Kellyae926052012-02-20 21:11:53 +0000285 return urbl;
286}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100287
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100288/*
Chris Kellyae926052012-02-20 21:11:53 +0000289 * Frees an urb link by putting it in the pool if there is enough space or
290 * deallocating it to heap otherwise.
291 * Context: any
292 */
293static void oz_free_urb_link(struct oz_urb_link *urbl)
294{
295 if (urbl) {
296 unsigned long irq_state;
James A Shacklefordba910802014-05-21 14:50:26 -0400297
Chris Kellyae926052012-02-20 21:11:53 +0000298 spin_lock_irqsave(&g_link_lock, irq_state);
299 if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
300 urbl->link.next = g_link_pool;
301 g_link_pool = &urbl->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100302 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000303 g_link_pool_size++;
304 }
305 spin_unlock_irqrestore(&g_link_lock, irq_state);
Sachin Kamat70101572012-11-20 17:10:10 +0530306 kfree(urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000307 }
308}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100309
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100310/*
Chris Kellyae926052012-02-20 21:11:53 +0000311 * Deallocates all the urb links in the pool.
312 * Context: unknown
313 */
314static void oz_empty_link_pool(void)
315{
316 struct list_head *e;
317 unsigned long irq_state;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100318
Chris Kellyae926052012-02-20 21:11:53 +0000319 spin_lock_irqsave(&g_link_lock, irq_state);
320 e = g_link_pool;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100321 g_link_pool = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000322 g_link_pool_size = 0;
323 spin_unlock_irqrestore(&g_link_lock, irq_state);
324 while (e) {
325 struct oz_urb_link *urbl =
326 container_of(e, struct oz_urb_link, link);
327 e = e->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800328 kfree(urbl);
Chris Kellyae926052012-02-20 21:11:53 +0000329 }
330}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100331
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100332/*
Chris Kellyae926052012-02-20 21:11:53 +0000333 * Allocates endpoint structure and optionally a buffer. If a buffer is
334 * allocated it immediately follows the endpoint structure.
335 * Context: softirq
336 */
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +0100337static struct oz_endpoint *oz_ep_alloc(int buffer_size, gfp_t mem_flags)
Chris Kellyae926052012-02-20 21:11:53 +0000338{
339 struct oz_endpoint *ep =
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800340 kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
Chris Kellyae926052012-02-20 21:11:53 +0000341 if (ep) {
Chris Kellyae926052012-02-20 21:11:53 +0000342 INIT_LIST_HEAD(&ep->urb_list);
343 INIT_LIST_HEAD(&ep->link);
344 ep->credit = -1;
345 if (buffer_size) {
346 ep->buffer_size = buffer_size;
347 ep->buffer = (u8 *)(ep+1);
348 }
349 }
350 return ep;
351}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100352
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100353/*
Chris Kellyae926052012-02-20 21:11:53 +0000354 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
355 * disabled.
356 * Context: softirq or process
357 */
Jérôme Pinot46374d32014-03-13 10:17:17 +0900358static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd,
359 struct urb *urb)
Chris Kellyae926052012-02-20 21:11:53 +0000360{
361 struct oz_urb_link *urbl;
362 struct list_head *e;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100363
Chris Kellyae926052012-02-20 21:11:53 +0000364 list_for_each(e, &ozhcd->urb_cancel_list) {
365 urbl = container_of(e, struct oz_urb_link, link);
366 if (urb == urbl->urb) {
367 list_del_init(e);
368 return urbl;
369 }
370 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100371 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000372}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100373
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100374/*
Chris Kellyae926052012-02-20 21:11:53 +0000375 * This is called when we have finished processing an urb. It unlinks it from
376 * the ep and returns it to the core.
377 * Context: softirq or process
378 */
379static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100380 int status)
Chris Kellyae926052012-02-20 21:11:53 +0000381{
382 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
383 unsigned long irq_state;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100384 struct oz_urb_link *cancel_urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100385
Chris Kellyae926052012-02-20 21:11:53 +0000386 spin_lock_irqsave(&g_tasklet_lock, irq_state);
387 usb_hcd_unlink_urb_from_ep(hcd, urb);
388 /* Clear hcpriv which will prevent it being put in the cancel list
389 * in the event that an attempt is made to cancel it.
390 */
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100391 urb->hcpriv = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000392 /* Walk the cancel list in case the urb is already sitting there.
393 * Since we process the cancel list in a tasklet rather than in
394 * the dequeue function this could happen.
395 */
396 cancel_urbl = oz_uncancel_urb(ozhcd, urb);
397 /* Note: we release lock but do not enable local irqs.
398 * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
399 * or at least other host controllers disable interrupts at this point
400 * so we do the same. We must, however, release the lock otherwise a
401 * deadlock will occur if an urb is submitted to our driver in the urb
402 * completion function. Because we disable interrupts it is possible
403 * that the urb_enqueue function can be called with them disabled.
404 */
405 spin_unlock(&g_tasklet_lock);
406 if (oz_forget_urb(urb)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100407 oz_dbg(ON, "ERROR Unknown URB %p\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +0000408 } else {
Chris Kellyae926052012-02-20 21:11:53 +0000409 atomic_dec(&g_pending_urbs);
Chris Kellyae926052012-02-20 21:11:53 +0000410 usb_hcd_giveback_urb(hcd, urb, status);
411 }
412 spin_lock(&g_tasklet_lock);
413 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
414 if (cancel_urbl)
415 oz_free_urb_link(cancel_urbl);
416}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100417
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100418/*
Chris Kellyae926052012-02-20 21:11:53 +0000419 * Deallocates an endpoint including deallocating any associated stream and
420 * returning any queued urbs to the core.
421 * Context: softirq
422 */
423static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
424{
Chris Kellyae926052012-02-20 21:11:53 +0000425 if (port) {
426 struct list_head list;
427 struct oz_hcd *ozhcd = port->ozhcd;
James A Shacklefordba910802014-05-21 14:50:26 -0400428
Chris Kellyae926052012-02-20 21:11:53 +0000429 INIT_LIST_HEAD(&list);
430 if (ep->flags & OZ_F_EP_HAVE_STREAM)
431 oz_usb_stream_delete(port->hpd, ep->ep_num);
432 /* Transfer URBs to the orphanage while we hold the lock. */
433 spin_lock_bh(&ozhcd->hcd_lock);
434 /* Note: this works even if ep->urb_list is empty.*/
435 list_replace_init(&ep->urb_list, &list);
436 /* Put the URBs in the orphanage. */
437 list_splice_tail(&list, &ozhcd->orphanage);
438 spin_unlock_bh(&ozhcd->hcd_lock);
439 }
Joe Perchesf724b582013-07-23 13:45:00 +0100440 oz_dbg(ON, "Freeing endpoint memory\n");
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800441 kfree(ep);
Chris Kellyae926052012-02-20 21:11:53 +0000442}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100443
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100444/*
Chris Kellyae926052012-02-20 21:11:53 +0000445 * Context: softirq
446 */
Peter Huewea7f74c32013-02-15 21:17:22 +0100447static void oz_complete_buffered_urb(struct oz_port *port,
448 struct oz_endpoint *ep,
Rupesh Gujare28a72292012-07-23 18:49:43 +0100449 struct urb *urb)
450{
Rupesh Gujare4882ad952013-08-23 18:33:31 +0100451 int data_len, available_space, copy_len;
Rupesh Gujare28a72292012-07-23 18:49:43 +0100452
Rupesh Gujare4882ad952013-08-23 18:33:31 +0100453 data_len = ep->buffer[ep->out_ix];
Rupesh Gujare28a72292012-07-23 18:49:43 +0100454 if (data_len <= urb->transfer_buffer_length)
455 available_space = data_len;
456 else
457 available_space = urb->transfer_buffer_length;
458
459 if (++ep->out_ix == ep->buffer_size)
460 ep->out_ix = 0;
461 copy_len = ep->buffer_size - ep->out_ix;
462 if (copy_len >= available_space)
463 copy_len = available_space;
464 memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
465
466 if (copy_len < available_space) {
467 memcpy((urb->transfer_buffer + copy_len), ep->buffer,
468 (available_space - copy_len));
469 ep->out_ix = available_space - copy_len;
470 } else {
471 ep->out_ix += copy_len;
472 }
473 urb->actual_length = available_space;
474 if (ep->out_ix == ep->buffer_size)
475 ep->out_ix = 0;
476
477 ep->buffered_units--;
Joe Perchesf724b582013-07-23 13:45:00 +0100478 oz_dbg(ON, "Trying to give back buffered frame of size=%d\n",
479 available_space);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100480 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Rupesh Gujare28a72292012-07-23 18:49:43 +0100481}
482
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100483/*
Rupesh Gujare28a72292012-07-23 18:49:43 +0100484 * Context: softirq
485 */
Chris Kellyae926052012-02-20 21:11:53 +0000486static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
487 struct urb *urb, u8 req_id)
488{
489 struct oz_urb_link *urbl;
Rupesh Gujare2c663352013-08-13 18:24:23 +0100490 struct oz_endpoint *ep = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000491 int err = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100492
Chris Kellyae926052012-02-20 21:11:53 +0000493 if (ep_addr >= OZ_NB_ENDPOINTS) {
Joe Perchesf724b582013-07-23 13:45:00 +0100494 oz_dbg(ON, "%s: Invalid endpoint number\n", __func__);
Chris Kellyae926052012-02-20 21:11:53 +0000495 return -EINVAL;
496 }
497 urbl = oz_alloc_urb_link();
498 if (!urbl)
499 return -ENOMEM;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100500 urbl->submit_counter = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000501 urbl->urb = urb;
502 urbl->req_id = req_id;
503 urbl->ep_num = ep_addr;
504 /* Hold lock while we insert the URB into the list within the
505 * endpoint structure.
506 */
507 spin_lock_bh(&port->ozhcd->hcd_lock);
508 /* If the urb has been unlinked while out of any list then
509 * complete it now.
510 */
511 if (urb->unlinked) {
512 spin_unlock_bh(&port->ozhcd->hcd_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100513 oz_dbg(ON, "urb %p unlinked so complete immediately\n", urb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100514 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000515 oz_free_urb_link(urbl);
516 return 0;
517 }
Rupesh Gujare2c663352013-08-13 18:24:23 +0100518
519 if (in_dir)
Chris Kellyae926052012-02-20 21:11:53 +0000520 ep = port->in_ep[ep_addr];
Rupesh Gujare2c663352013-08-13 18:24:23 +0100521 else
Chris Kellyae926052012-02-20 21:11:53 +0000522 ep = port->out_ep[ep_addr];
Rupesh Gujare2c663352013-08-13 18:24:23 +0100523 if (!ep) {
Rupesh Gujarebc9aece2013-08-05 18:40:12 +0100524 err = -ENOMEM;
525 goto out;
526 }
Rupesh Gujare28a72292012-07-23 18:49:43 +0100527
528 /*For interrupt endpoint check for buffered data
529 * & complete urb
530 */
531 if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
532 && ep->buffered_units > 0) {
533 oz_free_urb_link(urbl);
534 spin_unlock_bh(&port->ozhcd->hcd_lock);
535 oz_complete_buffered_urb(port, ep, urb);
536 return 0;
537 }
538
Rupesh Gujarec45905a2013-08-13 18:29:21 +0100539 if (port->hpd) {
Chris Kellyae926052012-02-20 21:11:53 +0000540 list_add_tail(&urbl->link, &ep->urb_list);
541 if (!in_dir && ep_addr && (ep->credit < 0)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100542 getrawmonotonic(&ep->timestamp);
Chris Kellyae926052012-02-20 21:11:53 +0000543 ep->credit = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000544 }
545 } else {
546 err = -EPIPE;
547 }
Rupesh Gujarebc9aece2013-08-05 18:40:12 +0100548out:
Chris Kellyae926052012-02-20 21:11:53 +0000549 spin_unlock_bh(&port->ozhcd->hcd_lock);
550 if (err)
551 oz_free_urb_link(urbl);
552 return err;
553}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100554
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100555/*
Chris Kellyae926052012-02-20 21:11:53 +0000556 * Removes an urb from the queue in the endpoint.
557 * Returns 0 if it is found and -EIDRM otherwise.
558 * Context: softirq
559 */
560static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
561 struct urb *urb)
562{
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100563 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000564 struct oz_endpoint *ep;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100565
Chris Kellyae926052012-02-20 21:11:53 +0000566 spin_lock_bh(&port->ozhcd->hcd_lock);
567 if (in_dir)
568 ep = port->in_ep[ep_addr];
569 else
570 ep = port->out_ep[ep_addr];
571 if (ep) {
572 struct list_head *e;
James A Shacklefordba910802014-05-21 14:50:26 -0400573
Chris Kellyae926052012-02-20 21:11:53 +0000574 list_for_each(e, &ep->urb_list) {
575 urbl = container_of(e, struct oz_urb_link, link);
576 if (urbl->urb == urb) {
577 list_del_init(e);
578 break;
579 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100580 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000581 }
582 }
583 spin_unlock_bh(&port->ozhcd->hcd_lock);
584 if (urbl)
585 oz_free_urb_link(urbl);
586 return urbl ? 0 : -EIDRM;
587}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100588
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100589/*
Chris Kellyae926052012-02-20 21:11:53 +0000590 * Finds an urb given its request id.
591 * Context: softirq
592 */
593static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
594 u8 req_id)
595{
596 struct oz_hcd *ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100597 struct urb *urb = NULL;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100598 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +0000599 struct oz_endpoint *ep;
600
601 spin_lock_bh(&ozhcd->hcd_lock);
602 ep = port->out_ep[ep_ix];
603 if (ep) {
604 struct list_head *e;
James A Shacklefordba910802014-05-21 14:50:26 -0400605
Chris Kellyae926052012-02-20 21:11:53 +0000606 list_for_each(e, &ep->urb_list) {
607 urbl = container_of(e, struct oz_urb_link, link);
608 if (urbl->req_id == req_id) {
609 urb = urbl->urb;
610 list_del_init(e);
611 break;
612 }
613 }
614 }
615 spin_unlock_bh(&ozhcd->hcd_lock);
616 /* If urb is non-zero then we we must have an urb link to delete.
617 */
618 if (urb)
619 oz_free_urb_link(urbl);
620 return urb;
621}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100622
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100623/*
Chris Kellyae926052012-02-20 21:11:53 +0000624 * Pre-condition: Port lock must be held.
625 * Context: softirq
626 */
627static void oz_acquire_port(struct oz_port *port, void *hpd)
628{
629 INIT_LIST_HEAD(&port->isoc_out_ep);
630 INIT_LIST_HEAD(&port->isoc_in_ep);
631 port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
632 port->status |= USB_PORT_STAT_CONNECTION |
633 (USB_PORT_STAT_C_CONNECTION << 16);
634 oz_usb_get(hpd);
635 port->hpd = hpd;
636}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100637
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100638/*
Chris Kellyae926052012-02-20 21:11:53 +0000639 * Context: softirq
640 */
641static struct oz_hcd *oz_hcd_claim(void)
642{
643 struct oz_hcd *ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100644
Chris Kellyae926052012-02-20 21:11:53 +0000645 spin_lock_bh(&g_hcdlock);
646 ozhcd = g_ozhcd;
647 if (ozhcd)
648 usb_get_hcd(ozhcd->hcd);
649 spin_unlock_bh(&g_hcdlock);
650 return ozhcd;
651}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100652
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100653/*
Chris Kellyae926052012-02-20 21:11:53 +0000654 * Context: softirq
655 */
656static inline void oz_hcd_put(struct oz_hcd *ozhcd)
657{
658 if (ozhcd)
659 usb_put_hcd(ozhcd->hcd);
660}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100661
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100662/*
Chris Kellyae926052012-02-20 21:11:53 +0000663 * This is called by the protocol handler to notify that a PD has arrived.
664 * We allocate a port to associate with the PD and create a structure for
665 * endpoint 0. This port is made the connection port.
666 * In the event that one of the other port is already a connection port then
667 * we fail.
668 * TODO We should be able to do better than fail and should be able remember
669 * that this port needs configuring and make it the connection port once the
670 * current connection port has been assigned an address. Collisions here are
671 * probably very rare indeed.
672 * Context: softirq
673 */
Rupesh Gujare0503d202013-08-13 18:29:22 +0100674struct oz_port *oz_hcd_pd_arrived(void *hpd)
Chris Kellyae926052012-02-20 21:11:53 +0000675{
676 int i;
Rupesh Gujare3bc0d882013-08-15 12:00:21 +0100677 struct oz_port *hport;
Rupesh Gujarea15e0422013-08-13 18:29:24 +0100678 struct oz_hcd *ozhcd;
Chris Kellyae926052012-02-20 21:11:53 +0000679 struct oz_endpoint *ep;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100680
Chris Kellyae926052012-02-20 21:11:53 +0000681 ozhcd = oz_hcd_claim();
Dan Carpenter95b20b82013-08-13 18:29:26 +0100682 if (!ozhcd)
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100683 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000684 /* Allocate an endpoint object in advance (before holding hcd lock) to
685 * use for out endpoint 0.
686 */
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +0100687 ep = oz_ep_alloc(0, GFP_ATOMIC);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100688 if (!ep)
689 goto err_put;
690
Chris Kellyae926052012-02-20 21:11:53 +0000691 spin_lock_bh(&ozhcd->hcd_lock);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100692 if (ozhcd->conn_port >= 0)
693 goto err_unlock;
694
Chris Kellyae926052012-02-20 21:11:53 +0000695 for (i = 0; i < OZ_NB_PORTS; i++) {
696 struct oz_port *port = &ozhcd->ports[i];
Dan Carpenter95b20b82013-08-13 18:29:26 +0100697
Chris Kellyae926052012-02-20 21:11:53 +0000698 spin_lock(&port->port_lock);
Rupesh Gujare0a7bfbf2013-08-22 17:38:48 +0100699 if (!(port->flags & (OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED))) {
Chris Kellyae926052012-02-20 21:11:53 +0000700 oz_acquire_port(port, hpd);
701 spin_unlock(&port->port_lock);
702 break;
703 }
704 spin_unlock(&port->port_lock);
705 }
Dan Carpenter95b20b82013-08-13 18:29:26 +0100706 if (i == OZ_NB_PORTS)
707 goto err_unlock;
708
709 ozhcd->conn_port = i;
710 hport = &ozhcd->ports[i];
711 hport->out_ep[0] = ep;
712 spin_unlock_bh(&ozhcd->hcd_lock);
713 if (ozhcd->flags & OZ_HDC_F_SUSPENDED)
714 usb_hcd_resume_root_hub(ozhcd->hcd);
715 usb_hcd_poll_rh_status(ozhcd->hcd);
Chris Kellyae926052012-02-20 21:11:53 +0000716 oz_hcd_put(ozhcd);
Dan Carpenter95b20b82013-08-13 18:29:26 +0100717
Chris Kellyae926052012-02-20 21:11:53 +0000718 return hport;
Dan Carpenter95b20b82013-08-13 18:29:26 +0100719
720err_unlock:
721 spin_unlock_bh(&ozhcd->hcd_lock);
722 oz_ep_free(NULL, ep);
723err_put:
724 oz_hcd_put(ozhcd);
725 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000726}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100727
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100728/*
Chris Kellyae926052012-02-20 21:11:53 +0000729 * This is called by the protocol handler to notify that the PD has gone away.
730 * We need to deallocate all resources and then request that the root hub is
731 * polled. We release the reference we hold on the PD.
732 * Context: softirq
733 */
Rupesh Gujare83db51f2013-08-15 12:00:22 +0100734void oz_hcd_pd_departed(struct oz_port *port)
Chris Kellyae926052012-02-20 21:11:53 +0000735{
Chris Kellyae926052012-02-20 21:11:53 +0000736 struct oz_hcd *ozhcd;
737 void *hpd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100738 struct oz_endpoint *ep = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000739
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100740 if (port == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100741 oz_dbg(ON, "%s: port = 0\n", __func__);
Chris Kellyae926052012-02-20 21:11:53 +0000742 return;
743 }
744 ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100745 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +0000746 return;
747 /* Check if this is the connection port - if so clear it.
748 */
749 spin_lock_bh(&ozhcd->hcd_lock);
750 if ((ozhcd->conn_port >= 0) &&
751 (port == &ozhcd->ports[ozhcd->conn_port])) {
Joe Perchesf724b582013-07-23 13:45:00 +0100752 oz_dbg(ON, "Clearing conn_port\n");
Chris Kellyae926052012-02-20 21:11:53 +0000753 ozhcd->conn_port = -1;
754 }
755 spin_lock(&port->port_lock);
756 port->flags |= OZ_PORT_F_DYING;
757 spin_unlock(&port->port_lock);
758 spin_unlock_bh(&ozhcd->hcd_lock);
759
760 oz_clean_endpoints_for_config(ozhcd->hcd, port);
761 spin_lock_bh(&port->port_lock);
762 hpd = port->hpd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100763 port->hpd = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000764 port->bus_addr = 0xff;
Rupesh Gujared7729832013-08-05 18:40:14 +0100765 port->config_num = 0;
Chris Kellyae926052012-02-20 21:11:53 +0000766 port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
767 port->flags |= OZ_PORT_F_CHANGED;
Rupesh Gujare68501432013-08-27 16:53:41 +0100768 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE);
Chris Kellyae926052012-02-20 21:11:53 +0000769 port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
770 /* If there is an endpont 0 then clear the pointer while we hold
771 * the spinlock be we deallocate it after releasing the lock.
772 */
773 if (port->out_ep[0]) {
774 ep = port->out_ep[0];
Peter Huewe7be7d6d2013-02-15 15:22:28 +0100775 port->out_ep[0] = NULL;
Chris Kellyae926052012-02-20 21:11:53 +0000776 }
777 spin_unlock_bh(&port->port_lock);
778 if (ep)
779 oz_ep_free(port, ep);
780 usb_hcd_poll_rh_status(ozhcd->hcd);
781 oz_usb_put(hpd);
782}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100783
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100784/*
Chris Kellyae926052012-02-20 21:11:53 +0000785 * Context: softirq
786 */
787void oz_hcd_pd_reset(void *hpd, void *hport)
788{
789 /* Cleanup the current configuration and report reset to the core.
790 */
791 struct oz_port *port = (struct oz_port *)hport;
792 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100793
Joe Perchesf724b582013-07-23 13:45:00 +0100794 oz_dbg(ON, "PD Reset\n");
Chris Kellyae926052012-02-20 21:11:53 +0000795 spin_lock_bh(&port->port_lock);
796 port->flags |= OZ_PORT_F_CHANGED;
797 port->status |= USB_PORT_STAT_RESET;
798 port->status |= (USB_PORT_STAT_C_RESET << 16);
799 spin_unlock_bh(&port->port_lock);
800 oz_clean_endpoints_for_config(ozhcd->hcd, port);
801 usb_hcd_poll_rh_status(ozhcd->hcd);
802}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100803
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100804/*
Chris Kellyae926052012-02-20 21:11:53 +0000805 * Context: softirq
806 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100807void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
Chris Kellyae926052012-02-20 21:11:53 +0000808 int length, int offset, int total_size)
809{
810 struct oz_port *port = (struct oz_port *)hport;
811 struct urb *urb;
812 int err = 0;
813
Joe Perchesf724b582013-07-23 13:45:00 +0100814 oz_dbg(ON, "oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
815 length, offset, total_size);
Chris Kellyae926052012-02-20 21:11:53 +0000816 urb = oz_find_urb_by_id(port, 0, req_id);
817 if (!urb)
818 return;
819 if (status == 0) {
820 int copy_len;
821 int required_size = urb->transfer_buffer_length;
James A Shacklefordba910802014-05-21 14:50:26 -0400822
Chris Kellyae926052012-02-20 21:11:53 +0000823 if (required_size > total_size)
824 required_size = total_size;
825 copy_len = required_size-offset;
826 if (length <= copy_len)
827 copy_len = length;
828 memcpy(urb->transfer_buffer+offset, desc, copy_len);
829 offset += copy_len;
830 if (offset < required_size) {
831 struct usb_ctrlrequest *setup =
832 (struct usb_ctrlrequest *)urb->setup_packet;
833 unsigned wvalue = le16_to_cpu(setup->wValue);
James A Shacklefordba910802014-05-21 14:50:26 -0400834
Chris Kellyae926052012-02-20 21:11:53 +0000835 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
836 err = -ENOMEM;
837 else if (oz_usb_get_desc_req(port->hpd, req_id,
838 setup->bRequestType, (u8)(wvalue>>8),
839 (u8)wvalue, setup->wIndex, offset,
840 required_size-offset)) {
841 oz_dequeue_ep_urb(port, 0, 0, urb);
842 err = -ENOMEM;
843 }
844 if (err == 0)
845 return;
846 }
847 }
848 urb->actual_length = total_size;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100849 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000850}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100851
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100852/*
Chris Kellyae926052012-02-20 21:11:53 +0000853 * Context: softirq
854 */
Chris Kellyae926052012-02-20 21:11:53 +0000855static void oz_display_conf_type(u8 t)
856{
857 switch (t) {
858 case USB_REQ_GET_STATUS:
Joe Perchesf724b582013-07-23 13:45:00 +0100859 oz_dbg(ON, "USB_REQ_GET_STATUS - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000860 break;
861 case USB_REQ_CLEAR_FEATURE:
Joe Perchesf724b582013-07-23 13:45:00 +0100862 oz_dbg(ON, "USB_REQ_CLEAR_FEATURE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000863 break;
864 case USB_REQ_SET_FEATURE:
Joe Perchesf724b582013-07-23 13:45:00 +0100865 oz_dbg(ON, "USB_REQ_SET_FEATURE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000866 break;
867 case USB_REQ_SET_ADDRESS:
Joe Perchesf724b582013-07-23 13:45:00 +0100868 oz_dbg(ON, "USB_REQ_SET_ADDRESS - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000869 break;
870 case USB_REQ_GET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +0100871 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000872 break;
873 case USB_REQ_SET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +0100874 oz_dbg(ON, "USB_REQ_SET_DESCRIPTOR - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000875 break;
876 case USB_REQ_GET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +0100877 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000878 break;
879 case USB_REQ_SET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +0100880 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000881 break;
882 case USB_REQ_GET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +0100883 oz_dbg(ON, "USB_REQ_GET_INTERFACE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000884 break;
885 case USB_REQ_SET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +0100886 oz_dbg(ON, "USB_REQ_SET_INTERFACE - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000887 break;
888 case USB_REQ_SYNCH_FRAME:
Joe Perchesf724b582013-07-23 13:45:00 +0100889 oz_dbg(ON, "USB_REQ_SYNCH_FRAME - cnf\n");
Chris Kellyae926052012-02-20 21:11:53 +0000890 break;
891 }
892}
Joe Perches05f608f2013-07-23 13:45:01 +0100893
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100894/*
Chris Kellyae926052012-02-20 21:11:53 +0000895 * Context: softirq
896 */
897static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
898 u8 rcode, u8 config_num)
899{
900 int rc = 0;
901 struct usb_hcd *hcd = port->ozhcd->hcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100902
Chris Kellyae926052012-02-20 21:11:53 +0000903 if (rcode == 0) {
904 port->config_num = config_num;
905 oz_clean_endpoints_for_config(hcd, port);
906 if (oz_build_endpoints_for_config(hcd, port,
907 &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
908 rc = -ENOMEM;
909 }
910 } else {
911 rc = -ENOMEM;
912 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100913 oz_complete_urb(hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +0000914}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100915
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100916/*
Chris Kellyae926052012-02-20 21:11:53 +0000917 * Context: softirq
918 */
919static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
920 u8 rcode, u8 if_num, u8 alt)
921{
922 struct usb_hcd *hcd = port->ozhcd->hcd;
923 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100924
Rupesh Gujare050596a2013-08-23 18:33:28 +0100925 if ((rcode == 0) && (port->config_num > 0)) {
Chris Kellyae926052012-02-20 21:11:53 +0000926 struct usb_host_config *config;
927 struct usb_host_interface *intf;
James A Shacklefordba910802014-05-21 14:50:26 -0400928
Joe Perchesf724b582013-07-23 13:45:00 +0100929 oz_dbg(ON, "Set interface %d alt %d\n", if_num, alt);
Chris Kellyae926052012-02-20 21:11:53 +0000930 oz_clean_endpoints_for_interface(hcd, port, if_num);
931 config = &urb->dev->config[port->config_num-1];
932 intf = &config->intf_cache[if_num]->altsetting[alt];
933 if (oz_build_endpoints_for_interface(hcd, port, intf,
934 GFP_ATOMIC))
935 rc = -ENOMEM;
936 else
937 port->iface[if_num].alt = alt;
938 } else {
939 rc = -ENOMEM;
940 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100941 oz_complete_urb(hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +0000942}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100943
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100944/*
Chris Kellyae926052012-02-20 21:11:53 +0000945 * Context: softirq
946 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100947void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
Chris Kellyae926052012-02-20 21:11:53 +0000948 int data_len)
949{
950 struct oz_port *port = (struct oz_port *)hport;
951 struct urb *urb;
952 struct usb_ctrlrequest *setup;
953 struct usb_hcd *hcd = port->ozhcd->hcd;
954 unsigned windex;
955 unsigned wvalue;
956
Joe Perchesf724b582013-07-23 13:45:00 +0100957 oz_dbg(ON, "oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
Chris Kellyae926052012-02-20 21:11:53 +0000958 urb = oz_find_urb_by_id(port, 0, req_id);
959 if (!urb) {
Joe Perchesf724b582013-07-23 13:45:00 +0100960 oz_dbg(ON, "URB not found\n");
Chris Kellyae926052012-02-20 21:11:53 +0000961 return;
962 }
963 setup = (struct usb_ctrlrequest *)urb->setup_packet;
964 windex = le16_to_cpu(setup->wIndex);
965 wvalue = le16_to_cpu(setup->wValue);
966 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
967 /* Standard requests */
968 oz_display_conf_type(setup->bRequest);
969 switch (setup->bRequest) {
970 case USB_REQ_SET_CONFIGURATION:
971 oz_hcd_complete_set_config(port, urb, rcode,
972 (u8)wvalue);
973 break;
974 case USB_REQ_SET_INTERFACE:
975 oz_hcd_complete_set_interface(port, urb, rcode,
976 (u8)windex, (u8)wvalue);
977 break;
978 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100979 oz_complete_urb(hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000980 }
981
982 } else {
983 int copy_len;
James A Shacklefordba910802014-05-21 14:50:26 -0400984
Joe Perchesf724b582013-07-23 13:45:00 +0100985 oz_dbg(ON, "VENDOR-CLASS - cnf\n");
Rupesh Gujare5494ebd2012-07-23 18:49:45 +0100986 if (data_len) {
987 if (data_len <= urb->transfer_buffer_length)
988 copy_len = data_len;
989 else
990 copy_len = urb->transfer_buffer_length;
Chris Kellyae926052012-02-20 21:11:53 +0000991 memcpy(urb->transfer_buffer, data, copy_len);
Rupesh Gujare5494ebd2012-07-23 18:49:45 +0100992 urb->actual_length = copy_len;
993 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100994 oz_complete_urb(hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +0000995 }
996}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100997
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100998/*
Chris Kellyae926052012-02-20 21:11:53 +0000999 * Context: softirq-serialized
1000 */
Peter Huewedc7f5b32013-02-15 21:17:24 +01001001static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
1002 int data_len)
Chris Kellyae926052012-02-20 21:11:53 +00001003{
1004 int space;
1005 int copy_len;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001006
Chris Kellyae926052012-02-20 21:11:53 +00001007 if (!ep->buffer)
1008 return -1;
1009 space = ep->out_ix-ep->in_ix-1;
1010 if (space < 0)
1011 space += ep->buffer_size;
1012 if (space < (data_len+1)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001013 oz_dbg(ON, "Buffer full\n");
Chris Kellyae926052012-02-20 21:11:53 +00001014 return -1;
1015 }
1016 ep->buffer[ep->in_ix] = (u8)data_len;
1017 if (++ep->in_ix == ep->buffer_size)
1018 ep->in_ix = 0;
1019 copy_len = ep->buffer_size - ep->in_ix;
1020 if (copy_len > data_len)
1021 copy_len = data_len;
1022 memcpy(&ep->buffer[ep->in_ix], data, copy_len);
1023
1024 if (copy_len < data_len) {
1025 memcpy(ep->buffer, data+copy_len, data_len-copy_len);
1026 ep->in_ix = data_len-copy_len;
1027 } else {
1028 ep->in_ix += copy_len;
1029 }
1030 if (ep->in_ix == ep->buffer_size)
1031 ep->in_ix = 0;
1032 ep->buffered_units++;
1033 return 0;
1034}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001035
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001036/*
Chris Kellyae926052012-02-20 21:11:53 +00001037 * Context: softirq-serialized
1038 */
Peter Huewedc7f5b32013-02-15 21:17:24 +01001039void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
Chris Kellyae926052012-02-20 21:11:53 +00001040{
1041 struct oz_port *port = (struct oz_port *)hport;
1042 struct oz_endpoint *ep;
1043 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001044
Chris Kellyae926052012-02-20 21:11:53 +00001045 spin_lock_bh(&ozhcd->hcd_lock);
1046 ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001047 if (ep == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001048 goto done;
1049 switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
1050 case USB_ENDPOINT_XFER_INT:
1051 case USB_ENDPOINT_XFER_BULK:
1052 if (!list_empty(&ep->urb_list)) {
1053 struct oz_urb_link *urbl =
1054 list_first_entry(&ep->urb_list,
1055 struct oz_urb_link, link);
1056 struct urb *urb;
1057 int copy_len;
James A Shacklefordba910802014-05-21 14:50:26 -04001058
Chris Kellyae926052012-02-20 21:11:53 +00001059 list_del_init(&urbl->link);
1060 spin_unlock_bh(&ozhcd->hcd_lock);
1061 urb = urbl->urb;
1062 oz_free_urb_link(urbl);
1063 if (data_len <= urb->transfer_buffer_length)
1064 copy_len = data_len;
1065 else
1066 copy_len = urb->transfer_buffer_length;
1067 memcpy(urb->transfer_buffer, data, copy_len);
1068 urb->actual_length = copy_len;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001069 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001070 return;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001071 } else {
Joe Perchesf724b582013-07-23 13:45:00 +01001072 oz_dbg(ON, "buffering frame as URB is not available\n");
Rupesh Gujare28a72292012-07-23 18:49:43 +01001073 oz_hcd_buffer_data(ep, data, data_len);
Chris Kellyae926052012-02-20 21:11:53 +00001074 }
1075 break;
1076 case USB_ENDPOINT_XFER_ISOC:
1077 oz_hcd_buffer_data(ep, data, data_len);
1078 break;
1079 }
1080done:
1081 spin_unlock_bh(&ozhcd->hcd_lock);
1082}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001083
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001084/*
Chris Kellyae926052012-02-20 21:11:53 +00001085 * Context: unknown
1086 */
1087static inline int oz_usb_get_frame_number(void)
1088{
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001089 return atomic_inc_return(&g_usb_frame_number);
Chris Kellyae926052012-02-20 21:11:53 +00001090}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001091
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001092/*
Chris Kellyae926052012-02-20 21:11:53 +00001093 * Context: softirq
1094 */
1095int oz_hcd_heartbeat(void *hport)
1096{
1097 int rc = 0;
1098 struct oz_port *port = (struct oz_port *)hport;
1099 struct oz_hcd *ozhcd = port->ozhcd;
1100 struct oz_urb_link *urbl;
1101 struct list_head xfr_list;
1102 struct list_head *e;
1103 struct list_head *n;
1104 struct urb *urb;
1105 struct oz_endpoint *ep;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001106 struct timespec ts, delta;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001107
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001108 getrawmonotonic(&ts);
Chris Kellyae926052012-02-20 21:11:53 +00001109 INIT_LIST_HEAD(&xfr_list);
1110 /* Check the OUT isoc endpoints to see if any URB data can be sent.
1111 */
1112 spin_lock_bh(&ozhcd->hcd_lock);
1113 list_for_each(e, &port->isoc_out_ep) {
1114 ep = ep_from_link(e);
1115 if (ep->credit < 0)
1116 continue;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001117 delta = timespec_sub(ts, ep->timestamp);
Rupesh Gujare5cf9d252013-08-05 12:14:04 +01001118 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
Chris Kellyae926052012-02-20 21:11:53 +00001119 if (ep->credit > ep->credit_ceiling)
1120 ep->credit = ep->credit_ceiling;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001121 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001122 while (ep->credit && !list_empty(&ep->urb_list)) {
1123 urbl = list_first_entry(&ep->urb_list,
1124 struct oz_urb_link, link);
1125 urb = urbl->urb;
Rupesh Gujare24168912012-07-23 18:49:44 +01001126 if ((ep->credit + 1) < urb->number_of_packets)
Chris Kellyae926052012-02-20 21:11:53 +00001127 break;
1128 ep->credit -= urb->number_of_packets;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001129 if (ep->credit < 0)
1130 ep->credit = 0;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001131 list_move_tail(&urbl->link, &xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001132 }
1133 }
1134 spin_unlock_bh(&ozhcd->hcd_lock);
1135 /* Send to PD and complete URBs.
1136 */
1137 list_for_each_safe(e, n, &xfr_list) {
Chris Kellyae926052012-02-20 21:11:53 +00001138 urbl = container_of(e, struct oz_urb_link, link);
1139 urb = urbl->urb;
Chris Kellyae926052012-02-20 21:11:53 +00001140 list_del_init(e);
1141 urb->error_count = 0;
1142 urb->start_frame = oz_usb_get_frame_number();
1143 oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1144 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001145 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001146 }
1147 /* Check the IN isoc endpoints to see if any URBs can be completed.
1148 */
1149 spin_lock_bh(&ozhcd->hcd_lock);
1150 list_for_each(e, &port->isoc_in_ep) {
1151 struct oz_endpoint *ep = ep_from_link(e);
James A Shacklefordba910802014-05-21 14:50:26 -04001152
Chris Kellyae926052012-02-20 21:11:53 +00001153 if (ep->flags & OZ_F_EP_BUFFERING) {
Rupesh Gujareb360cb92013-01-29 17:00:55 +00001154 if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
Chris Kellyae926052012-02-20 21:11:53 +00001155 ep->flags &= ~OZ_F_EP_BUFFERING;
1156 ep->credit = 0;
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001157 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001158 ep->start_frame = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001159 }
1160 continue;
1161 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001162 delta = timespec_sub(ts, ep->timestamp);
Rupesh Gujare5cf9d252013-08-05 12:14:04 +01001163 ep->credit += div_u64(timespec_to_ns(&delta), NSEC_PER_MSEC);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001164 ep->timestamp = ts;
Chris Kellyae926052012-02-20 21:11:53 +00001165 while (!list_empty(&ep->urb_list)) {
1166 struct oz_urb_link *urbl =
1167 list_first_entry(&ep->urb_list,
1168 struct oz_urb_link, link);
1169 struct urb *urb = urbl->urb;
1170 int len = 0;
1171 int copy_len;
1172 int i;
James A Shacklefordba910802014-05-21 14:50:26 -04001173
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001174 if (ep->credit < urb->number_of_packets)
Chris Kellyae926052012-02-20 21:11:53 +00001175 break;
1176 if (ep->buffered_units < urb->number_of_packets)
1177 break;
1178 urb->actual_length = 0;
1179 for (i = 0; i < urb->number_of_packets; i++) {
1180 len = ep->buffer[ep->out_ix];
1181 if (++ep->out_ix == ep->buffer_size)
1182 ep->out_ix = 0;
1183 copy_len = ep->buffer_size - ep->out_ix;
1184 if (copy_len > len)
1185 copy_len = len;
1186 memcpy(urb->transfer_buffer,
1187 &ep->buffer[ep->out_ix], copy_len);
1188 if (copy_len < len) {
1189 memcpy(urb->transfer_buffer+copy_len,
1190 ep->buffer, len-copy_len);
1191 ep->out_ix = len-copy_len;
1192 } else
1193 ep->out_ix += copy_len;
1194 if (ep->out_ix == ep->buffer_size)
1195 ep->out_ix = 0;
1196 urb->iso_frame_desc[i].offset =
1197 urb->actual_length;
1198 urb->actual_length += len;
1199 urb->iso_frame_desc[i].actual_length = len;
1200 urb->iso_frame_desc[i].status = 0;
1201 }
1202 ep->buffered_units -= urb->number_of_packets;
1203 urb->error_count = 0;
1204 urb->start_frame = ep->start_frame;
1205 ep->start_frame += urb->number_of_packets;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001206 list_move_tail(&urbl->link, &xfr_list);
Chris Kellyae926052012-02-20 21:11:53 +00001207 ep->credit -= urb->number_of_packets;
Chris Kellyae926052012-02-20 21:11:53 +00001208 }
1209 }
1210 if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1211 rc = 1;
1212 spin_unlock_bh(&ozhcd->hcd_lock);
1213 /* Complete the filled URBs.
1214 */
1215 list_for_each_safe(e, n, &xfr_list) {
1216 urbl = container_of(e, struct oz_urb_link, link);
1217 urb = urbl->urb;
1218 list_del_init(e);
1219 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001220 oz_complete_urb(port->ozhcd->hcd, urb, 0);
Chris Kellyae926052012-02-20 21:11:53 +00001221 }
1222 /* Check if there are any ep0 requests that have timed out.
1223 * If so resent to PD.
1224 */
1225 ep = port->out_ep[0];
1226 if (ep) {
1227 struct list_head *e;
1228 struct list_head *n;
James A Shacklefordba910802014-05-21 14:50:26 -04001229
Chris Kellyae926052012-02-20 21:11:53 +00001230 spin_lock_bh(&ozhcd->hcd_lock);
1231 list_for_each_safe(e, n, &ep->urb_list) {
1232 urbl = container_of(e, struct oz_urb_link, link);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001233 if (urbl->submit_counter > EP0_TIMEOUT_COUNTER) {
1234 oz_dbg(ON, "Request 0x%p timeout\n", urbl->urb);
Wei Yongjun094e74c2012-09-05 14:48:48 +08001235 list_move_tail(e, &xfr_list);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001236 urbl->submit_counter = 0;
1237 } else {
1238 urbl->submit_counter++;
Chris Kellyae926052012-02-20 21:11:53 +00001239 }
1240 }
1241 if (!list_empty(&ep->urb_list))
1242 rc = 1;
1243 spin_unlock_bh(&ozhcd->hcd_lock);
1244 e = xfr_list.next;
1245 while (e != &xfr_list) {
1246 urbl = container_of(e, struct oz_urb_link, link);
1247 e = e->next;
Joe Perchesf724b582013-07-23 13:45:00 +01001248 oz_dbg(ON, "Resending request to PD\n");
Chris Kellyae926052012-02-20 21:11:53 +00001249 oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1250 oz_free_urb_link(urbl);
1251 }
1252 }
1253 return rc;
1254}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001255
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001256/*
Chris Kellyae926052012-02-20 21:11:53 +00001257 * Context: softirq
1258 */
1259static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1260 struct oz_port *port,
1261 struct usb_host_interface *intf, gfp_t mem_flags)
1262{
1263 struct oz_hcd *ozhcd = port->ozhcd;
1264 int i;
1265 int if_ix = intf->desc.bInterfaceNumber;
1266 int request_heartbeat = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001267
Joe Perchesf724b582013-07-23 13:45:00 +01001268 oz_dbg(ON, "interface[%d] = %p\n", if_ix, intf);
Rupesh Gujared236dc12013-08-22 17:38:49 +01001269 if (if_ix >= port->num_iface || port->iface == NULL)
1270 return -ENOMEM;
Chris Kellyae926052012-02-20 21:11:53 +00001271 for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1272 struct usb_host_endpoint *hep = &intf->endpoint[i];
1273 u8 ep_addr = hep->desc.bEndpointAddress;
1274 u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1275 struct oz_endpoint *ep;
1276 int buffer_size = 0;
1277
Joe Perchesf724b582013-07-23 13:45:00 +01001278 oz_dbg(ON, "%d bEndpointAddress = %x\n", i, ep_addr);
Rupesh Gujare28a72292012-07-23 18:49:43 +01001279 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1280 switch (hep->desc.bmAttributes &
1281 USB_ENDPOINT_XFERTYPE_MASK) {
1282 case USB_ENDPOINT_XFER_ISOC:
Rupesh Gujare0f750be2013-08-23 18:33:29 +01001283 buffer_size = OZ_EP_BUFFER_SIZE_ISOC;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001284 break;
1285 case USB_ENDPOINT_XFER_INT:
Rupesh Gujare00d2a462013-08-23 18:33:30 +01001286 buffer_size = OZ_EP_BUFFER_SIZE_INT;
Rupesh Gujare28a72292012-07-23 18:49:43 +01001287 break;
1288 }
Chris Kellyae926052012-02-20 21:11:53 +00001289 }
1290
Rupesh Gujare5f1f7b12013-08-13 18:29:25 +01001291 ep = oz_ep_alloc(buffer_size, mem_flags);
Chris Kellyae926052012-02-20 21:11:53 +00001292 if (!ep) {
1293 oz_clean_endpoints_for_interface(hcd, port, if_ix);
1294 return -ENOMEM;
1295 }
1296 ep->attrib = hep->desc.bmAttributes;
1297 ep->ep_num = ep_num;
1298 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1299 == USB_ENDPOINT_XFER_ISOC) {
Joe Perchesf724b582013-07-23 13:45:00 +01001300 oz_dbg(ON, "wMaxPacketSize = %d\n",
1301 usb_endpoint_maxp(&hep->desc));
Chris Kellyae926052012-02-20 21:11:53 +00001302 ep->credit_ceiling = 200;
1303 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1304 ep->flags |= OZ_F_EP_BUFFERING;
Chris Kellyae926052012-02-20 21:11:53 +00001305 } else {
1306 ep->flags |= OZ_F_EP_HAVE_STREAM;
1307 if (oz_usb_stream_create(port->hpd, ep_num))
1308 ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1309 }
1310 }
1311 spin_lock_bh(&ozhcd->hcd_lock);
1312 if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1313 port->in_ep[ep_num] = ep;
1314 port->iface[if_ix].ep_mask |=
1315 (1<<(ep_num+OZ_NB_ENDPOINTS));
1316 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1317 == USB_ENDPOINT_XFER_ISOC) {
1318 list_add_tail(&ep->link, &port->isoc_in_ep);
1319 request_heartbeat = 1;
1320 }
1321 } else {
1322 port->out_ep[ep_num] = ep;
1323 port->iface[if_ix].ep_mask |= (1<<ep_num);
1324 if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1325 == USB_ENDPOINT_XFER_ISOC) {
1326 list_add_tail(&ep->link, &port->isoc_out_ep);
1327 request_heartbeat = 1;
1328 }
1329 }
1330 spin_unlock_bh(&ozhcd->hcd_lock);
1331 if (request_heartbeat && port->hpd)
1332 oz_usb_request_heartbeat(port->hpd);
1333 }
1334 return 0;
1335}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001336
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001337/*
Chris Kellyae926052012-02-20 21:11:53 +00001338 * Context: softirq
1339 */
1340static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1341 struct oz_port *port, int if_ix)
1342{
1343 struct oz_hcd *ozhcd = port->ozhcd;
1344 unsigned mask;
1345 int i;
1346 struct list_head ep_list;
1347
Joe Perchesf724b582013-07-23 13:45:00 +01001348 oz_dbg(ON, "Deleting endpoints for interface %d\n", if_ix);
Chris Kellyae926052012-02-20 21:11:53 +00001349 if (if_ix >= port->num_iface)
1350 return;
1351 INIT_LIST_HEAD(&ep_list);
1352 spin_lock_bh(&ozhcd->hcd_lock);
1353 mask = port->iface[if_ix].ep_mask;
1354 port->iface[if_ix].ep_mask = 0;
1355 for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1356 struct list_head *e;
1357 /* Gather OUT endpoints.
1358 */
1359 if ((mask & (1<<i)) && port->out_ep[i]) {
1360 e = &port->out_ep[i]->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001361 port->out_ep[i] = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001362 /* Remove from isoc list if present.
1363 */
Wei Yongjun094e74c2012-09-05 14:48:48 +08001364 list_move_tail(e, &ep_list);
Chris Kellyae926052012-02-20 21:11:53 +00001365 }
1366 /* Gather IN endpoints.
1367 */
1368 if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1369 e = &port->in_ep[i]->link;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001370 port->in_ep[i] = NULL;
Wei Yongjun094e74c2012-09-05 14:48:48 +08001371 list_move_tail(e, &ep_list);
Chris Kellyae926052012-02-20 21:11:53 +00001372 }
1373 }
1374 spin_unlock_bh(&ozhcd->hcd_lock);
1375 while (!list_empty(&ep_list)) {
1376 struct oz_endpoint *ep =
1377 list_first_entry(&ep_list, struct oz_endpoint, link);
1378 list_del_init(&ep->link);
1379 oz_ep_free(port, ep);
1380 }
1381}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001382
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001383/*
Chris Kellyae926052012-02-20 21:11:53 +00001384 * Context: softirq
1385 */
1386static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1387 struct oz_port *port, struct usb_host_config *config,
1388 gfp_t mem_flags)
1389{
1390 struct oz_hcd *ozhcd = port->ozhcd;
1391 int i;
1392 int num_iface = config->desc.bNumInterfaces;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001393
Chris Kellyae926052012-02-20 21:11:53 +00001394 if (num_iface) {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -08001395 struct oz_interface *iface;
1396
1397 iface = kmalloc(num_iface*sizeof(struct oz_interface),
Chris Kellyae926052012-02-20 21:11:53 +00001398 mem_flags | __GFP_ZERO);
1399 if (!iface)
1400 return -ENOMEM;
1401 spin_lock_bh(&ozhcd->hcd_lock);
1402 port->iface = iface;
1403 port->num_iface = num_iface;
1404 spin_unlock_bh(&ozhcd->hcd_lock);
1405 }
1406 for (i = 0; i < num_iface; i++) {
1407 struct usb_host_interface *intf =
1408 &config->intf_cache[i]->altsetting[0];
1409 if (oz_build_endpoints_for_interface(hcd, port, intf,
1410 mem_flags))
1411 goto fail;
1412 }
1413 return 0;
1414fail:
1415 oz_clean_endpoints_for_config(hcd, port);
1416 return -1;
1417}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001418
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001419/*
Chris Kellyae926052012-02-20 21:11:53 +00001420 * Context: softirq
1421 */
1422static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1423 struct oz_port *port)
1424{
1425 struct oz_hcd *ozhcd = port->ozhcd;
1426 int i;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001427
Joe Perchesf724b582013-07-23 13:45:00 +01001428 oz_dbg(ON, "Deleting endpoints for configuration\n");
Chris Kellyae926052012-02-20 21:11:53 +00001429 for (i = 0; i < port->num_iface; i++)
1430 oz_clean_endpoints_for_interface(hcd, port, i);
1431 spin_lock_bh(&ozhcd->hcd_lock);
1432 if (port->iface) {
Joe Perchesf724b582013-07-23 13:45:00 +01001433 oz_dbg(ON, "Freeing interfaces object\n");
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -08001434 kfree(port->iface);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001435 port->iface = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001436 }
1437 port->num_iface = 0;
1438 spin_unlock_bh(&ozhcd->hcd_lock);
1439}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001440
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001441/*
Chris Kellyae926052012-02-20 21:11:53 +00001442 * Context: tasklet
1443 */
1444static void *oz_claim_hpd(struct oz_port *port)
1445{
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001446 void *hpd;
Chris Kellyae926052012-02-20 21:11:53 +00001447 struct oz_hcd *ozhcd = port->ozhcd;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001448
Chris Kellyae926052012-02-20 21:11:53 +00001449 spin_lock_bh(&ozhcd->hcd_lock);
1450 hpd = port->hpd;
1451 if (hpd)
1452 oz_usb_get(hpd);
1453 spin_unlock_bh(&ozhcd->hcd_lock);
1454 return hpd;
1455}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001456
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001457/*
Chris Kellyae926052012-02-20 21:11:53 +00001458 * Context: tasklet
1459 */
1460static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1461 gfp_t mem_flags)
1462{
1463 struct usb_ctrlrequest *setup;
1464 unsigned windex;
1465 unsigned wvalue;
1466 unsigned wlength;
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001467 void *hpd;
Chris Kellyae926052012-02-20 21:11:53 +00001468 u8 req_id;
1469 int rc = 0;
1470 unsigned complete = 0;
1471
1472 int port_ix = -1;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001473 struct oz_port *port = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001474
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001475 oz_dbg(URB, "[%s]:(%p)\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001476 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1477 if (port_ix < 0) {
1478 rc = -EPIPE;
1479 goto out;
1480 }
1481 port = &ozhcd->ports[port_ix];
1482 if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1483 || (port->flags & OZ_PORT_F_DYING)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001484 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1485 port_ix, urb->dev->devnum);
Chris Kellyae926052012-02-20 21:11:53 +00001486 rc = -EPIPE;
1487 goto out;
1488 }
1489 /* Store port in private context data.
1490 */
1491 urb->hcpriv = port;
1492 setup = (struct usb_ctrlrequest *)urb->setup_packet;
1493 windex = le16_to_cpu(setup->wIndex);
1494 wvalue = le16_to_cpu(setup->wValue);
1495 wlength = le16_to_cpu(setup->wLength);
Joe Perchesf724b582013-07-23 13:45:00 +01001496 oz_dbg(CTRL_DETAIL, "bRequestType = %x\n", setup->bRequestType);
1497 oz_dbg(CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1498 oz_dbg(CTRL_DETAIL, "wValue = %x\n", wvalue);
1499 oz_dbg(CTRL_DETAIL, "wIndex = %x\n", windex);
1500 oz_dbg(CTRL_DETAIL, "wLength = %x\n", wlength);
Chris Kellyae926052012-02-20 21:11:53 +00001501
1502 req_id = port->next_req_id++;
1503 hpd = oz_claim_hpd(port);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001504 if (hpd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001505 oz_dbg(ON, "Cannot claim port\n");
Chris Kellyae926052012-02-20 21:11:53 +00001506 rc = -EPIPE;
1507 goto out;
1508 }
1509
1510 if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1511 /* Standard requests
1512 */
1513 switch (setup->bRequest) {
1514 case USB_REQ_GET_DESCRIPTOR:
Joe Perchesf724b582013-07-23 13:45:00 +01001515 oz_dbg(ON, "USB_REQ_GET_DESCRIPTOR - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001516 break;
1517 case USB_REQ_SET_ADDRESS:
Joe Perchesf724b582013-07-23 13:45:00 +01001518 oz_dbg(ON, "USB_REQ_SET_ADDRESS - req\n");
1519 oz_dbg(ON, "Port %d address is 0x%x\n",
1520 ozhcd->conn_port,
1521 (u8)le16_to_cpu(setup->wValue));
Chris Kellyae926052012-02-20 21:11:53 +00001522 spin_lock_bh(&ozhcd->hcd_lock);
1523 if (ozhcd->conn_port >= 0) {
1524 ozhcd->ports[ozhcd->conn_port].bus_addr =
1525 (u8)le16_to_cpu(setup->wValue);
Joe Perchesf724b582013-07-23 13:45:00 +01001526 oz_dbg(ON, "Clearing conn_port\n");
Chris Kellyae926052012-02-20 21:11:53 +00001527 ozhcd->conn_port = -1;
1528 }
1529 spin_unlock_bh(&ozhcd->hcd_lock);
1530 complete = 1;
1531 break;
1532 case USB_REQ_SET_CONFIGURATION:
Joe Perchesf724b582013-07-23 13:45:00 +01001533 oz_dbg(ON, "USB_REQ_SET_CONFIGURATION - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001534 break;
1535 case USB_REQ_GET_CONFIGURATION:
Masanari Iida8dc24592012-04-25 23:28:35 +09001536 /* We short circuit this case and reply directly since
Chris Kellyae926052012-02-20 21:11:53 +00001537 * we have the selected configuration number cached.
1538 */
Joe Perchesf724b582013-07-23 13:45:00 +01001539 oz_dbg(ON, "USB_REQ_GET_CONFIGURATION - reply now\n");
Chris Kellyae926052012-02-20 21:11:53 +00001540 if (urb->transfer_buffer_length >= 1) {
1541 urb->actual_length = 1;
1542 *((u8 *)urb->transfer_buffer) =
1543 port->config_num;
1544 complete = 1;
1545 } else {
1546 rc = -EPIPE;
1547 }
1548 break;
1549 case USB_REQ_GET_INTERFACE:
Masanari Iida8dc24592012-04-25 23:28:35 +09001550 /* We short circuit this case and reply directly since
Chris Kellyae926052012-02-20 21:11:53 +00001551 * we have the selected interface alternative cached.
1552 */
Joe Perchesf724b582013-07-23 13:45:00 +01001553 oz_dbg(ON, "USB_REQ_GET_INTERFACE - reply now\n");
Chris Kellyae926052012-02-20 21:11:53 +00001554 if (urb->transfer_buffer_length >= 1) {
1555 urb->actual_length = 1;
1556 *((u8 *)urb->transfer_buffer) =
1557 port->iface[(u8)windex].alt;
Joe Perchesf724b582013-07-23 13:45:00 +01001558 oz_dbg(ON, "interface = %d alt = %d\n",
1559 windex, port->iface[(u8)windex].alt);
Chris Kellyae926052012-02-20 21:11:53 +00001560 complete = 1;
1561 } else {
1562 rc = -EPIPE;
1563 }
1564 break;
1565 case USB_REQ_SET_INTERFACE:
Joe Perchesf724b582013-07-23 13:45:00 +01001566 oz_dbg(ON, "USB_REQ_SET_INTERFACE - req\n");
Chris Kellyae926052012-02-20 21:11:53 +00001567 break;
1568 }
1569 }
1570 if (!rc && !complete) {
1571 int data_len = 0;
James A Shacklefordba910802014-05-21 14:50:26 -04001572
Chris Kellyae926052012-02-20 21:11:53 +00001573 if ((setup->bRequestType & USB_DIR_IN) == 0)
1574 data_len = wlength;
Rupesh Gujare5494ebd2012-07-23 18:49:45 +01001575 urb->actual_length = data_len;
Chris Kellyae926052012-02-20 21:11:53 +00001576 if (oz_usb_control_req(port->hpd, req_id, setup,
1577 urb->transfer_buffer, data_len)) {
1578 rc = -ENOMEM;
1579 } else {
1580 /* Note: we are queuing the request after we have
Justin P. Mattock595914f2012-03-19 08:17:50 -07001581 * submitted it to be transmitted. If the request were
Chris Kellyae926052012-02-20 21:11:53 +00001582 * to complete before we queued it then it would not
1583 * be found in the queue. It seems impossible for
1584 * this to happen but if it did the request would
1585 * be resubmitted so the problem would hopefully
1586 * resolve itself. Putting the request into the
1587 * queue before it has been sent is worse since the
1588 * urb could be cancelled while we are using it
1589 * to build the request.
1590 */
1591 if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1592 rc = -ENOMEM;
1593 }
1594 }
1595 oz_usb_put(hpd);
1596out:
1597 if (rc || complete) {
Joe Perchesf724b582013-07-23 13:45:00 +01001598 oz_dbg(ON, "Completing request locally\n");
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001599 oz_complete_urb(ozhcd->hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +00001600 } else {
1601 oz_usb_request_heartbeat(port->hpd);
1602 }
1603}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001604
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001605/*
Chris Kellyae926052012-02-20 21:11:53 +00001606 * Context: tasklet
1607 */
1608static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1609{
1610 int rc = 0;
1611 struct oz_port *port = urb->hcpriv;
1612 u8 ep_addr;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001613
Chris Kellyae926052012-02-20 21:11:53 +00001614 /* When we are paranoid we keep a list of urbs which we check against
1615 * before handing one back. This is just for debugging during
1616 * development and should be turned off in the released driver.
1617 */
1618 oz_remember_urb(urb);
1619 /* Check buffer is valid.
1620 */
1621 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1622 return -EINVAL;
1623 /* Check if there is a device at the port - refuse if not.
1624 */
1625 if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1626 return -EPIPE;
1627 ep_addr = usb_pipeendpoint(urb->pipe);
1628 if (ep_addr) {
1629 /* If the request is not for EP0 then queue it.
1630 */
1631 if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1632 urb, 0))
1633 rc = -EPIPE;
1634 } else {
1635 oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1636 }
1637 return rc;
1638}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001639
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001640/*
Chris Kellyae926052012-02-20 21:11:53 +00001641 * Context: tasklet
1642 */
1643static void oz_urb_process_tasklet(unsigned long unused)
1644{
1645 unsigned long irq_state;
1646 struct urb *urb;
1647 struct oz_hcd *ozhcd = oz_hcd_claim();
1648 int rc = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001649
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001650 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001651 return;
1652 /* This is called from a tasklet so is in softirq context but the urb
1653 * list is filled from any context so we need to lock
1654 * appropriately while removing urbs.
1655 */
1656 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1657 while (!list_empty(&ozhcd->urb_pending_list)) {
1658 struct oz_urb_link *urbl =
1659 list_first_entry(&ozhcd->urb_pending_list,
1660 struct oz_urb_link, link);
1661 list_del_init(&urbl->link);
1662 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1663 urb = urbl->urb;
1664 oz_free_urb_link(urbl);
1665 rc = oz_urb_process(ozhcd, urb);
1666 if (rc)
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001667 oz_complete_urb(ozhcd->hcd, urb, rc);
Chris Kellyae926052012-02-20 21:11:53 +00001668 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1669 }
1670 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1671 oz_hcd_put(ozhcd);
1672}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001673
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001674/*
Chris Kellyae926052012-02-20 21:11:53 +00001675 * This function searches for the urb in any of the lists it could be in.
1676 * If it is found it is removed from the list and completed. If the urb is
1677 * being processed then it won't be in a list so won't be found. However, the
1678 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1679 * to a non-zero value. When an attempt is made to put the urb back in a list
1680 * the unlinked field will be checked and the urb will then be completed.
1681 * Context: tasklet
1682 */
1683static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1684{
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001685 struct oz_urb_link *urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001686 struct list_head *e;
1687 struct oz_hcd *ozhcd;
1688 unsigned long irq_state;
1689 u8 ix;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001690
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001691 if (port == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001692 oz_dbg(ON, "%s: ERROR: (%p) port is null\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001693 return;
1694 }
1695 ozhcd = port->ozhcd;
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001696 if (ozhcd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01001697 oz_dbg(ON, "%s; ERROR: (%p) ozhcd is null\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001698 return;
1699 }
1700
1701 /* Look in the tasklet queue.
1702 */
1703 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1704 list_for_each(e, &ozhcd->urb_cancel_list) {
1705 urbl = container_of(e, struct oz_urb_link, link);
1706 if (urb == urbl->urb) {
1707 list_del_init(e);
1708 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1709 goto out2;
1710 }
1711 }
1712 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001713 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001714
1715 /* Look in the orphanage.
1716 */
1717 spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1718 list_for_each(e, &ozhcd->orphanage) {
1719 urbl = container_of(e, struct oz_urb_link, link);
1720 if (urbl->urb == urb) {
1721 list_del(e);
Joe Perchesf724b582013-07-23 13:45:00 +01001722 oz_dbg(ON, "Found urb in orphanage\n");
Chris Kellyae926052012-02-20 21:11:53 +00001723 goto out;
1724 }
1725 }
1726 ix = (ep_num & 0xf);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001727 urbl = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001728 if ((ep_num & USB_DIR_IN) && ix)
1729 urbl = oz_remove_urb(port->in_ep[ix], urb);
1730 else
1731 urbl = oz_remove_urb(port->out_ep[ix], urb);
1732out:
1733 spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1734out2:
1735 if (urbl) {
1736 urb->actual_length = 0;
1737 oz_free_urb_link(urbl);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001738 oz_complete_urb(ozhcd->hcd, urb, -EPIPE);
Chris Kellyae926052012-02-20 21:11:53 +00001739 }
1740}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001741
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001742/*
Chris Kellyae926052012-02-20 21:11:53 +00001743 * Context: tasklet
1744 */
1745static void oz_urb_cancel_tasklet(unsigned long unused)
1746{
1747 unsigned long irq_state;
1748 struct urb *urb;
1749 struct oz_hcd *ozhcd = oz_hcd_claim();
Rupesh Gujare18f81912013-08-13 18:24:21 +01001750
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001751 if (ozhcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001752 return;
1753 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1754 while (!list_empty(&ozhcd->urb_cancel_list)) {
1755 struct oz_urb_link *urbl =
1756 list_first_entry(&ozhcd->urb_cancel_list,
1757 struct oz_urb_link, link);
1758 list_del_init(&urbl->link);
1759 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1760 urb = urbl->urb;
1761 if (urb->unlinked)
1762 oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1763 oz_free_urb_link(urbl);
1764 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1765 }
1766 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1767 oz_hcd_put(ozhcd);
1768}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001769
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001770/*
Chris Kellyae926052012-02-20 21:11:53 +00001771 * Context: unknown
1772 */
1773static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1774{
1775 if (ozhcd) {
1776 struct oz_urb_link *urbl;
James A Shacklefordba910802014-05-21 14:50:26 -04001777
Chris Kellyae926052012-02-20 21:11:53 +00001778 while (!list_empty(&ozhcd->orphanage)) {
1779 urbl = list_first_entry(&ozhcd->orphanage,
1780 struct oz_urb_link, link);
1781 list_del(&urbl->link);
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001782 oz_complete_urb(ozhcd->hcd, urbl->urb, status);
Chris Kellyae926052012-02-20 21:11:53 +00001783 oz_free_urb_link(urbl);
1784 }
1785 }
1786}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001787
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001788/*
Chris Kellyae926052012-02-20 21:11:53 +00001789 * Context: unknown
1790 */
1791static int oz_hcd_start(struct usb_hcd *hcd)
1792{
Chris Kellyae926052012-02-20 21:11:53 +00001793 hcd->power_budget = 200;
1794 hcd->state = HC_STATE_RUNNING;
1795 hcd->uses_new_polling = 1;
1796 return 0;
1797}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001798
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001799/*
Chris Kellyae926052012-02-20 21:11:53 +00001800 * Context: unknown
1801 */
1802static void oz_hcd_stop(struct usb_hcd *hcd)
1803{
Chris Kellyae926052012-02-20 21:11:53 +00001804}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001805
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001806/*
Chris Kellyae926052012-02-20 21:11:53 +00001807 * Context: unknown
1808 */
1809static void oz_hcd_shutdown(struct usb_hcd *hcd)
1810{
Chris Kellyae926052012-02-20 21:11:53 +00001811}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001812
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001813/*
Chris Kellyae926052012-02-20 21:11:53 +00001814 * Called to queue an urb for the device.
1815 * This function should return a non-zero error code if it fails the urb but
1816 * should not call usb_hcd_giveback_urb().
1817 * Context: any
1818 */
1819static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1820 gfp_t mem_flags)
1821{
1822 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001823 int rc;
Chris Kellyae926052012-02-20 21:11:53 +00001824 int port_ix;
1825 struct oz_port *port;
1826 unsigned long irq_state;
1827 struct oz_urb_link *urbl;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001828
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001829 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001830 if (unlikely(ozhcd == NULL)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001831 oz_dbg(URB, "Refused urb(%p) not ozhcd\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +00001832 return -EPIPE;
1833 }
1834 if (unlikely(hcd->state != HC_STATE_RUNNING)) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001835 oz_dbg(URB, "Refused urb(%p) not running\n", urb);
Chris Kellyae926052012-02-20 21:11:53 +00001836 return -EPIPE;
1837 }
1838 port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1839 if (port_ix < 0)
1840 return -EPIPE;
1841 port = &ozhcd->ports[port_ix];
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001842 if (port == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00001843 return -EPIPE;
Rupesh Gujare0a7bfbf2013-08-22 17:38:48 +01001844 if (!(port->flags & OZ_PORT_F_PRESENT) ||
1845 (port->flags & OZ_PORT_F_CHANGED)) {
Joe Perchesf724b582013-07-23 13:45:00 +01001846 oz_dbg(ON, "Refusing URB port_ix = %d devnum = %d\n",
1847 port_ix, urb->dev->devnum);
Chris Kellyae926052012-02-20 21:11:53 +00001848 return -EPIPE;
1849 }
1850 urb->hcpriv = port;
1851 /* Put request in queue for processing by tasklet.
1852 */
1853 urbl = oz_alloc_urb_link();
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001854 if (unlikely(urbl == NULL))
Chris Kellyae926052012-02-20 21:11:53 +00001855 return -ENOMEM;
1856 urbl->urb = urb;
1857 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1858 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1859 if (unlikely(rc)) {
1860 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1861 oz_free_urb_link(urbl);
1862 return rc;
1863 }
1864 list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1865 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1866 tasklet_schedule(&g_urb_process_tasklet);
1867 atomic_inc(&g_pending_urbs);
1868 return 0;
1869}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001870
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001871/*
Chris Kellyae926052012-02-20 21:11:53 +00001872 * Context: tasklet
1873 */
1874static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1875 struct urb *urb)
1876{
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001877 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +00001878 struct list_head *e;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001879
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001880 if (unlikely(ep == NULL))
1881 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001882 list_for_each(e, &ep->urb_list) {
1883 urbl = container_of(e, struct oz_urb_link, link);
1884 if (urbl->urb == urb) {
1885 list_del_init(e);
1886 if (usb_pipeisoc(urb->pipe)) {
1887 ep->credit -= urb->number_of_packets;
1888 if (ep->credit < 0)
1889 ep->credit = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001890 }
1891 return urbl;
1892 }
1893 }
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001894 return NULL;
Chris Kellyae926052012-02-20 21:11:53 +00001895}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001896
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001897/*
Chris Kellyae926052012-02-20 21:11:53 +00001898 * Called to dequeue a previously submitted urb for the device.
1899 * Context: any
1900 */
1901static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1902{
1903 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
Rupesh Gujarea15e0422013-08-13 18:29:24 +01001904 struct oz_urb_link *urbl;
Chris Kellyae926052012-02-20 21:11:53 +00001905 int rc;
1906 unsigned long irq_state;
Rupesh Gujare18f81912013-08-13 18:24:21 +01001907
Rupesh Gujare8fd07002013-07-30 13:31:50 +01001908 oz_dbg(URB, "%s: (%p)\n", __func__, urb);
Chris Kellyae926052012-02-20 21:11:53 +00001909 urbl = oz_alloc_urb_link();
Peter Huewe7be7d6d2013-02-15 15:22:28 +01001910 if (unlikely(urbl == NULL))
Chris Kellyae926052012-02-20 21:11:53 +00001911 return -ENOMEM;
1912 spin_lock_irqsave(&g_tasklet_lock, irq_state);
1913 /* The following function checks the urb is still in the queue
1914 * maintained by the core and that the unlinked field is zero.
1915 * If both are true the function sets the unlinked field and returns
1916 * zero. Otherwise it returns an error.
1917 */
1918 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1919 /* We have to check we haven't completed the urb or are about
1920 * to complete it. When we do we set hcpriv to 0 so if this has
1921 * already happened we don't put the urb in the cancel queue.
1922 */
1923 if ((rc == 0) && urb->hcpriv) {
1924 urbl->urb = urb;
1925 urbl->port = (struct oz_port *)urb->hcpriv;
1926 urbl->ep_num = usb_pipeendpoint(urb->pipe);
1927 if (usb_pipein(urb->pipe))
1928 urbl->ep_num |= USB_DIR_IN;
1929 list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1930 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1931 tasklet_schedule(&g_urb_cancel_tasklet);
1932 } else {
1933 spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1934 oz_free_urb_link(urbl);
1935 }
1936 return rc;
1937}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001938
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001939/*
Chris Kellyae926052012-02-20 21:11:53 +00001940 * Context: unknown
1941 */
1942static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1943 struct usb_host_endpoint *ep)
1944{
Chris Kellyae926052012-02-20 21:11:53 +00001945}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001946
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001947/*
Chris Kellyae926052012-02-20 21:11:53 +00001948 * Context: unknown
1949 */
1950static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1951 struct usb_host_endpoint *ep)
1952{
Chris Kellyae926052012-02-20 21:11:53 +00001953}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001954
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001955/*
Chris Kellyae926052012-02-20 21:11:53 +00001956 * Context: unknown
1957 */
1958static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1959{
Joe Perchesf724b582013-07-23 13:45:00 +01001960 oz_dbg(ON, "oz_hcd_get_frame_number\n");
Chris Kellyae926052012-02-20 21:11:53 +00001961 return oz_usb_get_frame_number();
1962}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001963
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001964/*
Chris Kellyae926052012-02-20 21:11:53 +00001965 * Context: softirq
1966 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1967 * always do that in softirq context.
1968 */
1969static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1970{
1971 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1972 int i;
1973
Chris Kellyae926052012-02-20 21:11:53 +00001974 buf[0] = 0;
Rupesh Gujarea6669812013-08-05 18:40:15 +01001975 buf[1] = 0;
Chris Kellyae926052012-02-20 21:11:53 +00001976
1977 spin_lock_bh(&ozhcd->hcd_lock);
1978 for (i = 0; i < OZ_NB_PORTS; i++) {
1979 if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
Joe Perchesf724b582013-07-23 13:45:00 +01001980 oz_dbg(HUB, "Port %d changed\n", i);
Chris Kellyae926052012-02-20 21:11:53 +00001981 ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
Rupesh Gujarea6669812013-08-05 18:40:15 +01001982 if (i < 7)
Rupesh Gujare05352812013-08-13 18:24:24 +01001983 buf[0] |= 1 << (i + 1);
Rupesh Gujarea6669812013-08-05 18:40:15 +01001984 else
Rupesh Gujare05352812013-08-13 18:24:24 +01001985 buf[1] |= 1 << (i - 7);
Chris Kellyae926052012-02-20 21:11:53 +00001986 }
1987 }
1988 spin_unlock_bh(&ozhcd->hcd_lock);
Rupesh Gujarea6669812013-08-05 18:40:15 +01001989 if (buf[0] != 0 || buf[1] != 0)
1990 return 2;
1991 else
1992 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00001993}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01001994
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01001995/*
Chris Kellyae926052012-02-20 21:11:53 +00001996 * Context: process
1997 */
1998static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1999 struct usb_hub_descriptor *desc)
2000{
Chris Kellyae926052012-02-20 21:11:53 +00002001 memset(desc, 0, sizeof(*desc));
2002 desc->bDescriptorType = 0x29;
2003 desc->bDescLength = 9;
Jérôme Pinotc6328242014-03-13 10:20:55 +09002004 desc->wHubCharacteristics = (__force __u16)cpu_to_le16(0x0001);
Chris Kellyae926052012-02-20 21:11:53 +00002005 desc->bNbrPorts = OZ_NB_PORTS;
2006}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002007
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002008/*
Chris Kellyae926052012-02-20 21:11:53 +00002009 * Context: process
2010 */
2011static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2012{
2013 struct oz_port *port;
Chris Kellyae926052012-02-20 21:11:53 +00002014 u8 port_id = (u8)windex;
2015 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2016 unsigned set_bits = 0;
2017 unsigned clear_bits = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002018
Chris Kellyae926052012-02-20 21:11:53 +00002019 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2020 return -EPIPE;
2021 port = &ozhcd->ports[port_id-1];
2022 switch (wvalue) {
2023 case USB_PORT_FEAT_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002024 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00002025 break;
2026 case USB_PORT_FEAT_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002027 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00002028 break;
2029 case USB_PORT_FEAT_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002030 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002031 break;
2032 case USB_PORT_FEAT_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002033 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002034 break;
2035 case USB_PORT_FEAT_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002036 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00002037 set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
2038 clear_bits = USB_PORT_STAT_RESET;
2039 ozhcd->ports[port_id-1].bus_addr = 0;
2040 break;
2041 case USB_PORT_FEAT_POWER:
Joe Perchesf724b582013-07-23 13:45:00 +01002042 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
Chris Kellyae926052012-02-20 21:11:53 +00002043 set_bits |= USB_PORT_STAT_POWER;
2044 break;
2045 case USB_PORT_FEAT_LOWSPEED:
Joe Perchesf724b582013-07-23 13:45:00 +01002046 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
Chris Kellyae926052012-02-20 21:11:53 +00002047 break;
2048 case USB_PORT_FEAT_C_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002049 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00002050 break;
2051 case USB_PORT_FEAT_C_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002052 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00002053 break;
2054 case USB_PORT_FEAT_C_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002055 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002056 break;
2057 case USB_PORT_FEAT_C_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002058 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002059 break;
2060 case USB_PORT_FEAT_C_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002061 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00002062 break;
2063 case USB_PORT_FEAT_TEST:
Joe Perchesf724b582013-07-23 13:45:00 +01002064 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
Chris Kellyae926052012-02-20 21:11:53 +00002065 break;
2066 case USB_PORT_FEAT_INDICATOR:
Joe Perchesf724b582013-07-23 13:45:00 +01002067 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
Chris Kellyae926052012-02-20 21:11:53 +00002068 break;
2069 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002070 oz_dbg(HUB, "Other %d\n", wvalue);
Chris Kellyae926052012-02-20 21:11:53 +00002071 break;
2072 }
2073 if (set_bits || clear_bits) {
2074 spin_lock_bh(&port->port_lock);
2075 port->status &= ~clear_bits;
2076 port->status |= set_bits;
2077 spin_unlock_bh(&port->port_lock);
2078 }
Joe Perchesf724b582013-07-23 13:45:00 +01002079 oz_dbg(HUB, "Port[%d] status = 0x%x\n", port_id, port->status);
Peter Senna Tschudina3c97192014-05-20 12:33:47 +02002080 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00002081}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002082
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002083/*
Chris Kellyae926052012-02-20 21:11:53 +00002084 * Context: process
2085 */
2086static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2087{
2088 struct oz_port *port;
Chris Kellyae926052012-02-20 21:11:53 +00002089 u8 port_id = (u8)windex;
2090 struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2091 unsigned clear_bits = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002092
Chris Kellyae926052012-02-20 21:11:53 +00002093 if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2094 return -EPIPE;
2095 port = &ozhcd->ports[port_id-1];
2096 switch (wvalue) {
2097 case USB_PORT_FEAT_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002098 oz_dbg(HUB, "USB_PORT_FEAT_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00002099 break;
2100 case USB_PORT_FEAT_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002101 oz_dbg(HUB, "USB_PORT_FEAT_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00002102 clear_bits = USB_PORT_STAT_ENABLE;
2103 break;
2104 case USB_PORT_FEAT_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002105 oz_dbg(HUB, "USB_PORT_FEAT_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002106 break;
2107 case USB_PORT_FEAT_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002108 oz_dbg(HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002109 break;
2110 case USB_PORT_FEAT_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002111 oz_dbg(HUB, "USB_PORT_FEAT_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00002112 break;
2113 case USB_PORT_FEAT_POWER:
Joe Perchesf724b582013-07-23 13:45:00 +01002114 oz_dbg(HUB, "USB_PORT_FEAT_POWER\n");
Chris Kellyae926052012-02-20 21:11:53 +00002115 clear_bits |= USB_PORT_STAT_POWER;
2116 break;
2117 case USB_PORT_FEAT_LOWSPEED:
Joe Perchesf724b582013-07-23 13:45:00 +01002118 oz_dbg(HUB, "USB_PORT_FEAT_LOWSPEED\n");
Chris Kellyae926052012-02-20 21:11:53 +00002119 break;
2120 case USB_PORT_FEAT_C_CONNECTION:
Joe Perchesf724b582013-07-23 13:45:00 +01002121 oz_dbg(HUB, "USB_PORT_FEAT_C_CONNECTION\n");
Chris Kellyae926052012-02-20 21:11:53 +00002122 clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2123 break;
2124 case USB_PORT_FEAT_C_ENABLE:
Joe Perchesf724b582013-07-23 13:45:00 +01002125 oz_dbg(HUB, "USB_PORT_FEAT_C_ENABLE\n");
Chris Kellyae926052012-02-20 21:11:53 +00002126 clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2127 break;
2128 case USB_PORT_FEAT_C_SUSPEND:
Joe Perchesf724b582013-07-23 13:45:00 +01002129 oz_dbg(HUB, "USB_PORT_FEAT_C_SUSPEND\n");
Chris Kellyae926052012-02-20 21:11:53 +00002130 break;
2131 case USB_PORT_FEAT_C_OVER_CURRENT:
Joe Perchesf724b582013-07-23 13:45:00 +01002132 oz_dbg(HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
Chris Kellyae926052012-02-20 21:11:53 +00002133 break;
2134 case USB_PORT_FEAT_C_RESET:
Joe Perchesf724b582013-07-23 13:45:00 +01002135 oz_dbg(HUB, "USB_PORT_FEAT_C_RESET\n");
Chris Kellyae926052012-02-20 21:11:53 +00002136 clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2137 break;
2138 case USB_PORT_FEAT_TEST:
Joe Perchesf724b582013-07-23 13:45:00 +01002139 oz_dbg(HUB, "USB_PORT_FEAT_TEST\n");
Chris Kellyae926052012-02-20 21:11:53 +00002140 break;
2141 case USB_PORT_FEAT_INDICATOR:
Joe Perchesf724b582013-07-23 13:45:00 +01002142 oz_dbg(HUB, "USB_PORT_FEAT_INDICATOR\n");
Chris Kellyae926052012-02-20 21:11:53 +00002143 break;
2144 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002145 oz_dbg(HUB, "Other %d\n", wvalue);
Chris Kellyae926052012-02-20 21:11:53 +00002146 break;
2147 }
2148 if (clear_bits) {
2149 spin_lock_bh(&port->port_lock);
2150 port->status &= ~clear_bits;
2151 spin_unlock_bh(&port->port_lock);
2152 }
Joe Perchesf724b582013-07-23 13:45:00 +01002153 oz_dbg(HUB, "Port[%d] status = 0x%x\n",
2154 port_id, ozhcd->ports[port_id-1].status);
Peter Senna Tschudina3c97192014-05-20 12:33:47 +02002155 return 0;
Chris Kellyae926052012-02-20 21:11:53 +00002156}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002157
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002158/*
Chris Kellyae926052012-02-20 21:11:53 +00002159 * Context: process
2160 */
2161static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2162{
2163 struct oz_hcd *ozhcd;
Rupesh Gujarea15e0422013-08-13 18:29:24 +01002164 u32 status;
Rupesh Gujare18f81912013-08-13 18:24:21 +01002165
Chris Kellyae926052012-02-20 21:11:53 +00002166 if ((windex < 1) || (windex > OZ_NB_PORTS))
2167 return -EPIPE;
2168 ozhcd = oz_hcd_private(hcd);
Joe Perchesf724b582013-07-23 13:45:00 +01002169 oz_dbg(HUB, "GetPortStatus windex = %d\n", windex);
Chris Kellyae926052012-02-20 21:11:53 +00002170 status = ozhcd->ports[windex-1].status;
2171 put_unaligned(cpu_to_le32(status), (__le32 *)buf);
Joe Perchesf724b582013-07-23 13:45:00 +01002172 oz_dbg(HUB, "Port[%d] status = %x\n", windex, status);
Chris Kellyae926052012-02-20 21:11:53 +00002173 return 0;
2174}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002175
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002176/*
Chris Kellyae926052012-02-20 21:11:53 +00002177 * Context: process
2178 */
2179static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2180 u16 windex, char *buf, u16 wlength)
2181{
2182 int err = 0;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002183
Chris Kellyae926052012-02-20 21:11:53 +00002184 switch (req_type) {
2185 case ClearHubFeature:
Joe Perchesf724b582013-07-23 13:45:00 +01002186 oz_dbg(HUB, "ClearHubFeature: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002187 break;
2188 case ClearPortFeature:
2189 err = oz_clear_port_feature(hcd, wvalue, windex);
2190 break;
2191 case GetHubDescriptor:
2192 oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2193 break;
2194 case GetHubStatus:
Joe Perchesf724b582013-07-23 13:45:00 +01002195 oz_dbg(HUB, "GetHubStatus: req_type = 0x%x\n", req_type);
Jérôme Pinotc6328242014-03-13 10:20:55 +09002196 put_unaligned(cpu_to_le32(0), (__le32 *)buf);
Chris Kellyae926052012-02-20 21:11:53 +00002197 break;
2198 case GetPortStatus:
2199 err = oz_get_port_status(hcd, windex, buf);
2200 break;
2201 case SetHubFeature:
Joe Perchesf724b582013-07-23 13:45:00 +01002202 oz_dbg(HUB, "SetHubFeature: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002203 break;
2204 case SetPortFeature:
2205 err = oz_set_port_feature(hcd, wvalue, windex);
2206 break;
2207 default:
Joe Perchesf724b582013-07-23 13:45:00 +01002208 oz_dbg(HUB, "Other: %d\n", req_type);
Chris Kellyae926052012-02-20 21:11:53 +00002209 break;
2210 }
2211 return err;
2212}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002213
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002214/*
Chris Kellyae926052012-02-20 21:11:53 +00002215 * Context: process
2216 */
2217static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2218{
2219 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002220
Chris Kellyae926052012-02-20 21:11:53 +00002221 ozhcd = oz_hcd_private(hcd);
2222 spin_lock_bh(&ozhcd->hcd_lock);
2223 hcd->state = HC_STATE_SUSPENDED;
2224 ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2225 spin_unlock_bh(&ozhcd->hcd_lock);
2226 return 0;
2227}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002228
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002229/*
Chris Kellyae926052012-02-20 21:11:53 +00002230 * Context: process
2231 */
2232static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2233{
2234 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002235
Chris Kellyae926052012-02-20 21:11:53 +00002236 ozhcd = oz_hcd_private(hcd);
2237 spin_lock_bh(&ozhcd->hcd_lock);
2238 ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2239 hcd->state = HC_STATE_RUNNING;
2240 spin_unlock_bh(&ozhcd->hcd_lock);
2241 return 0;
2242}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002243
Chris Kellyae926052012-02-20 21:11:53 +00002244static void oz_plat_shutdown(struct platform_device *dev)
2245{
Chris Kellyae926052012-02-20 21:11:53 +00002246}
Joe Perches30f1e5a2013-07-23 13:44:59 +01002247
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002248/*
Chris Kellyae926052012-02-20 21:11:53 +00002249 * Context: process
2250 */
2251static int oz_plat_probe(struct platform_device *dev)
2252{
2253 int i;
2254 int err;
2255 struct usb_hcd *hcd;
2256 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002257
Chris Kellyae926052012-02-20 21:11:53 +00002258 hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002259 if (hcd == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +01002260 oz_dbg(ON, "Failed to created hcd object OK\n");
Chris Kellyae926052012-02-20 21:11:53 +00002261 return -ENOMEM;
2262 }
2263 ozhcd = oz_hcd_private(hcd);
2264 memset(ozhcd, 0, sizeof(*ozhcd));
2265 INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2266 INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2267 INIT_LIST_HEAD(&ozhcd->orphanage);
2268 ozhcd->hcd = hcd;
2269 ozhcd->conn_port = -1;
2270 spin_lock_init(&ozhcd->hcd_lock);
2271 for (i = 0; i < OZ_NB_PORTS; i++) {
2272 struct oz_port *port = &ozhcd->ports[i];
James A Shacklefordba910802014-05-21 14:50:26 -04002273
Chris Kellyae926052012-02-20 21:11:53 +00002274 port->ozhcd = ozhcd;
2275 port->flags = 0;
2276 port->status = 0;
2277 port->bus_addr = 0xff;
2278 spin_lock_init(&port->port_lock);
2279 }
2280 err = usb_add_hcd(hcd, 0, 0);
2281 if (err) {
Joe Perchesf724b582013-07-23 13:45:00 +01002282 oz_dbg(ON, "Failed to add hcd object OK\n");
Chris Kellyae926052012-02-20 21:11:53 +00002283 usb_put_hcd(hcd);
2284 return -1;
2285 }
Peter Chen3c9740a2013-11-05 10:46:02 +08002286 device_wakeup_enable(hcd->self.controller);
2287
Chris Kellyae926052012-02-20 21:11:53 +00002288 spin_lock_bh(&g_hcdlock);
2289 g_ozhcd = ozhcd;
2290 spin_unlock_bh(&g_hcdlock);
2291 return 0;
2292}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002293
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002294/*
Chris Kellyae926052012-02-20 21:11:53 +00002295 * Context: unknown
2296 */
2297static int oz_plat_remove(struct platform_device *dev)
2298{
2299 struct usb_hcd *hcd = platform_get_drvdata(dev);
2300 struct oz_hcd *ozhcd;
Joe Perches30f1e5a2013-07-23 13:44:59 +01002301
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002302 if (hcd == NULL)
Chris Kellyae926052012-02-20 21:11:53 +00002303 return -1;
2304 ozhcd = oz_hcd_private(hcd);
2305 spin_lock_bh(&g_hcdlock);
2306 if (ozhcd == g_ozhcd)
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002307 g_ozhcd = NULL;
Chris Kellyae926052012-02-20 21:11:53 +00002308 spin_unlock_bh(&g_hcdlock);
Joe Perchesf724b582013-07-23 13:45:00 +01002309 oz_dbg(ON, "Clearing orphanage\n");
Chris Kellyae926052012-02-20 21:11:53 +00002310 oz_hcd_clear_orphanage(ozhcd, -EPIPE);
Joe Perchesf724b582013-07-23 13:45:00 +01002311 oz_dbg(ON, "Removing hcd\n");
Chris Kellyae926052012-02-20 21:11:53 +00002312 usb_remove_hcd(hcd);
2313 usb_put_hcd(hcd);
2314 oz_empty_link_pool();
2315 return 0;
2316}
Joe Perchesf724b582013-07-23 13:45:00 +01002317
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002318/*
Chris Kellyae926052012-02-20 21:11:53 +00002319 * Context: unknown
2320 */
2321static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2322{
Chris Kellyae926052012-02-20 21:11:53 +00002323 return 0;
2324}
Joe Perchesf724b582013-07-23 13:45:00 +01002325
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002326
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002327/*
Chris Kellyae926052012-02-20 21:11:53 +00002328 * Context: unknown
2329 */
2330static int oz_plat_resume(struct platform_device *dev)
2331{
Chris Kellyae926052012-02-20 21:11:53 +00002332 return 0;
2333}
Joe Perchesf724b582013-07-23 13:45:00 +01002334
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002335/*
Chris Kellyae926052012-02-20 21:11:53 +00002336 * Context: process
2337 */
2338int oz_hcd_init(void)
2339{
2340 int err;
Rupesh Gujare18f81912013-08-13 18:24:21 +01002341
Chris Kellyae926052012-02-20 21:11:53 +00002342 if (usb_disabled())
2343 return -ENODEV;
2344 tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2345 tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2346 err = platform_driver_register(&g_oz_plat_drv);
Joe Perchesf724b582013-07-23 13:45:00 +01002347 oz_dbg(ON, "platform_driver_register() returned %d\n", err);
Chris Kellyae926052012-02-20 21:11:53 +00002348 if (err)
2349 goto error;
2350 g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
Peter Huewe7be7d6d2013-02-15 15:22:28 +01002351 if (g_plat_dev == NULL) {
Chris Kellyae926052012-02-20 21:11:53 +00002352 err = -ENOMEM;
2353 goto error1;
2354 }
Joe Perchesf724b582013-07-23 13:45:00 +01002355 oz_dbg(ON, "platform_device_alloc() succeeded\n");
Chris Kellyae926052012-02-20 21:11:53 +00002356 err = platform_device_add(g_plat_dev);
2357 if (err)
2358 goto error2;
Joe Perchesf724b582013-07-23 13:45:00 +01002359 oz_dbg(ON, "platform_device_add() succeeded\n");
Chris Kellyae926052012-02-20 21:11:53 +00002360 return 0;
2361error2:
2362 platform_device_put(g_plat_dev);
2363error1:
2364 platform_driver_unregister(&g_oz_plat_drv);
2365error:
2366 tasklet_disable(&g_urb_process_tasklet);
2367 tasklet_disable(&g_urb_cancel_tasklet);
Joe Perchesf724b582013-07-23 13:45:00 +01002368 oz_dbg(ON, "oz_hcd_init() failed %d\n", err);
Chris Kellyae926052012-02-20 21:11:53 +00002369 return err;
2370}
Rupesh Gujare6e244a82013-08-13 18:24:22 +01002371
Rupesh Gujare4e7fb822013-08-23 16:11:02 +01002372/*
Chris Kellyae926052012-02-20 21:11:53 +00002373 * Context: process
2374 */
2375void oz_hcd_term(void)
2376{
Rupesh Gujare0140eb22013-08-27 16:53:42 +01002377 msleep(OZ_HUB_DEBOUNCE_TIMEOUT);
Xiaotian Feng984a4a02012-10-31 18:56:48 +08002378 tasklet_kill(&g_urb_process_tasklet);
2379 tasklet_kill(&g_urb_cancel_tasklet);
Chris Kellyae926052012-02-20 21:11:53 +00002380 platform_device_unregister(g_plat_dev);
2381 platform_driver_unregister(&g_oz_plat_drv);
Joe Perchesf724b582013-07-23 13:45:00 +01002382 oz_dbg(ON, "Pending urbs:%d\n", atomic_read(&g_pending_urbs));
Chris Kellyae926052012-02-20 21:11:53 +00002383}