blob: abf9dfe664500b90a0b01b34730944392f21f03f [file] [log] [blame]
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -07001/*
2 * Greybus "AP" USB driver
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * Released under the GPLv2 only.
7 */
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/errno.h>
12#include <linux/usb.h>
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070013#include "greybus.h"
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -070014
15static const struct usb_device_id id_table[] = {
16 { USB_DEVICE(0x0000, 0x0000) }, // FIXME
17 { },
18};
19MODULE_DEVICE_TABLE(usb, id_table);
20
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070021struct es1_ap_dev {
22 struct usb_device *usb_dev;
23 struct usb_interface *usb_intf;
24
25 __u8 ap_in_endpoint;
26 __u8 ap_out_endpoint;
27 u8 *ap_buffer;
28
29};
30
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070031/*
32 * Hack, we "know" we will only have one of these at any one time, so only
33 * create one static structure pointer.
34 */
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070035static struct es1_ap_dev *es1_ap_dev;
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070036
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070037static void ap_in_callback(struct urb *urb)
38{
39 struct device *dev = &urb->dev->dev;
40 int status = urb->status;
41 int retval;
42
43 switch (status) {
44 case 0:
45 break;
46 case -EOVERFLOW:
47 dev_err(dev, "%s: overflow actual length is %d\n",
48 __func__, urb->actual_length);
49 case -ECONNRESET:
50 case -ENOENT:
51 case -ESHUTDOWN:
52 case -EILSEQ:
53 /* device is gone, stop sending */
54 return;
55 default:
56 dev_err(dev, "%s: unknown status %d\n", __func__, status);
57 goto exit;
58 }
59
60 /* We have a message, create a new message structure, add it to the
61 * list, and wake up our thread that will process the messages.
62 */
63 gb_new_ap_msg(urb->transfer_buffer, urb->actual_length);
64
65exit:
66 /* resubmit the urb to get more messages */
67 retval = usb_submit_urb(urb, GFP_ATOMIC);
68 if (retval)
69 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
70}
71
72static void ap_out_callback(struct urb *urb)
73{
74 struct device *dev = &urb->dev->dev;
75 int status = urb->status;
76
77 switch (status) {
78 case 0:
79 break;
80 case -EOVERFLOW:
81 dev_err(dev, "%s: overflow actual length is %d\n",
82 __func__, urb->actual_length);
83 case -ECONNRESET:
84 case -ENOENT:
85 case -ESHUTDOWN:
86 case -EILSEQ:
87 /* device is gone, stop sending */
88 return;
89 default:
90 dev_err(dev, "%s: unknown status %d\n", __func__, status);
91 goto exit;
92 }
93
94 // FIXME - queue up next AP message to send???
95exit:
96 return;
97}
98
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -070099
100static int ap_probe(struct usb_interface *interface,
101 const struct usb_device_id *id)
102{
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700103 struct usb_host_interface *iface_desc;
104 struct usb_endpoint_descriptor *endpoint;
105 size_t buffer_size;
106 int i;
107
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700108 if (es1_ap_dev) {
109 dev_err(&interface->dev, "Already have a es1_ap_dev???\n");
110 return -ENODEV;
111 }
112 es1_ap_dev = kzalloc(sizeof(*es1_ap_dev), GFP_KERNEL);
113 if (!es1_ap_dev)
114 return -ENOMEM;
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700115
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700116 // FIXME
117 // figure out endpoint for talking to the AP.
118 iface_desc = interface->cur_altsetting;
119 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
120 endpoint = &iface_desc->endpoint[i].desc;
121
122 if (usb_endpoint_is_bulk_in(endpoint)) {
123 buffer_size = usb_endpoint_maxp(endpoint);
124 // FIXME - Save buffer_size?
125 es1_ap_dev->ap_in_endpoint = endpoint->bEndpointAddress;
126 }
127 if (usb_endpoint_is_bulk_out(endpoint)) {
128 // FIXME - anything else about this we need?
129 es1_ap_dev->ap_out_endpoint = endpoint->bEndpointAddress;
130 }
131 // FIXME - properly exit once found the AP endpoint
132 // FIXME - set up cport endpoints
133 }
134
135 // FIXME - allocate buffer
136 // FIXME = start up talking, then create the gb "devices" based on what the AP tells us.
137
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700138 es1_ap_dev->usb_intf = interface;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700139 es1_ap_dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700140 usb_set_intfdata(interface, es1_ap_dev);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700141 return 0;
142}
143
144static void ap_disconnect(struct usb_interface *interface)
145{
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700146 es1_ap_dev = usb_get_intfdata(interface);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700147
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700148 /* Tear down everything! */
149
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700150 usb_put_dev(es1_ap_dev->usb_dev);
151 kfree(es1_ap_dev->ap_buffer);
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700152 kfree(es1_ap_dev);
153 es1_ap_dev = NULL;
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700154
155}
156
157static struct usb_driver es1_ap_driver = {
158 .name = "es1_ap_driver",
159 .probe = ap_probe,
160 .disconnect = ap_disconnect,
161 .id_table = id_table,
162};
163
164module_usb_driver(es1_ap_driver);
165
166MODULE_LICENSE("GPL");
167MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");