blob: a08e4ddc4f2036a170c2b5e253e59730196c1aa7 [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
85static struct greybus_host_driver es1_driver = {
86 .hd_priv_size = sizeof(struct es1_ap_dev),
87 .alloc_gbuf = alloc_gbuf,
88 .free_gbuf = free_gbuf,
89};
90
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -070091
Greg Kroah-Hartmanec909872014-09-01 14:39:14 -070092void ap_in_callback(struct urb *urb)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -070093{
94 struct device *dev = &urb->dev->dev;
95 int status = urb->status;
96 int retval;
97
98 switch (status) {
99 case 0:
100 break;
101 case -EOVERFLOW:
102 dev_err(dev, "%s: overflow actual length is %d\n",
103 __func__, urb->actual_length);
104 case -ECONNRESET:
105 case -ENOENT:
106 case -ESHUTDOWN:
107 case -EILSEQ:
108 /* device is gone, stop sending */
109 return;
110 default:
111 dev_err(dev, "%s: unknown status %d\n", __func__, status);
112 goto exit;
113 }
114
115 /* We have a message, create a new message structure, add it to the
116 * list, and wake up our thread that will process the messages.
117 */
118 gb_new_ap_msg(urb->transfer_buffer, urb->actual_length);
119
120exit:
121 /* resubmit the urb to get more messages */
122 retval = usb_submit_urb(urb, GFP_ATOMIC);
123 if (retval)
124 dev_err(dev, "Can not submit urb for AP data: %d\n", retval);
125}
126
Greg Kroah-Hartmanec909872014-09-01 14:39:14 -0700127void ap_out_callback(struct urb *urb)
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700128{
129 struct device *dev = &urb->dev->dev;
130 int status = urb->status;
131
132 switch (status) {
133 case 0:
134 break;
135 case -EOVERFLOW:
136 dev_err(dev, "%s: overflow actual length is %d\n",
137 __func__, urb->actual_length);
138 case -ECONNRESET:
139 case -ENOENT:
140 case -ESHUTDOWN:
141 case -EILSEQ:
142 /* device is gone, stop sending */
143 return;
144 default:
145 dev_err(dev, "%s: unknown status %d\n", __func__, status);
146 goto exit;
147 }
148
149 // FIXME - queue up next AP message to send???
150exit:
151 return;
152}
153
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700154
155static int ap_probe(struct usb_interface *interface,
156 const struct usb_device_id *id)
157{
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700158 struct es1_ap_dev *es1;
159 struct greybus_host_device *hd;
160 struct usb_device *udev;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700161 struct usb_host_interface *iface_desc;
162 struct usb_endpoint_descriptor *endpoint;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700163 int i;
164
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700165 udev = usb_get_dev(interface_to_usbdev(interface));
166
167 hd = greybus_create_hd(&es1_driver, &udev->dev);
168 if (!hd)
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700169 return -ENOMEM;
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700170
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700171 es1 = hd_to_es1(hd);
172 es1->hd = hd;
173
174 /* Control endpoint is the pipe to talk to this AP, so save it off */
175 endpoint = &udev->ep0.desc;
176 es1->ap_comm_endpoint = endpoint->bEndpointAddress;
177
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700178 // FIXME
179 // figure out endpoint for talking to the AP.
180 iface_desc = interface->cur_altsetting;
181 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
182 endpoint = &iface_desc->endpoint[i].desc;
183
184 if (usb_endpoint_is_bulk_in(endpoint)) {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700185 es1->ap_in_endpoint = endpoint->bEndpointAddress;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700186 }
187 if (usb_endpoint_is_bulk_out(endpoint)) {
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700188 es1->ap_out_endpoint = endpoint->bEndpointAddress;
Greg Kroah-Hartman6f83ab72014-08-30 17:30:04 -0700189 }
190 // FIXME - properly exit once found the AP endpoint
191 // FIXME - set up cport endpoints
192 }
193
194 // FIXME - allocate buffer
195 // FIXME = start up talking, then create the gb "devices" based on what the AP tells us.
196
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700197 es1->usb_intf = interface;
198 es1->usb_dev = udev;
199 usb_set_intfdata(interface, es1);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700200 return 0;
201}
202
203static void ap_disconnect(struct usb_interface *interface)
204{
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700205 struct es1_ap_dev *es1;
206
207 es1 = usb_get_intfdata(interface);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700208
Greg Kroah-Hartmanf1eec302014-08-30 17:18:14 -0700209 /* Tear down everything! */
210
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700211 usb_put_dev(es1->usb_dev);
212 kfree(es1->ap_buffer);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700213
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700214 // FIXME
215 //greybus_destroy_hd(es1->hd);
Greg Kroah-Hartmanba4468d42014-08-30 17:06:54 -0700216}
217
218static struct usb_driver es1_ap_driver = {
219 .name = "es1_ap_driver",
220 .probe = ap_probe,
221 .disconnect = ap_disconnect,
222 .id_table = id_table,
223};
224
225module_usb_driver(es1_ap_driver);
226
227MODULE_LICENSE("GPL");
228MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");