1 /*
2  * USB NB Camera driver
3  */
4 
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
7 #include <linux/wrapper.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 
11 #include "usbvideo.h"
12 
13 #define	ULTRACAM_VENDOR_ID	0x0461
14 #define	ULTRACAM_PRODUCT_ID	0x0813
15 
16 #define MAX_CAMERAS		4	/* How many devices we allow to connect */
17 
18 /*
19  * This structure lives in uvd->user field.
20  */
21 typedef struct {
22 	int initialized;	/* Had we already sent init sequence? */
23 	int camera_model;	/* What type of IBM camera we got? */
24         int has_hdr;
25 } ultracam_t;
26 #define	ULTRACAM_T(uvd)	((ultracam_t *)((uvd)->user_data))
27 
28 static struct usbvideo *cams = NULL;
29 
30 static int debug = 0;
31 
32 static int flags = 0; /* FLAGS_DISPLAY_HINTS | FLAGS_OVERLAY_STATS; */
33 
34 static const int min_canvasWidth  = 8;
35 static const int min_canvasHeight = 4;
36 
37 //static int lighting = 1; /* Medium */
38 
39 #define SHARPNESS_MIN	0
40 #define SHARPNESS_MAX	6
41 //static int sharpness = 4; /* Low noise, good details */
42 
43 #define FRAMERATE_MIN	0
44 #define FRAMERATE_MAX	6
45 static int framerate = -1;
46 
47 /*
48  * Here we define several initialization variables. They may
49  * be used to automatically set color, hue, brightness and
50  * contrast to desired values. This is particularly useful in
51  * case of webcams (which have no controls and no on-screen
52  * output) and also when a client V4L software is used that
53  * does not have some of those controls. In any case it's
54  * good to have startup values as options.
55  *
56  * These values are all in [0..255] range. This simplifies
57  * operation. Note that actual values of V4L variables may
58  * be scaled up (as much as << 8). User can see that only
59  * on overlay output, however, or through a V4L client.
60  */
61 static int init_brightness = 128;
62 static int init_contrast = 192;
63 static int init_color = 128;
64 static int init_hue = 128;
65 static int hue_correction = 128;
66 
67 MODULE_PARM(debug, "i");
68 MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
69 MODULE_PARM(flags, "i");
70 MODULE_PARM_DESC(flags,
71 		"Bitfield: 0=VIDIOCSYNC, "
72 		"1=B/W, "
73 		"2=show hints, "
74 		"3=show stats, "
75 		"4=test pattern, "
76 		"5=separate frames, "
77 		"6=clean frames");
78 MODULE_PARM(framerate, "i");
79 MODULE_PARM_DESC(framerate, "Framerate setting: 0=slowest, 6=fastest (default=2)");
80 MODULE_PARM(lighting, "i");
81 MODULE_PARM_DESC(lighting, "Photosensitivity: 0=bright, 1=medium (default), 2=low light");
82 MODULE_PARM(sharpness, "i");
83 MODULE_PARM_DESC(sharpness, "Model1 noise reduction: 0=smooth, 6=sharp (default=4)");
84 
85 MODULE_PARM(init_brightness, "i");
86 MODULE_PARM_DESC(init_brightness, "Brightness preconfiguration: 0-255 (default=128)");
87 MODULE_PARM(init_contrast, "i");
88 MODULE_PARM_DESC(init_contrast, "Contrast preconfiguration: 0-255 (default=192)");
89 MODULE_PARM(init_color, "i");
90 MODULE_PARM_DESC(init_color, "Color preconfiguration: 0-255 (default=128)");
91 MODULE_PARM(init_hue, "i");
92 MODULE_PARM_DESC(init_hue, "Hue preconfiguration: 0-255 (default=128)");
93 MODULE_PARM(hue_correction, "i");
94 MODULE_PARM_DESC(hue_correction, "YUV colorspace regulation: 0-255 (default=128)");
95 
96 /*
97  * ultracam_ProcessIsocData()
98  *
99  * Generic routine to parse the ring queue data. It employs either
100  * ultracam_find_header() or ultracam_parse_lines() to do most
101  * of work.
102  *
103  * 02-Nov-2000 First (mostly dummy) version.
104  * 06-Nov-2000 Rewrote to dump all data into frame.
105  */
ultracam_ProcessIsocData(struct uvd * uvd,struct usbvideo_frame * frame)106 void ultracam_ProcessIsocData(struct uvd *uvd, struct usbvideo_frame *frame)
107 {
108 	int n;
109 
110 	assert(uvd != NULL);
111 	assert(frame != NULL);
112 
113 	/* Try to move data from queue into frame buffer */
114 	n = RingQueue_GetLength(&uvd->dp);
115 	if (n > 0) {
116 		int m;
117 		/* See how much spare we have left */
118 		m = uvd->max_frame_size - frame->seqRead_Length;
119 		if (n > m)
120 			n = m;
121 		/* Now move that much data into frame buffer */
122 		RingQueue_Dequeue(
123 			&uvd->dp,
124 			frame->data + frame->seqRead_Length,
125 			m);
126 		frame->seqRead_Length += m;
127 	}
128 	/* See if we filled the frame */
129 	if (frame->seqRead_Length >= uvd->max_frame_size) {
130 		frame->frameState = FrameState_Done;
131 		uvd->curframe = -1;
132 		uvd->stats.frame_num++;
133 	}
134 }
135 
136 /*
137  * ultracam_veio()
138  *
139  * History:
140  * 1/27/00  Added check for dev == NULL; this happens if camera is unplugged.
141  */
ultracam_veio(struct uvd * uvd,unsigned char req,unsigned short value,unsigned short index,int is_out)142 static int ultracam_veio(
143 	struct uvd *uvd,
144 	unsigned char req,
145 	unsigned short value,
146 	unsigned short index,
147 	int is_out)
148 {
149 	static const char proc[] = "ultracam_veio";
150 	unsigned char cp[8] /* = { 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef } */;
151 	int i;
152 
153 	if (!CAMERA_IS_OPERATIONAL(uvd))
154 		return 0;
155 
156 	if (!is_out) {
157 		i = usb_control_msg(
158 			uvd->dev,
159 			usb_rcvctrlpipe(uvd->dev, 0),
160 			req,
161 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
162 			value,
163 			index,
164 			cp,
165 			sizeof(cp),
166 			HZ);
167 #if 1
168 		info("USB => %02x%02x%02x%02x%02x%02x%02x%02x "
169 		       "(req=$%02x val=$%04x ind=$%04x)",
170 		       cp[0],cp[1],cp[2],cp[3],cp[4],cp[5],cp[6],cp[7],
171 		       req, value, index);
172 #endif
173 	} else {
174 		i = usb_control_msg(
175 			uvd->dev,
176 			usb_sndctrlpipe(uvd->dev, 0),
177 			req,
178 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
179 			value,
180 			index,
181 			NULL,
182 			0,
183 			HZ);
184 	}
185 	if (i < 0) {
186 		err("%s: ERROR=%d. Camera stopped; Reconnect or reload driver.",
187 		    proc, i);
188 		uvd->last_error = i;
189 	}
190 	return i;
191 }
192 
193 /*
194  * ultracam_calculate_fps()
195  */
ultracam_calculate_fps(struct uvd * uvd)196 static int ultracam_calculate_fps(struct uvd *uvd)
197 {
198 	return 3 + framerate*4 + framerate/2;
199 }
200 
201 /*
202  * ultracam_adjust_contrast()
203  */
ultracam_adjust_contrast(struct uvd * uvd)204 static void ultracam_adjust_contrast(struct uvd *uvd)
205 {
206 }
207 
208 /*
209  * ultracam_change_lighting_conditions()
210  */
ultracam_change_lighting_conditions(struct uvd * uvd)211 static void ultracam_change_lighting_conditions(struct uvd *uvd)
212 {
213 }
214 
215 /*
216  * ultracam_set_sharpness()
217  *
218  * Cameras model 1 have internal smoothing feature. It is controlled by value in
219  * range [0..6], where 0 is most smooth and 6 is most sharp (raw image, I guess).
220  * Recommended value is 4. Cameras model 2 do not have this feature at all.
221  */
ultracam_set_sharpness(struct uvd * uvd)222 static void ultracam_set_sharpness(struct uvd *uvd)
223 {
224 }
225 
226 /*
227  * ultracam_set_brightness()
228  *
229  * This procedure changes brightness of the picture.
230  */
ultracam_set_brightness(struct uvd * uvd)231 static void ultracam_set_brightness(struct uvd *uvd)
232 {
233 }
234 
ultracam_set_hue(struct uvd * uvd)235 static void ultracam_set_hue(struct uvd *uvd)
236 {
237 }
238 
239 /*
240  * ultracam_adjust_picture()
241  *
242  * This procedure gets called from V4L interface to update picture settings.
243  * Here we change brightness and contrast.
244  */
ultracam_adjust_picture(struct uvd * uvd)245 static void ultracam_adjust_picture(struct uvd *uvd)
246 {
247 	ultracam_adjust_contrast(uvd);
248 	ultracam_set_brightness(uvd);
249 	ultracam_set_hue(uvd);
250 }
251 
252 /*
253  * ultracam_video_stop()
254  *
255  * This code tells camera to stop streaming. The interface remains
256  * configured and bandwidth - claimed.
257  */
ultracam_video_stop(struct uvd * uvd)258 static void ultracam_video_stop(struct uvd *uvd)
259 {
260 }
261 
262 /*
263  * ultracam_reinit_iso()
264  *
265  * This procedure sends couple of commands to the camera and then
266  * resets the video pipe. This sequence was observed to reinit the
267  * camera or, at least, to initiate ISO data stream.
268  */
ultracam_reinit_iso(struct uvd * uvd,int do_stop)269 static void ultracam_reinit_iso(struct uvd *uvd, int do_stop)
270 {
271 }
272 
ultracam_video_start(struct uvd * uvd)273 static void ultracam_video_start(struct uvd *uvd)
274 {
275 	ultracam_change_lighting_conditions(uvd);
276 	ultracam_set_sharpness(uvd);
277 	ultracam_reinit_iso(uvd, 0);
278 }
279 
ultracam_resetPipe(struct uvd * uvd)280 static int ultracam_resetPipe(struct uvd *uvd)
281 {
282 	usb_clear_halt(uvd->dev, uvd->video_endp);
283 	return 0;
284 }
285 
ultracam_alternateSetting(struct uvd * uvd,int setting)286 static int ultracam_alternateSetting(struct uvd *uvd, int setting)
287 {
288 	static const char proc[] = "ultracam_alternateSetting";
289 	int i;
290 	i = usb_set_interface(uvd->dev, uvd->iface, setting);
291 	if (i < 0) {
292 		err("%s: usb_set_interface error", proc);
293 		uvd->last_error = i;
294 		return -EBUSY;
295 	}
296 	return 0;
297 }
298 
299 /*
300  * Return negative code on failure, 0 on success.
301  */
ultracam_setup_on_open(struct uvd * uvd)302 static int ultracam_setup_on_open(struct uvd *uvd)
303 {
304 	int setup_ok = 0; /* Success by default */
305 	/* Send init sequence only once, it's large! */
306 	if (!ULTRACAM_T(uvd)->initialized) {
307 		ultracam_alternateSetting(uvd, 0x04);
308 		ultracam_alternateSetting(uvd, 0x00);
309 		ultracam_veio(uvd, 0x02, 0x0004, 0x000b, 1);
310 		ultracam_veio(uvd, 0x02, 0x0001, 0x0005, 1);
311 		ultracam_veio(uvd, 0x02, 0x8000, 0x0000, 1);
312 		ultracam_veio(uvd, 0x00, 0x0000, 0x0000, 1);
313 		ultracam_veio(uvd, 0x00, 0x00b0, 0x0001, 1);
314 		ultracam_veio(uvd, 0x00, 0x0000, 0x0002, 1);
315 		ultracam_veio(uvd, 0x00, 0x000c, 0x0003, 1);
316 		ultracam_veio(uvd, 0x00, 0x000b, 0x0004, 1);
317 		ultracam_veio(uvd, 0x00, 0x0000, 0x0005, 1);
318 		ultracam_veio(uvd, 0x00, 0x0000, 0x0006, 1);
319 		ultracam_veio(uvd, 0x00, 0x0079, 0x0007, 1);
320 		ultracam_veio(uvd, 0x00, 0x003b, 0x0008, 1);
321 		ultracam_veio(uvd, 0x00, 0x0002, 0x000f, 1);
322 		ultracam_veio(uvd, 0x00, 0x0001, 0x0010, 1);
323 		ultracam_veio(uvd, 0x00, 0x0000, 0x0011, 1);
324 		ultracam_veio(uvd, 0x00, 0x0000, 0x00bf, 1);
325 		ultracam_veio(uvd, 0x00, 0x0001, 0x00c0, 1);
326 		ultracam_veio(uvd, 0x00, 0x0010, 0x00cb, 1);
327 		ultracam_veio(uvd, 0x01, 0x00a4, 0x0001, 1);
328 		ultracam_veio(uvd, 0x01, 0x0010, 0x0002, 1);
329 		ultracam_veio(uvd, 0x01, 0x0066, 0x0007, 1);
330 		ultracam_veio(uvd, 0x01, 0x000b, 0x0008, 1);
331 		ultracam_veio(uvd, 0x01, 0x0034, 0x0009, 1);
332 		ultracam_veio(uvd, 0x01, 0x0000, 0x000a, 1);
333 		ultracam_veio(uvd, 0x01, 0x002e, 0x000b, 1);
334 		ultracam_veio(uvd, 0x01, 0x00d6, 0x000c, 1);
335 		ultracam_veio(uvd, 0x01, 0x00fc, 0x000d, 1);
336 		ultracam_veio(uvd, 0x01, 0x00f1, 0x000e, 1);
337 		ultracam_veio(uvd, 0x01, 0x00da, 0x000f, 1);
338 		ultracam_veio(uvd, 0x01, 0x0036, 0x0010, 1);
339 		ultracam_veio(uvd, 0x01, 0x000b, 0x0011, 1);
340 		ultracam_veio(uvd, 0x01, 0x0001, 0x0012, 1);
341 		ultracam_veio(uvd, 0x01, 0x0000, 0x0013, 1);
342 		ultracam_veio(uvd, 0x01, 0x0000, 0x0014, 1);
343 		ultracam_veio(uvd, 0x01, 0x0087, 0x0051, 1);
344 		ultracam_veio(uvd, 0x01, 0x0040, 0x0052, 1);
345 		ultracam_veio(uvd, 0x01, 0x0058, 0x0053, 1);
346 		ultracam_veio(uvd, 0x01, 0x0040, 0x0054, 1);
347 		ultracam_veio(uvd, 0x01, 0x0000, 0x0040, 1);
348 		ultracam_veio(uvd, 0x01, 0x0010, 0x0041, 1);
349 		ultracam_veio(uvd, 0x01, 0x0020, 0x0042, 1);
350 		ultracam_veio(uvd, 0x01, 0x0030, 0x0043, 1);
351 		ultracam_veio(uvd, 0x01, 0x0040, 0x0044, 1);
352 		ultracam_veio(uvd, 0x01, 0x0050, 0x0045, 1);
353 		ultracam_veio(uvd, 0x01, 0x0060, 0x0046, 1);
354 		ultracam_veio(uvd, 0x01, 0x0070, 0x0047, 1);
355 		ultracam_veio(uvd, 0x01, 0x0080, 0x0048, 1);
356 		ultracam_veio(uvd, 0x01, 0x0090, 0x0049, 1);
357 		ultracam_veio(uvd, 0x01, 0x00a0, 0x004a, 1);
358 		ultracam_veio(uvd, 0x01, 0x00b0, 0x004b, 1);
359 		ultracam_veio(uvd, 0x01, 0x00c0, 0x004c, 1);
360 		ultracam_veio(uvd, 0x01, 0x00d0, 0x004d, 1);
361 		ultracam_veio(uvd, 0x01, 0x00e0, 0x004e, 1);
362 		ultracam_veio(uvd, 0x01, 0x00f0, 0x004f, 1);
363 		ultracam_veio(uvd, 0x01, 0x00ff, 0x0050, 1);
364 		ultracam_veio(uvd, 0x01, 0x0000, 0x0056, 1);
365 		ultracam_veio(uvd, 0x00, 0x0080, 0x00c1, 1);
366 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c2, 1);
367 		ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
368 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
369 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
370 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
371 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
372 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
373 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
374 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
375 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
376 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
377 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
378 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
379 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
380 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
381 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
382 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
383 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
384 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
385 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
386 		ultracam_veio(uvd, 0x00, 0x0080, 0x00c1, 1);
387 		ultracam_veio(uvd, 0x00, 0x0004, 0x00c2, 1);
388 		ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
389 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
390 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
391 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
392 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
393 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
394 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
395 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
396 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
397 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
398 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
399 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
400 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
401 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
402 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
403 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
404 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
405 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
406 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
407 		ultracam_veio(uvd, 0x00, 0x0002, 0x00c1, 1);
408 		ultracam_veio(uvd, 0x00, 0x0020, 0x00c2, 1);
409 		ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
410 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
411 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
412 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
413 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
414 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
415 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
416 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
417 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
418 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
419 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
420 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
421 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
422 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
423 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
424 		ultracam_veio(uvd, 0x00, 0x0040, 0x00c1, 1);
425 		ultracam_veio(uvd, 0x00, 0x0017, 0x00c2, 1);
426 		ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
427 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
428 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
429 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
430 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
431 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
432 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
433 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
434 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c3, 1);
435 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c4, 1);
436 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c5, 1);
437 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c6, 1);
438 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c7, 1);
439 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c8, 1);
440 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c9, 1);
441 		ultracam_veio(uvd, 0x00, 0x00c0, 0x00c1, 1);
442 		ultracam_veio(uvd, 0x00, 0x0000, 0x00c2, 1);
443 		ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
444 		ultracam_veio(uvd, 0x02, 0xc040, 0x0001, 1);
445 		ultracam_veio(uvd, 0x01, 0x0000, 0x0008, 0);
446 		ultracam_veio(uvd, 0x01, 0x0000, 0x0009, 0);
447 		ultracam_veio(uvd, 0x01, 0x0000, 0x000a, 0);
448 		ultracam_veio(uvd, 0x01, 0x0000, 0x000b, 0);
449 		ultracam_veio(uvd, 0x01, 0x0000, 0x000c, 0);
450 		ultracam_veio(uvd, 0x01, 0x0000, 0x000d, 0);
451 		ultracam_veio(uvd, 0x01, 0x0000, 0x000e, 0);
452 		ultracam_veio(uvd, 0x01, 0x0000, 0x000f, 0);
453 		ultracam_veio(uvd, 0x01, 0x0000, 0x0010, 0);
454 		ultracam_veio(uvd, 0x01, 0x000b, 0x0008, 1);
455 		ultracam_veio(uvd, 0x01, 0x0034, 0x0009, 1);
456 		ultracam_veio(uvd, 0x01, 0x0000, 0x000a, 1);
457 		ultracam_veio(uvd, 0x01, 0x002e, 0x000b, 1);
458 		ultracam_veio(uvd, 0x01, 0x00d6, 0x000c, 1);
459 		ultracam_veio(uvd, 0x01, 0x00fc, 0x000d, 1);
460 		ultracam_veio(uvd, 0x01, 0x00f1, 0x000e, 1);
461 		ultracam_veio(uvd, 0x01, 0x00da, 0x000f, 1);
462 		ultracam_veio(uvd, 0x01, 0x0036, 0x0010, 1);
463 		ultracam_veio(uvd, 0x01, 0x0000, 0x0001, 0);
464 		ultracam_veio(uvd, 0x01, 0x0064, 0x0001, 1);
465 		ultracam_veio(uvd, 0x01, 0x0059, 0x0051, 1);
466 		ultracam_veio(uvd, 0x01, 0x003f, 0x0052, 1);
467 		ultracam_veio(uvd, 0x01, 0x0094, 0x0053, 1);
468 		ultracam_veio(uvd, 0x01, 0x00ff, 0x0011, 1);
469 		ultracam_veio(uvd, 0x01, 0x0003, 0x0012, 1);
470 		ultracam_veio(uvd, 0x01, 0x00f7, 0x0013, 1);
471 		ultracam_veio(uvd, 0x00, 0x0009, 0x0011, 1);
472 		ultracam_veio(uvd, 0x00, 0x0000, 0x0001, 1);
473 		ultracam_veio(uvd, 0x00, 0x0000, 0x0000, 1);
474 		ultracam_veio(uvd, 0x00, 0x0020, 0x00c1, 1);
475 		ultracam_veio(uvd, 0x00, 0x0010, 0x00c2, 1);
476 		ultracam_veio(uvd, 0x00, 0x0000, 0x00ca, 1);
477 		ultracam_alternateSetting(uvd, 0x04);
478 		ultracam_veio(uvd, 0x02, 0x0000, 0x0001, 1);
479 		ultracam_veio(uvd, 0x02, 0x0000, 0x0001, 1);
480 		ultracam_veio(uvd, 0x02, 0x0000, 0x0006, 1);
481 		ultracam_veio(uvd, 0x02, 0x9000, 0x0007, 1);
482 		ultracam_veio(uvd, 0x02, 0x0042, 0x0001, 1);
483 		ultracam_veio(uvd, 0x02, 0x0000, 0x000b, 0);
484 		ultracam_resetPipe(uvd);
485 		ULTRACAM_T(uvd)->initialized = (setup_ok != 0);
486 	}
487 	return setup_ok;
488 }
489 
ultracam_configure_video(struct uvd * uvd)490 static void ultracam_configure_video(struct uvd *uvd)
491 {
492 	if (uvd == NULL)
493 		return;
494 
495 	RESTRICT_TO_RANGE(init_brightness, 0, 255);
496 	RESTRICT_TO_RANGE(init_contrast, 0, 255);
497 	RESTRICT_TO_RANGE(init_color, 0, 255);
498 	RESTRICT_TO_RANGE(init_hue, 0, 255);
499 	RESTRICT_TO_RANGE(hue_correction, 0, 255);
500 
501 	memset(&uvd->vpic, 0, sizeof(uvd->vpic));
502 	memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
503 
504 	uvd->vpic.colour = init_color << 8;
505 	uvd->vpic.hue = init_hue << 8;
506 	uvd->vpic.brightness = init_brightness << 8;
507 	uvd->vpic.contrast = init_contrast << 8;
508 	uvd->vpic.whiteness = 105 << 8; /* This one isn't used */
509 	uvd->vpic.depth = 24;
510 	uvd->vpic.palette = VIDEO_PALETTE_RGB24;
511 
512 	memset(&uvd->vcap, 0, sizeof(uvd->vcap));
513 	strcpy(uvd->vcap.name, "IBM Ultra Camera");
514 	uvd->vcap.type = VID_TYPE_CAPTURE;
515 	uvd->vcap.channels = 1;
516 	uvd->vcap.audios = 0;
517 	uvd->vcap.maxwidth = VIDEOSIZE_X(uvd->canvas);
518 	uvd->vcap.maxheight = VIDEOSIZE_Y(uvd->canvas);
519 	uvd->vcap.minwidth = min_canvasWidth;
520 	uvd->vcap.minheight = min_canvasHeight;
521 
522 	memset(&uvd->vchan, 0, sizeof(uvd->vchan));
523 	uvd->vchan.flags = 0;
524 	uvd->vchan.tuners = 0;
525 	uvd->vchan.channel = 0;
526 	uvd->vchan.type = VIDEO_TYPE_CAMERA;
527 	strcpy(uvd->vchan.name, "Camera");
528 }
529 
530 /*
531  * ultracam_probe()
532  *
533  * This procedure queries device descriptor and accepts the interface
534  * if it looks like our camera.
535  *
536  * History:
537  * 12-Nov-2000 Reworked to comply with new probe() signature.
538  * 23-Jan-2001 Added compatibility with 2.2.x kernels.
539  */
ultracam_probe(struct usb_device * dev,unsigned int ifnum,const struct usb_device_id * devid)540 static void *ultracam_probe(struct usb_device *dev, unsigned int ifnum ,const struct usb_device_id *devid)
541 {
542 	struct uvd *uvd = NULL;
543 	int i, nas;
544 	int actInterface=-1, inactInterface=-1, maxPS=0;
545 	unsigned char video_ep = 0;
546 
547 	if (debug >= 1)
548 		info("ultracam_probe(%p,%u.)", dev, ifnum);
549 
550 	/* We don't handle multi-config cameras */
551 	if (dev->descriptor.bNumConfigurations != 1)
552 		return NULL;
553 
554 	/* Is it an IBM camera? */
555 	if ((dev->descriptor.idVendor != ULTRACAM_VENDOR_ID) ||
556 	    (dev->descriptor.idProduct != ULTRACAM_PRODUCT_ID))
557 		return NULL;
558 
559 	info("IBM Ultra camera found (rev. 0x%04x)", dev->descriptor.bcdDevice);
560 
561 	/* Validate found interface: must have one ISO endpoint */
562 	nas = dev->actconfig->interface[ifnum].num_altsetting;
563 	if (debug > 0)
564 		info("Number of alternate settings=%d.", nas);
565 	if (nas < 8) {
566 		err("Too few alternate settings for this camera!");
567 		return NULL;
568 	}
569 	/* Validate all alternate settings */
570 	for (i=0; i < nas; i++) {
571 		const struct usb_interface_descriptor *interface;
572 		const struct usb_endpoint_descriptor *endpoint;
573 
574 		interface = &dev->actconfig->interface[ifnum].altsetting[i];
575 		if (interface->bNumEndpoints != 1) {
576 			err("Interface %d. has %u. endpoints!",
577 			    ifnum, (unsigned)(interface->bNumEndpoints));
578 			return NULL;
579 		}
580 		endpoint = &interface->endpoint[0];
581 		if (video_ep == 0)
582 			video_ep = endpoint->bEndpointAddress;
583 		else if (video_ep != endpoint->bEndpointAddress) {
584 			err("Alternate settings have different endpoint addresses!");
585 			return NULL;
586 		}
587 		if ((endpoint->bmAttributes & 0x03) != 0x01) {
588 			err("Interface %d. has non-ISO endpoint!", ifnum);
589 			return NULL;
590 		}
591 		if ((endpoint->bEndpointAddress & 0x80) == 0) {
592 			err("Interface %d. has ISO OUT endpoint!", ifnum);
593 			return NULL;
594 		}
595 		if (endpoint->wMaxPacketSize == 0) {
596 			if (inactInterface < 0)
597 				inactInterface = i;
598 			else {
599 				err("More than one inactive alt. setting!");
600 				return NULL;
601 			}
602 		} else {
603 			if (actInterface < 0) {
604 				actInterface = i;
605 				maxPS = endpoint->wMaxPacketSize;
606 				if (debug > 0)
607 					info("Active setting=%d. maxPS=%d.", i, maxPS);
608 			} else {
609 				/* Got another active alt. setting */
610 				if (maxPS < endpoint->wMaxPacketSize) {
611 					/* This one is better! */
612 					actInterface = i;
613 					maxPS = endpoint->wMaxPacketSize;
614 					if (debug > 0) {
615 						info("Even better ctive setting=%d. maxPS=%d.",
616 						     i, maxPS);
617 					}
618 				}
619 			}
620 		}
621 	}
622 	if ((maxPS <= 0) || (actInterface < 0) || (inactInterface < 0)) {
623 		err("Failed to recognize the camera!");
624 		return NULL;
625 	}
626 
627 	/* Code below may sleep, need to lock module while we are here */
628 	MOD_INC_USE_COUNT;
629 	uvd = usbvideo_AllocateDevice(cams);
630 	if (uvd != NULL) {
631 		/* Here uvd is a fully allocated uvd object */
632 		uvd->flags = flags;
633 		uvd->debug = debug;
634 		uvd->dev = dev;
635 		uvd->iface = ifnum;
636 		uvd->ifaceAltInactive = inactInterface;
637 		uvd->ifaceAltActive = actInterface;
638 		uvd->video_endp = video_ep;
639 		uvd->iso_packet_len = maxPS;
640 		uvd->paletteBits = 1L << VIDEO_PALETTE_RGB24;
641 		uvd->defaultPalette = VIDEO_PALETTE_RGB24;
642 		uvd->canvas = VIDEOSIZE(640, 480);	/* FIXME */
643 		uvd->videosize = uvd->canvas; /* ultracam_size_to_videosize(size);*/
644 
645 		/* Initialize ibmcam-specific data */
646 		assert(ULTRACAM_T(uvd) != NULL);
647 		ULTRACAM_T(uvd)->camera_model = 0; /* Not used yet */
648 		ULTRACAM_T(uvd)->initialized = 0;
649 
650 		ultracam_configure_video(uvd);
651 
652 		i = usbvideo_RegisterVideoDevice(uvd);
653 		if (i != 0) {
654 			err("usbvideo_RegisterVideoDevice() failed.");
655 			uvd = NULL;
656 		}
657 	}
658 	MOD_DEC_USE_COUNT;
659 	return uvd;
660 }
661 
662 
663 static struct usb_device_id id_table[] = {
664 	{ USB_DEVICE(ULTRACAM_VENDOR_ID, ULTRACAM_PRODUCT_ID) },
665 	{ }  /* Terminating entry */
666 };
667 
668 /*
669  * ultracam_init()
670  *
671  * This code is run to initialize the driver.
672  */
ultracam_init(void)673 static int __init ultracam_init(void)
674 {
675 	struct usbvideo_cb cbTbl;
676 	memset(&cbTbl, 0, sizeof(cbTbl));
677 	cbTbl.probe = ultracam_probe;
678 	cbTbl.setupOnOpen = ultracam_setup_on_open;
679 	cbTbl.videoStart = ultracam_video_start;
680 	cbTbl.videoStop = ultracam_video_stop;
681 	cbTbl.processData = ultracam_ProcessIsocData;
682 	cbTbl.postProcess = usbvideo_DeinterlaceFrame;
683 	cbTbl.adjustPicture = ultracam_adjust_picture;
684 	cbTbl.getFPS = ultracam_calculate_fps;
685 	return usbvideo_register(
686 		&cams,
687 		MAX_CAMERAS,
688 		sizeof(ultracam_t),
689 		"ultracam",
690 		&cbTbl,
691 		THIS_MODULE,
692 		id_table);
693 }
694 
ultracam_cleanup(void)695 static void __exit ultracam_cleanup(void)
696 {
697 	usbvideo_Deregister(&cams);
698 }
699 
700 MODULE_DEVICE_TABLE(usb, id_table);
701 MODULE_LICENSE("GPL");
702 
703 module_init(ultracam_init);
704 module_exit(ultracam_cleanup);
705