-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathamba_usb.c
488 lines (410 loc) · 13 KB
/
amba_usb.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
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* amba264 USB driver - 1.0
*
* This file is licensed under the GPL. See COPYING in the package.
* Based on usb-skeleton.c 2.0 by Greg Kroah-Hartman ([email protected])
*
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#define DRIVER_AUTHOR "Lechee.Lai"
#define DRIVER_DESC "Quanta AmbaA2 USB Driver"
/* Define these values to match your devices */
#define USB_AMBA_VENDOR_ID 0x4255
#define USB_AMBA_PRODUCT_ID 0x0003
/* table of devices that work with this driver */
static struct usb_device_id amba_table [] = {
{ USB_DEVICE(USB_AMBA_VENDOR_ID, USB_AMBA_PRODUCT_ID) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, amba_table);
/* to prevent a race between open and disconnect */
static DEFINE_MUTEX(amba_open_lock);
/* Get a minor range for your devices from the usb maintainer */
#define USB_AMBA_MINOR_BASE 192
/* our private defines. if this grows any larger, use your own .h file */
#define MAX_TRANSFER (PAGE_SIZE - 512)
/* MAX_TRANSFER is chosen so that the VM is not stressed by
allocations > PAGE_SIZE and the number of packets in a page
is an integer 512 is the largest possible packet on EHCI */
#define WRITES_IN_FLIGHT 8
/* arbitrarily chosen */
/* Structure to hold all of our device specific stuff */
struct usb_amba {
struct usb_device *udev; /* the usb device for this device */
struct usb_interface *interface; /* the interface for this device */
struct semaphore limit_sem; /* limiting the number of writes in progress */
unsigned char *bulk_in_buffer; /* the buffer to receive data */
size_t bulk_in_size; /* the size of the receive buffer */
__u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
__u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
struct kref kref;
struct mutex io_mutex; /* synchronize I/O with disconnect */
};
typedef struct _encode_size {
unsigned int h264_size;
unsigned int mjpeg_size;
} encode_size;
#define to_amba_dev(d) container_of(d, struct usb_amba, kref)
static struct usb_driver amba_driver;
int amba_SND(struct usb_amba *ambaA2,
__u8 request, __u8 requesttype,
__u16 value, __u16 index, __u8 len)
{
char *dummy_buffer = kzalloc(4, GFP_KERNEL);
int result;
if (!dummy_buffer)
return -ENOMEM;
result = usb_control_msg(ambaA2->udev,
usb_sndctrlpipe(ambaA2->udev, 0),
request, requesttype, value, index,
dummy_buffer, len, 1000);
kfree(dummy_buffer);
return result;
}
static inline void amba_RCV(struct usb_amba *ambaA2,
__u8 request, __u8 requesttype,
__u16 value, __u16 index,
char *buf, __u8 len)
{
int result;
result = usb_control_msg(ambaA2->udev,
usb_rcvctrlpipe(ambaA2->udev, 0),
request, requesttype, value, index,
buf, len, 1000);
}
int cmd_set_encode_format(struct usb_amba *ambaA2, unsigned int encode_format)
{
unsigned char buf[0x10];
buf[0]=encode_format;
int ret;
ret = usb_control_msg(ambaA2->udev,
usb_sndctrlpipe(ambaA2->udev, 0),
0x01, 0x21, 0x0b00, 0xf000,
(unsigned char *)&encode_format, sizeof(encode_format),
1000);
if (ret != 4) {
info("AMBA(E)::cmd_set_encode_format()");
}
return ret;
}
int cmd_set_encode_size(struct usb_amba *ambaA2, unsigned int h264_size, unsigned int mjpeg_size)
{
struct _encode_size es;
int ret;
es.h264_size = h264_size;
es.mjpeg_size = mjpeg_size;
ret = usb_control_msg(ambaA2->udev,
usb_sndctrlpipe(ambaA2->udev, 0),
0x01, 0x21, 0x1500, 0xf000,
(unsigned char *)&es, sizeof(encode_size),
1000);
if (ret != sizeof(encode_size)) {
info("AMBA(E)cmd_set_encode_size()");
}
return ret;
}
void cmd_set_append_header(struct usb_amba *ambaA2, unsigned int value)
{
unsigned char buf[4];
int ret;
buf[0] = value & 0x0ff;
buf[1] = (value >> 8) & 0x0ff;
buf[2] = (value >> 16) & 0x0ff;
buf[3] = (value >> 24) & 0x0ff;
ret = usb_control_msg(ambaA2->udev,
usb_sndctrlpipe(ambaA2->udev, 0),
0x01, 0x21, 0x3900, 0xf000,
buf, sizeof(buf),
1000);
if (ret != 4) {
info("AMBA(E)::cmd_set_append_header()");
}
}
void cmd_set_h264_bitrate(struct usb_amba *ambaA2, unsigned int value)
{
char buf[4];
int ret = 0;
buf[0] = value & 0x0ff;
buf[1] = (value >> 8) & 0x0ff;
buf[2] = (value >> 16) & 0x0ff;
buf[3] = (value >> 24) & 0x0ff;
ret = usb_control_msg(ambaA2->udev,
usb_sndctrlpipe(ambaA2->udev, 0),
0x01, 0x21, 0x1000, 0xf000,
buf, sizeof(buf),
1000);
if (ret != 4) {
info("(AMBA(E)::cmd_set_h264_bitrate()");
} else {
}
}
static void amba_delete(struct kref *kref)
{
struct usb_amba *dev = to_amba_dev(kref);
usb_put_dev(dev->udev);
kfree(dev->bulk_in_buffer);
kfree(dev);
}
static int amba_open(struct inode *inode, struct file *file)
{
struct usb_amba *dev;
struct usb_interface *interface;
int subminor;
int retval = 0;
subminor = iminor(inode);
// info("AMBA(i):: Amba open");
mutex_lock(&amba_open_lock);
interface = usb_find_interface(&amba_driver, subminor);
if (!interface) {
mutex_unlock(&amba_open_lock);
err ("%s - error, can't find device for minor %d",
__FUNCTION__, subminor);
retval = -ENODEV;
goto exit;
}
dev = usb_get_intfdata(interface);
if (!dev) {
mutex_unlock(&amba_open_lock);
retval = -ENODEV;
goto exit;
}
/* increment our usage count for the device */
kref_get(&dev->kref);
/* now we can drop the lock */
mutex_unlock(&amba_open_lock);
if (retval) {
kref_put(&dev->kref, amba_delete);
goto exit;
}
/* save our object in the file's private structure */
file->private_data = dev;
exit:
return retval;
}
static int amba_release(struct inode *inode, struct file *file)
{
struct usb_amba *dev;
dev = (struct usb_amba *)file->private_data;
if (dev == NULL)
return -ENODEV;
/* allow the device to be autosuspended */
mutex_lock(&dev->io_mutex);
mutex_unlock(&dev->io_mutex);
/* decrement the count on our device */
kref_put(&dev->kref, amba_delete);
return 0;
}
static ssize_t amba_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
{
struct usb_amba *dev;
int retval;
int bytes_read;
char *AMBA_buf;
AMBA_buf = kzalloc(128*1024, GFP_KERNEL);
if (!AMBA_buf) {
err("Out of memory 128K");
return -ENOMEM;
}
dev = (struct usb_amba *)file->private_data;
mutex_lock(&dev->io_mutex);
if (!dev->interface) { /* disconnect() was called */
retval = -ENODEV;
goto exit;
}
/* start Encode */
// amba_SND(dev,0x01,0x21,0x0700,0xf000,4);
// info("AMBA(i):: Amba bulk read");
/* do a blocking bulk read to get data from the device */
dev->bulk_in_endpointAddr = 0x81;
dev->bulk_in_size = 128*1024;
retval = usb_bulk_msg(dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
AMBA_buf,
min(dev->bulk_in_size, count),
&bytes_read, 5000);
/* if the read was successful, copy the data to userspace */
if (!retval) {
if (copy_to_user(buffer, AMBA_buf, bytes_read))
retval = -EFAULT;
else
retval = bytes_read;
}
/* Stop Encode */
// amba_SND(dev,0x01,0x21,0x0800,0xf000,4);
exit:
kfree(AMBA_buf);
mutex_unlock(&dev->io_mutex);
return retval;
}
/**
* amba_ioctl
*/
static int amba_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
struct usb_amba *dev;
dev = (struct usb_amba *)file->private_data;
switch (cmd) {
case 0:
amba_SND(dev,0x01,0x21,0x0700,0xf000,4);
info("AMBA(I)::ioctl 0 START encode");
break;
case 1:
amba_SND(dev,0x01,0x21,0x0800,0xf000,4);
info("AMBA(I)::ioctl 1 STOP encode");
break;
default:
return -1;
}
return 0;
}
static const struct file_operations amba_fops = {
.owner = THIS_MODULE,
.read = amba_read,
.open = amba_open,
.ioctl = amba_ioctl,
.release = amba_release,
};
/*
* usb class driver info in order to get a minor number from the usb core,
* and to have the device registered with the driver core
*/
static struct usb_class_driver amba_class = {
.name = "ambaA2",
.fops = &amba_fops,
.minor_base = USB_AMBA_MINOR_BASE,
};
/*
* Initialize device controls.
*/
int amba_init_device(struct usb_amba *dev)
{
char *buf = kzalloc(4, GFP_KERNEL);
int retval;
if (!buf)
return -ENOMEM;
info("AMBA(i):: Amba INIT USB device");
/* GetChipVersion */
amba_RCV(dev,0x81,0xA1,0x0300,0xf000,buf,4);
/* GetFirmware */
amba_RCV(dev,0x81,0xA1,0x0400,0xf000,buf,12);
/* SetEncodeFormat */
/* set encode format 0:H264 1:MJPEG 2:H264+MJPEG*/
cmd_set_encode_format(dev,0);
/* SetAppendHeader */
cmd_set_append_header(dev,1);
/* SelEncodeSize */
/* set encode size H264 2:1280x720 3:640x480 10:253x288 MJPEG 0:640x480 5: 253x288 */
cmd_set_encode_size(dev, 2, 5);
/* SetBitRate */
/* set H264 bitrate 1000Kbps */
cmd_set_h264_bitrate(dev, 1000);
/* SetFrameRate */
retval=amba_SND(dev,0x01,0x21,0x0c00,0xf000,4);
/* SetVinDevice */
retval=amba_SND(dev,0x01,0x21,0x1600,0xf000,4);
/* SetVoutDevice */
retval=amba_SND(dev,0x01,0x21,0x1900,0xf000,4);
/* BootDSP */
retval=amba_SND(dev,0x01,0x21,0x0600,0xf000,0);
/* SetPara */
retval=amba_SND(dev,0x01,0x21,0x3100,0xf000,20);
kfree(buf);
return 0;
}
static int amba_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_amba *dev;
int retval = -ENOMEM;
if (interface->num_altsetting == 1) {
info("AMBA(i)::Skip this device");
return -1;
}
/* allocate memory for our device state and initialize it */
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev) {
err("Out of memory");
goto error;
}
kref_init(&dev->kref);
sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
mutex_init(&dev->io_mutex);
dev->udev = usb_get_dev(interface_to_usbdev(interface));
dev->interface = interface;
usb_set_interface(dev->udev,interface->altsetting->desc.bInterfaceNumber,1);
/* save our data pointer in this interface device */
usb_set_intfdata(interface, dev);
/* Initialize controls */
if (amba_init_device(dev) < 0) {
err("Cound not init amba device");
goto error;
}
/* we can register the device now, as it is ready */
retval = usb_register_dev(interface, &amba_class);
if (retval) {
/* something prevented us from registering this driver */
err("Not able to get a minor for this device.");
usb_set_intfdata(interface, NULL);
goto error;
}
/* let the user know what node this device is now attached to */
info("ambaUSB device now attached to ambaUSB-%d", interface->minor);
return 0;
error:
if (dev)
/* this frees allocated memory */
kref_put(&dev->kref, amba_delete);
return retval;
}
static void amba_disconnect(struct usb_interface *interface)
{
struct usb_amba *dev;
int minor = interface->minor;
/* prevent amba_open() from racing amba_disconnect() */
mutex_lock(&amba_open_lock);
dev = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
/* give back our minor */
usb_deregister_dev(interface, &amba_class);
mutex_unlock(&amba_open_lock);
/* prevent more I/O from starting */
mutex_lock(&dev->io_mutex);
dev->interface = NULL;
mutex_unlock(&dev->io_mutex);
/* decrement our usage count */
kref_put(&dev->kref, amba_delete);
info("ambaUSB #%d now disconnected", minor);
}
static struct usb_driver amba_driver = {
.name = "AMBA264",
.probe = amba_probe,
.disconnect = amba_disconnect,
.id_table = amba_table,
// .supports_autosuspend = 1,
};
static int __init usb_amba_init(void)
{
int result;
/* register this driver with the USB subsystem */
result = usb_register(&amba_driver);
if (result)
err("usb_register failed. Error number %d", result);
return result;
}
static void __exit usb_amba_exit(void)
{
/* deregister this driver with the USB subsystem */
usb_deregister(&amba_driver);
}
module_init(usb_amba_init);
module_exit(usb_amba_exit);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");