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