blob: 12a5d0cf76ba919e379e05034579b8754589f413 [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-Hartman43cc32a2014-09-07 13:51:12 -070014#include "svc_msg.h"
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -070015
16static const struct usb_device_id id_table[] = {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070017 { USB_DEVICE(0xffff, 0x0001) }, // FIXME
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -070018 { },
19};
20MODULE_DEVICE_TABLE(usb, id_table);
21
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070022struct es1_ap_dev {
23 struct usb_device *usb_dev;
24 struct usb_interface *usb_intf;
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070025 struct greybus_host_device *hd;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070026
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070027 __u8 ap_comm_endpoint; /* endpoint to talk to the AP */
28 __u8 ap_in_endpoint; /* bulk in for CPort data */
29 __u8 ap_out_endpoint; /* bulk out for CPort data */
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -070030 u8 *ap_buffer;
31
32};
33
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070034static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd)
35{
36 return (struct es1_ap_dev *)(hd->hd_priv);
37}
38
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070039/*
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070040 * Allocate the actual buffer for this gbuf and device and cport
41 *
42 * We are responsible for setting the following fields in a struct gbuf:
43 * void *hcpriv;
44 * void *transfer_buffer;
45 * u32 transfer_buffer_length;
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070046 */
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -070047static int alloc_gbuf(struct gbuf *gbuf, unsigned int size, gfp_t gfp_mask)
48{
49 struct es1_ap_dev *es1 = hd_to_es1(gbuf->gdev->hd);
50 u8 *buffer;
51
52 /* For ES2 we need to figure out what cport is going to what endpoint,
53 * but for ES1, it's so dirt simple, we don't have a choice...
54 *
55 * Also, do a "slow" allocation now, if we need speed, use a cache
56 */
57 buffer = kmalloc(size + 1, gfp_mask);
58 if (!buffer)
59 return -ENOMEM;
60
61 /*
62 * we will encode the cport number in the first byte of the buffer, so
63 * set the second byte to be the "transfer buffer"
64 */
65 buffer[0] = gbuf->cport->number;
66 gbuf->transfer_buffer = &buffer[1];
67 gbuf->transfer_buffer_length = size;
68
69 gbuf->hdpriv = es1; /* really, we could do something else here... */
70
71 return 0;
72}
73
74/* Free the memory we allocated with a gbuf */
75static void free_gbuf(struct gbuf *gbuf)
76{
77 u8 *transfer_buffer;
78 u8 *buffer;
79
80 transfer_buffer = gbuf->transfer_buffer;
81 buffer = &transfer_buffer[-1]; /* yes, we mean -1 */
82 kfree(buffer);
83}
84
Greg Kroah-Hartman43cc32a2014-09-07 13:51:12 -070085static struct svc_msg *svc_msg_alloc(enum svc_function_type type)
86{
87 struct svc_msg *svc_msg;
88
89 svc_msg = kzalloc((sizeof *svc_msg), GFP_KERNEL);
90 if (!svc_msg)
91 return NULL;
92
93 // FIXME - verify we are only sending message types we should be
94 svc_msg->header.type = type;
95 return svc_msg;
96}
97
98static void svc_msg_free(struct svc_msg *svc_msg)
99{
100 kfree(svc_msg);
101}
102
103static int svc_msg_send(struct svc_msg *svc_msg)
104{
105 // FIXME - Do something with this message!
106
107
108 svc_msg_free(svc_msg);
109 return 0;
110}
111
112
113static void svc_handshake(struct svc_function_handshake *handshake,
114 struct es1_ap_dev *es1)
115{
116 struct svc_msg *svc_msg;
117
118 /* A new SVC communication channel, let's verify it was for us */
119 if (handshake->handshake_type != SVC_HANDSHAKE_SVC_HELLO) {
120 /* we don't know what to do with this, log it and return */
121 dev_dbg(&es1->usb_intf->dev,
122 "received invalid handshake type %d\n",
123 handshake->handshake_type);
124 return;
125 }
126
127 /* Send back a AP_HELLO message */
128 svc_msg = svc_msg_alloc(SVC_FUNCTION_HANDSHAKE);
129 if (!svc_msg)
130 return;
131
132 svc_msg->handshake.handshake_type = SVC_HANDSHAKE_AP_HELLO;
133 svc_msg_send(svc_msg);
134}
135
136static void svc_management(struct svc_function_unipro_management *management,
137 struct es1_ap_dev *es1)
138{
139 /* What? An AP should not get this message */
140 dev_err(&es1->usb_intf->dev, "Got an svc management message???\n");
141}
142
143static void svc_hotplug(struct svc_function_hotplug *hotplug,
144 struct es1_ap_dev *es1)
145{
146 u8 module_id = hotplug->module_id;
147
148 switch (hotplug->hotplug_event) {
149 case SVC_HOTPLUG_EVENT:
150 dev_dbg(&es1->usb_intf->dev, "module id %d added\n",
151 module_id);
152 // FIXME - add the module to the system
153 break;
154
155 case SVC_HOTUNPLUG_EVENT:
156 dev_dbg(&es1->usb_intf->dev, "module id %d removed\n",
157 module_id);
158 // FIXME - remove the module from the system
159 break;
160
161 default:
162 dev_err(&es1->usb_intf->dev, "received invalid hotplug message type %d\n",
163 hotplug->hotplug_event);
164 break;
165 }
166}
167
168static void svc_ddb(struct svc_function_ddb *ddb, struct es1_ap_dev *es1)
169{
170 /* What? An AP should not get this message */
171 dev_err(&es1->usb_intf->dev, "Got an svc DDB message???\n");
172}
173
174static void svc_power(struct svc_function_power *power, struct es1_ap_dev *es1)
175{
176 u8 module_id = power->module_id;
177
178 if (power->power_type != SVC_POWER_BATTERY_STATUS) {
179 dev_err(&es1->usb_intf->dev, "received invalid power type %d\n",
180 power->power_type);
181 return;
182 }
183
184 dev_dbg(&es1->usb_intf->dev, "power status for module id %d is %d\n",
185 module_id, power->status.status);
186
187 // FIXME - do something with the power information, like update our
188 // battery information...
189}
190
191static void svc_epm(struct svc_function_epm *epm, struct es1_ap_dev *es1)
192{
193 /* What? An AP should not get this message */
194 dev_err(&es1->usb_intf->dev, "Got an EPM message???\n");
195}
196
197static void svc_suspend(struct svc_function_suspend *suspend,
198 struct es1_ap_dev *es1)
199{
200 /* What? An AP should not get this message */
201 dev_err(&es1->usb_intf->dev, "Got an suspend message???\n");
202}
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700203
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700204/* Main message loop for ap messages */
205/* Odds are, most of this logic can move to core.c someday, but as we only have
206 * one host controller driver for now, let's leave it here */
207static void ap_msg(struct svc_msg *svc_msg, struct greybus_host_device *hd)
208{
209 struct es1_ap_dev *es1 = hd_to_es1(hd);
210
211 /* Look at the message to figure out what to do with it */
Greg Kroah-Hartman43cc32a2014-09-07 13:51:12 -0700212 switch (svc_msg->header.type) {
213 case SVC_FUNCTION_HANDSHAKE:
214 svc_handshake(&svc_msg->handshake, es1);
215 break;
216 case SVC_FUNCTION_UNIPRO_NETWORK_MANAGEMENT:
217 svc_management(&svc_msg->management, es1);
218 break;
219 case SVC_FUNCTION_HOTPLUG:
220 svc_hotplug(&svc_msg->hotplug, es1);
221 break;
222 case SVC_FUNCTION_DDB:
223 svc_ddb(&svc_msg->ddb, es1);
224 break;
225 case SVC_FUNCTION_POWER:
226 svc_power(&svc_msg->power, es1);
227 break;
228 case SVC_FUNCTION_EPM:
229 svc_epm(&svc_msg->epm, es1);
230 break;
231 case SVC_FUNCTION_SUSPEND:
232 svc_suspend(&svc_msg->suspend, es1);
233 break;
234 default:
235 dev_err(&es1->usb_intf->dev, "received invalid SVC message type %d\n",
236 svc_msg->header.type);
237 }
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700238}
239
240
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700241static struct greybus_host_driver es1_driver = {
242 .hd_priv_size = sizeof(struct es1_ap_dev),
243 .alloc_gbuf = alloc_gbuf,
244 .free_gbuf = free_gbuf,
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700245 .ap_msg = ap_msg,
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700246};
247
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700248
Greg Kroah-Hartmanec909872014-09-01 14:39:14 -0700249void ap_in_callback(struct urb *urb)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700250{
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700251 struct es1_ap_dev *es1 = urb->context;
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700252 struct device *dev = &urb->dev->dev;
253 int status = urb->status;
254 int retval;
255
256 switch (status) {
257 case 0:
258 break;
259 case -EOVERFLOW:
260 dev_err(dev, "%s: overflow actual length is %d\n",
261 __func__, urb->actual_length);
262 case -ECONNRESET:
263 case -ENOENT:
264 case -ESHUTDOWN:
265 case -EILSEQ:
266 /* device is gone, stop sending */
267 return;
268 default:
269 dev_err(dev, "%s: unknown status %d\n", __func__, status);
270 goto exit;
271 }
272
273 /* We have a message, create a new message structure, add it to the
274 * list, and wake up our thread that will process the messages.
275 */
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700276 gb_new_ap_msg(urb->transfer_buffer, urb->actual_length, es1->hd);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700277
278exit:
279 /* resubmit the urb to get more messages */
280 retval = usb_submit_urb(urb, GFP_ATOMIC);
281 if (retval)
282 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
283}
284
Greg Kroah-Hartmanec909872014-09-01 14:39:14 -0700285void ap_out_callback(struct urb *urb)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700286{
287 struct device *dev = &urb->dev->dev;
288 int status = urb->status;
289
290 switch (status) {
291 case 0:
292 break;
293 case -EOVERFLOW:
294 dev_err(dev, "%s: overflow actual length is %d\n",
295 __func__, urb->actual_length);
296 case -ECONNRESET:
297 case -ENOENT:
298 case -ESHUTDOWN:
299 case -EILSEQ:
300 /* device is gone, stop sending */
301 return;
302 default:
303 dev_err(dev, "%s: unknown status %d\n", __func__, status);
304 goto exit;
305 }
306
307 // FIXME - queue up next AP message to send???
308exit:
309 return;
310}
311
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700312
313static int ap_probe(struct usb_interface *interface,
314 const struct usb_device_id *id)
315{
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700316 struct es1_ap_dev *es1;
317 struct greybus_host_device *hd;
318 struct usb_device *udev;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700319 struct usb_host_interface *iface_desc;
320 struct usb_endpoint_descriptor *endpoint;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700321 int i;
322
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700323 udev = usb_get_dev(interface_to_usbdev(interface));
324
325 hd = greybus_create_hd(&es1_driver, &udev->dev);
326 if (!hd)
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700327 return -ENOMEM;
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700328
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700329 es1 = hd_to_es1(hd);
330 es1->hd = hd;
331
332 /* Control endpoint is the pipe to talk to this AP, so save it off */
333 endpoint = &udev->ep0.desc;
334 es1->ap_comm_endpoint = endpoint->bEndpointAddress;
335
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700336 // FIXME
337 // figure out endpoint for talking to the AP.
338 iface_desc = interface->cur_altsetting;
339 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
340 endpoint = &iface_desc->endpoint[i].desc;
341
342 if (usb_endpoint_is_bulk_in(endpoint)) {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700343 es1->ap_in_endpoint = endpoint->bEndpointAddress;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700344 }
345 if (usb_endpoint_is_bulk_out(endpoint)) {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700346 es1->ap_out_endpoint = endpoint->bEndpointAddress;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700347 }
348 // FIXME - properly exit once found the AP endpoint
349 // FIXME - set up cport endpoints
350 }
351
352 // FIXME - allocate buffer
353 // FIXME = start up talking, then create the gb "devices" based on what the AP tells us.
354
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700355 es1->usb_intf = interface;
356 es1->usb_dev = udev;
357 usb_set_intfdata(interface, es1);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700358 return 0;
359}
360
361static void ap_disconnect(struct usb_interface *interface)
362{
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700363 struct es1_ap_dev *es1;
364
365 es1 = usb_get_intfdata(interface);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700366
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700367 /* Tear down everything! */
368
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700369 usb_put_dev(es1->usb_dev);
370 kfree(es1->ap_buffer);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700371
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700372 greybus_remove_hd(es1->hd);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700373}
374
375static struct usb_driver es1_ap_driver = {
376 .name = "es1_ap_driver",
377 .probe = ap_probe,
378 .disconnect = ap_disconnect,
379 .id_table = id_table,
380};
381
382module_usb_driver(es1_ap_driver);
383
384MODULE_LICENSE("GPL");
385MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");