Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / media / video / gspca / spca1528.c
blob070b9c33b5177a3f917633721f29f372740e0f70
1 /*
2 * spca1528 subdriver
4 * Copyright (C) 2010-2011 Jean-Francois Moine (http://moinejf.free.fr)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #define MODULE_NAME "spca1528"
25 #include "gspca.h"
26 #include "jpeg.h"
28 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
29 MODULE_DESCRIPTION("SPCA1528 USB Camera Driver");
30 MODULE_LICENSE("GPL");
32 /* specific webcam descriptor */
33 struct sd {
34 struct gspca_dev gspca_dev; /* !! must be the first item */
36 u8 brightness;
37 u8 contrast;
38 u8 hue;
39 u8 color;
40 u8 sharpness;
42 u8 pkt_seq;
44 u8 jpeg_hdr[JPEG_HDR_SZ];
47 /* V4L2 controls supported by the driver */
48 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
49 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
50 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
51 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
52 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
53 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
54 static int sd_setcolor(struct gspca_dev *gspca_dev, __s32 val);
55 static int sd_getcolor(struct gspca_dev *gspca_dev, __s32 *val);
56 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
57 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
59 static const struct ctrl sd_ctrls[] = {
62 .id = V4L2_CID_BRIGHTNESS,
63 .type = V4L2_CTRL_TYPE_INTEGER,
64 .name = "Brightness",
65 .minimum = 0,
66 .maximum = 255,
67 .step = 1,
68 #define BRIGHTNESS_DEF 128
69 .default_value = BRIGHTNESS_DEF,
71 .set = sd_setbrightness,
72 .get = sd_getbrightness,
76 .id = V4L2_CID_CONTRAST,
77 .type = V4L2_CTRL_TYPE_INTEGER,
78 .name = "Contrast",
79 .minimum = 0,
80 .maximum = 8,
81 .step = 1,
82 #define CONTRAST_DEF 1
83 .default_value = CONTRAST_DEF,
85 .set = sd_setcontrast,
86 .get = sd_getcontrast,
90 .id = V4L2_CID_HUE,
91 .type = V4L2_CTRL_TYPE_INTEGER,
92 .name = "Hue",
93 .minimum = 0,
94 .maximum = 255,
95 .step = 1,
96 #define HUE_DEF 0
97 .default_value = HUE_DEF,
99 .set = sd_sethue,
100 .get = sd_gethue,
104 .id = V4L2_CID_SATURATION,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Saturation",
107 .minimum = 0,
108 .maximum = 8,
109 .step = 1,
110 #define COLOR_DEF 1
111 .default_value = COLOR_DEF,
113 .set = sd_setcolor,
114 .get = sd_getcolor,
118 .id = V4L2_CID_SHARPNESS,
119 .type = V4L2_CTRL_TYPE_INTEGER,
120 .name = "Sharpness",
121 .minimum = 0,
122 .maximum = 255,
123 .step = 1,
124 #define SHARPNESS_DEF 0
125 .default_value = SHARPNESS_DEF,
127 .set = sd_setsharpness,
128 .get = sd_getsharpness,
132 static const struct v4l2_pix_format vga_mode[] = {
133 /* (does not work correctly)
134 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
135 .bytesperline = 176,
136 .sizeimage = 176 * 144 * 5 / 8 + 590,
137 .colorspace = V4L2_COLORSPACE_JPEG,
138 .priv = 3},
140 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
141 .bytesperline = 320,
142 .sizeimage = 320 * 240 * 4 / 8 + 590,
143 .colorspace = V4L2_COLORSPACE_JPEG,
144 .priv = 2},
145 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
146 .bytesperline = 640,
147 .sizeimage = 640 * 480 * 3 / 8 + 590,
148 .colorspace = V4L2_COLORSPACE_JPEG,
149 .priv = 1},
152 /* read <len> bytes to gspca usb_buf */
153 static void reg_r(struct gspca_dev *gspca_dev,
154 u8 req,
155 u16 index,
156 int len)
158 #if USB_BUF_SZ < 64
159 #error "USB buffer too small"
160 #endif
161 struct usb_device *dev = gspca_dev->dev;
162 int ret;
164 if (gspca_dev->usb_err < 0)
165 return;
166 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
167 req,
168 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
169 0x0000, /* value */
170 index,
171 gspca_dev->usb_buf, len,
172 500);
173 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x", req, index,
174 gspca_dev->usb_buf[0]);
175 if (ret < 0) {
176 pr_err("reg_r err %d\n", ret);
177 gspca_dev->usb_err = ret;
181 static void reg_w(struct gspca_dev *gspca_dev,
182 u8 req,
183 u16 value,
184 u16 index)
186 struct usb_device *dev = gspca_dev->dev;
187 int ret;
189 if (gspca_dev->usb_err < 0)
190 return;
191 PDEBUG(D_USBO, "SET %02x %04x %04x", req, value, index);
192 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
193 req,
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
195 value, index,
196 NULL, 0, 500);
197 if (ret < 0) {
198 pr_err("reg_w err %d\n", ret);
199 gspca_dev->usb_err = ret;
203 static void reg_wb(struct gspca_dev *gspca_dev,
204 u8 req,
205 u16 value,
206 u16 index,
207 u8 byte)
209 struct usb_device *dev = gspca_dev->dev;
210 int ret;
212 if (gspca_dev->usb_err < 0)
213 return;
214 PDEBUG(D_USBO, "SET %02x %04x %04x %02x", req, value, index, byte);
215 gspca_dev->usb_buf[0] = byte;
216 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
217 req,
218 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
219 value, index,
220 gspca_dev->usb_buf, 1, 500);
221 if (ret < 0) {
222 pr_err("reg_w err %d\n", ret);
223 gspca_dev->usb_err = ret;
227 static void wait_status_0(struct gspca_dev *gspca_dev)
229 int i, w;
231 i = 16;
232 w = 0;
233 do {
234 reg_r(gspca_dev, 0x21, 0x0000, 1);
235 if (gspca_dev->usb_buf[0] == 0)
236 return;
237 w += 15;
238 msleep(w);
239 } while (--i > 0);
240 PDEBUG(D_ERR, "wait_status_0 timeout");
241 gspca_dev->usb_err = -ETIME;
244 static void wait_status_1(struct gspca_dev *gspca_dev)
246 int i;
248 i = 10;
249 do {
250 reg_r(gspca_dev, 0x21, 0x0001, 1);
251 msleep(10);
252 if (gspca_dev->usb_buf[0] == 1) {
253 reg_wb(gspca_dev, 0x21, 0x0000, 0x0001, 0x00);
254 reg_r(gspca_dev, 0x21, 0x0001, 1);
255 return;
257 } while (--i > 0);
258 PDEBUG(D_ERR, "wait_status_1 timeout");
259 gspca_dev->usb_err = -ETIME;
262 static void setbrightness(struct gspca_dev *gspca_dev)
264 struct sd *sd = (struct sd *) gspca_dev;
266 reg_wb(gspca_dev, 0xc0, 0x0000, 0x00c0, sd->brightness);
269 static void setcontrast(struct gspca_dev *gspca_dev)
271 struct sd *sd = (struct sd *) gspca_dev;
273 reg_wb(gspca_dev, 0xc1, 0x0000, 0x00c1, sd->contrast);
276 static void sethue(struct gspca_dev *gspca_dev)
278 struct sd *sd = (struct sd *) gspca_dev;
280 reg_wb(gspca_dev, 0xc2, 0x0000, 0x0000, sd->hue);
283 static void setcolor(struct gspca_dev *gspca_dev)
285 struct sd *sd = (struct sd *) gspca_dev;
287 reg_wb(gspca_dev, 0xc3, 0x0000, 0x00c3, sd->color);
290 static void setsharpness(struct gspca_dev *gspca_dev)
292 struct sd *sd = (struct sd *) gspca_dev;
294 reg_wb(gspca_dev, 0xc4, 0x0000, 0x00c4, sd->sharpness);
297 /* this function is called at probe time */
298 static int sd_config(struct gspca_dev *gspca_dev,
299 const struct usb_device_id *id)
301 struct sd *sd = (struct sd *) gspca_dev;
303 gspca_dev->cam.cam_mode = vga_mode;
304 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
305 gspca_dev->cam.npkt = 128; /* number of packets per ISOC message */
306 /*fixme: 256 in ms-win traces*/
308 sd->brightness = BRIGHTNESS_DEF;
309 sd->contrast = CONTRAST_DEF;
310 sd->hue = HUE_DEF;
311 sd->color = COLOR_DEF;
312 sd->sharpness = SHARPNESS_DEF;
314 return 0;
317 /* this function is called at probe and resume time */
318 static int sd_init(struct gspca_dev *gspca_dev)
320 reg_w(gspca_dev, 0x00, 0x0001, 0x2067);
321 reg_w(gspca_dev, 0x00, 0x00d0, 0x206b);
322 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
323 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
324 msleep(8);
325 reg_w(gspca_dev, 0x00, 0x00c0, 0x206b);
326 reg_w(gspca_dev, 0x00, 0x0000, 0x206c);
327 reg_w(gspca_dev, 0x00, 0x0001, 0x2069);
329 reg_r(gspca_dev, 0x20, 0x0000, 1);
330 reg_r(gspca_dev, 0x20, 0x0000, 5);
331 reg_r(gspca_dev, 0x23, 0x0000, 64);
332 PDEBUG(D_PROBE, "%s%s", &gspca_dev->usb_buf[0x1c],
333 &gspca_dev->usb_buf[0x30]);
334 reg_r(gspca_dev, 0x23, 0x0001, 64);
335 return gspca_dev->usb_err;
338 /* function called at start time before URB creation */
339 static int sd_isoc_init(struct gspca_dev *gspca_dev)
341 u8 mode;
343 reg_r(gspca_dev, 0x00, 0x2520, 1);
344 wait_status_0(gspca_dev);
345 reg_w(gspca_dev, 0xc5, 0x0003, 0x0000);
346 wait_status_1(gspca_dev);
348 wait_status_0(gspca_dev);
349 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
350 reg_wb(gspca_dev, 0x25, 0x0000, 0x0004, mode);
351 reg_r(gspca_dev, 0x25, 0x0004, 1);
352 reg_wb(gspca_dev, 0x27, 0x0000, 0x0000, 0x06); /* 420 */
353 reg_r(gspca_dev, 0x27, 0x0000, 1);
355 /* not useful..
356 gspca_dev->alt = 4; * use alternate setting 3 */
358 return gspca_dev->usb_err;
361 /* -- start the camera -- */
362 static int sd_start(struct gspca_dev *gspca_dev)
364 struct sd *sd = (struct sd *) gspca_dev;
366 /* initialize the JPEG header */
367 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
368 0x22); /* JPEG 411 */
370 /* the JPEG quality shall be 85% */
371 jpeg_set_qual(sd->jpeg_hdr, 85);
373 /* set the controls */
374 setbrightness(gspca_dev);
375 setcontrast(gspca_dev);
376 sethue(gspca_dev);
377 setcolor(gspca_dev);
378 setsharpness(gspca_dev);
380 msleep(5);
381 reg_r(gspca_dev, 0x00, 0x2520, 1);
382 msleep(8);
384 /* start the capture */
385 wait_status_0(gspca_dev);
386 reg_w(gspca_dev, 0x31, 0x0000, 0x0004); /* start request */
387 wait_status_1(gspca_dev);
388 wait_status_0(gspca_dev);
389 msleep(200);
391 sd->pkt_seq = 0;
392 return gspca_dev->usb_err;
395 static void sd_stopN(struct gspca_dev *gspca_dev)
397 /* stop the capture */
398 wait_status_0(gspca_dev);
399 reg_w(gspca_dev, 0x31, 0x0000, 0x0000); /* stop request */
400 wait_status_1(gspca_dev);
401 wait_status_0(gspca_dev);
404 /* move a packet adding 0x00 after 0xff */
405 static void add_packet(struct gspca_dev *gspca_dev,
406 u8 *data,
407 int len)
409 int i;
411 i = 0;
412 do {
413 if (data[i] == 0xff) {
414 gspca_frame_add(gspca_dev, INTER_PACKET,
415 data, i + 1);
416 len -= i;
417 data += i;
418 *data = 0x00;
419 i = 0;
421 } while (++i < len);
422 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
425 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
426 u8 *data, /* isoc packet */
427 int len) /* iso packet length */
429 struct sd *sd = (struct sd *) gspca_dev;
430 static const u8 ffd9[] = {0xff, 0xd9};
432 /* image packets start with:
433 * 02 8n
434 * with <n> bit:
435 * 0x01: even (0) / odd (1) image
436 * 0x02: end of image when set
438 if (len < 3)
439 return; /* empty packet */
440 if (*data == 0x02) {
441 if (data[1] & 0x02) {
442 sd->pkt_seq = !(data[1] & 1);
443 add_packet(gspca_dev, data + 2, len - 2);
444 gspca_frame_add(gspca_dev, LAST_PACKET,
445 ffd9, 2);
446 return;
448 if ((data[1] & 1) != sd->pkt_seq)
449 goto err;
450 if (gspca_dev->last_packet_type == LAST_PACKET)
451 gspca_frame_add(gspca_dev, FIRST_PACKET,
452 sd->jpeg_hdr, JPEG_HDR_SZ);
453 add_packet(gspca_dev, data + 2, len - 2);
454 return;
456 err:
457 gspca_dev->last_packet_type = DISCARD_PACKET;
460 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
462 struct sd *sd = (struct sd *) gspca_dev;
464 sd->brightness = val;
465 if (gspca_dev->streaming)
466 setbrightness(gspca_dev);
467 return gspca_dev->usb_err;
470 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
472 struct sd *sd = (struct sd *) gspca_dev;
474 *val = sd->brightness;
475 return 0;
478 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
480 struct sd *sd = (struct sd *) gspca_dev;
482 sd->contrast = val;
483 if (gspca_dev->streaming)
484 setcontrast(gspca_dev);
485 return gspca_dev->usb_err;
488 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
490 struct sd *sd = (struct sd *) gspca_dev;
492 *val = sd->contrast;
493 return 0;
496 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
498 struct sd *sd = (struct sd *) gspca_dev;
500 sd->hue = val;
501 if (gspca_dev->streaming)
502 sethue(gspca_dev);
503 return gspca_dev->usb_err;
506 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
508 struct sd *sd = (struct sd *) gspca_dev;
510 *val = sd->hue;
511 return 0;
514 static int sd_setcolor(struct gspca_dev *gspca_dev, __s32 val)
516 struct sd *sd = (struct sd *) gspca_dev;
518 sd->color = val;
519 if (gspca_dev->streaming)
520 setcolor(gspca_dev);
521 return gspca_dev->usb_err;
524 static int sd_getcolor(struct gspca_dev *gspca_dev, __s32 *val)
526 struct sd *sd = (struct sd *) gspca_dev;
528 *val = sd->color;
529 return 0;
532 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
534 struct sd *sd = (struct sd *) gspca_dev;
536 sd->sharpness = val;
537 if (gspca_dev->streaming)
538 setsharpness(gspca_dev);
539 return gspca_dev->usb_err;
542 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
544 struct sd *sd = (struct sd *) gspca_dev;
546 *val = sd->sharpness;
547 return 0;
550 /* sub-driver description */
551 static const struct sd_desc sd_desc = {
552 .name = MODULE_NAME,
553 .ctrls = sd_ctrls,
554 .nctrls = ARRAY_SIZE(sd_ctrls),
555 .config = sd_config,
556 .init = sd_init,
557 .isoc_init = sd_isoc_init,
558 .start = sd_start,
559 .stopN = sd_stopN,
560 .pkt_scan = sd_pkt_scan,
563 /* -- module initialisation -- */
564 static const struct usb_device_id device_table[] = {
565 {USB_DEVICE(0x04fc, 0x1528)},
568 MODULE_DEVICE_TABLE(usb, device_table);
570 /* -- device connect -- */
571 static int sd_probe(struct usb_interface *intf,
572 const struct usb_device_id *id)
574 /* the video interface for isochronous transfer is 1 */
575 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
576 return -ENODEV;
578 return gspca_dev_probe2(intf, id, &sd_desc, sizeof(struct sd),
579 THIS_MODULE);
582 static struct usb_driver sd_driver = {
583 .name = MODULE_NAME,
584 .id_table = device_table,
585 .probe = sd_probe,
586 .disconnect = gspca_disconnect,
587 #ifdef CONFIG_PM
588 .suspend = gspca_suspend,
589 .resume = gspca_resume,
590 #endif
593 module_usb_driver(sd_driver);