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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31#define MODULE_NAME "vicam"
32#define HEADER_SIZE 64
33
34#include <linux/workqueue.h>
35#include <linux/slab.h>
36#include <linux/firmware.h>
37#include <linux/ihex.h>
38#include "gspca.h"
39
40#define VICAM_FIRMWARE "vicam/firmware.fw"
41
42MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
43MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
44MODULE_LICENSE("GPL");
45MODULE_FIRMWARE(VICAM_FIRMWARE);
46
47struct sd {
48 struct gspca_dev gspca_dev;
49 struct work_struct work_struct;
50};
51
52
53
54
55static struct v4l2_pix_format vicam_mode[] = {
56 { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
57 .bytesperline = 256,
58 .sizeimage = 256 * 122,
59 .colorspace = V4L2_COLORSPACE_SRGB,},
60
61 { 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
62 .bytesperline = 256,
63 .sizeimage = 256 * 200,
64 .colorspace = V4L2_COLORSPACE_SRGB,},
65 { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
66 .bytesperline = 256,
67 .sizeimage = 256 * 240,
68 .colorspace = V4L2_COLORSPACE_SRGB,},
69#if 0
70 { 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
71 .bytesperline = 512,
72 .sizeimage = 512 * 122,
73 .colorspace = V4L2_COLORSPACE_SRGB,},
74#endif
75 { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
76 .bytesperline = 512,
77 .sizeimage = 512 * 244,
78 .colorspace = V4L2_COLORSPACE_SRGB,},
79};
80
81static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
82 u16 value, u16 index, u8 *data, u16 len)
83{
84 int ret;
85
86 ret = usb_control_msg(gspca_dev->dev,
87 usb_sndctrlpipe(gspca_dev->dev, 0),
88 request,
89 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
90 value, index, data, len, 1000);
91 if (ret < 0)
92 pr_err("control msg req %02X error %d\n", request, ret);
93
94 return ret;
95}
96
97static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
98{
99 int ret;
100
101 ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
102 if (ret < 0)
103 return ret;
104
105 if (state)
106 ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
107
108 return ret;
109}
110
111
112
113
114static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
115{
116 int ret, unscaled_height, act_len = 0;
117 u8 *req_data = gspca_dev->usb_buf;
118 s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
119 s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
120
121 memset(req_data, 0, 16);
122 req_data[0] = gain;
123 if (gspca_dev->pixfmt.width == 256)
124 req_data[1] |= 0x01;
125 if (gspca_dev->pixfmt.height <= 122) {
126 req_data[1] |= 0x10;
127 unscaled_height = gspca_dev->pixfmt.height * 2;
128 } else
129 unscaled_height = gspca_dev->pixfmt.height;
130 req_data[2] = 0x90;
131 if (unscaled_height <= 200)
132 req_data[3] = 0x06;
133 else if (unscaled_height <= 242)
134 req_data[3] = 0x07;
135 else
136 req_data[3] = 0x08;
137
138 if (expo < 256) {
139
140 req_data[4] = 255 - expo;
141 req_data[5] = 0x00;
142 req_data[6] = 0x00;
143 req_data[7] = 0x01;
144 } else {
145
146 req_data[4] = 0x00;
147 req_data[5] = 0x00;
148 req_data[6] = expo & 0xFF;
149 req_data[7] = expo >> 8;
150 }
151 req_data[8] = ((244 - unscaled_height) / 2) & ~0x01;
152
153
154 mutex_lock(&gspca_dev->usb_lock);
155 ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
156 mutex_unlock(&gspca_dev->usb_lock);
157 if (ret < 0)
158 return ret;
159
160 ret = usb_bulk_msg(gspca_dev->dev,
161 usb_rcvbulkpipe(gspca_dev->dev, 0x81),
162 data, size, &act_len, 10000);
163
164 if (ret < 0 || act_len != size) {
165 pr_err("bulk read fail (%d) len %d/%d\n",
166 ret, act_len, size);
167 return -EIO;
168 }
169 return 0;
170}
171
172
173
174
175
176
177
178
179
180static void vicam_dostream(struct work_struct *work)
181{
182 struct sd *sd = container_of(work, struct sd, work_struct);
183 struct gspca_dev *gspca_dev = &sd->gspca_dev;
184 int ret, frame_sz;
185 u8 *buffer;
186
187 frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
188 HEADER_SIZE;
189 buffer = kmalloc(frame_sz, GFP_KERNEL | GFP_DMA);
190 if (!buffer) {
191 pr_err("Couldn't allocate USB buffer\n");
192 goto exit;
193 }
194
195 while (gspca_dev->present && gspca_dev->streaming) {
196#ifdef CONFIG_PM
197 if (gspca_dev->frozen)
198 break;
199#endif
200 ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
201 if (ret < 0)
202 break;
203
204
205
206
207
208
209 gspca_frame_add(gspca_dev, FIRST_PACKET,
210 buffer + HEADER_SIZE,
211 frame_sz - HEADER_SIZE);
212 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
213 }
214exit:
215 kfree(buffer);
216}
217
218
219static int sd_config(struct gspca_dev *gspca_dev,
220 const struct usb_device_id *id)
221{
222 struct cam *cam = &gspca_dev->cam;
223 struct sd *sd = (struct sd *)gspca_dev;
224
225
226 cam->bulk = 1;
227 cam->bulk_size = 64;
228 cam->cam_mode = vicam_mode;
229 cam->nmodes = ARRAY_SIZE(vicam_mode);
230
231 INIT_WORK(&sd->work_struct, vicam_dostream);
232
233 return 0;
234}
235
236
237static int sd_init(struct gspca_dev *gspca_dev)
238{
239 int ret;
240 const struct ihex_binrec *rec;
241 const struct firmware *uninitialized_var(fw);
242 u8 *firmware_buf;
243
244 ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
245 &gspca_dev->dev->dev);
246 if (ret) {
247 pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
248 return ret;
249 }
250
251 firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
252 if (!firmware_buf) {
253 ret = -ENOMEM;
254 goto exit;
255 }
256 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
257 memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
258 ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
259 be16_to_cpu(rec->len));
260 if (ret < 0)
261 break;
262 }
263
264 kfree(firmware_buf);
265exit:
266 release_firmware(fw);
267 return ret;
268}
269
270
271static int sd_start(struct gspca_dev *gspca_dev)
272{
273 struct sd *sd = (struct sd *)gspca_dev;
274 int ret;
275
276 ret = vicam_set_camera_power(gspca_dev, 1);
277 if (ret < 0)
278 return ret;
279
280 schedule_work(&sd->work_struct);
281
282 return 0;
283}
284
285
286
287static void sd_stop0(struct gspca_dev *gspca_dev)
288{
289 struct sd *dev = (struct sd *)gspca_dev;
290
291
292 mutex_unlock(&gspca_dev->usb_lock);
293
294 flush_work(&dev->work_struct);
295 mutex_lock(&gspca_dev->usb_lock);
296
297 if (gspca_dev->present)
298 vicam_set_camera_power(gspca_dev, 0);
299}
300
301static int sd_init_controls(struct gspca_dev *gspca_dev)
302{
303 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
304
305 gspca_dev->vdev.ctrl_handler = hdl;
306 v4l2_ctrl_handler_init(hdl, 2);
307 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
308 V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
309 gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
310 V4L2_CID_GAIN, 0, 255, 1, 200);
311
312 if (hdl->error) {
313 pr_err("Could not initialize controls\n");
314 return hdl->error;
315 }
316 return 0;
317}
318
319
320static const struct usb_device_id device_table[] = {
321 {USB_DEVICE(0x04c1, 0x009d)},
322 {USB_DEVICE(0x0602, 0x1001)},
323 {}
324};
325
326MODULE_DEVICE_TABLE(usb, device_table);
327
328
329static const struct sd_desc sd_desc = {
330 .name = MODULE_NAME,
331 .config = sd_config,
332 .init = sd_init,
333 .init_controls = sd_init_controls,
334 .start = sd_start,
335 .stop0 = sd_stop0,
336};
337
338
339static int sd_probe(struct usb_interface *intf,
340 const struct usb_device_id *id)
341{
342 return gspca_dev_probe(intf, id,
343 &sd_desc,
344 sizeof(struct sd),
345 THIS_MODULE);
346}
347
348static struct usb_driver sd_driver = {
349 .name = MODULE_NAME,
350 .id_table = device_table,
351 .probe = sd_probe,
352 .disconnect = gspca_disconnect,
353#ifdef CONFIG_PM
354 .suspend = gspca_suspend,
355 .resume = gspca_resume,
356 .reset_resume = gspca_resume,
357#endif
358};
359
360module_usb_driver(sd_driver);
361