summaryrefslogtreecommitdiff
path: root/gm12u320_main.c
blob: 804ca55df10367dd841f965a27bb31123e76d988 (plain)
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
/*
 * Copyright (C) 2012-2015 Red Hat Inc.
 *
 * Based in parts on the udl code. Based in parts on the gm12u320 fb driver:
 * Copyright (C) 2013 Viacheslav Nurmekhamitov <slavrn@yandex.ru>
 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License v2. See the file COPYING in the main directory of this archive for
 * more details.
 */
#include <drm/drmP.h>
#include "gm12u320_drv.h"

#define DATA_RCV_EPT			2
#define DATA_SND_EPT			3

#define DATA_BLOCK_HEADER_SIZE		84
#define DATA_BLOCK_CONTENT_SIZE		64512
#define DATA_BLOCK_FOOTER_SIZE		20
#define DATA_BLOCK_SIZE			DATA_BLOCK_HEADER_SIZE + \
					DATA_BLOCK_CONTENT_SIZE + \
					DATA_BLOCK_FOOTER_SIZE
#define DATA_LAST_BLOCK_CONTENT_SIZE	4032
#define DATA_LAST_BLOCK_SIZE		DATA_BLOCK_HEADER_SIZE + \
					DATA_LAST_BLOCK_CONTENT_SIZE + \
					DATA_BLOCK_FOOTER_SIZE

#define CMD_SIZE			31
#define READ_BLOCK_SIZE			13

#define CMD_TIMEOUT		msecs_to_jiffies(200)
#define DATA_TIMEOUT		msecs_to_jiffies(1000)
#define IDLE_TIMEOUT		msecs_to_jiffies(2000)
#define FIRST_FRAME_TIMEOUT	msecs_to_jiffies(2000)

static const char cmd_data[CMD_SIZE] =
	{0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x68, 0xfc, 0x00, 0x00,
	0x00, 0x00, 0x10, 0xff, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x80, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

static const char cmd_draw[CMD_SIZE] =
	{0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x10, 0xfe, 0x00, 0x00, 0x00, 0xc0, 0xd1, 0x05, 0x00, 0x40,
	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };

static const char data_block_header[DATA_BLOCK_HEADER_SIZE] =
	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0xfb, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x04, 0x15, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x01, 0x00, 0x00, 0xdb };

static const char data_last_block_header[DATA_BLOCK_HEADER_SIZE] =
	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0xfb, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x2a, 0x00, 0x20, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x01, 0x00, 0x00, 0xd7 };

static const char data_block_footer[DATA_BLOCK_FOOTER_SIZE] =
	{ 0xfb, 0x14, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x4f };

static const char bl_get_set_brightness[CMD_SIZE] =
	{0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
	0x80, 0x01, 0x10, 0xfd, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x35, 0x00, 0x00,
	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };

static int gm12u320_usb_alloc(struct gm12u320_device *gm12u320)
{
	int i, k;
	int block_size;
	const char *header;

	gm12u320->cmd_buf = kmalloc(CMD_SIZE, GFP_KERNEL);
	if (!gm12u320->cmd_buf)
		return -ENOMEM;

	for (k = 0; k < GM12U320_FRAME_COUNT; k++) {
		for (i = 0; i < GM12U320_BLOCK_COUNT; i++) {
			if (i == GM12U320_BLOCK_COUNT - 1) {
				block_size = DATA_LAST_BLOCK_SIZE;
				header = data_last_block_header;
			} else {
				block_size = DATA_BLOCK_SIZE;
				header = data_block_header;
			}

			gm12u320->data_buf[k][i] = kzalloc(block_size,
							   GFP_KERNEL);
			if (!gm12u320->data_buf[k][i])
				return -ENOMEM;

			memcpy(gm12u320->data_buf[k][i], header,
			       DATA_BLOCK_HEADER_SIZE);
			memcpy(gm12u320->data_buf[k][i] +
					(block_size - DATA_BLOCK_FOOTER_SIZE),
			       data_block_footer, DATA_BLOCK_FOOTER_SIZE);
		}
	}
	return 0;
}

static void gm12u320_usb_free(struct gm12u320_device *gm12u320)
{
	int i, k;

	for (k = 0; k < GM12U320_FRAME_COUNT; k++)
		for (i = 0; i < GM12U320_BLOCK_COUNT; i++)
			kfree(gm12u320->data_buf[k][i]);

	kfree(gm12u320->cmd_buf);
}

void gm12u320_32bpp_to_24bpp_packed(u8 *dst, u8 *src, int len)
{
	while (len--) {
		*dst++ = *src++;
		*dst++ = *src++;
		*dst++ = *src++;
		src++;
	}
}

void gm12u320_update_frame(struct gm12u320_framebuffer *fb)
{
	struct drm_device *dev = fb->base.dev;
	struct gm12u320_device *gm12u320 = dev->dev_private;
	int block, dst_offset, frame, len, remain, ret;
	unsigned long flags;
	u8 *src;
	int x1 = 0;
	int x2 = GM12U320_USER_WIDTH;
	int y1 = 0;
	int y2 = GM12U320_HEIGHT;

	if (!fb->obj->vmapping) {
		ret = gm12u320_gem_vmap(fb->obj);
		if (ret == -ENOMEM) {
			DRM_ERROR("failed to vmap fb\n");
			return;
		}
		if (!fb->obj->vmapping) {
			DRM_ERROR("failed to vmapping\n");
			return;
		}
	}

	spin_lock_irqsave(&gm12u320->frame_lock, flags);
	frame = !gm12u320->current_frame;
	spin_unlock_irqrestore(&gm12u320->frame_lock, flags);

	x1 += (GM12U320_REAL_WIDTH - GM12U320_USER_WIDTH) / 2;
	x2 += (GM12U320_REAL_WIDTH - GM12U320_USER_WIDTH) / 2;

	src = fb->obj->vmapping;
	for (; y1 < y2; y1++) {
		remain = 0;
		len = (x2 - x1) * 3;
		dst_offset = (y1 * GM12U320_REAL_WIDTH + x1) * 3;
		block = dst_offset / DATA_BLOCK_CONTENT_SIZE;
		dst_offset %= DATA_BLOCK_CONTENT_SIZE;

		if ((dst_offset + len) > DATA_BLOCK_CONTENT_SIZE) {
			remain = dst_offset + len - DATA_BLOCK_CONTENT_SIZE;
			len = DATA_BLOCK_CONTENT_SIZE - dst_offset;
		}

		dst_offset += DATA_BLOCK_HEADER_SIZE;
		len /= 3;

		gm12u320_32bpp_to_24bpp_packed(
			gm12u320->data_buf[frame][block] + dst_offset,
			src, len);

		if (remain) {
			block++;
			dst_offset = DATA_BLOCK_HEADER_SIZE;
			gm12u320_32bpp_to_24bpp_packed(
				gm12u320->data_buf[frame][block] + dst_offset,
				src + len * 4, remain / 3);
		}
		src += fb->base.pitches[0];
	}

	spin_lock_irqsave(&gm12u320->frame_lock, flags);
	gm12u320->next_frame = frame;
	spin_unlock_irqrestore(&gm12u320->frame_lock, flags);

	wake_up(&gm12u320->frame_waitq);
}

static int gm12u320_frame_ready(struct gm12u320_device *gm12u320)
{
	int ret;

	spin_lock(&gm12u320->frame_lock);
	ret = gm12u320->next_frame != gm12u320->current_frame;
	spin_unlock(&gm12u320->frame_lock);

	return ret;
}

static void gm12u320_frame_work(struct work_struct *work)
{
	struct gm12u320_device *gm12u320 =
		container_of(work, struct gm12u320_device, frame_work);
	int draw_status_timeout = FIRST_FRAME_TIMEOUT;
	int block, block_size, frame, len, ret;

	while (1) {
		/*
		 * We must draw a frame every 2s otherwise the projector
		 * switches back to showing its logo.
		 */
		wait_event_timeout(gm12u320->frame_waitq,
				   gm12u320_frame_ready(gm12u320),
				   IDLE_TIMEOUT);

		spin_lock(&gm12u320->frame_lock);
		frame = gm12u320->current_frame = gm12u320->next_frame;
		spin_unlock(&gm12u320->frame_lock);

		for (block = 0; block < GM12U320_BLOCK_COUNT; block++) {
			if (block == GM12U320_BLOCK_COUNT - 1)
				block_size = DATA_LAST_BLOCK_SIZE;
			else
				block_size = DATA_BLOCK_SIZE;

			/* Send data command to device */
			memcpy(gm12u320->cmd_buf, cmd_data, CMD_SIZE);
			gm12u320->cmd_buf[8] = block_size & 0xff;
			gm12u320->cmd_buf[9] = block_size >> 8;
			gm12u320->cmd_buf[20] = 0xfc - block * 4;
			gm12u320->cmd_buf[21] = block | (frame << 7);

			ret = usb_bulk_msg(gm12u320->udev,
				   usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
				   gm12u320->cmd_buf, CMD_SIZE, &len, CMD_TIMEOUT);
			if (ret || len != CMD_SIZE)
				goto err;

			/* Send data block to device */
			ret = usb_bulk_msg(gm12u320->udev,
				   usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
				   gm12u320->data_buf[frame][block], block_size,
				   &len, DATA_TIMEOUT);
			if (ret || len != block_size)
				goto err;

			/* Read status */
			ret = usb_bulk_msg(gm12u320->udev,
				   usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
				   gm12u320->cmd_buf, READ_BLOCK_SIZE, &len,
				   CMD_TIMEOUT);
			if (ret || len != READ_BLOCK_SIZE)
				goto err;
		}

		/* Send draw command to device */
		memcpy(gm12u320->cmd_buf, cmd_draw, CMD_SIZE);
		ret = usb_bulk_msg(gm12u320->udev,
				   usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
				   gm12u320->cmd_buf, CMD_SIZE, &len, CMD_TIMEOUT);
		if (ret || len != CMD_SIZE)
			goto err;

		/* Read status */
		ret = usb_bulk_msg(gm12u320->udev,
			   usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
			   gm12u320->cmd_buf, READ_BLOCK_SIZE, &len,
			   draw_status_timeout);
		if (ret || len != READ_BLOCK_SIZE)
			goto err;

		draw_status_timeout = CMD_TIMEOUT;
	}
err:
	/* Do not log errors caused by module unload or device unplug */
	if (ret != -ENOENT && ret != -ECONNRESET && ret != -ESHUTDOWN &&
	    ret != -ENODEV)
		dev_err(&gm12u320->udev->dev, "Frame update error: %d\n", ret);
}

int gm12u320_driver_load(struct drm_device *dev, unsigned long flags)
{
	struct usb_device *udev = (void*)flags;
	struct gm12u320_device *gm12u320;
	int ret = -ENOMEM;

	DRM_DEBUG("\n");
	gm12u320 = kzalloc(sizeof(struct gm12u320_device), GFP_KERNEL);
	if (!gm12u320)
		return -ENOMEM;

	gm12u320->udev = udev;
	gm12u320->ddev = dev;
	dev->dev_private = gm12u320;

	INIT_WORK(&gm12u320->frame_work, gm12u320_frame_work);
	spin_lock_init(&gm12u320->frame_lock);
	init_waitqueue_head(&gm12u320->frame_waitq);

	/*
	 * These are deliberately different so that we send out an empty
	 * screen to replace the projector logo immediately.
	 */
	gm12u320->current_frame = 1;
	gm12u320->next_frame = 0;

	ret = gm12u320_usb_alloc(gm12u320);
	if (ret)
		goto err;

	gm12u320->frame_workq = create_singlethread_workqueue(DRIVER_NAME);
	if (!gm12u320->frame_workq) {
		ret = -ENOMEM;
		goto err;
	}

	DRM_DEBUG("\n");
	ret = gm12u320_modeset_init(dev);
	if (ret)
		goto err;

	ret = gm12u320_fbdev_init(dev);
	if (ret)
		goto err;

	ret = drm_vblank_init(dev, 1);
	if (ret)
		goto err_fb;

	queue_work(gm12u320->frame_workq, &gm12u320->frame_work);

	return 0;
err_fb:
	gm12u320_fbdev_cleanup(dev);
err:
	gm12u320_usb_free(gm12u320);
	kfree(gm12u320);
	DRM_ERROR("%d\n", ret);
	return ret;
}

int gm12u320_driver_unload(struct drm_device *dev)
{
	struct gm12u320_device *gm12u320 = dev->dev_private;

	/* Wake up frame_work, so that it sees the disconnect and exits */
	wake_up(&gm12u320->frame_waitq);
	cancel_work_sync(&gm12u320->frame_work);
	destroy_workqueue(gm12u320->frame_workq);
	gm12u320_usb_free(gm12u320);

	drm_vblank_cleanup(dev);
	gm12u320_fbdev_cleanup(dev);
	gm12u320_modeset_cleanup(dev);
	kfree(gm12u320);
	return 0;
}