-
Notifications
You must be signed in to change notification settings - Fork 30
/
fl2000_streaming.c
417 lines (339 loc) · 10.7 KB
/
fl2000_streaming.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// SPDX-License-Identifier: GPL-2.0
/*
* Original driver uses default altsetting (#0) of streaming interface, which allows bursts of bulk
* transfers of 15x1024 bytes on output. But the HW actually works incorrectly here: it uses same
* endpoint #1 across interfaces 1 and 2, which is not allowed by USB specification: endpoint
* addresses can be shared only between alternate settings, not interfaces. In order to workaround
* this we use isochronous transfers instead of bulk. There is a possibility that we still can use
* bulk transfers with interface 0, but this is yet to be checked.
*
* (C) Copyright 2017, Fresco Logic, Incorporated.
* (C) Copyright 2018-2020, Artem Mygaiev
*/
#include "fl2000.h"
/* Triple buffering:
* - one buffer for HDMI rendering
* - one buffer for USB transmission
* - one buffer for DRM/KMS data copy
*/
#define FL2000_SB_MIN 3
#define FL2000_SB_NUM (FL2000_SB_MIN + 1)
#define FL2000_URB_TIMEOUT 100
struct fl2000_stream_buf {
struct list_head list;
struct sg_table sgt;
struct page **pages;
unsigned int nr_pages;
void *vaddr;
};
struct fl2000_stream {
struct usb_device *usb_dev;
struct drm_crtc *crtc;
/* Each buffer journey: render->transmit->wait->... */
struct list_head render_list;
struct list_head transmit_list;
struct list_head wait_list;
spinlock_t list_lock; /* List access from bh and interrupt contexts */
size_t buf_size;
u32 bytes_pix;
struct work_struct work;
struct workqueue_struct *work_queue;
struct semaphore work_sem;
bool enabled;
struct usb_anchor anchor;
};
static void fl2000_free_sb(struct fl2000_stream_buf *sb)
{
vunmap(sb->vaddr);
sg_free_table(&sb->sgt);
for (int i = 0; i < sb->nr_pages && sb->pages[i]; i++)
__free_page(sb->pages[i]);
kfree(sb->pages);
kfree(sb);
}
static struct fl2000_stream_buf *fl2000_alloc_sb(unsigned int size)
{
int ret;
struct fl2000_stream_buf *sb;
unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
sb = kzalloc(sizeof(*sb), GFP_KERNEL);
if (!sb)
return NULL;
sb->nr_pages = nr_pages;
sb->pages = kcalloc(nr_pages, sizeof(*sb->pages), GFP_KERNEL);
if (!sb->pages)
goto error;
for (int i = 0; i < nr_pages; i++) {
sb->pages[i] = alloc_page(GFP_KERNEL);
if (!sb->pages[i])
goto error;
}
ret = sg_alloc_table_from_pages(&sb->sgt, sb->pages, nr_pages, 0, size, GFP_KERNEL);
if (ret != 0)
goto error;
sb->vaddr = vmap(sb->pages, nr_pages, VM_MAP, PAGE_KERNEL);
if (!sb->vaddr)
goto error;
INIT_LIST_HEAD(&sb->list);
memset(sb->vaddr, 0, nr_pages << PAGE_SHIFT);
return sb;
error:
fl2000_free_sb(sb);
return NULL;
}
static void fl2000_stream_put_buffers(struct fl2000_stream *stream)
{
struct fl2000_stream_buf *cur_sb;
struct fl2000_stream_buf *temp_sb;
list_for_each_entry_safe(cur_sb, temp_sb, &stream->render_list, list) {
list_del(&cur_sb->list);
fl2000_free_sb(cur_sb);
}
}
static int fl2000_stream_get_buffers(struct fl2000_stream *stream, unsigned int size)
{
int ret;
struct fl2000_stream_buf *cur_sb;
BUG_ON(!list_empty(&stream->render_list));
for (int i = 0; i < FL2000_SB_NUM; i++) {
cur_sb = fl2000_alloc_sb(size);
if (!cur_sb) {
ret = -ENOMEM;
goto error;
}
list_add(&cur_sb->list, &stream->render_list);
}
return 0;
error:
fl2000_stream_put_buffers(stream);
return ret;
}
static void fl2000_stream_release(struct device *dev, void *res)
{
struct fl2000_stream *stream = res;
UNUSED(dev);
fl2000_stream_disable(stream);
destroy_workqueue(stream->work_queue);
fl2000_stream_put_buffers(stream);
}
static void fl2000_stream_data_completion(struct urb *urb)
{
struct fl2000_stream_buf *cur_sb = urb->transfer_buffer;
struct usb_device *usb_dev = urb->dev;
struct fl2000_stream *stream = urb->context;
if (stream) {
spin_lock_irq(&stream->list_lock);
list_move_tail(&cur_sb->list, &stream->render_list);
spin_unlock(&stream->list_lock);
drm_crtc_handle_vblank(stream->crtc);
/* Kick transmit workqueue */
up(&stream->work_sem);
fl2000_urb_status(usb_dev, urb->status, urb->pipe);
}
usb_free_urb(urb);
}
/* TODO: convert to tasklet */
static void fl2000_stream_work(struct work_struct *work)
{
int ret;
struct fl2000_stream *stream = container_of(work, struct fl2000_stream, work);
struct usb_device *usb_dev = stream->usb_dev;
struct fl2000_stream_buf *cur_sb;
struct fl2000_stream_buf *last_sb;
struct urb *data_urb;
while (stream->enabled) {
ret = down_interruptible(&stream->work_sem);
if (ret) {
dev_err(&usb_dev->dev, "Work interrupt error %d", ret);
stream->enabled = false;
return;
}
spin_lock_irq(&stream->list_lock);
/* If no buffers are available for immediate transmission - then copy latest
* transmission data
*/
if (list_empty(&stream->transmit_list)) {
if (list_empty(&stream->wait_list))
last_sb = list_last_entry(&stream->render_list,
struct fl2000_stream_buf, list);
else
last_sb = list_last_entry(&stream->wait_list,
struct fl2000_stream_buf, list);
cur_sb = list_first_entry(&stream->render_list, struct fl2000_stream_buf,
list);
memcpy(cur_sb->vaddr, last_sb->vaddr, stream->buf_size);
} else {
cur_sb = list_first_entry(&stream->transmit_list, struct fl2000_stream_buf,
list);
}
list_move_tail(&cur_sb->list, &stream->wait_list);
spin_unlock(&stream->list_lock);
data_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!data_urb) {
dev_err(&usb_dev->dev, "Data URB allocation error");
stream->enabled = false;
return;
}
/* Endpoint 1 bulk out. We store pointer to current stream buffer structure in
* transfer_buffer field of URB which is unused due to SGT
*/
usb_fill_bulk_urb(data_urb, usb_dev, usb_sndbulkpipe(usb_dev, 1), cur_sb,
(int)stream->buf_size, fl2000_stream_data_completion, stream);
data_urb->interval = 0;
data_urb->sg = cur_sb->sgt.sgl;
data_urb->num_sgs = cur_sb->sgt.nents;
data_urb->transfer_flags |= URB_ZERO_PACKET;
usb_anchor_urb(data_urb, &stream->anchor);
ret = fl2000_submit_urb(data_urb);
if (ret) {
dev_err(&usb_dev->dev, "Data URB error %d", ret);
usb_free_urb(data_urb);
stream->enabled = false;
}
}
}
static void fl2000_xrgb888_to_rgb888_line(u8 *dbuf, u32 *sbuf, u32 pixels)
{
unsigned int xx = 0;
for (unsigned int x = 0; x < pixels; x++) {
dbuf[xx++ ^ 4] = (sbuf[x] & 0x000000FF) >> 0;
dbuf[xx++ ^ 4] = (sbuf[x] & 0x0000FF00) >> 8;
dbuf[xx++ ^ 4] = (sbuf[x] & 0x00FF0000) >> 16;
}
}
static void fl2000_xrgb888_to_rgb565_line(u16 *dbuf, u32 *sbuf, u32 pixels)
{
for (unsigned int x = 0; x < pixels; x++) {
u16 val565 = ((sbuf[x] & 0x00F80000) >> 8) | ((sbuf[x] & 0x0000FC00) >> 5) |
((sbuf[x] & 0x000000F8) >> 3);
dbuf[x ^ 2] = val565;
}
}
void fl2000_stream_compress(struct fl2000_stream *stream, void *src, unsigned int height,
unsigned int width, unsigned int pitch)
{
struct fl2000_stream_buf *cur_sb;
void *dst;
u32 dst_line_len;
BUG_ON(list_empty(&stream->render_list));
spin_lock_irq(&stream->list_lock);
cur_sb = list_first_entry(&stream->render_list, struct fl2000_stream_buf, list);
dst = cur_sb->vaddr;
dst_line_len = width * stream->bytes_pix;
for (unsigned int y = 0; y < height; y++) {
switch (stream->bytes_pix) {
case 2:
fl2000_xrgb888_to_rgb565_line(dst, src, width);
break;
case 3:
fl2000_xrgb888_to_rgb888_line(dst, src, width);
break;
default: /* Shouldn't happen */
break;
}
src += pitch;
dst += dst_line_len;
}
list_move_tail(&cur_sb->list, &stream->transmit_list);
spin_unlock(&stream->list_lock);
}
int fl2000_stream_mode_set(struct fl2000_stream *stream, int pixels, u32 bytes_pix)
{
int ret;
unsigned int size;
/* Round buffer size up to multiple of 8 to meet HW expectations */
size = (pixels * bytes_pix + 7) & ~7U;
stream->bytes_pix = bytes_pix;
/* If there are buffers with same size - keep them */
if (stream->buf_size == size)
return 0;
/* Destroy wrong size buffers if they exist */
if (!list_empty(&stream->render_list))
fl2000_stream_put_buffers(stream);
/* Allocate new buffers */
ret = fl2000_stream_get_buffers(stream, size);
if (ret) {
fl2000_stream_put_buffers(stream);
stream->buf_size = 0;
return ret;
}
stream->buf_size = size;
return 0;
}
int fl2000_stream_enable(struct fl2000_stream *stream)
{
BUG_ON(list_empty(&stream->transmit_list));
sema_init(&stream->work_sem, 0);
stream->enabled = true;
queue_work(stream->work_queue, &stream->work);
/* Kick transmit workqueue with minimum buffers submitted */
for (int i = 0; i < FL2000_SB_MIN; i++)
up(&stream->work_sem);
return 0;
}
void fl2000_stream_disable(struct fl2000_stream *stream)
{
struct fl2000_stream_buf *cur_sb;
stream->enabled = false;
drain_workqueue(stream->work_queue);
if (!usb_wait_anchor_empty_timeout(&stream->anchor, 1000))
usb_kill_anchored_urbs(&stream->anchor);
spin_lock_irq(&stream->list_lock);
while (!list_empty(&stream->transmit_list)) {
cur_sb = list_first_entry(&stream->transmit_list, struct fl2000_stream_buf, list);
list_move_tail(&cur_sb->list, &stream->render_list);
}
while (!list_empty(&stream->wait_list)) {
cur_sb = list_first_entry(&stream->wait_list, struct fl2000_stream_buf, list);
list_move_tail(&cur_sb->list, &stream->render_list);
}
spin_unlock(&stream->list_lock);
}
/**
* fl2000_stream_create() - streaming processing context creation
* @interface: streaming transfers interface
*
* This function is called only on Streaming interface probe
*
* It shall not initiate any USB transfers. URB is not allocated here because we do not know the
* stream requirements yet.
*
* Return: Operation result
*/
struct fl2000_stream *fl2000_stream_create(struct usb_device *usb_dev, struct drm_crtc *crtc)
{
int ret;
struct fl2000_stream *stream;
/* Altsettung 1 on interface 0 */
ret = usb_set_interface(usb_dev, FL2000_USBIF_AVCONTROL, 1);
if (ret) {
dev_err(&usb_dev->dev, "Cannot set streaming interface for bulk transfers");
return ERR_PTR(ret);
}
stream = devres_alloc(&fl2000_stream_release, sizeof(*stream), GFP_KERNEL);
if (!stream) {
dev_err(&usb_dev->dev, "Cannot allocate stream");
return ERR_PTR(-ENOMEM);
}
devres_add(&usb_dev->dev, stream);
INIT_WORK(&stream->work, &fl2000_stream_work);
INIT_LIST_HEAD(&stream->render_list);
INIT_LIST_HEAD(&stream->transmit_list);
INIT_LIST_HEAD(&stream->wait_list);
spin_lock_init(&stream->list_lock);
init_usb_anchor(&stream->anchor);
sema_init(&stream->work_sem, 0);
stream->usb_dev = usb_dev;
stream->crtc = crtc;
stream->work_queue = create_workqueue("fl2000_stream");
if (!stream->work_queue) {
dev_err(&usb_dev->dev, "Allocate streaming workqueue failed");
devres_release(&usb_dev->dev, fl2000_stream_release, NULL, NULL);
return ERR_PTR(-ENOMEM);
}
return stream;
}
void fl2000_stream_destroy(struct usb_device *usb_dev)
{
devres_release(&usb_dev->dev, fl2000_stream_release, NULL, NULL);
}