1/*
2 * cpia_usb CPiA USB driver
3 *
4 * Supports CPiA based parallel port Video Camera's.
5 *
6 * Copyright (C) 1999        Jochen Scharrlach <Jochen.Scharrlach@schwaben.de>
7 * Copyright (C) 1999, 2000  Johannes Erdfelt <johannes@erdfelt.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/wait.h>
28#include <linux/sched.h>
29#include <linux/list.h>
30#include <linux/slab.h>
31#include <linux/vmalloc.h>
32#include <linux/usb.h>
33
34#include "cpia.h"
35
36#define USB_REQ_CPIA_GRAB_FRAME			0xC1
37#define USB_REQ_CPIA_UPLOAD_FRAME		0xC2
38#define  WAIT_FOR_NEXT_FRAME			0
39#define  FORCE_FRAME_UPLOAD			1
40
41#define FRAMES_PER_DESC		10
42#define FRAME_SIZE_PER_DESC	960	/* Shouldn't be hardcoded */
43#define CPIA_NUMSBUF		2
44#define STREAM_BUF_SIZE		(PAGE_SIZE * 4)
45#define SCRATCH_BUF_SIZE	(STREAM_BUF_SIZE * 2)
46
47struct cpia_sbuf {
48	char *data;
49	struct urb *urb;
50};
51
52#define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)
53enum framebuf_status {
54	FRAME_EMPTY,
55	FRAME_READING,
56	FRAME_READY,
57	FRAME_ERROR,
58};
59
60struct framebuf {
61	int length;
62	enum framebuf_status status;
63	u8 data[FRAMEBUF_LEN];
64	struct framebuf *next;
65};
66
67struct usb_cpia {
68	/* Device structure */
69	struct usb_device *dev;
70
71	unsigned char iface;
72	wait_queue_head_t wq_stream;
73
74	int cursbuf;		/* Current receiving sbuf */
75	struct cpia_sbuf sbuf[CPIA_NUMSBUF];		/* Double buffering */
76
77	int streaming;
78	int open;
79	int present;
80	struct framebuf *buffers[3];
81	struct framebuf *curbuff, *workbuff;
82};
83
84static int cpia_usb_open(void *privdata);
85static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
86			             void *cbdata);
87static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data);
88static int cpia_usb_streamStart(void *privdata);
89static int cpia_usb_streamStop(void *privdata);
90static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock);
91static int cpia_usb_close(void *privdata);
92
93#define ABOUT "USB driver for Vision CPiA based cameras"
94
95static struct cpia_camera_ops cpia_usb_ops = {
96	cpia_usb_open,
97	cpia_usb_registerCallback,
98	cpia_usb_transferCmd,
99	cpia_usb_streamStart,
100	cpia_usb_streamStop,
101	cpia_usb_streamRead,
102	cpia_usb_close,
103	0
104};
105
106static struct cam_data *cam_list;
107static spinlock_t cam_list_lock_usb;
108
109static void cpia_usb_complete(struct urb *urb)
110{
111	int i;
112	char *cdata;
113	struct usb_cpia *ucpia;
114
115	if (!urb || !urb->context)
116		return;
117
118	ucpia = (struct usb_cpia *) urb->context;
119
120	if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)
121		return;
122
123	if (ucpia->workbuff->status == FRAME_EMPTY) {
124		ucpia->workbuff->status = FRAME_READING;
125		ucpia->workbuff->length = 0;
126	}
127
128	for (i = 0; i < urb->number_of_packets; i++) {
129		int n = urb->iso_frame_desc[i].actual_length;
130		int st = urb->iso_frame_desc[i].status;
131
132		cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
133
134		if (st)
135			printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n", i, n, st);
136
137		if (FRAMEBUF_LEN < ucpia->workbuff->length + n) {
138			printk(KERN_DEBUG "cpia: scratch buf overflow!scr_len: %d, n: %d\n", ucpia->workbuff->length, n);
139			return;
140		}
141
142		if (n) {
143			if ((ucpia->workbuff->length > 0) ||
144			    (0x19 == cdata[0] && 0x68 == cdata[1])) {
145				memcpy(ucpia->workbuff->data + ucpia->workbuff->length, cdata, n);
146				ucpia->workbuff->length += n;
147			} else
148				DBG("Ignoring packet!\n");
149		} else {
150			if (ucpia->workbuff->length > 4 &&
151			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-1] &&
152			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-2] &&
153			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-3] &&
154			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-4]) {
155				ucpia->workbuff->status = FRAME_READY;
156				ucpia->curbuff = ucpia->workbuff;
157				ucpia->workbuff = ucpia->workbuff->next;
158				ucpia->workbuff->status = FRAME_EMPTY;
159				ucpia->workbuff->length = 0;
160
161				if (waitqueue_active(&ucpia->wq_stream))
162					wake_up_interruptible(&ucpia->wq_stream);
163			}
164		}
165	}
166}
167
168static int cpia_usb_open(void *privdata)
169{
170	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
171	struct urb *urb;
172	int ret, retval = 0, fx, err;
173
174	if (!ucpia)
175		return -EINVAL;
176
177	ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
178	if (!ucpia->sbuf[0].data)
179		return -EINVAL;
180
181	ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * FRAME_SIZE_PER_DESC, GFP_KERNEL);
182	if (!ucpia->sbuf[1].data) {
183		retval = -EINVAL;
184		goto error_0;
185	}
186
187	ret = usb_set_interface(ucpia->dev, ucpia->iface, 3);
188	if (ret < 0) {
189		printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)\n", ret);
190		retval = -EBUSY;
191		goto error_1;
192	}
193
194	ucpia->buffers[0]->status = FRAME_EMPTY;
195	ucpia->buffers[0]->length = 0;
196	ucpia->buffers[1]->status = FRAME_EMPTY;
197	ucpia->buffers[1]->length = 0;
198	ucpia->buffers[2]->status = FRAME_EMPTY;
199	ucpia->buffers[2]->length = 0;
200	ucpia->curbuff = ucpia->buffers[0];
201	ucpia->workbuff = ucpia->buffers[1];
202
203	/* We double buffer the Iso lists */
204	urb = usb_alloc_urb(FRAMES_PER_DESC);
205	if (!urb) {
206		printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");
207		retval = -ENOMEM;
208		goto error_1;
209	}
210
211	ucpia->sbuf[0].urb = urb;
212	urb->dev = ucpia->dev;
213	urb->context = ucpia;
214	urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
215	urb->transfer_flags = USB_ISO_ASAP;
216	urb->transfer_buffer = ucpia->sbuf[0].data;
217	urb->complete = cpia_usb_complete;
218	urb->number_of_packets = FRAMES_PER_DESC;
219	urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
220	for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
221		urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
222		urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
223	}
224
225	urb = usb_alloc_urb(FRAMES_PER_DESC);
226	if (!urb) {
227		printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 1\n");
228		retval = -ENOMEM;
229		goto error_urb0;
230	}
231
232	ucpia->sbuf[1].urb = urb;
233	urb->dev = ucpia->dev;
234	urb->context = ucpia;
235	urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);
236	urb->transfer_flags = USB_ISO_ASAP;
237	urb->transfer_buffer = ucpia->sbuf[1].data;
238	urb->complete = cpia_usb_complete;
239	urb->number_of_packets = FRAMES_PER_DESC;
240	urb->transfer_buffer_length = FRAME_SIZE_PER_DESC * FRAMES_PER_DESC;
241	for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
242		urb->iso_frame_desc[fx].offset = FRAME_SIZE_PER_DESC * fx;
243		urb->iso_frame_desc[fx].length = FRAME_SIZE_PER_DESC;
244	}
245
246	ucpia->sbuf[1].urb->next = ucpia->sbuf[0].urb;
247	ucpia->sbuf[0].urb->next = ucpia->sbuf[1].urb;
248
249	err = usb_submit_urb(ucpia->sbuf[0].urb);
250	if (err) {
251		printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d\n",
252			err);
253		goto error_urb1;
254	}
255	err = usb_submit_urb(ucpia->sbuf[1].urb);
256	if (err) {
257		printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d\n",
258			err);
259		goto error_urb1;
260	}
261
262	ucpia->streaming = 1;
263	ucpia->open = 1;
264
265	return 0;
266
267error_urb1:		/* free urb 1 */
268	usb_free_urb(ucpia->sbuf[1].urb);
269	ucpia->sbuf[1].urb = NULL;
270error_urb0:		/* free urb 0 */
271	usb_free_urb(ucpia->sbuf[0].urb);
272	ucpia->sbuf[0].urb = NULL;
273error_1:
274	kfree (ucpia->sbuf[1].data);
275	ucpia->sbuf[1].data = NULL;
276error_0:
277	kfree (ucpia->sbuf[0].data);
278	ucpia->sbuf[0].data = NULL;
279
280	return retval;
281}
282
283//
284// convenience functions
285//
286
287/****************************************************************************
288 *
289 *  WritePacket
290 *
291 ***************************************************************************/
292static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size)
293{
294	if (!packet)
295		return -EINVAL;
296
297	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
298			 packet[1] + (packet[0] << 8),
299			 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
300			 packet[2] + (packet[3] << 8),
301			 packet[4] + (packet[5] << 8), buf, size, HZ);
302}
303
304/****************************************************************************
305 *
306 *  ReadPacket
307 *
308 ***************************************************************************/
309static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size)
310{
311	if (!packet || size <= 0)
312		return -EINVAL;
313
314	return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
315			 packet[1] + (packet[0] << 8),
316			 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
317			 packet[2] + (packet[3] << 8),
318			 packet[4] + (packet[5] << 8), buf, size, HZ);
319}
320
321static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data)
322{
323	int err = 0;
324	int databytes;
325	struct usb_cpia *ucpia = (struct usb_cpia *)privdata;
326	struct usb_device *udev = ucpia->dev;
327
328	if (!udev) {
329		DBG("Internal driver error: udev is NULL\n");
330		return -EINVAL;
331	}
332
333	if (!command) {
334		DBG("Internal driver error: command is NULL\n");
335		return -EINVAL;
336	}
337
338	databytes = (((int)command[7])<<8) | command[6];
339
340	if (command[0] == DATA_IN) {
341		u8 buffer[8];
342
343		if (!data) {
344			DBG("Internal driver error: data is NULL\n");
345			return -EINVAL;
346		}
347
348		err = ReadPacket(udev, command, buffer, 8);
349		if (err < 0)
350			return err;
351
352		memcpy(data, buffer, databytes);
353	} else if(command[0] == DATA_OUT)
354		WritePacket(udev, command, data, databytes);
355	else {
356		DBG("Unexpected first byte of command: %x\n", command[0]);
357		err = -EINVAL;
358	}
359
360	return 0;
361}
362
363static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),
364	void *cbdata)
365{
366	return -ENODEV;
367}
368
369static int cpia_usb_streamStart(void *privdata)
370{
371	return -ENODEV;
372}
373
374static int cpia_usb_streamStop(void *privdata)
375{
376	return -ENODEV;
377}
378
379static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock)
380{
381	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
382	struct framebuf *mybuff;
383
384	if (!ucpia || !ucpia->present)
385		return -1;
386
387	if (ucpia->curbuff->status != FRAME_READY)
388		interruptible_sleep_on(&ucpia->wq_stream);
389	else
390		DBG("Frame already waiting!\n");
391
392	mybuff = ucpia->curbuff;
393
394	if (!mybuff)
395		return -1;
396
397	if (mybuff->status != FRAME_READY || mybuff->length < 4) {
398		DBG("Something went wrong!\n");
399		return -1;
400	}
401
402	memcpy(frame, mybuff->data, mybuff->length);
403	mybuff->status = FRAME_EMPTY;
404
405/*   DBG("read done, %d bytes, Header: %x/%x, Footer: %x%x%x%x\n",  */
406/*       mybuff->length, frame[0], frame[1], */
407/*       frame[mybuff->length-4], frame[mybuff->length-3],  */
408/*       frame[mybuff->length-2], frame[mybuff->length-1]); */
409
410	return mybuff->length;
411}
412
413static void cpia_usb_free_resources(struct usb_cpia *ucpia, int try)
414{
415	if (!ucpia->streaming)
416		return;
417
418	ucpia->streaming = 0;
419
420	/* Set packet size to 0 */
421	if (try) {
422		int ret;
423
424		ret = usb_set_interface(ucpia->dev, ucpia->iface, 0);
425		if (ret < 0) {
426			printk(KERN_ERR "usb_set_interface error (ret = %d)\n", ret);
427			return;
428		}
429	}
430
431	/* Unschedule all of the iso td's */
432	if (ucpia->sbuf[1].urb) {
433		usb_unlink_urb(ucpia->sbuf[1].urb);
434		usb_free_urb(ucpia->sbuf[1].urb);
435		ucpia->sbuf[1].urb = NULL;
436	}
437
438	if (ucpia->sbuf[1].data) {
439		kfree(ucpia->sbuf[1].data);
440		ucpia->sbuf[1].data = NULL;
441	}
442
443	if (ucpia->sbuf[0].urb) {
444		usb_unlink_urb(ucpia->sbuf[0].urb);
445		usb_free_urb(ucpia->sbuf[0].urb);
446		ucpia->sbuf[0].urb = NULL;
447	}
448
449	if (ucpia->sbuf[0].data) {
450		kfree(ucpia->sbuf[0].data);
451		ucpia->sbuf[0].data = NULL;
452	}
453}
454
455static int cpia_usb_close(void *privdata)
456{
457	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;
458
459	ucpia->open = 0;
460
461	cpia_usb_free_resources(ucpia, 1);
462
463	if (!ucpia->present)
464		kfree(ucpia);
465
466	return 0;
467}
468
469int cpia_usb_init(void)
470{
471	/* return -ENODEV; */
472	return 0;
473}
474
475/* Probing and initializing */
476
477static void *cpia_probe(struct usb_device *udev, unsigned int ifnum,
478			const struct usb_device_id *id)
479{
480	struct usb_interface_descriptor *interface;
481	struct usb_cpia *ucpia;
482	struct cam_data *cam;
483	int ret;
484
485	/* A multi-config CPiA camera? */
486	if (udev->descriptor.bNumConfigurations != 1)
487		return NULL;
488
489	interface = &udev->actconfig->interface[ifnum].altsetting[0];
490
491	printk(KERN_INFO "USB CPiA camera found\n");
492
493	ucpia = kmalloc(sizeof(*ucpia), GFP_KERNEL);
494	if (!ucpia) {
495		printk(KERN_ERR "couldn't kmalloc cpia struct\n");
496		return NULL;
497	}
498
499	memset(ucpia, 0, sizeof(*ucpia));
500
501	ucpia->dev = udev;
502	ucpia->iface = interface->bInterfaceNumber;
503	init_waitqueue_head(&ucpia->wq_stream);
504
505	ucpia->buffers[0] = vmalloc(sizeof(*ucpia->buffers[0]));
506	if (!ucpia->buffers[0]) {
507		printk(KERN_ERR "couldn't vmalloc frame buffer 0\n");
508		goto fail_alloc_0;
509	}
510
511	ucpia->buffers[1] = vmalloc(sizeof(*ucpia->buffers[1]));
512	if (!ucpia->buffers[1]) {
513		printk(KERN_ERR "couldn't vmalloc frame buffer 1\n");
514		goto fail_alloc_1;
515	}
516
517	ucpia->buffers[2] = vmalloc(sizeof(*ucpia->buffers[2]));
518	if (!ucpia->buffers[2]) {
519		printk(KERN_ERR "couldn't vmalloc frame buffer 2\n");
520		goto fail_alloc_2;
521	}
522
523	ucpia->buffers[0]->next = ucpia->buffers[1];
524	ucpia->buffers[1]->next = ucpia->buffers[2];
525	ucpia->buffers[2]->next = ucpia->buffers[0];
526
527	ret = usb_set_interface(udev, ucpia->iface, 0);
528	if (ret < 0) {
529		printk(KERN_ERR "cpia_probe: usb_set_interface error (ret = %d)\n", ret);
530		/* goto fail_all; */
531	}
532
533	/* Before register_camera, important */
534	ucpia->present = 1;
535
536	cam = cpia_register_camera(&cpia_usb_ops, ucpia);
537	if (!cam) {
538		LOG("failed to cpia_register_camera\n");
539		goto fail_all;
540	}
541
542	spin_lock( &cam_list_lock_usb );
543	cpia_add_to_list(cam_list, cam);
544	spin_unlock( &cam_list_lock_usb );
545
546	return cam;
547
548fail_all:
549	vfree(ucpia->buffers[2]);
550	ucpia->buffers[2] = NULL;
551fail_alloc_2:
552	vfree(ucpia->buffers[1]);
553	ucpia->buffers[1] = NULL;
554fail_alloc_1:
555	vfree(ucpia->buffers[0]);
556	ucpia->buffers[0] = NULL;
557fail_alloc_0:
558
559	return NULL;
560}
561
562static void cpia_disconnect(struct usb_device *dev, void *ptr);
563
564static struct usb_device_id cpia_id_table [] = {
565	{ USB_DEVICE(0x0553, 0x0002) },
566	{ USB_DEVICE(0x0813, 0x0001) },
567	{ }					/* Terminating entry */
568};
569
570MODULE_DEVICE_TABLE (usb, cpia_id_table);
571MODULE_LICENSE("GPL");
572
573
574static struct usb_driver cpia_driver = {
575	name:		"cpia",
576	probe:		cpia_probe,
577	disconnect:	cpia_disconnect,
578	id_table:	cpia_id_table,
579};
580
581/* don't use dev, it may be NULL! (see usb_cpia_cleanup) */
582/* _disconnect from usb_cpia_cleanup is not necessary since usb_deregister */
583/* will do it for us as well as passing a udev structure - jerdfelt */
584static void cpia_disconnect(struct usb_device *udev, void *ptr)
585{
586	struct cam_data *cam = (struct cam_data *) ptr;
587	struct usb_cpia *ucpia = (struct usb_cpia *) cam->lowlevel_data;
588
589	spin_lock( &cam_list_lock_usb );
590	cpia_remove_from_list(cam);
591	spin_unlock( &cam_list_lock_usb );
592
593	/* Don't even try to reset the altsetting if we're disconnected */
594	cpia_usb_free_resources(ucpia, 0);
595
596	ucpia->present = 0;
597
598	cpia_unregister_camera(cam);
599
600	ucpia->curbuff->status = FRAME_ERROR;
601
602	if (waitqueue_active(&ucpia->wq_stream))
603		wake_up_interruptible(&ucpia->wq_stream);
604
605	usb_driver_release_interface(&cpia_driver,
606				     &udev->actconfig->interface[0]);
607
608	ucpia->curbuff = ucpia->workbuff = NULL;
609
610	if (ucpia->buffers[2]) {
611		vfree(ucpia->buffers[2]);
612		ucpia->buffers[2] = NULL;
613	}
614
615	if (ucpia->buffers[1]) {
616		vfree(ucpia->buffers[1]);
617		ucpia->buffers[1] = NULL;
618	}
619
620	if (ucpia->buffers[0]) {
621		vfree(ucpia->buffers[0]);
622		ucpia->buffers[0] = NULL;
623	}
624
625	if (!ucpia->open) {
626		kfree(ucpia);
627		cam->lowlevel_data = NULL;
628	}
629}
630
631static int __init usb_cpia_init(void)
632{
633	cam_list = NULL;
634	spin_lock_init(&cam_list_lock_usb);
635	return usb_register(&cpia_driver);
636}
637
638static void __exit usb_cpia_cleanup(void)
639{
640/*
641	struct cam_data *cam;
642
643	while ((cam = cam_list) != NULL)
644		cpia_disconnect(NULL, cam);
645*/
646
647	usb_deregister(&cpia_driver);
648}
649
650
651module_init (usb_cpia_init);
652module_exit (usb_cpia_cleanup);
653
654