Commit | Line | Data |
---|---|---|
f587027e GKH |
1 | /* |
2 | * Greybus "AP" USB driver for "ES2" controller chips | |
3 | * | |
142f8ddf AE |
4 | * Copyright 2014-2015 Google Inc. |
5 | * Copyright 2014-2015 Linaro Ltd. | |
f587027e GKH |
6 | * |
7 | * Released under the GPLv2 only. | |
8 | */ | |
ca3ec299 | 9 | #include <linux/kthread.h> |
f587027e GKH |
10 | #include <linux/sizes.h> |
11 | #include <linux/usb.h> | |
ca3ec299 GKH |
12 | #include <linux/kfifo.h> |
13 | #include <linux/debugfs.h> | |
c14118a8 | 14 | #include <linux/list.h> |
491e60d6 | 15 | #include <asm/unaligned.h> |
f587027e | 16 | |
7d0e76d6 | 17 | #include "arpc.h" |
f587027e | 18 | #include "greybus.h" |
495787a7 | 19 | #include "greybus_trace.h" |
f587027e | 20 | #include "kernel_ver.h" |
3f2a809e | 21 | #include "connection.h" |
05061507 | 22 | |
54f34e1e | 23 | |
60793c9b JH |
24 | /* Default timeout for USB vendor requests. */ |
25 | #define ES2_USB_CTRL_TIMEOUT 500 | |
26 | ||
54f34e1e JH |
27 | /* Default timeout for ARPC CPort requests */ |
28 | #define ES2_ARPC_CPORT_TIMEOUT 500 | |
29 | ||
05061507 JH |
30 | /* Fixed CPort numbers */ |
31 | #define ES2_CPORT_CDSI0 16 | |
32 | #define ES2_CPORT_CDSI1 17 | |
33 | ||
4b1d8204 AE |
34 | /* Memory sizes for the buffers sent to/from the ES2 controller */ |
35 | #define ES2_GBUF_MSG_SIZE_MAX 2048 | |
f587027e | 36 | |
9d9d3777 | 37 | /* Memory sizes for the ARPC buffers */ |
c14118a8 | 38 | #define ARPC_OUT_SIZE_MAX U16_MAX |
9d9d3777 AB |
39 | #define ARPC_IN_SIZE_MAX 128 |
40 | ||
f587027e | 41 | static const struct usb_device_id id_table[] = { |
8f0a654f | 42 | { USB_DEVICE(0x18d1, 0x1eaf) }, |
f587027e GKH |
43 | { }, |
44 | }; | |
45 | MODULE_DEVICE_TABLE(usb, id_table); | |
46 | ||
ca3ec299 | 47 | #define APB1_LOG_SIZE SZ_16K |
ca3ec299 | 48 | |
606addd2 AB |
49 | /* Number of bulk in and bulk out couple */ |
50 | #define NUM_BULKS 7 | |
51 | ||
9d9d3777 AB |
52 | /* Expected number of bulk out endpoints */ |
53 | #define NUM_BULKS_OUT NUM_BULKS | |
54 | ||
55 | /* Expected number of bulk in endpoints (including ARPC endpoint) */ | |
56 | #define NUM_BULKS_IN (NUM_BULKS + 1) | |
57 | ||
f587027e GKH |
58 | /* |
59 | * Number of CPort IN urbs in flight at any point in time. | |
60 | * Adjust if we are having stalls in the USB buffer due to not enough urbs in | |
61 | * flight. | |
62 | */ | |
63 | #define NUM_CPORT_IN_URB 4 | |
64 | ||
65 | /* Number of CPort OUT urbs in flight at any point in time. | |
66 | * Adjust if we get messages saying we are out of urbs in the system log. | |
67 | */ | |
606addd2 | 68 | #define NUM_CPORT_OUT_URB (8 * NUM_BULKS) |
f587027e | 69 | |
9d9d3777 AB |
70 | /* |
71 | * Number of ARPC in urbs in flight at any point in time. | |
72 | */ | |
73 | #define NUM_ARPC_IN_URB 2 | |
74 | ||
ddc09acd AB |
75 | /* |
76 | * @endpoint: bulk in endpoint for CPort data | |
77 | * @urb: array of urbs for the CPort in messages | |
78 | * @buffer: array of buffers for the @cport_in_urb urbs | |
79 | */ | |
4b1d8204 | 80 | struct es2_cport_in { |
ddc09acd AB |
81 | __u8 endpoint; |
82 | struct urb *urb[NUM_CPORT_IN_URB]; | |
83 | u8 *buffer[NUM_CPORT_IN_URB]; | |
84 | }; | |
85 | ||
86 | /* | |
87 | * @endpoint: bulk out endpoint for CPort data | |
88 | */ | |
4b1d8204 | 89 | struct es2_cport_out { |
ddc09acd AB |
90 | __u8 endpoint; |
91 | }; | |
92 | ||
f587027e | 93 | /** |
4b1d8204 | 94 | * es2_ap_dev - ES2 USB Bridge to AP structure |
f587027e GKH |
95 | * @usb_dev: pointer to the USB device we are. |
96 | * @usb_intf: pointer to the USB interface we are bound to. | |
2537636a | 97 | * @hd: pointer to our gb_host_device structure |
ddc09acd | 98 | |
ddc09acd AB |
99 | * @cport_in: endpoint, urbs and buffer for cport in messages |
100 | * @cport_out: endpoint for for cport out messages | |
f587027e GKH |
101 | * @cport_out_urb: array of urbs for the CPort out messages |
102 | * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or | |
103 | * not. | |
3e136cc9 JH |
104 | * @cport_out_urb_cancelled: array of flags indicating whether the |
105 | * corresponding @cport_out_urb is being cancelled | |
f587027e | 106 | * @cport_out_urb_lock: locks the @cport_out_urb_busy "list" |
1482b3e1 | 107 | * |
3be0e17d AE |
108 | * @apb_log_task: task pointer for logging thread |
109 | * @apb_log_dentry: file system entry for the log file interface | |
110 | * @apb_log_enable_dentry: file system entry for enabling logging | |
111 | * @apb_log_fifo: kernel FIFO to carry logged data | |
9d9d3777 AB |
112 | * @arpc_urb: array of urbs for the ARPC in messages |
113 | * @arpc_buffer: array of buffers for the @arpc_urb urbs | |
114 | * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC | |
c14118a8 AB |
115 | * @arpc_id_cycle: gives an unique id to ARPC |
116 | * @arpc_lock: locks ARPC list | |
117 | * @arpcs: list of in progress ARPCs | |
f587027e | 118 | */ |
4b1d8204 | 119 | struct es2_ap_dev { |
f587027e GKH |
120 | struct usb_device *usb_dev; |
121 | struct usb_interface *usb_intf; | |
2537636a | 122 | struct gb_host_device *hd; |
f587027e | 123 | |
4b1d8204 AE |
124 | struct es2_cport_in cport_in[NUM_BULKS]; |
125 | struct es2_cport_out cport_out[NUM_BULKS]; | |
f587027e GKH |
126 | struct urb *cport_out_urb[NUM_CPORT_OUT_URB]; |
127 | bool cport_out_urb_busy[NUM_CPORT_OUT_URB]; | |
3e136cc9 | 128 | bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB]; |
f587027e | 129 | spinlock_t cport_out_urb_lock; |
fc1a536e | 130 | |
045d3561 JH |
131 | bool cdsi1_in_use; |
132 | ||
c011d558 | 133 | int *cport_to_ep; |
1482b3e1 | 134 | |
3be0e17d AE |
135 | struct task_struct *apb_log_task; |
136 | struct dentry *apb_log_dentry; | |
137 | struct dentry *apb_log_enable_dentry; | |
138 | DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE); | |
9d9d3777 AB |
139 | |
140 | __u8 arpc_endpoint_in; | |
141 | struct urb *arpc_urb[NUM_ARPC_IN_URB]; | |
142 | u8 *arpc_buffer[NUM_ARPC_IN_URB]; | |
c14118a8 AB |
143 | |
144 | int arpc_id_cycle; | |
145 | spinlock_t arpc_lock; | |
146 | struct list_head arpcs; | |
fc1a536e AB |
147 | }; |
148 | ||
e5acf736 AB |
149 | /** |
150 | * cport_to_ep - information about cport to endpoints mapping | |
151 | * @cport_id: the id of cport to map to endpoints | |
152 | * @endpoint_in: the endpoint number to use for in transfer | |
153 | * @endpoint_out: he endpoint number to use for out transfer | |
154 | */ | |
fc1a536e AB |
155 | struct cport_to_ep { |
156 | __le16 cport_id; | |
157 | __u8 endpoint_in; | |
158 | __u8 endpoint_out; | |
f587027e GKH |
159 | }; |
160 | ||
c8a657ba BD |
161 | /** |
162 | * timesync_enable_request - Enable timesync in an APBridge | |
163 | * @count: number of TimeSync Pulses to expect | |
164 | * @frame_time: the initial FrameTime at the first TimeSync Pulse | |
165 | * @strobe_delay: the expected delay in microseconds between each TimeSync Pulse | |
166 | * @refclk: The AP mandated reference clock to run FrameTime at | |
167 | */ | |
168 | struct timesync_enable_request { | |
169 | __u8 count; | |
170 | __le64 frame_time; | |
171 | __le32 strobe_delay; | |
172 | __le32 refclk; | |
173 | } __packed; | |
174 | ||
175 | /** | |
176 | * timesync_authoritative_request - Transmit authoritative FrameTime to APBridge | |
177 | * @frame_time: An array of authoritative FrameTimes provided by the SVC | |
178 | * and relayed to the APBridge by the AP | |
179 | */ | |
180 | struct timesync_authoritative_request { | |
181 | __le64 frame_time[GB_TIMESYNC_MAX_STROBES]; | |
182 | } __packed; | |
183 | ||
c14118a8 AB |
184 | struct arpc { |
185 | struct list_head list; | |
186 | struct arpc_request_message *req; | |
187 | struct arpc_response_message *resp; | |
188 | struct completion response_received; | |
189 | bool active; | |
190 | }; | |
191 | ||
2537636a | 192 | static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd) |
f587027e | 193 | { |
4b1d8204 | 194 | return (struct es2_ap_dev *)&hd->hd_priv; |
f587027e GKH |
195 | } |
196 | ||
197 | static void cport_out_callback(struct urb *urb); | |
4b1d8204 AE |
198 | static void usb_log_enable(struct es2_ap_dev *es2); |
199 | static void usb_log_disable(struct es2_ap_dev *es2); | |
c14118a8 AB |
200 | static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload, |
201 | size_t size, int *result, unsigned int timeout); | |
f587027e | 202 | |
e5acf736 | 203 | /* Get the endpoints pair mapped to the cport */ |
4b1d8204 | 204 | static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id) |
fc1a536e | 205 | { |
4b1d8204 | 206 | if (cport_id >= es2->hd->num_cports) |
fc1a536e | 207 | return 0; |
4b1d8204 | 208 | return es2->cport_to_ep[cport_id]; |
fc1a536e AB |
209 | } |
210 | ||
608f6619 GKH |
211 | /* Disable for now until we work all of this out to keep a warning-free build */ |
212 | #if 0 | |
e5acf736 | 213 | /* Test if the endpoints pair is already mapped to a cport */ |
4b1d8204 | 214 | static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair) |
fc1a536e AB |
215 | { |
216 | int i; | |
217 | ||
4b1d8204 AE |
218 | for (i = 0; i < es2->hd->num_cports; i++) { |
219 | if (es2->cport_to_ep[i] == ep_pair) | |
fc1a536e AB |
220 | return 1; |
221 | } | |
222 | return 0; | |
223 | } | |
224 | ||
e5acf736 | 225 | /* Configure the endpoint mapping and send the request to APBridge */ |
4b1d8204 | 226 | static int map_cport_to_ep(struct es2_ap_dev *es2, |
1ff3dc92 | 227 | u16 cport_id, int ep_pair) |
fc1a536e AB |
228 | { |
229 | int retval; | |
230 | struct cport_to_ep *cport_to_ep; | |
231 | ||
1ff3dc92 | 232 | if (ep_pair < 0 || ep_pair >= NUM_BULKS) |
fc1a536e | 233 | return -EINVAL; |
4b1d8204 | 234 | if (cport_id >= es2->hd->num_cports) |
fc1a536e | 235 | return -EINVAL; |
4b1d8204 | 236 | if (ep_pair && ep_pair_in_use(es2, ep_pair)) |
fc1a536e AB |
237 | return -EINVAL; |
238 | ||
239 | cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL); | |
240 | if (!cport_to_ep) | |
241 | return -ENOMEM; | |
242 | ||
4b1d8204 | 243 | es2->cport_to_ep[cport_id] = ep_pair; |
fc1a536e | 244 | cport_to_ep->cport_id = cpu_to_le16(cport_id); |
4b1d8204 AE |
245 | cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint; |
246 | cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint; | |
fc1a536e | 247 | |
4b1d8204 AE |
248 | retval = usb_control_msg(es2->usb_dev, |
249 | usb_sndctrlpipe(es2->usb_dev, 0), | |
e5273381 | 250 | GB_APB_REQUEST_EP_MAPPING, |
fc1a536e AB |
251 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
252 | 0x00, 0x00, | |
253 | (char *)cport_to_ep, | |
254 | sizeof(*cport_to_ep), | |
60793c9b | 255 | ES2_USB_CTRL_TIMEOUT); |
fc1a536e AB |
256 | if (retval == sizeof(*cport_to_ep)) |
257 | retval = 0; | |
258 | kfree(cport_to_ep); | |
259 | ||
260 | return retval; | |
261 | } | |
262 | ||
e5acf736 | 263 | /* Unmap a cport: use the muxed endpoints pair */ |
4b1d8204 | 264 | static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id) |
fc1a536e | 265 | { |
4b1d8204 | 266 | return map_cport_to_ep(es2, cport_id, 0); |
fc1a536e | 267 | } |
608f6619 | 268 | #endif |
fc1a536e | 269 | |
ed4596e9 | 270 | static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd) |
8e2b7daa | 271 | { |
8e2b7daa | 272 | struct usb_device *udev = es2->usb_dev; |
ed4596e9 | 273 | u8 *data; |
8e2b7daa LP |
274 | int retval; |
275 | ||
ed4596e9 GKH |
276 | data = kmalloc(size, GFP_KERNEL); |
277 | if (!data) | |
ccb58035 | 278 | return -ENOMEM; |
ed4596e9 | 279 | memcpy(data, req, size); |
8e2b7daa LP |
280 | |
281 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
ed4596e9 | 282 | cmd, |
8e2b7daa | 283 | USB_DIR_OUT | USB_TYPE_VENDOR | |
ed4596e9 | 284 | USB_RECIP_INTERFACE, |
60793c9b | 285 | 0, 0, data, size, ES2_USB_CTRL_TIMEOUT); |
ccb58035 | 286 | if (retval < 0) |
ed4596e9 GKH |
287 | dev_err(&udev->dev, "%s: return error %d\n", __func__, retval); |
288 | else | |
289 | retval = 0; | |
8e2b7daa | 290 | |
ed4596e9 | 291 | kfree(data); |
ccb58035 | 292 | return retval; |
8e2b7daa | 293 | } |
ed4596e9 GKH |
294 | |
295 | static void ap_urb_complete(struct urb *urb) | |
296 | { | |
297 | struct usb_ctrlrequest *dr = urb->context; | |
298 | ||
299 | kfree(dr); | |
300 | usb_free_urb(urb); | |
301 | } | |
302 | ||
303 | static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd) | |
304 | { | |
305 | struct usb_device *udev = es2->usb_dev; | |
306 | struct urb *urb; | |
307 | struct usb_ctrlrequest *dr; | |
308 | u8 *buf; | |
309 | int retval; | |
310 | ||
311 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
312 | if (!urb) | |
313 | return -ENOMEM; | |
314 | ||
315 | dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC); | |
316 | if (!dr) { | |
317 | usb_free_urb(urb); | |
318 | return -ENOMEM; | |
319 | } | |
320 | ||
321 | buf = (u8 *)dr + sizeof(*dr); | |
322 | memcpy(buf, req, size); | |
323 | ||
324 | dr->bRequest = cmd; | |
325 | dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE; | |
326 | dr->wValue = 0; | |
327 | dr->wIndex = 0; | |
328 | dr->wLength = cpu_to_le16(size); | |
329 | ||
330 | usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0), | |
331 | (unsigned char *)dr, buf, size, | |
332 | ap_urb_complete, dr); | |
333 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
334 | if (retval) { | |
335 | usb_free_urb(urb); | |
336 | kfree(dr); | |
337 | } | |
338 | return retval; | |
339 | } | |
340 | ||
341 | static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd, | |
342 | bool async) | |
343 | { | |
344 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
345 | ||
346 | if (async) | |
347 | return output_async(es2, req, size, cmd); | |
348 | ||
349 | return output_sync(es2, req, size, cmd); | |
350 | } | |
8e2b7daa | 351 | |
0ce68ce4 JH |
352 | static int es2_cport_in_enable(struct es2_ap_dev *es2, |
353 | struct es2_cport_in *cport_in) | |
354 | { | |
355 | struct urb *urb; | |
356 | int ret; | |
357 | int i; | |
358 | ||
359 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { | |
360 | urb = cport_in->urb[i]; | |
361 | ||
362 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
363 | if (ret) { | |
364 | dev_err(&es2->usb_dev->dev, | |
365 | "failed to submit in-urb: %d\n", ret); | |
366 | goto err_kill_urbs; | |
367 | } | |
368 | } | |
369 | ||
370 | return 0; | |
371 | ||
372 | err_kill_urbs: | |
373 | for (--i; i >= 0; --i) { | |
374 | urb = cport_in->urb[i]; | |
375 | usb_kill_urb(urb); | |
376 | } | |
377 | ||
378 | return ret; | |
379 | } | |
380 | ||
f6624ca7 JH |
381 | static void es2_cport_in_disable(struct es2_ap_dev *es2, |
382 | struct es2_cport_in *cport_in) | |
383 | { | |
384 | struct urb *urb; | |
385 | int i; | |
386 | ||
387 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { | |
388 | urb = cport_in->urb[i]; | |
389 | usb_kill_urb(urb); | |
390 | } | |
391 | } | |
392 | ||
9d9d3777 AB |
393 | static int es2_arpc_in_enable(struct es2_ap_dev *es2) |
394 | { | |
395 | struct urb *urb; | |
396 | int ret; | |
397 | int i; | |
398 | ||
399 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { | |
400 | urb = es2->arpc_urb[i]; | |
401 | ||
402 | ret = usb_submit_urb(urb, GFP_KERNEL); | |
403 | if (ret) { | |
404 | dev_err(&es2->usb_dev->dev, | |
405 | "failed to submit arpc in-urb: %d\n", ret); | |
406 | goto err_kill_urbs; | |
407 | } | |
408 | } | |
409 | ||
410 | return 0; | |
411 | ||
412 | err_kill_urbs: | |
413 | for (--i; i >= 0; --i) { | |
414 | urb = es2->arpc_urb[i]; | |
415 | usb_kill_urb(urb); | |
416 | } | |
417 | ||
418 | return ret; | |
419 | } | |
420 | ||
421 | static void es2_arpc_in_disable(struct es2_ap_dev *es2) | |
422 | { | |
423 | struct urb *urb; | |
424 | int i; | |
425 | ||
426 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { | |
427 | urb = es2->arpc_urb[i]; | |
428 | usb_kill_urb(urb); | |
429 | } | |
430 | } | |
431 | ||
4b1d8204 | 432 | static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask) |
f587027e GKH |
433 | { |
434 | struct urb *urb = NULL; | |
435 | unsigned long flags; | |
436 | int i; | |
437 | ||
4b1d8204 | 438 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
f587027e GKH |
439 | |
440 | /* Look in our pool of allocated urbs first, as that's the "fastest" */ | |
441 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
4b1d8204 AE |
442 | if (es2->cport_out_urb_busy[i] == false && |
443 | es2->cport_out_urb_cancelled[i] == false) { | |
444 | es2->cport_out_urb_busy[i] = true; | |
445 | urb = es2->cport_out_urb[i]; | |
f587027e GKH |
446 | break; |
447 | } | |
448 | } | |
4b1d8204 | 449 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
f587027e GKH |
450 | if (urb) |
451 | return urb; | |
452 | ||
453 | /* | |
454 | * Crap, pool is empty, complain to the syslog and go allocate one | |
455 | * dynamically as we have to succeed. | |
456 | */ | |
ed972e3a | 457 | dev_dbg(&es2->usb_dev->dev, |
f587027e GKH |
458 | "No free CPort OUT urbs, having to dynamically allocate one!\n"); |
459 | return usb_alloc_urb(0, gfp_mask); | |
460 | } | |
461 | ||
4b1d8204 | 462 | static void free_urb(struct es2_ap_dev *es2, struct urb *urb) |
f587027e GKH |
463 | { |
464 | unsigned long flags; | |
465 | int i; | |
466 | /* | |
467 | * See if this was an urb in our pool, if so mark it "free", otherwise | |
468 | * we need to free it ourselves. | |
469 | */ | |
4b1d8204 | 470 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
f587027e | 471 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
4b1d8204 AE |
472 | if (urb == es2->cport_out_urb[i]) { |
473 | es2->cport_out_urb_busy[i] = false; | |
f587027e GKH |
474 | urb = NULL; |
475 | break; | |
476 | } | |
477 | } | |
4b1d8204 | 478 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
f587027e GKH |
479 | |
480 | /* If urb is not NULL, then we need to free this urb */ | |
481 | usb_free_urb(urb); | |
482 | } | |
483 | ||
d29b3d63 AE |
484 | /* |
485 | * We (ab)use the operation-message header pad bytes to transfer the | |
486 | * cport id in order to minimise overhead. | |
487 | */ | |
488 | static void | |
489 | gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id) | |
490 | { | |
4bc1389d | 491 | header->pad[0] = cport_id; |
d29b3d63 AE |
492 | } |
493 | ||
494 | /* Clear the pad bytes used for the CPort id */ | |
495 | static void gb_message_cport_clear(struct gb_operation_msg_hdr *header) | |
496 | { | |
4bc1389d | 497 | header->pad[0] = 0; |
d29b3d63 AE |
498 | } |
499 | ||
500 | /* Extract the CPort id packed into the header, and clear it */ | |
501 | static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header) | |
502 | { | |
4bc1389d | 503 | u16 cport_id = header->pad[0]; |
d29b3d63 AE |
504 | |
505 | gb_message_cport_clear(header); | |
506 | ||
507 | return cport_id; | |
508 | } | |
509 | ||
f587027e | 510 | /* |
3e136cc9 JH |
511 | * Returns zero if the message was successfully queued, or a negative errno |
512 | * otherwise. | |
f587027e | 513 | */ |
2537636a | 514 | static int message_send(struct gb_host_device *hd, u16 cport_id, |
7cf7bca9 | 515 | struct gb_message *message, gfp_t gfp_mask) |
f587027e | 516 | { |
4b1d8204 AE |
517 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
518 | struct usb_device *udev = es2->usb_dev; | |
7cf7bca9 | 519 | size_t buffer_size; |
f587027e GKH |
520 | int retval; |
521 | struct urb *urb; | |
1ff3dc92 | 522 | int ep_pair; |
3e136cc9 | 523 | unsigned long flags; |
f587027e | 524 | |
f587027e GKH |
525 | /* |
526 | * The data actually transferred will include an indication | |
527 | * of where the data should be sent. Do one last check of | |
528 | * the target CPort id before filling it in. | |
529 | */ | |
144670c2 | 530 | if (!cport_id_valid(hd, cport_id)) { |
100e9000 | 531 | dev_err(&udev->dev, "invalid cport %u\n", cport_id); |
3e136cc9 | 532 | return -EINVAL; |
f587027e | 533 | } |
f587027e GKH |
534 | |
535 | /* Find a free urb */ | |
4b1d8204 | 536 | urb = next_free_urb(es2, gfp_mask); |
f587027e | 537 | if (!urb) |
3e136cc9 JH |
538 | return -ENOMEM; |
539 | ||
4b1d8204 | 540 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
3e136cc9 | 541 | message->hcpriv = urb; |
4b1d8204 | 542 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
f587027e | 543 | |
d29b3d63 AE |
544 | /* Pack the cport id into the message header */ |
545 | gb_message_cport_pack(message->header, cport_id); | |
491e60d6 | 546 | |
821c620a | 547 | buffer_size = sizeof(*message->header) + message->payload_size; |
491e60d6 | 548 | |
4b1d8204 | 549 | ep_pair = cport_to_ep_pair(es2, cport_id); |
f587027e | 550 | usb_fill_bulk_urb(urb, udev, |
606addd2 | 551 | usb_sndbulkpipe(udev, |
4b1d8204 | 552 | es2->cport_out[ep_pair].endpoint), |
821c620a | 553 | message->buffer, buffer_size, |
7cf7bca9 | 554 | cport_out_callback, message); |
977e209a | 555 | urb->transfer_flags |= URB_ZERO_PACKET; |
495787a7 AE |
556 | |
557 | trace_gb_message_submit(message); | |
558 | ||
f587027e GKH |
559 | retval = usb_submit_urb(urb, gfp_mask); |
560 | if (retval) { | |
05e30955 | 561 | dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval); |
3e136cc9 | 562 | |
4b1d8204 | 563 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
3e136cc9 | 564 | message->hcpriv = NULL; |
4b1d8204 | 565 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
3e136cc9 | 566 | |
4b1d8204 | 567 | free_urb(es2, urb); |
d29b3d63 | 568 | gb_message_cport_clear(message->header); |
3e136cc9 JH |
569 | |
570 | return retval; | |
f587027e GKH |
571 | } |
572 | ||
3e136cc9 | 573 | return 0; |
f587027e GKH |
574 | } |
575 | ||
576 | /* | |
3e136cc9 | 577 | * Can not be called in atomic context. |
f587027e | 578 | */ |
3e136cc9 | 579 | static void message_cancel(struct gb_message *message) |
f587027e | 580 | { |
2537636a | 581 | struct gb_host_device *hd = message->operation->connection->hd; |
4b1d8204 | 582 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
3e136cc9 JH |
583 | struct urb *urb; |
584 | int i; | |
f587027e | 585 | |
3e136cc9 JH |
586 | might_sleep(); |
587 | ||
07af9340 | 588 | spin_lock_irq(&es2->cport_out_urb_lock); |
3e136cc9 JH |
589 | urb = message->hcpriv; |
590 | ||
591 | /* Prevent dynamically allocated urb from being deallocated. */ | |
592 | usb_get_urb(urb); | |
593 | ||
594 | /* Prevent pre-allocated urb from being reused. */ | |
595 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
4b1d8204 AE |
596 | if (urb == es2->cport_out_urb[i]) { |
597 | es2->cport_out_urb_cancelled[i] = true; | |
3e136cc9 JH |
598 | break; |
599 | } | |
600 | } | |
07af9340 | 601 | spin_unlock_irq(&es2->cport_out_urb_lock); |
3e136cc9 JH |
602 | |
603 | usb_kill_urb(urb); | |
604 | ||
605 | if (i < NUM_CPORT_OUT_URB) { | |
07af9340 | 606 | spin_lock_irq(&es2->cport_out_urb_lock); |
4b1d8204 | 607 | es2->cport_out_urb_cancelled[i] = false; |
07af9340 | 608 | spin_unlock_irq(&es2->cport_out_urb_lock); |
3e136cc9 JH |
609 | } |
610 | ||
611 | usb_free_urb(urb); | |
f587027e GKH |
612 | } |
613 | ||
2537636a | 614 | static int cport_reset(struct gb_host_device *hd, u16 cport_id) |
82ee1e6c | 615 | { |
4b1d8204 AE |
616 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
617 | struct usb_device *udev = es2->usb_dev; | |
c468999e | 618 | struct arpc_cport_reset_req req; |
82ee1e6c | 619 | int retval; |
4ae2d962 | 620 | int result; |
82ee1e6c | 621 | |
e7b848c2 JH |
622 | switch (cport_id) { |
623 | case GB_SVC_CPORT_ID: | |
045d3561 JH |
624 | case ES2_CPORT_CDSI0: |
625 | case ES2_CPORT_CDSI1: | |
e7b848c2 JH |
626 | return 0; |
627 | } | |
628 | ||
c14118a8 | 629 | req.cport_id = cpu_to_le16(cport_id); |
c468999e | 630 | retval = arpc_sync(es2, ARPC_TYPE_CPORT_RESET, &req, sizeof(req), |
54f34e1e | 631 | &result, ES2_ARPC_CPORT_TIMEOUT); |
c14118a8 | 632 | if (retval == -EREMOTEIO) { |
2f3db927 | 633 | dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id, |
c14118a8 | 634 | result); |
82ee1e6c FP |
635 | } |
636 | ||
c14118a8 | 637 | return retval; |
82ee1e6c FP |
638 | } |
639 | ||
045d3561 JH |
640 | static int es2_cport_allocate(struct gb_host_device *hd, int cport_id, |
641 | unsigned long flags) | |
642 | { | |
643 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
644 | struct ida *id_map = &hd->cport_id_map; | |
645 | int ida_start, ida_end; | |
646 | ||
647 | switch (cport_id) { | |
648 | case ES2_CPORT_CDSI0: | |
649 | case ES2_CPORT_CDSI1: | |
650 | dev_err(&hd->dev, "cport %d not available\n", cport_id); | |
651 | return -EBUSY; | |
652 | } | |
653 | ||
654 | if (flags & GB_CONNECTION_FLAG_OFFLOADED && | |
655 | flags & GB_CONNECTION_FLAG_CDSI1) { | |
656 | if (es2->cdsi1_in_use) { | |
657 | dev_err(&hd->dev, "CDSI1 already in use\n"); | |
658 | return -EBUSY; | |
659 | } | |
660 | ||
661 | es2->cdsi1_in_use = true; | |
662 | ||
663 | return ES2_CPORT_CDSI1; | |
664 | } | |
665 | ||
666 | if (cport_id < 0) { | |
667 | ida_start = 0; | |
668 | ida_end = hd->num_cports; | |
669 | } else if (cport_id < hd->num_cports) { | |
670 | ida_start = cport_id; | |
671 | ida_end = cport_id + 1; | |
672 | } else { | |
673 | dev_err(&hd->dev, "cport %d not available\n", cport_id); | |
674 | return -EINVAL; | |
675 | } | |
676 | ||
677 | return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL); | |
678 | } | |
679 | ||
680 | static void es2_cport_release(struct gb_host_device *hd, u16 cport_id) | |
681 | { | |
682 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
683 | ||
684 | switch (cport_id) { | |
685 | case ES2_CPORT_CDSI1: | |
686 | es2->cdsi1_in_use = false; | |
687 | return; | |
688 | } | |
689 | ||
690 | ida_simple_remove(&hd->cport_id_map, cport_id); | |
691 | } | |
692 | ||
74ec7598 JH |
693 | static int cport_enable(struct gb_host_device *hd, u16 cport_id, |
694 | unsigned long flags) | |
695 | { | |
696 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
697 | struct usb_device *udev = es2->usb_dev; | |
698 | struct gb_apb_request_cport_flags *req; | |
6a1d2959 | 699 | u32 connection_flags; |
74ec7598 JH |
700 | int ret; |
701 | ||
702 | req = kzalloc(sizeof(*req), GFP_KERNEL); | |
703 | if (!req) | |
704 | return -ENOMEM; | |
705 | ||
6a1d2959 | 706 | connection_flags = 0; |
74ec7598 | 707 | if (flags & GB_CONNECTION_FLAG_CONTROL) |
6a1d2959 | 708 | connection_flags |= GB_APB_CPORT_FLAG_CONTROL; |
74ec7598 | 709 | if (flags & GB_CONNECTION_FLAG_HIGH_PRIO) |
6a1d2959 BD |
710 | connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO; |
711 | ||
712 | req->flags = cpu_to_le32(connection_flags); | |
74ec7598 JH |
713 | |
714 | dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__, | |
6a1d2959 | 715 | cport_id, connection_flags); |
74ec7598 JH |
716 | |
717 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
718 | GB_APB_REQUEST_CPORT_FLAGS, | |
719 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
720 | USB_RECIP_INTERFACE, cport_id, 0, | |
60793c9b | 721 | req, sizeof(*req), ES2_USB_CTRL_TIMEOUT); |
74ec7598 JH |
722 | if (ret != sizeof(*req)) { |
723 | dev_err(&udev->dev, "failed to set cport flags for port %d\n", | |
724 | cport_id); | |
725 | if (ret >= 0) | |
726 | ret = -EIO; | |
727 | ||
728 | goto out; | |
729 | } | |
730 | ||
731 | ret = 0; | |
732 | out: | |
733 | kfree(req); | |
734 | ||
735 | return ret; | |
736 | } | |
737 | ||
6adcf441 | 738 | static int cport_disable(struct gb_host_device *hd, u16 cport_id) |
82ee1e6c FP |
739 | { |
740 | int retval; | |
741 | ||
e7b848c2 JH |
742 | retval = cport_reset(hd, cport_id); |
743 | if (retval) | |
744 | return retval; | |
82ee1e6c FP |
745 | |
746 | return 0; | |
747 | } | |
748 | ||
a9dc1cf5 JH |
749 | static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id) |
750 | { | |
751 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
752 | struct device *dev = &es2->usb_dev->dev; | |
753 | struct arpc_cport_connected_req req; | |
754 | int ret; | |
755 | ||
756 | req.cport_id = cpu_to_le16(cport_id); | |
757 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req), | |
758 | NULL, ES2_ARPC_CPORT_TIMEOUT); | |
759 | if (ret) { | |
760 | dev_err(dev, "failed to set connected state for cport %u: %d\n", | |
761 | cport_id, ret); | |
762 | return ret; | |
763 | } | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
2537636a | 768 | static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id) |
608ab2fe BD |
769 | { |
770 | int retval; | |
4b1d8204 AE |
771 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
772 | struct usb_device *udev = es2->usb_dev; | |
608ab2fe | 773 | |
608ab2fe | 774 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
e5273381 | 775 | GB_APB_REQUEST_LATENCY_TAG_EN, |
608ab2fe BD |
776 | USB_DIR_OUT | USB_TYPE_VENDOR | |
777 | USB_RECIP_INTERFACE, cport_id, 0, NULL, | |
60793c9b | 778 | 0, ES2_USB_CTRL_TIMEOUT); |
608ab2fe BD |
779 | |
780 | if (retval < 0) | |
781 | dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n", | |
782 | cport_id); | |
783 | return retval; | |
784 | } | |
785 | ||
2537636a | 786 | static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id) |
608ab2fe BD |
787 | { |
788 | int retval; | |
4b1d8204 AE |
789 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
790 | struct usb_device *udev = es2->usb_dev; | |
608ab2fe | 791 | |
608ab2fe | 792 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
e5273381 | 793 | GB_APB_REQUEST_LATENCY_TAG_DIS, |
608ab2fe BD |
794 | USB_DIR_OUT | USB_TYPE_VENDOR | |
795 | USB_RECIP_INTERFACE, cport_id, 0, NULL, | |
60793c9b | 796 | 0, ES2_USB_CTRL_TIMEOUT); |
608ab2fe BD |
797 | |
798 | if (retval < 0) | |
799 | dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n", | |
800 | cport_id); | |
801 | return retval; | |
802 | } | |
803 | ||
aa2a5459 | 804 | static int cport_features_enable(struct gb_host_device *hd, u16 cport_id) |
e70055b3 FP |
805 | { |
806 | int retval; | |
807 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
808 | struct usb_device *udev = es2->usb_dev; | |
809 | ||
810 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
aa2a5459 | 811 | GB_APB_REQUEST_CPORT_FEAT_EN, |
e70055b3 FP |
812 | USB_DIR_OUT | USB_TYPE_VENDOR | |
813 | USB_RECIP_INTERFACE, cport_id, 0, NULL, | |
60793c9b | 814 | 0, ES2_USB_CTRL_TIMEOUT); |
e70055b3 | 815 | if (retval < 0) |
aa2a5459 | 816 | dev_err(&udev->dev, "Cannot enable CPort features for cport %u: %d\n", |
e70055b3 FP |
817 | cport_id, retval); |
818 | return retval; | |
819 | } | |
820 | ||
aa2a5459 | 821 | static int cport_features_disable(struct gb_host_device *hd, u16 cport_id) |
e70055b3 FP |
822 | { |
823 | int retval; | |
824 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
825 | struct usb_device *udev = es2->usb_dev; | |
826 | ||
827 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
aa2a5459 | 828 | GB_APB_REQUEST_CPORT_FEAT_DIS, |
e70055b3 FP |
829 | USB_DIR_OUT | USB_TYPE_VENDOR | |
830 | USB_RECIP_INTERFACE, cport_id, 0, NULL, | |
60793c9b | 831 | 0, ES2_USB_CTRL_TIMEOUT); |
e70055b3 FP |
832 | if (retval < 0) |
833 | dev_err(&udev->dev, | |
aa2a5459 | 834 | "Cannot disable CPort features for cport %u: %d\n", |
e70055b3 FP |
835 | cport_id, retval); |
836 | return retval; | |
837 | } | |
838 | ||
c8a657ba BD |
839 | static int timesync_enable(struct gb_host_device *hd, u8 count, |
840 | u64 frame_time, u32 strobe_delay, u32 refclk) | |
841 | { | |
842 | int retval; | |
843 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
844 | struct usb_device *udev = es2->usb_dev; | |
845 | struct gb_control_timesync_enable_request *request; | |
846 | ||
847 | request = kzalloc(sizeof(*request), GFP_KERNEL); | |
848 | if (!request) | |
849 | return -ENOMEM; | |
850 | ||
851 | request->count = count; | |
852 | request->frame_time = cpu_to_le64(frame_time); | |
853 | request->strobe_delay = cpu_to_le32(strobe_delay); | |
854 | request->refclk = cpu_to_le32(refclk); | |
855 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
83d474f7 | 856 | GB_APB_REQUEST_TIMESYNC_ENABLE, |
c8a657ba BD |
857 | USB_DIR_OUT | USB_TYPE_VENDOR | |
858 | USB_RECIP_INTERFACE, 0, 0, request, | |
60793c9b | 859 | sizeof(*request), ES2_USB_CTRL_TIMEOUT); |
c8a657ba BD |
860 | if (retval < 0) |
861 | dev_err(&udev->dev, "Cannot enable timesync %d\n", retval); | |
862 | ||
863 | kfree(request); | |
864 | return retval; | |
865 | } | |
866 | ||
867 | static int timesync_disable(struct gb_host_device *hd) | |
868 | { | |
869 | int retval; | |
870 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
871 | struct usb_device *udev = es2->usb_dev; | |
872 | ||
873 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
83d474f7 | 874 | GB_APB_REQUEST_TIMESYNC_DISABLE, |
c8a657ba BD |
875 | USB_DIR_OUT | USB_TYPE_VENDOR | |
876 | USB_RECIP_INTERFACE, 0, 0, NULL, | |
60793c9b | 877 | 0, ES2_USB_CTRL_TIMEOUT); |
c8a657ba BD |
878 | if (retval < 0) |
879 | dev_err(&udev->dev, "Cannot disable timesync %d\n", retval); | |
880 | ||
881 | return retval; | |
882 | } | |
883 | ||
884 | static int timesync_authoritative(struct gb_host_device *hd, u64 *frame_time) | |
885 | { | |
886 | int retval, i; | |
887 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
888 | struct usb_device *udev = es2->usb_dev; | |
889 | struct timesync_authoritative_request *request; | |
890 | ||
891 | request = kzalloc(sizeof(*request), GFP_KERNEL); | |
892 | if (!request) | |
893 | return -ENOMEM; | |
894 | ||
895 | for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++) | |
896 | request->frame_time[i] = cpu_to_le64(frame_time[i]); | |
897 | ||
898 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
83d474f7 | 899 | GB_APB_REQUEST_TIMESYNC_AUTHORITATIVE, |
c8a657ba BD |
900 | USB_DIR_OUT | USB_TYPE_VENDOR | |
901 | USB_RECIP_INTERFACE, 0, 0, request, | |
60793c9b | 902 | sizeof(*request), ES2_USB_CTRL_TIMEOUT); |
c8a657ba BD |
903 | if (retval < 0) |
904 | dev_err(&udev->dev, "Cannot timesync authoritative out %d\n", retval); | |
905 | ||
906 | kfree(request); | |
907 | return retval; | |
908 | } | |
909 | ||
910 | static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time) | |
911 | { | |
912 | int retval; | |
913 | struct es2_ap_dev *es2 = hd_to_es2(hd); | |
914 | struct usb_device *udev = es2->usb_dev; | |
5319a889 | 915 | __le64 *response_frame_time; |
c8a657ba BD |
916 | |
917 | response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL); | |
918 | if (!response_frame_time) | |
919 | return -ENOMEM; | |
920 | ||
921 | retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
83d474f7 | 922 | GB_APB_REQUEST_TIMESYNC_GET_LAST_EVENT, |
c8a657ba BD |
923 | USB_DIR_IN | USB_TYPE_VENDOR | |
924 | USB_RECIP_INTERFACE, 0, 0, response_frame_time, | |
60793c9b JH |
925 | sizeof(*response_frame_time), |
926 | ES2_USB_CTRL_TIMEOUT); | |
c8a657ba BD |
927 | |
928 | if (retval != sizeof(*response_frame_time)) { | |
929 | dev_err(&udev->dev, "Cannot get last TimeSync event: %d\n", | |
930 | retval); | |
931 | ||
932 | if (retval >= 0) | |
933 | retval = -EIO; | |
934 | ||
935 | goto out; | |
936 | } | |
937 | *frame_time = le64_to_cpu(*response_frame_time); | |
938 | retval = 0; | |
939 | out: | |
940 | kfree(response_frame_time); | |
941 | return retval; | |
942 | } | |
943 | ||
a8cc020f | 944 | static struct gb_hd_driver es2_driver = { |
c8a657ba BD |
945 | .hd_priv_size = sizeof(struct es2_ap_dev), |
946 | .message_send = message_send, | |
947 | .message_cancel = message_cancel, | |
948 | .cport_allocate = es2_cport_allocate, | |
949 | .cport_release = es2_cport_release, | |
74ec7598 | 950 | .cport_enable = cport_enable, |
6adcf441 | 951 | .cport_disable = cport_disable, |
a9dc1cf5 | 952 | .cport_connected = es2_cport_connected, |
c8a657ba BD |
953 | .latency_tag_enable = latency_tag_enable, |
954 | .latency_tag_disable = latency_tag_disable, | |
955 | .output = output, | |
956 | .cport_features_enable = cport_features_enable, | |
957 | .cport_features_disable = cport_features_disable, | |
958 | .timesync_enable = timesync_enable, | |
959 | .timesync_disable = timesync_disable, | |
960 | .timesync_authoritative = timesync_authoritative, | |
961 | .timesync_get_last_event = timesync_get_last_event, | |
f587027e GKH |
962 | }; |
963 | ||
964 | /* Common function to report consistent warnings based on URB status */ | |
965 | static int check_urb_status(struct urb *urb) | |
966 | { | |
967 | struct device *dev = &urb->dev->dev; | |
968 | int status = urb->status; | |
969 | ||
970 | switch (status) { | |
971 | case 0: | |
972 | return 0; | |
973 | ||
974 | case -EOVERFLOW: | |
975 | dev_err(dev, "%s: overflow actual length is %d\n", | |
976 | __func__, urb->actual_length); | |
977 | case -ECONNRESET: | |
978 | case -ENOENT: | |
979 | case -ESHUTDOWN: | |
980 | case -EILSEQ: | |
981 | case -EPROTO: | |
982 | /* device is gone, stop sending */ | |
983 | return status; | |
984 | } | |
985 | dev_err(dev, "%s: unknown status %d\n", __func__, status); | |
986 | ||
987 | return -EAGAIN; | |
988 | } | |
989 | ||
57bc17ff | 990 | static void es2_destroy(struct es2_ap_dev *es2) |
f587027e | 991 | { |
f587027e | 992 | struct usb_device *udev; |
606addd2 | 993 | int bulk_in; |
f587027e GKH |
994 | int i; |
995 | ||
74cd6503 | 996 | debugfs_remove(es2->apb_log_enable_dentry); |
4b1d8204 | 997 | usb_log_disable(es2); |
ca3ec299 | 998 | |
f587027e GKH |
999 | /* Tear down everything! */ |
1000 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
4b1d8204 | 1001 | struct urb *urb = es2->cport_out_urb[i]; |
f587027e GKH |
1002 | |
1003 | if (!urb) | |
1004 | break; | |
1005 | usb_kill_urb(urb); | |
1006 | usb_free_urb(urb); | |
4b1d8204 AE |
1007 | es2->cport_out_urb[i] = NULL; |
1008 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ | |
f587027e GKH |
1009 | } |
1010 | ||
9d9d3777 AB |
1011 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { |
1012 | struct urb *urb = es2->arpc_urb[i]; | |
1013 | ||
1014 | if (!urb) | |
1015 | break; | |
1016 | usb_free_urb(urb); | |
1017 | kfree(es2->arpc_buffer[i]); | |
1018 | es2->arpc_buffer[i] = NULL; | |
1019 | } | |
1020 | ||
606addd2 | 1021 | for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) { |
4b1d8204 AE |
1022 | struct es2_cport_in *cport_in = &es2->cport_in[bulk_in]; |
1023 | ||
606addd2 AB |
1024 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
1025 | struct urb *urb = cport_in->urb[i]; | |
1026 | ||
1027 | if (!urb) | |
1028 | break; | |
606addd2 AB |
1029 | usb_free_urb(urb); |
1030 | kfree(cport_in->buffer[i]); | |
1031 | cport_in->buffer[i] = NULL; | |
1032 | } | |
f587027e GKH |
1033 | } |
1034 | ||
a5202862 JH |
1035 | kfree(es2->cport_to_ep); |
1036 | ||
52033fde VA |
1037 | /* release reserved CDSI0 and CDSI1 cports */ |
1038 | gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1); | |
1039 | gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0); | |
1040 | ||
4b1d8204 | 1041 | udev = es2->usb_dev; |
c1700479 | 1042 | gb_hd_put(es2->hd); |
f587027e GKH |
1043 | |
1044 | usb_put_dev(udev); | |
1045 | } | |
1046 | ||
f587027e GKH |
1047 | static void cport_in_callback(struct urb *urb) |
1048 | { | |
2537636a | 1049 | struct gb_host_device *hd = urb->context; |
f587027e | 1050 | struct device *dev = &urb->dev->dev; |
491e60d6 | 1051 | struct gb_operation_msg_hdr *header; |
f587027e GKH |
1052 | int status = check_urb_status(urb); |
1053 | int retval; | |
1054 | u16 cport_id; | |
f587027e GKH |
1055 | |
1056 | if (status) { | |
1057 | if ((status == -EAGAIN) || (status == -EPROTO)) | |
1058 | goto exit; | |
62de6e0a VK |
1059 | |
1060 | /* The urb is being unlinked */ | |
1061 | if (status == -ENOENT || status == -ESHUTDOWN) | |
1062 | return; | |
1063 | ||
f587027e GKH |
1064 | dev_err(dev, "urb cport in error %d (dropped)\n", status); |
1065 | return; | |
1066 | } | |
1067 | ||
491e60d6 | 1068 | if (urb->actual_length < sizeof(*header)) { |
305a031e | 1069 | dev_err(dev, "short message received\n"); |
f587027e GKH |
1070 | goto exit; |
1071 | } | |
1072 | ||
d29b3d63 | 1073 | /* Extract the CPort id, which is packed in the message header */ |
491e60d6 | 1074 | header = urb->transfer_buffer; |
d29b3d63 | 1075 | cport_id = gb_message_cport_unpack(header); |
f587027e | 1076 | |
6872c461 | 1077 | if (cport_id_valid(hd, cport_id)) { |
821c620a | 1078 | greybus_data_rcvd(hd, cport_id, urb->transfer_buffer, |
491e60d6 | 1079 | urb->actual_length); |
6872c461 | 1080 | } else { |
100e9000 | 1081 | dev_err(dev, "invalid cport id %u received\n", cport_id); |
6872c461 | 1082 | } |
f587027e GKH |
1083 | exit: |
1084 | /* put our urb back in the request pool */ | |
1085 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
1086 | if (retval) | |
305a031e | 1087 | dev_err(dev, "failed to resubmit in-urb: %d\n", retval); |
f587027e GKH |
1088 | } |
1089 | ||
1090 | static void cport_out_callback(struct urb *urb) | |
1091 | { | |
7cf7bca9 | 1092 | struct gb_message *message = urb->context; |
2537636a | 1093 | struct gb_host_device *hd = message->operation->connection->hd; |
4b1d8204 | 1094 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
f587027e | 1095 | int status = check_urb_status(urb); |
3e136cc9 | 1096 | unsigned long flags; |
f587027e | 1097 | |
d29b3d63 | 1098 | gb_message_cport_clear(message->header); |
491e60d6 | 1099 | |
4b1d8204 | 1100 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
58c85123 | 1101 | message->hcpriv = NULL; |
4b1d8204 | 1102 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
58c85123 | 1103 | |
f587027e | 1104 | /* |
7cf7bca9 JH |
1105 | * Tell the submitter that the message send (attempt) is |
1106 | * complete, and report the status. | |
f587027e | 1107 | */ |
7cf7bca9 JH |
1108 | greybus_message_sent(hd, message, status); |
1109 | ||
4b1d8204 | 1110 | free_urb(es2, urb); |
f587027e GKH |
1111 | } |
1112 | ||
c14118a8 AB |
1113 | static struct arpc *arpc_alloc(void *payload, u16 size, u8 type) |
1114 | { | |
1115 | struct arpc *rpc; | |
1116 | ||
1117 | if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX) | |
1118 | return NULL; | |
1119 | ||
1120 | rpc = kzalloc(sizeof(*rpc), GFP_KERNEL); | |
1121 | if (!rpc) | |
1122 | return NULL; | |
1123 | ||
1124 | INIT_LIST_HEAD(&rpc->list); | |
1125 | rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL); | |
1126 | if (!rpc->req) | |
1127 | goto err_free_rpc; | |
1128 | ||
1129 | rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL); | |
fcba5d04 | 1130 | if (!rpc->resp) |
c14118a8 AB |
1131 | goto err_free_req; |
1132 | ||
1133 | rpc->req->type = type; | |
1134 | rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size); | |
1135 | memcpy(rpc->req->data, payload, size); | |
1136 | ||
1137 | init_completion(&rpc->response_received); | |
1138 | ||
1139 | return rpc; | |
1140 | ||
1141 | err_free_req: | |
1142 | kfree(rpc->req); | |
1143 | err_free_rpc: | |
1144 | kfree(rpc); | |
1145 | ||
1146 | return NULL; | |
1147 | } | |
1148 | ||
1149 | static void arpc_free(struct arpc *rpc) | |
1150 | { | |
1151 | kfree(rpc->req); | |
1152 | kfree(rpc->resp); | |
1153 | kfree(rpc); | |
1154 | } | |
1155 | ||
165a74ab | 1156 | static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id) |
c14118a8 AB |
1157 | { |
1158 | struct arpc *rpc; | |
1159 | ||
1160 | list_for_each_entry(rpc, &es2->arpcs, list) { | |
165a74ab | 1161 | if (rpc->req->id == id) |
c14118a8 AB |
1162 | return rpc; |
1163 | } | |
1164 | ||
1165 | return NULL; | |
1166 | } | |
1167 | ||
1168 | static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc) | |
1169 | { | |
1170 | rpc->active = true; | |
e818027c | 1171 | rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++); |
c0219cbf | 1172 | list_add_tail(&rpc->list, &es2->arpcs); |
c14118a8 AB |
1173 | } |
1174 | ||
1175 | static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc) | |
1176 | { | |
1177 | if (rpc->active) { | |
1178 | rpc->active = false; | |
1179 | list_del(&rpc->list); | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout) | |
1184 | { | |
1185 | struct usb_device *udev = es2->usb_dev; | |
1186 | int retval; | |
1187 | ||
1188 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
83d474f7 | 1189 | GB_APB_REQUEST_ARPC_RUN, |
c14118a8 AB |
1190 | USB_DIR_OUT | USB_TYPE_VENDOR | |
1191 | USB_RECIP_INTERFACE, | |
1192 | 0, 0, | |
e818027c | 1193 | rpc->req, le16_to_cpu(rpc->req->size), |
60793c9b | 1194 | ES2_USB_CTRL_TIMEOUT); |
e818027c | 1195 | if (retval != le16_to_cpu(rpc->req->size)) { |
c14118a8 AB |
1196 | dev_err(&udev->dev, |
1197 | "failed to send ARPC request %d: %d\n", | |
1198 | rpc->req->type, retval); | |
1199 | if (retval > 0) | |
1200 | retval = -EIO; | |
1201 | return retval; | |
1202 | } | |
1203 | ||
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload, | |
1208 | size_t size, int *result, unsigned int timeout) | |
1209 | { | |
1210 | struct arpc *rpc; | |
1211 | unsigned long flags; | |
1212 | int retval; | |
1213 | ||
121bae2b JH |
1214 | if (result) |
1215 | *result = 0; | |
4ae2d962 | 1216 | |
c14118a8 AB |
1217 | rpc = arpc_alloc(payload, size, type); |
1218 | if (!rpc) | |
1219 | return -ENOMEM; | |
1220 | ||
1221 | spin_lock_irqsave(&es2->arpc_lock, flags); | |
1222 | arpc_add(es2, rpc); | |
1223 | spin_unlock_irqrestore(&es2->arpc_lock, flags); | |
1224 | ||
1225 | retval = arpc_send(es2, rpc, timeout); | |
1226 | if (retval) | |
1227 | goto out_arpc_del; | |
1228 | ||
1229 | retval = wait_for_completion_interruptible_timeout( | |
1230 | &rpc->response_received, | |
1231 | msecs_to_jiffies(timeout)); | |
1232 | if (retval <= 0) { | |
1233 | if (!retval) | |
1234 | retval = -ETIMEDOUT; | |
1235 | goto out_arpc_del; | |
1236 | } | |
1237 | ||
4ae2d962 | 1238 | if (rpc->resp->result) { |
c14118a8 | 1239 | retval = -EREMOTEIO; |
121bae2b JH |
1240 | if (result) |
1241 | *result = rpc->resp->result; | |
4ae2d962 | 1242 | } else { |
34873949 | 1243 | retval = 0; |
4ae2d962 | 1244 | } |
c14118a8 AB |
1245 | |
1246 | out_arpc_del: | |
1247 | spin_lock_irqsave(&es2->arpc_lock, flags); | |
1248 | arpc_del(es2, rpc); | |
1249 | spin_unlock_irqrestore(&es2->arpc_lock, flags); | |
1250 | arpc_free(rpc); | |
1251 | ||
1252 | if (retval < 0 && retval != -EREMOTEIO) { | |
1253 | dev_err(&es2->usb_dev->dev, | |
1254 | "failed to execute ARPC: %d\n", retval); | |
1255 | } | |
1256 | ||
1257 | return retval; | |
1258 | } | |
1259 | ||
9d9d3777 AB |
1260 | static void arpc_in_callback(struct urb *urb) |
1261 | { | |
c14118a8 | 1262 | struct es2_ap_dev *es2 = urb->context; |
9d9d3777 AB |
1263 | struct device *dev = &urb->dev->dev; |
1264 | int status = check_urb_status(urb); | |
c14118a8 AB |
1265 | struct arpc *rpc; |
1266 | struct arpc_response_message *resp; | |
1267 | unsigned long flags; | |
9d9d3777 AB |
1268 | int retval; |
1269 | ||
1270 | if (status) { | |
1271 | if ((status == -EAGAIN) || (status == -EPROTO)) | |
1272 | goto exit; | |
1273 | ||
1274 | /* The urb is being unlinked */ | |
1275 | if (status == -ENOENT || status == -ESHUTDOWN) | |
1276 | return; | |
1277 | ||
1278 | dev_err(dev, "arpc in-urb error %d (dropped)\n", status); | |
1279 | return; | |
1280 | } | |
1281 | ||
c14118a8 AB |
1282 | if (urb->actual_length < sizeof(*resp)) { |
1283 | dev_err(dev, "short aprc response received\n"); | |
1284 | goto exit; | |
1285 | } | |
1286 | ||
1287 | resp = urb->transfer_buffer; | |
1288 | spin_lock_irqsave(&es2->arpc_lock, flags); | |
165a74ab | 1289 | rpc = arpc_find(es2, resp->id); |
c14118a8 | 1290 | if (!rpc) { |
178457e1 JH |
1291 | dev_err(dev, "invalid arpc response id received: %u\n", |
1292 | le16_to_cpu(resp->id)); | |
c14118a8 AB |
1293 | spin_unlock_irqrestore(&es2->arpc_lock, flags); |
1294 | goto exit; | |
1295 | } | |
1296 | ||
1297 | arpc_del(es2, rpc); | |
1298 | memcpy(rpc->resp, resp, sizeof(*resp)); | |
1299 | complete(&rpc->response_received); | |
1300 | spin_unlock_irqrestore(&es2->arpc_lock, flags); | |
1301 | ||
9d9d3777 AB |
1302 | exit: |
1303 | /* put our urb back in the request pool */ | |
1304 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
1305 | if (retval) | |
1306 | dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval); | |
1307 | } | |
1308 | ||
c15ccabe | 1309 | #define APB1_LOG_MSG_SIZE 64 |
3be0e17d | 1310 | static void apb_log_get(struct es2_ap_dev *es2, char *buf) |
ca3ec299 | 1311 | { |
ca3ec299 GKH |
1312 | int retval; |
1313 | ||
ca3ec299 | 1314 | do { |
4b1d8204 AE |
1315 | retval = usb_control_msg(es2->usb_dev, |
1316 | usb_rcvctrlpipe(es2->usb_dev, 0), | |
e5273381 | 1317 | GB_APB_REQUEST_LOG, |
ca3ec299 GKH |
1318 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
1319 | 0x00, 0x00, | |
1320 | buf, | |
c15ccabe | 1321 | APB1_LOG_MSG_SIZE, |
60793c9b | 1322 | ES2_USB_CTRL_TIMEOUT); |
ca3ec299 | 1323 | if (retval > 0) |
3be0e17d | 1324 | kfifo_in(&es2->apb_log_fifo, buf, retval); |
ca3ec299 GKH |
1325 | } while (retval > 0); |
1326 | } | |
1327 | ||
3be0e17d | 1328 | static int apb_log_poll(void *data) |
ca3ec299 | 1329 | { |
4b1d8204 | 1330 | struct es2_ap_dev *es2 = data; |
c15ccabe JH |
1331 | char *buf; |
1332 | ||
1333 | buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL); | |
1334 | if (!buf) | |
1335 | return -ENOMEM; | |
1336 | ||
ca3ec299 GKH |
1337 | while (!kthread_should_stop()) { |
1338 | msleep(1000); | |
3be0e17d | 1339 | apb_log_get(es2, buf); |
ca3ec299 | 1340 | } |
c15ccabe JH |
1341 | |
1342 | kfree(buf); | |
1343 | ||
ca3ec299 GKH |
1344 | return 0; |
1345 | } | |
1346 | ||
3be0e17d | 1347 | static ssize_t apb_log_read(struct file *f, char __user *buf, |
ca3ec299 GKH |
1348 | size_t count, loff_t *ppos) |
1349 | { | |
8995a39d | 1350 | struct es2_ap_dev *es2 = f->f_inode->i_private; |
ca3ec299 GKH |
1351 | ssize_t ret; |
1352 | size_t copied; | |
1353 | char *tmp_buf; | |
1354 | ||
1355 | if (count > APB1_LOG_SIZE) | |
1356 | count = APB1_LOG_SIZE; | |
1357 | ||
1358 | tmp_buf = kmalloc(count, GFP_KERNEL); | |
1359 | if (!tmp_buf) | |
1360 | return -ENOMEM; | |
1361 | ||
3be0e17d | 1362 | copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count); |
ca3ec299 GKH |
1363 | ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied); |
1364 | ||
1365 | kfree(tmp_buf); | |
1366 | ||
1367 | return ret; | |
1368 | } | |
1369 | ||
3be0e17d AE |
1370 | static const struct file_operations apb_log_fops = { |
1371 | .read = apb_log_read, | |
ca3ec299 GKH |
1372 | }; |
1373 | ||
4b1d8204 | 1374 | static void usb_log_enable(struct es2_ap_dev *es2) |
ca3ec299 | 1375 | { |
3be0e17d | 1376 | if (!IS_ERR_OR_NULL(es2->apb_log_task)) |
ca3ec299 GKH |
1377 | return; |
1378 | ||
1379 | /* get log from APB1 */ | |
3be0e17d AE |
1380 | es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log"); |
1381 | if (IS_ERR(es2->apb_log_task)) | |
ca3ec299 | 1382 | return; |
3be0e17d AE |
1383 | /* XXX We will need to rename this per APB */ |
1384 | es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO, | |
05a84919 | 1385 | gb_debugfs_get(), es2, |
3be0e17d | 1386 | &apb_log_fops); |
ca3ec299 GKH |
1387 | } |
1388 | ||
4b1d8204 | 1389 | static void usb_log_disable(struct es2_ap_dev *es2) |
ca3ec299 | 1390 | { |
3be0e17d | 1391 | if (IS_ERR_OR_NULL(es2->apb_log_task)) |
ca3ec299 GKH |
1392 | return; |
1393 | ||
3be0e17d AE |
1394 | debugfs_remove(es2->apb_log_dentry); |
1395 | es2->apb_log_dentry = NULL; | |
ca3ec299 | 1396 | |
3be0e17d AE |
1397 | kthread_stop(es2->apb_log_task); |
1398 | es2->apb_log_task = NULL; | |
ca3ec299 GKH |
1399 | } |
1400 | ||
3be0e17d | 1401 | static ssize_t apb_log_enable_read(struct file *f, char __user *buf, |
ca3ec299 GKH |
1402 | size_t count, loff_t *ppos) |
1403 | { | |
1482b3e1 | 1404 | struct es2_ap_dev *es2 = f->f_inode->i_private; |
3be0e17d | 1405 | int enable = !IS_ERR_OR_NULL(es2->apb_log_task); |
4b1d8204 | 1406 | char tmp_buf[3]; |
ca3ec299 GKH |
1407 | |
1408 | sprintf(tmp_buf, "%d\n", enable); | |
1409 | return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3); | |
1410 | } | |
1411 | ||
3be0e17d | 1412 | static ssize_t apb_log_enable_write(struct file *f, const char __user *buf, |
ca3ec299 GKH |
1413 | size_t count, loff_t *ppos) |
1414 | { | |
1415 | int enable; | |
1416 | ssize_t retval; | |
1482b3e1 | 1417 | struct es2_ap_dev *es2 = f->f_inode->i_private; |
ca3ec299 GKH |
1418 | |
1419 | retval = kstrtoint_from_user(buf, count, 10, &enable); | |
1420 | if (retval) | |
1421 | return retval; | |
1422 | ||
1423 | if (enable) | |
4b1d8204 | 1424 | usb_log_enable(es2); |
ca3ec299 | 1425 | else |
4b1d8204 | 1426 | usb_log_disable(es2); |
ca3ec299 GKH |
1427 | |
1428 | return count; | |
1429 | } | |
1430 | ||
3be0e17d AE |
1431 | static const struct file_operations apb_log_enable_fops = { |
1432 | .read = apb_log_enable_read, | |
1433 | .write = apb_log_enable_write, | |
ca3ec299 GKH |
1434 | }; |
1435 | ||
3be0e17d | 1436 | static int apb_get_cport_count(struct usb_device *udev) |
24a6112f FP |
1437 | { |
1438 | int retval; | |
1439 | __le16 *cport_count; | |
1440 | ||
478ce720 | 1441 | cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL); |
24a6112f FP |
1442 | if (!cport_count) |
1443 | return -ENOMEM; | |
1444 | ||
1445 | retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
e5273381 | 1446 | GB_APB_REQUEST_CPORT_COUNT, |
24a6112f FP |
1447 | USB_DIR_IN | USB_TYPE_VENDOR | |
1448 | USB_RECIP_INTERFACE, 0, 0, cport_count, | |
60793c9b | 1449 | sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT); |
478ce720 | 1450 | if (retval != sizeof(*cport_count)) { |
24a6112f FP |
1451 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", |
1452 | retval); | |
478ce720 JH |
1453 | |
1454 | if (retval >= 0) | |
1455 | retval = -EIO; | |
1456 | ||
24a6112f FP |
1457 | goto out; |
1458 | } | |
1459 | ||
1460 | retval = le16_to_cpu(*cport_count); | |
1461 | ||
1462 | /* We need to fit a CPort ID in one byte of a message header */ | |
1463 | if (retval > U8_MAX) { | |
1464 | retval = U8_MAX; | |
1465 | dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n"); | |
1466 | } | |
1467 | ||
1468 | out: | |
1469 | kfree(cport_count); | |
1470 | return retval; | |
1471 | } | |
1472 | ||
f587027e | 1473 | /* |
4d5c446b JH |
1474 | * The ES2 USB Bridge device has 15 endpoints |
1475 | * 1 Control - usual USB stuff + AP -> APBridgeA messages | |
1476 | * 7 Bulk IN - CPort data in | |
1477 | * 7 Bulk OUT - CPort data out | |
f587027e GKH |
1478 | */ |
1479 | static int ap_probe(struct usb_interface *interface, | |
1480 | const struct usb_device_id *id) | |
1481 | { | |
4b1d8204 | 1482 | struct es2_ap_dev *es2; |
2537636a | 1483 | struct gb_host_device *hd; |
f587027e GKH |
1484 | struct usb_device *udev; |
1485 | struct usb_host_interface *iface_desc; | |
1486 | struct usb_endpoint_descriptor *endpoint; | |
606addd2 AB |
1487 | int bulk_in = 0; |
1488 | int bulk_out = 0; | |
be5d01f1 | 1489 | int retval; |
f587027e | 1490 | int i; |
24a6112f | 1491 | int num_cports; |
4bc1389d | 1492 | |
f587027e GKH |
1493 | udev = usb_get_dev(interface_to_usbdev(interface)); |
1494 | ||
3be0e17d | 1495 | num_cports = apb_get_cport_count(udev); |
24a6112f FP |
1496 | if (num_cports < 0) { |
1497 | usb_put_dev(udev); | |
1498 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", | |
1499 | num_cports); | |
1500 | return num_cports; | |
1501 | } | |
1502 | ||
d6e139bc JH |
1503 | hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX, |
1504 | num_cports); | |
8ea70fe0 | 1505 | if (IS_ERR(hd)) { |
f587027e | 1506 | usb_put_dev(udev); |
8ea70fe0 | 1507 | return PTR_ERR(hd); |
f587027e GKH |
1508 | } |
1509 | ||
4b1d8204 AE |
1510 | es2 = hd_to_es2(hd); |
1511 | es2->hd = hd; | |
1512 | es2->usb_intf = interface; | |
1513 | es2->usb_dev = udev; | |
1514 | spin_lock_init(&es2->cport_out_urb_lock); | |
3be0e17d | 1515 | INIT_KFIFO(es2->apb_log_fifo); |
4b1d8204 | 1516 | usb_set_intfdata(interface, es2); |
f587027e | 1517 | |
05061507 JH |
1518 | /* |
1519 | * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated | |
1520 | * dynamically. | |
1521 | */ | |
1522 | retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0); | |
1523 | if (retval) | |
1524 | goto error; | |
1525 | retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1); | |
1526 | if (retval) | |
1527 | goto error; | |
1528 | ||
4b1d8204 | 1529 | es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep), |
c011d558 | 1530 | GFP_KERNEL); |
4b1d8204 | 1531 | if (!es2->cport_to_ep) { |
c011d558 FP |
1532 | retval = -ENOMEM; |
1533 | goto error; | |
1534 | } | |
1535 | ||
4d5c446b | 1536 | /* find all bulk endpoints */ |
f587027e GKH |
1537 | iface_desc = interface->cur_altsetting; |
1538 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
1539 | endpoint = &iface_desc->endpoint[i].desc; | |
1540 | ||
b767ee40 | 1541 | if (usb_endpoint_is_bulk_in(endpoint)) { |
9d9d3777 AB |
1542 | if (bulk_in < NUM_BULKS) |
1543 | es2->cport_in[bulk_in].endpoint = | |
1544 | endpoint->bEndpointAddress; | |
1545 | else | |
1546 | es2->arpc_endpoint_in = | |
1547 | endpoint->bEndpointAddress; | |
1548 | bulk_in++; | |
f587027e | 1549 | } else if (usb_endpoint_is_bulk_out(endpoint)) { |
4b1d8204 | 1550 | es2->cport_out[bulk_out++].endpoint = |
606addd2 | 1551 | endpoint->bEndpointAddress; |
f587027e GKH |
1552 | } else { |
1553 | dev_err(&udev->dev, | |
b933fa4a | 1554 | "Unknown endpoint type found, address 0x%02x\n", |
f587027e GKH |
1555 | endpoint->bEndpointAddress); |
1556 | } | |
1557 | } | |
9d9d3777 | 1558 | if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) { |
f587027e | 1559 | dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n"); |
be5d01f1 | 1560 | retval = -ENODEV; |
f587027e GKH |
1561 | goto error; |
1562 | } | |
1563 | ||
0ce68ce4 | 1564 | /* Allocate buffers for our cport in messages */ |
606addd2 | 1565 | for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) { |
4b1d8204 AE |
1566 | struct es2_cport_in *cport_in = &es2->cport_in[bulk_in]; |
1567 | ||
606addd2 AB |
1568 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
1569 | struct urb *urb; | |
1570 | u8 *buffer; | |
1571 | ||
1572 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
be5d01f1 JH |
1573 | if (!urb) { |
1574 | retval = -ENOMEM; | |
606addd2 | 1575 | goto error; |
be5d01f1 | 1576 | } |
4b1d8204 | 1577 | buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL); |
be5d01f1 JH |
1578 | if (!buffer) { |
1579 | retval = -ENOMEM; | |
606addd2 | 1580 | goto error; |
be5d01f1 | 1581 | } |
606addd2 AB |
1582 | |
1583 | usb_fill_bulk_urb(urb, udev, | |
1584 | usb_rcvbulkpipe(udev, | |
1585 | cport_in->endpoint), | |
4b1d8204 | 1586 | buffer, ES2_GBUF_MSG_SIZE_MAX, |
606addd2 AB |
1587 | cport_in_callback, hd); |
1588 | cport_in->urb[i] = urb; | |
1589 | cport_in->buffer[i] = buffer; | |
606addd2 | 1590 | } |
f587027e GKH |
1591 | } |
1592 | ||
9d9d3777 AB |
1593 | /* Allocate buffers for ARPC in messages */ |
1594 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { | |
1595 | struct urb *urb; | |
1596 | u8 *buffer; | |
1597 | ||
1598 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
1599 | if (!urb) { | |
1600 | retval = -ENOMEM; | |
1601 | goto error; | |
1602 | } | |
1603 | buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL); | |
1604 | if (!buffer) { | |
1605 | retval = -ENOMEM; | |
1606 | goto error; | |
1607 | } | |
1608 | ||
1609 | usb_fill_bulk_urb(urb, udev, | |
1610 | usb_rcvbulkpipe(udev, | |
1611 | es2->arpc_endpoint_in), | |
1612 | buffer, ARPC_IN_SIZE_MAX, | |
1613 | arpc_in_callback, es2); | |
1614 | ||
1615 | es2->arpc_urb[i] = urb; | |
1616 | es2->arpc_buffer[i] = buffer; | |
1617 | } | |
1618 | ||
f587027e GKH |
1619 | /* Allocate urbs for our CPort OUT messages */ |
1620 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { | |
1621 | struct urb *urb; | |
1622 | ||
1623 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
be5d01f1 JH |
1624 | if (!urb) { |
1625 | retval = -ENOMEM; | |
f587027e | 1626 | goto error; |
be5d01f1 | 1627 | } |
f587027e | 1628 | |
4b1d8204 AE |
1629 | es2->cport_out_urb[i] = urb; |
1630 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ | |
f587027e GKH |
1631 | } |
1632 | ||
3be0e17d AE |
1633 | /* XXX We will need to rename this per APB */ |
1634 | es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable", | |
86f918ee | 1635 | (S_IWUSR | S_IRUGO), |
4b1d8204 | 1636 | gb_debugfs_get(), es2, |
3be0e17d | 1637 | &apb_log_enable_fops); |
0ce68ce4 | 1638 | |
c14118a8 AB |
1639 | INIT_LIST_HEAD(&es2->arpcs); |
1640 | spin_lock_init(&es2->arpc_lock); | |
1641 | ||
9d9d3777 AB |
1642 | if (es2_arpc_in_enable(es2)) |
1643 | goto error; | |
1644 | ||
c1700479 JH |
1645 | retval = gb_hd_add(hd); |
1646 | if (retval) | |
9d9d3777 | 1647 | goto err_disable_arpc_in; |
c1700479 | 1648 | |
0ce68ce4 JH |
1649 | for (i = 0; i < NUM_BULKS; ++i) { |
1650 | retval = es2_cport_in_enable(es2, &es2->cport_in[i]); | |
1651 | if (retval) | |
57bc17ff | 1652 | goto err_disable_cport_in; |
0ce68ce4 JH |
1653 | } |
1654 | ||
f587027e | 1655 | return 0; |
57bc17ff JH |
1656 | |
1657 | err_disable_cport_in: | |
1658 | for (--i; i >= 0; --i) | |
1659 | es2_cport_in_disable(es2, &es2->cport_in[i]); | |
c1700479 | 1660 | gb_hd_del(hd); |
9d9d3777 AB |
1661 | err_disable_arpc_in: |
1662 | es2_arpc_in_disable(es2); | |
f587027e | 1663 | error: |
57bc17ff | 1664 | es2_destroy(es2); |
f587027e GKH |
1665 | |
1666 | return retval; | |
1667 | } | |
1668 | ||
2437f1c6 VK |
1669 | static void ap_disconnect(struct usb_interface *interface) |
1670 | { | |
1671 | struct es2_ap_dev *es2 = usb_get_intfdata(interface); | |
1672 | int i; | |
1673 | ||
1674 | gb_hd_del(es2->hd); | |
1675 | ||
1676 | for (i = 0; i < NUM_BULKS; ++i) | |
1677 | es2_cport_in_disable(es2, &es2->cport_in[i]); | |
9d9d3777 | 1678 | es2_arpc_in_disable(es2); |
2437f1c6 VK |
1679 | |
1680 | es2_destroy(es2); | |
1681 | } | |
1682 | ||
4b1d8204 | 1683 | static struct usb_driver es2_ap_driver = { |
c13c8bf0 | 1684 | .name = "es2_ap_driver", |
f587027e GKH |
1685 | .probe = ap_probe, |
1686 | .disconnect = ap_disconnect, | |
1687 | .id_table = id_table, | |
a0f997bd | 1688 | .soft_unbind = 1, |
f587027e GKH |
1689 | }; |
1690 | ||
4b1d8204 | 1691 | module_usb_driver(es2_ap_driver); |
f587027e | 1692 | |
6cf42a44 | 1693 | MODULE_LICENSE("GPL v2"); |
f587027e | 1694 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>"); |