-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathz800.c
475 lines (388 loc) · 10.7 KB
/
z800.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* eMagin Z800 HMD USB driver
*
* The USB packet info came from the public release of the eMagin
* EMA SDK v2.2. Many thanks to eMagin for releasing that data.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Copyright (C) 2006 Adam King <[email protected]>
*
* v0.6 (ak) initial version
*/
#include "z800.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#include <linux/delay.h>
#define Z800_VENDOR_ID 0x1641
#define Z800_PRODUCT_ID 0x0120
#define X800_VENDOR_ID 0x1642
#define X800_PRODUCT_ID 0x0120
const u8 BYTEVERB_PEEK_EEPROM = 0x81;
const u8 BYTEVERB_SET_POKE_ADDR = 0x82;
const u8 BYTEVERB_POKE_EEPROM = 0x83;
const u8 BYTEVERB_RESET = 0x84;
const u8 BYTEVERB_SLEEP = 0x85;
const u8 BYTEVERB_PEEK_OLED_0 = 0x86;
const u8 BYTEVERB_PEEK_OLED_1 = 0x87;
const u8 BYTEVERB_STEP_BRIGHTNESS = 0x88;
const u8 BYTEVERB_QUERY_STATE = 0x89;
const u8 BYTEVERB_WAKE = 0x8A;
const u8 BYTEVERB_3DMODE = 0x8B;
const u8 BYTEVERB_3DFLIP = 0x8C;
static struct usb_device_id z800_table [] = {
{ USB_DEVICE(Z800_VENDOR_ID, Z800_PRODUCT_ID) },
{ USB_DEVICE(X800_VENDOR_ID, X800_PRODUCT_ID) },
{ }
};
MODULE_DEVICE_TABLE(usb, z800_table);
static struct usb_driver z800_driver;
struct usb_z800 {
struct usb_device * udev;
struct usb_interface *interface;
struct urb *read_urb;
struct urb *write_urb;
int read_urb_running;
int wait_on_write;
u8 eeprom_byte;
char *write_buffer;
char *read_buffer;
};
static int getFirmware( struct usb_z800 *dev, char *buf );
static int z800_usb_write( struct usb_z800 *dev, u8 verb, u8 noun );
static int readBuffer( struct usb_z800 *dev );
static void z800_abort_transfers(struct usb_z800 *dev)
{
info( "Aborting transfer..." );
/* if a write is currently occuring, wait
till it's done
*/
while( dev->wait_on_write )
msleep(10);
if( dev->read_urb_running) {
dev->read_urb_running = 0;
usb_kill_urb(dev->read_urb);
}
}
/*
* File ops
*/
static int z800_open(struct inode *inode, struct file *file)
{
struct usb_z800 *dev;
struct usb_interface *interface;
int subminor;
int retval = 0;
subminor = iminor(inode);
interface = usb_find_interface(&z800_driver, subminor);
if (!interface) {
err ("Z800: %s - error, can't find device for minor %d",
__FUNCTION__, subminor);
retval = -ENODEV;
goto exit;
}
dev = usb_get_intfdata(interface);
if (!dev) {
retval = -ENODEV;
goto exit;
}
/* create a write and read urb */
dev->write_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!dev->write_urb) {
retval = -ENOMEM;
goto exit;
}
dev->read_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!dev->read_urb) {
retval = -ENOMEM;
goto exit;
}
dev->write_buffer = kmalloc( 64, GFP_KERNEL );
dev->read_buffer = kmalloc( 64, GFP_KERNEL );
dev->read_urb_running = 0;
dev->wait_on_write = 0;
dev->eeprom_byte = 0;
/* save our object in the file's private structure */
file->private_data = dev;
readBuffer(dev);
info("============ Z800 Opened =============");
exit:
return retval;
}
static int z800_release(struct inode *inode, struct file *file)
{
struct usb_z800 *dev;
dev = (struct usb_z800 *)file->private_data;
if (dev == NULL)
return -ENODEV;
/* shutdown any running urbs */
z800_abort_transfers(dev);
usb_free_urb(dev->write_urb);
usb_free_urb(dev->read_urb);
kfree(dev->write_buffer);
kfree(dev->read_buffer);
info("=========== Z800 Released ===========");
return 0;
}
static int z800_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
char buf[8];
struct usb_z800 *dev;
dev = (struct usb_z800 *)file->private_data;
if( dev == NULL )
return -ENODEV;
memset(buf, 0, 8);
switch(cmd) {
case IOCTL_GET_FIRMWARE_VERSION:
info( "z800_ioctl: ---- doing firmware... ------" );
if( getFirmware(dev, buf) != 0 )
return -EFAULT;
if( copy_to_user((void __user *)arg, buf, 2) !=0 )
return -EFAULT;
break;
case IOCTL_SLEEP:
info( "z800_ioctl: ---- doing sleep... ------" );
z800_usb_write( dev, BYTEVERB_SLEEP, 0 );
break;
case IOCTL_KEEPALIVE:
info( "z800_ioctl: ---- doing wake... -------" );
z800_usb_write( dev, BYTEVERB_WAKE, 0 );
break;
case IOCTL_SET_ENABLE_3D:
info( "z800_ioctl: ---- doing enable3d[%x] ---", (arg?1:0));
z800_usb_write( dev, BYTEVERB_3DMODE, (u8)(arg?1:0) );
break;
case IOCTL_CYCLEBRIGHTNESS:
info( "z800_ioctl: ---- doing stepbright... -------" );
z800_usb_write( dev, BYTEVERB_STEP_BRIGHTNESS, 0 );
break;
default:
info( "z800: UNKNOWN ioctl" );
return -ENOTTY;
}
return 0;
}
static void write_callback( struct urb *urb, struct pt_regs *regs )
{
struct usb_z800 *dev = urb->context;
/* info( "write_callback: status = %d\n", urb->status ); */
if (urb->status)
info( "write-callback ERROR status: %d %d",
urb->status, urb->actual_length );
dev->wait_on_write = 0;
}
/* FIXME: we shouldn't constantly resubmit the in urb - but only
when we're expecting an urb from the z800.
*/
static void read_callback( struct urb *urb, struct pt_regs *regs )
{
int retval;
struct usb_z800 *dev = urb->context;
retval = 0;
if( urb->status ) {
if( urb->status == -ENOENT ||
urb->status == -ECONNRESET ||
urb->status == -ESHUTDOWN ) {
switch(urb->status)
{
case -ENOENT:
info( "read_callback: HELP - ENOENT!" );
return;
case -ECONNRESET:
info( "read_callback: HELP - ECONNRESET!" );
return;
case -ESHUTDOWN:
info( "read_callback: HELP - ESHUTDOWN!" );
return;
}
return;
}
}
if( urb->actual_length > 0 ) {
/* info( "read_callback:data %d [%x][%x][%x][%x][%x][%x]",
urb->actual_length,
(u8)dev->read_buffer[0],
(u8)dev->read_buffer[1],
(u8)dev->read_buffer[2],
(u8)dev->read_buffer[3],
(u8)dev->read_buffer[4],
(u8)dev->read_buffer[5]
);
*/ /* we're only interested in BYTEVERB_PEEK_EEPROM */
if( (u8)dev->read_buffer[2] == BYTEVERB_PEEK_EEPROM ) {
dev->eeprom_byte = (u8)dev->read_buffer[4];
}
}
if(dev->read_urb_running) {
retval = usb_submit_urb( dev->read_urb, GFP_ATOMIC);
if( retval )
info( "read_callback: failed to resubmit (%d)", retval );
}
}
static int getFirmware( struct usb_z800 *dev, char *buf )
{
int waitCount;
memset( buf, 0, 8 );
/* get the MSB of the firmware version */
waitCount = 0;
z800_usb_write( dev, BYTEVERB_PEEK_EEPROM, 0 );
/* FIXME: should replace with a read wait_event */
while( dev->eeprom_byte == 0 )
{
msleep(50);
waitCount++;
/* only wait for 1s */
if( waitCount > 20 ) {
info( "getFirmware: Unable to get MSB. Aborting..." );
return -1;
}
}
buf[0] = dev->eeprom_byte;
dev->eeprom_byte = 0;
/* now the lsb */
waitCount = 0;
z800_usb_write( dev, BYTEVERB_PEEK_EEPROM, 1 );
/* FIXME: should replace with a read wait_event */
while( dev->eeprom_byte == 0 )
{
msleep(50);
waitCount++;
/* only wait for 1s */
if( waitCount > 20 ) {
info( "getFirmware: Unable to get LSB. Aborting..." );
return -1;
}
}
buf[1] = dev->eeprom_byte;
dev->eeprom_byte = 0;
info( "getFirmware: [%x.%x]", (u8)buf[0], (u8)buf[1] );
return 0;
}
static int z800_usb_write( struct usb_z800 *dev, u8 verb, u8 noun )
{
int retval = 0;
/* if another write is already in progress,
wait until it's sent
FIXME: should replace with a wait_event */
while(dev->wait_on_write) msleep(1);
dev->wait_on_write = 1;
memset(dev->write_buffer, 0, 64);
dev->write_buffer[0] = verb;
dev->write_buffer[1] = noun;
/* info("writeBuffer: sending urb. verb = %x noun = %x", verb, noun); */
usb_fill_int_urb( dev->write_urb, dev->udev,
usb_sndintpipe( dev->udev, 0x02),
dev->write_buffer,
35, // why 35?? I dunno - it's what eMagin uses.
(usb_complete_t)write_callback,
dev, 8 );
retval = usb_submit_urb( dev->write_urb, GFP_KERNEL );
return retval;
}
static int readBuffer( struct usb_z800 *dev )
{
int retval = 0;
memset(dev->read_buffer, 0, 64);
/*info("readBuffer: filling and submitting urb"); */
usb_fill_int_urb( dev->read_urb, dev->udev,
usb_rcvintpipe( dev->udev, 0x81),
dev->read_buffer,
64,
(usb_complete_t)read_callback,
dev, 8 );
retval = usb_submit_urb( dev->read_urb, GFP_KERNEL );
dev->read_urb_running = 1;
return retval;
}
static struct file_operations z800_fops = {
.owner = THIS_MODULE,
.ioctl = z800_ioctl,
.open = z800_open,
.release = z800_release,
};
/*
* USB Ops
*/
static struct usb_class_driver z800_class = {
.name = "z800:%d",
.fops = &z800_fops,
.minor_base = 155,
};
static int z800_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
int retval;
struct usb_z800 *dev;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (dev == NULL) {
err("Out of memory");
return -ENOMEM;
}
dev->udev = usb_get_dev(interface_to_usbdev(interface));
dev->interface = interface;
usb_set_intfdata(interface, dev);
retval = usb_register_dev(interface, &z800_class);
if (retval) {
err("Not able to get a minor for this device.");
usb_set_intfdata(interface, NULL);
return retval;
}
info("Z800 now registered");
return 0;
}
static void z800_disconnect(struct usb_interface *interface)
{
struct usb_z800 *dev;
int minor = interface->minor;
/* prevent z800_open() from racing z800_disconnect() */
/* FIXME: don't use the kernel lock */
lock_kernel();
dev = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
/* give back our minor */
usb_deregister_dev(interface, &z800_class);
dev->interface = NULL;
/* if the device is not opened, then we clean up right now */
/* FIXME: MEMORY LEAK if device is open when unplugged */
/* if (!dev->open_count) {
kfree(dev);
}
*/
unlock_kernel();
info("Z800 #%d now disconnected", minor);
}
static struct usb_driver z800_driver = {
.name = "z800",
.id_table = z800_table,
.probe = z800_probe,
.disconnect = z800_disconnect,
};
static int __init z800_init(void)
{
int retval = 0;
printk( KERN_ALERT "Registering Z800 Device \n" );
retval = usb_register(&z800_driver);
if (retval)
err("usb_register failed. Error number %d", retval);
return retval;
}
static void __exit z800_exit(void)
{
printk( KERN_ALERT "z800 exiting...\n" );
usb_deregister(&z800_driver);
}
module_init(z800_init);
module_exit(z800_exit);
MODULE_LICENSE ("GPL");
MODULE_AUTHOR ("Adam King <[email protected]>");
MODULE_VERSION ("1:0.8");
MODULE_DESCRIPTION("USB Z800 EMagin Driver");