blob: 8589273f35a5a849c14beb9907830a56031a17bb [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[] = {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070016 { USB_DEVICE(0xffff, 0x0001) }, // FIXME
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -070017 { },
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;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070024 struct greybus_host_device *hd;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070025
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070026 __u8 ap_comm_endpoint; /* endpoint to talk to the AP */
27 __u8 ap_in_endpoint; /* bulk in for CPort data */
28 __u8 ap_out_endpoint; /* bulk out for CPort data */
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070029 u8 *ap_buffer;
30
31};
32
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070033static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
34{
35 return (struct es1_ap_dev *)(hd->hd_priv);
36}
37
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070038/*
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070039 * Allocate the actual buffer for this gbuf and device and cport
40 *
41 * We are responsible for setting the following fields in a struct gbuf:
42 * void *hcpriv;
43 * void *transfer_buffer;
44 * u32 transfer_buffer_length;
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070045 */
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070046static int alloc_gbuf(struct gbuf *gbuf, unsigned int size, gfp_t gfp_mask)
47{
48 struct es1_ap_dev *es1 = hd_to_es1(gbuf->gdev->hd);
49 u8 *buffer;
50
51 /* For ES2 we need to figure out what cport is going to what endpoint,
52 * but for ES1, it's so dirt simple, we don't have a choice...
53 *
54 * Also, do a "slow" allocation now, if we need speed, use a cache
55 */
56 buffer = kmalloc(size + 1, gfp_mask);
57 if (!buffer)
58 return -ENOMEM;
59
60 /*
61 * we will encode the cport number in the first byte of the buffer, so
62 * set the second byte to be the "transfer buffer"
63 */
64 buffer[0] = gbuf->cport->number;
65 gbuf->transfer_buffer = &buffer[1];
66 gbuf->transfer_buffer_length = size;
67
68 gbuf->hdpriv = es1; /* really, we could do something else here... */
69
70 return 0;
71}
72
73/* Free the memory we allocated with a gbuf */
74static void free_gbuf(struct gbuf *gbuf)
75{
76 u8 *transfer_buffer;
77 u8 *buffer;
78
79 transfer_buffer = gbuf->transfer_buffer;
80 buffer = &transfer_buffer[-1]; /* yes, we mean -1 */
81 kfree(buffer);
82}
83
84
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -070085/* Main message loop for ap messages */
86/* Odds are, most of this logic can move to core.c someday, but as we only have
87 * one host controller driver for now, let's leave it here */
88static void ap_msg(struct svc_msg *svc_msg, struct greybus_host_device *hd)
89{
90 struct es1_ap_dev *es1 = hd_to_es1(hd);
91
92 /* Look at the message to figure out what to do with it */
93
94
95}
96
97
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070098static struct greybus_host_driver es1_driver = {
99 .hd_priv_size = sizeof(struct es1_ap_dev),
100 .alloc_gbuf = alloc_gbuf,
101 .free_gbuf = free_gbuf,
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700102 .ap_msg = ap_msg,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700103};
104
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700105
Greg Kroah-Hartmanec909872014-09-01 14:39:14 -0700106void ap_in_callback(struct urb *urb)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700107{
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700108 struct es1_ap_dev *es1 = urb->context;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700109 struct device *dev = &urb->dev->dev;
110 int status = urb->status;
111 int retval;
112
113 switch (status) {
114 case 0:
115 break;
116 case -EOVERFLOW:
117 dev_err(dev, "%s: overflow actual length is %d\n",
118 __func__, urb->actual_length);
119 case -ECONNRESET:
120 case -ENOENT:
121 case -ESHUTDOWN:
122 case -EILSEQ:
123 /* device is gone, stop sending */
124 return;
125 default:
126 dev_err(dev, "%s: unknown status %d\n", __func__, status);
127 goto exit;
128 }
129
130 /* We have a message, create a new message structure, add it to the
131 * list, and wake up our thread that will process the messages.
132 */
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700133 gb_new_ap_msg(urb->transfer_buffer, urb->actual_length, es1->hd);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700134
135exit:
136 /* resubmit the urb to get more messages */
137 retval = usb_submit_urb(urb, GFP_ATOMIC);
138 if (retval)
139 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
140}
141
Greg Kroah-Hartmanec909872014-09-01 14:39:14 -0700142void ap_out_callback(struct urb *urb)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700143{
144 struct device *dev = &urb->dev->dev;
145 int status = urb->status;
146
147 switch (status) {
148 case 0:
149 break;
150 case -EOVERFLOW:
151 dev_err(dev, "%s: overflow actual length is %d\n",
152 __func__, urb->actual_length);
153 case -ECONNRESET:
154 case -ENOENT:
155 case -ESHUTDOWN:
156 case -EILSEQ:
157 /* device is gone, stop sending */
158 return;
159 default:
160 dev_err(dev, "%s: unknown status %d\n", __func__, status);
161 goto exit;
162 }
163
164 // FIXME - queue up next AP message to send???
165exit:
166 return;
167}
168
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700169
170static int ap_probe(struct usb_interface *interface,
171 const struct usb_device_id *id)
172{
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700173 struct es1_ap_dev *es1;
174 struct greybus_host_device *hd;
175 struct usb_device *udev;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700176 struct usb_host_interface *iface_desc;
177 struct usb_endpoint_descriptor *endpoint;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700178 int i;
179
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700180 udev = usb_get_dev(interface_to_usbdev(interface));
181
182 hd = greybus_create_hd(&es1_driver, &udev->dev);
183 if (!hd)
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700184 return -ENOMEM;
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700185
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700186 es1 = hd_to_es1(hd);
187 es1->hd = hd;
188
189 /* Control endpoint is the pipe to talk to this AP, so save it off */
190 endpoint = &udev->ep0.desc;
191 es1->ap_comm_endpoint = endpoint->bEndpointAddress;
192
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700193 // FIXME
194 // figure out endpoint for talking to the AP.
195 iface_desc = interface->cur_altsetting;
196 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
197 endpoint = &iface_desc->endpoint[i].desc;
198
199 if (usb_endpoint_is_bulk_in(endpoint)) {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700200 es1->ap_in_endpoint = endpoint->bEndpointAddress;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700201 }
202 if (usb_endpoint_is_bulk_out(endpoint)) {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700203 es1->ap_out_endpoint = endpoint->bEndpointAddress;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700204 }
205 // FIXME - properly exit once found the AP endpoint
206 // FIXME - set up cport endpoints
207 }
208
209 // FIXME - allocate buffer
210 // FIXME = start up talking, then create the gb "devices" based on what the AP tells us.
211
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700212 es1->usb_intf = interface;
213 es1->usb_dev = udev;
214 usb_set_intfdata(interface, es1);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700215 return 0;
216}
217
218static void ap_disconnect(struct usb_interface *interface)
219{
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700220 struct es1_ap_dev *es1;
221
222 es1 = usb_get_intfdata(interface);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700223
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700224 /* Tear down everything! */
225
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700226 usb_put_dev(es1->usb_dev);
227 kfree(es1->ap_buffer);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700228
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700229 greybus_remove_hd(es1->hd);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700230}
231
232static struct usb_driver es1_ap_driver = {
233 .name = "es1_ap_driver",
234 .probe = ap_probe,
235 .disconnect = ap_disconnect,
236 .id_table = id_table,
237};
238
239module_usb_driver(es1_ap_driver);
240
241MODULE_LICENSE("GPL");
242MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");