Merge git://git.kernel.org/pub/scm/linux/kernel/git/joern/logfs
[sfrench/cifs-2.6.git] / drivers / media / video / gspca / stv06xx / stv06xx.c
1 /*
2  * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
3  *                    Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
4  * Copyright (c) 2002, 2003 Tuukka Toivonen
5  * Copyright (c) 2008 Erik Andrén
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * P/N 861037:      Sensor HDCS1000        ASIC STV0600
22  * P/N 861050-0010: Sensor HDCS1000        ASIC STV0600
23  * P/N 861050-0020: Sensor Photobit PB100  ASIC STV0600-1 - QuickCam Express
24  * P/N 861055:      Sensor ST VV6410       ASIC STV0610   - LEGO cam
25  * P/N 861075-0040: Sensor HDCS1000        ASIC
26  * P/N 961179-0700: Sensor ST VV6410       ASIC STV0602   - Dexxa WebCam USB
27  * P/N 861040-0000: Sensor ST VV6410       ASIC STV0610   - QuickCam Web
28  */
29
30 #include <linux/input.h>
31 #include "stv06xx_sensor.h"
32
33 MODULE_AUTHOR("Erik Andrén");
34 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
35 MODULE_LICENSE("GPL");
36
37 static int dump_bridge;
38 static int dump_sensor;
39
40 int stv06xx_write_bridge(struct sd *sd, u16 address, u16 i2c_data)
41 {
42         int err;
43         struct usb_device *udev = sd->gspca_dev.dev;
44         __u8 *buf = sd->gspca_dev.usb_buf;
45         u8 len = (i2c_data > 0xff) ? 2 : 1;
46
47         buf[0] = i2c_data & 0xff;
48         buf[1] = (i2c_data >> 8) & 0xff;
49
50         err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
51                               0x04, 0x40, address, 0, buf, len,
52                               STV06XX_URB_MSG_TIMEOUT);
53
54         PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d",
55                i2c_data, address, err);
56
57         return (err < 0) ? err : 0;
58 }
59
60 int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data)
61 {
62         int err;
63         struct usb_device *udev = sd->gspca_dev.dev;
64         __u8 *buf = sd->gspca_dev.usb_buf;
65
66         err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
67                               0x04, 0xc0, address, 0, buf, 1,
68                               STV06XX_URB_MSG_TIMEOUT);
69
70         *i2c_data = buf[0];
71
72         PDEBUG(D_CONF, "Reading 0x%x from address 0x%x, status %d",
73                *i2c_data, address, err);
74
75         return (err < 0) ? err : 0;
76 }
77
78 /* Wraps the normal write sensor bytes / words functions for writing a
79    single value */
80 int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value)
81 {
82         if (sd->sensor->i2c_len == 2) {
83                 u16 data[2] = { address, value };
84                 return stv06xx_write_sensor_words(sd, data, 1);
85         } else {
86                 u8 data[2] = { address, value };
87                 return stv06xx_write_sensor_bytes(sd, data, 1);
88         }
89 }
90
91 static int stv06xx_write_sensor_finish(struct sd *sd)
92 {
93         int err = 0;
94
95         if (sd->bridge == BRIDGE_STV610) {
96                 struct usb_device *udev = sd->gspca_dev.dev;
97                 __u8 *buf = sd->gspca_dev.usb_buf;
98
99                 buf[0] = 0;
100                 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
101                                       0x04, 0x40, 0x1704, 0, buf, 1,
102                                       STV06XX_URB_MSG_TIMEOUT);
103         }
104
105         return (err < 0) ? err : 0;
106 }
107
108 int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
109 {
110         int err, i, j;
111         struct usb_device *udev = sd->gspca_dev.dev;
112         __u8 *buf = sd->gspca_dev.usb_buf;
113
114         PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
115         for (i = 0; i < len;) {
116                 /* Build the command buffer */
117                 memset(buf, 0, I2C_BUFFER_LENGTH);
118                 for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) {
119                         buf[j] = data[2*i];
120                         buf[0x10 + j] = data[2*i+1];
121                         PDEBUG(D_CONF, "I2C: Writing 0x%02x to reg 0x%02x",
122                         data[2*i+1], data[2*i]);
123                 }
124                 buf[0x20] = sd->sensor->i2c_addr;
125                 buf[0x21] = j - 1; /* Number of commands to send - 1 */
126                 buf[0x22] = I2C_WRITE_CMD;
127                 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
128                                       0x04, 0x40, 0x0400, 0, buf,
129                                       I2C_BUFFER_LENGTH,
130                                       STV06XX_URB_MSG_TIMEOUT);
131                 if (err < 0)
132                         return err;
133         }
134         return stv06xx_write_sensor_finish(sd);
135 }
136
137 int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
138 {
139         int err, i, j;
140         struct usb_device *udev = sd->gspca_dev.dev;
141         __u8 *buf = sd->gspca_dev.usb_buf;
142
143         PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
144
145         for (i = 0; i < len;) {
146                 /* Build the command buffer */
147                 memset(buf, 0, I2C_BUFFER_LENGTH);
148                 for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) {
149                         buf[j] = data[2*i];
150                         buf[0x10 + j * 2] = data[2*i+1];
151                         buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8;
152                         PDEBUG(D_CONF, "I2C: Writing 0x%04x to reg 0x%02x",
153                                 data[2*i+1], data[2*i]);
154                 }
155                 buf[0x20] = sd->sensor->i2c_addr;
156                 buf[0x21] = j - 1; /* Number of commands to send - 1 */
157                 buf[0x22] = I2C_WRITE_CMD;
158                 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
159                                 0x04, 0x40, 0x0400, 0, buf,
160                                 I2C_BUFFER_LENGTH,
161                                 STV06XX_URB_MSG_TIMEOUT);
162                 if (err < 0)
163                         return err;
164         }
165         return stv06xx_write_sensor_finish(sd);
166 }
167
168 int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
169 {
170         int err;
171         struct usb_device *udev = sd->gspca_dev.dev;
172         __u8 *buf = sd->gspca_dev.usb_buf;
173
174         err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush);
175         if (err < 0)
176                 return err;
177
178         /* Clear mem */
179         memset(buf, 0, I2C_BUFFER_LENGTH);
180
181         buf[0] = address;
182         buf[0x20] = sd->sensor->i2c_addr;
183         buf[0x21] = 0;
184
185         /* Read I2C register */
186         buf[0x22] = I2C_READ_CMD;
187
188         err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
189                               0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH,
190                               STV06XX_URB_MSG_TIMEOUT);
191         if (err < 0) {
192                 PDEBUG(D_ERR, "I2C: Read error writing address: %d", err);
193                 return err;
194         }
195
196         err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
197                               0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len,
198                               STV06XX_URB_MSG_TIMEOUT);
199         if (sd->sensor->i2c_len == 2)
200                 *value = buf[0] | (buf[1] << 8);
201         else
202                 *value = buf[0];
203
204         PDEBUG(D_CONF, "I2C: Read 0x%x from address 0x%x, status: %d",
205                *value, address, err);
206
207         return (err < 0) ? err : 0;
208 }
209
210 /* Dumps all bridge registers */
211 static void stv06xx_dump_bridge(struct sd *sd)
212 {
213         int i;
214         u8 data, buf;
215
216         info("Dumping all stv06xx bridge registers");
217         for (i = 0x1400; i < 0x160f; i++) {
218                 stv06xx_read_bridge(sd, i, &data);
219
220                 info("Read 0x%x from address 0x%x", data, i);
221         }
222
223         info("Testing stv06xx bridge registers for writability");
224         for (i = 0x1400; i < 0x160f; i++) {
225                 stv06xx_read_bridge(sd, i, &data);
226                 buf = data;
227
228                 stv06xx_write_bridge(sd, i, 0xff);
229                 stv06xx_read_bridge(sd, i, &data);
230                 if (data == 0xff)
231                         info("Register 0x%x is read/write", i);
232                 else if (data != buf)
233                         info("Register 0x%x is read/write,"
234                              " but only partially", i);
235                 else
236                         info("Register 0x%x is read-only", i);
237
238                 stv06xx_write_bridge(sd, i, buf);
239         }
240 }
241
242 /* this function is called at probe and resume time */
243 static int stv06xx_init(struct gspca_dev *gspca_dev)
244 {
245         struct sd *sd = (struct sd *) gspca_dev;
246         int err;
247
248         PDEBUG(D_PROBE, "Initializing camera");
249
250         /* Let the usb init settle for a bit
251            before performing the initialization */
252         msleep(250);
253
254         err = sd->sensor->init(sd);
255
256         if (dump_sensor && sd->sensor->dump)
257                 sd->sensor->dump(sd);
258
259         return (err < 0) ? err : 0;
260 }
261
262 /* Start the camera */
263 static int stv06xx_start(struct gspca_dev *gspca_dev)
264 {
265         struct sd *sd = (struct sd *) gspca_dev;
266         int err;
267
268         /* Prepare the sensor for start */
269         err = sd->sensor->start(sd);
270         if (err < 0)
271                 goto out;
272
273         /* Start isochronous streaming */
274         err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
275
276 out:
277         if (err < 0)
278                 PDEBUG(D_STREAM, "Starting stream failed");
279         else
280                 PDEBUG(D_STREAM, "Started streaming");
281
282         return (err < 0) ? err : 0;
283 }
284
285 static void stv06xx_stopN(struct gspca_dev *gspca_dev)
286 {
287         int err;
288         struct sd *sd = (struct sd *) gspca_dev;
289
290         /* stop ISO-streaming */
291         err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
292         if (err < 0)
293                 goto out;
294
295         err = sd->sensor->stop(sd);
296
297 out:
298         if (err < 0)
299                 PDEBUG(D_STREAM, "Failed to stop stream");
300         else
301                 PDEBUG(D_STREAM, "Stopped streaming");
302 }
303
304 /*
305  * Analyse an USB packet of the data stream and store it appropriately.
306  * Each packet contains an integral number of chunks. Each chunk has
307  * 2-bytes identification, followed by 2-bytes that describe the chunk
308  * length. Known/guessed chunk identifications are:
309  * 8001/8005/C001/C005 - Begin new frame
310  * 8002/8006/C002/C006 - End frame
311  * 0200/4200           - Contains actual image data, bayer or compressed
312  * 0005                - 11 bytes of unknown data
313  * 0100                - 2 bytes of unknown data
314  * The 0005 and 0100 chunks seem to appear only in compressed stream.
315  */
316 static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev,
317                         u8 *data,                       /* isoc packet */
318                         int len)                        /* iso packet length */
319 {
320         struct sd *sd = (struct sd *) gspca_dev;
321
322         PDEBUG(D_PACK, "Packet of length %d arrived", len);
323
324         /* A packet may contain several frames
325            loop until the whole packet is reached */
326         while (len) {
327                 int id, chunk_len;
328
329                 if (len < 4) {
330                         PDEBUG(D_PACK, "Packet is smaller than 4 bytes");
331                         return;
332                 }
333
334                 /* Capture the id */
335                 id = (data[0] << 8) | data[1];
336
337                 /* Capture the chunk length */
338                 chunk_len = (data[2] << 8) | data[3];
339                 PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len);
340
341                 data += 4;
342                 len -= 4;
343
344                 if (len < chunk_len) {
345                         PDEBUG(D_ERR, "URB packet length is smaller"
346                                 " than the specified chunk length");
347                         gspca_dev->last_packet_type = DISCARD_PACKET;
348                         return;
349                 }
350
351                 /* First byte seem to be 02=data 2nd byte is unknown??? */
352                 if (sd->bridge == BRIDGE_ST6422 && (id & 0xFF00) == 0x0200)
353                         goto frame_data;
354
355                 switch (id) {
356                 case 0x0200:
357                 case 0x4200:
358 frame_data:
359                         PDEBUG(D_PACK, "Frame data packet detected");
360
361                         if (sd->to_skip) {
362                                 int skip = (sd->to_skip < chunk_len) ?
363                                             sd->to_skip : chunk_len;
364                                 data += skip;
365                                 len -= skip;
366                                 chunk_len -= skip;
367                                 sd->to_skip -= skip;
368                         }
369
370                         gspca_frame_add(gspca_dev, INTER_PACKET,
371                                         data, chunk_len);
372                         break;
373
374                 case 0x8001:
375                 case 0x8005:
376                 case 0xc001:
377                 case 0xc005:
378                         PDEBUG(D_PACK, "Starting new frame");
379
380                         /* Create a new frame, chunk length should be zero */
381                         gspca_frame_add(gspca_dev, FIRST_PACKET,
382                                         NULL, 0);
383
384                         if (sd->bridge == BRIDGE_ST6422)
385                                 sd->to_skip = gspca_dev->width * 4;
386
387                         if (chunk_len)
388                                 PDEBUG(D_ERR, "Chunk length is "
389                                               "non-zero on a SOF");
390                         break;
391
392                 case 0x8002:
393                 case 0x8006:
394                 case 0xc002:
395                         PDEBUG(D_PACK, "End of frame detected");
396
397                         /* Complete the last frame (if any) */
398                         gspca_frame_add(gspca_dev, LAST_PACKET,
399                                         NULL, 0);
400
401                         if (chunk_len)
402                                 PDEBUG(D_ERR, "Chunk length is "
403                                               "non-zero on a EOF");
404                         break;
405
406                 case 0x0005:
407                         PDEBUG(D_PACK, "Chunk 0x005 detected");
408                         /* Unknown chunk with 11 bytes of data,
409                            occurs just before end of each frame
410                            in compressed mode */
411                         break;
412
413                 case 0x0100:
414                         PDEBUG(D_PACK, "Chunk 0x0100 detected");
415                         /* Unknown chunk with 2 bytes of data,
416                            occurs 2-3 times per USB interrupt */
417                         break;
418                 case 0x42ff:
419                         PDEBUG(D_PACK, "Chunk 0x42ff detected");
420                         /* Special chunk seen sometimes on the ST6422 */
421                         break;
422                 default:
423                         PDEBUG(D_PACK, "Unknown chunk 0x%04x detected", id);
424                         /* Unknown chunk */
425                 }
426                 data    += chunk_len;
427                 len     -= chunk_len;
428         }
429 }
430
431 #ifdef CONFIG_INPUT
432 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
433                         u8 *data,               /* interrupt packet data */
434                         int len)                /* interrupt packet length */
435 {
436         int ret = -EINVAL;
437
438         if (len == 1 && data[0] == 0x80) {
439                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
440                 input_sync(gspca_dev->input_dev);
441                 ret = 0;
442         }
443
444         if (len == 1 && data[0] == 0x88) {
445                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
446                 input_sync(gspca_dev->input_dev);
447                 ret = 0;
448         }
449
450         return ret;
451 }
452 #endif
453
454 static int stv06xx_config(struct gspca_dev *gspca_dev,
455                           const struct usb_device_id *id);
456
457 /* sub-driver description */
458 static const struct sd_desc sd_desc = {
459         .name = MODULE_NAME,
460         .config = stv06xx_config,
461         .init = stv06xx_init,
462         .start = stv06xx_start,
463         .stopN = stv06xx_stopN,
464         .pkt_scan = stv06xx_pkt_scan,
465 #ifdef CONFIG_INPUT
466         .int_pkt_scan = sd_int_pkt_scan,
467 #endif
468 };
469
470 /* This function is called at probe time */
471 static int stv06xx_config(struct gspca_dev *gspca_dev,
472                           const struct usb_device_id *id)
473 {
474         struct sd *sd = (struct sd *) gspca_dev;
475         struct cam *cam;
476
477         PDEBUG(D_PROBE, "Configuring camera");
478
479         cam = &gspca_dev->cam;
480         sd->desc = sd_desc;
481         sd->bridge = id->driver_info;
482         gspca_dev->sd_desc = &sd->desc;
483
484         if (dump_bridge)
485                 stv06xx_dump_bridge(sd);
486
487         sd->sensor = &stv06xx_sensor_st6422;
488         if (!sd->sensor->probe(sd))
489                 return 0;
490
491         sd->sensor = &stv06xx_sensor_vv6410;
492         if (!sd->sensor->probe(sd))
493                 return 0;
494
495         sd->sensor = &stv06xx_sensor_hdcs1x00;
496         if (!sd->sensor->probe(sd))
497                 return 0;
498
499         sd->sensor = &stv06xx_sensor_hdcs1020;
500         if (!sd->sensor->probe(sd))
501                 return 0;
502
503         sd->sensor = &stv06xx_sensor_pb0100;
504         if (!sd->sensor->probe(sd))
505                 return 0;
506
507         sd->sensor = NULL;
508         return -ENODEV;
509 }
510
511
512
513 /* -- module initialisation -- */
514 static const __devinitdata struct usb_device_id device_table[] = {
515         /* QuickCam Express */
516         {USB_DEVICE(0x046d, 0x0840), .driver_info = BRIDGE_STV600 },
517         /* LEGO cam / QuickCam Web */
518         {USB_DEVICE(0x046d, 0x0850), .driver_info = BRIDGE_STV610 },
519         /* Dexxa WebCam USB */
520         {USB_DEVICE(0x046d, 0x0870), .driver_info = BRIDGE_STV602 },
521         /* QuickCam Messenger */
522         {USB_DEVICE(0x046D, 0x08F0), .driver_info = BRIDGE_ST6422 },
523         /* QuickCam Communicate */
524         {USB_DEVICE(0x046D, 0x08F5), .driver_info = BRIDGE_ST6422 },
525         /* QuickCam Messenger (new) */
526         {USB_DEVICE(0x046D, 0x08F6), .driver_info = BRIDGE_ST6422 },
527         {}
528 };
529 MODULE_DEVICE_TABLE(usb, device_table);
530
531 /* -- device connect -- */
532 static int sd_probe(struct usb_interface *intf,
533                         const struct usb_device_id *id)
534 {
535         PDEBUG(D_PROBE, "Probing for a stv06xx device");
536         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
537                                THIS_MODULE);
538 }
539
540 static void sd_disconnect(struct usb_interface *intf)
541 {
542         struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
543         struct sd *sd = (struct sd *) gspca_dev;
544         PDEBUG(D_PROBE, "Disconnecting the stv06xx device");
545
546         if (sd->sensor->disconnect)
547                 sd->sensor->disconnect(sd);
548         gspca_disconnect(intf);
549 }
550
551 static struct usb_driver sd_driver = {
552         .name = MODULE_NAME,
553         .id_table = device_table,
554         .probe = sd_probe,
555         .disconnect = sd_disconnect,
556 #ifdef CONFIG_PM
557         .suspend = gspca_suspend,
558         .resume = gspca_resume,
559 #endif
560 };
561
562 /* -- module insert / remove -- */
563 static int __init sd_mod_init(void)
564 {
565         int ret;
566         ret = usb_register(&sd_driver);
567         if (ret < 0)
568                 return ret;
569         PDEBUG(D_PROBE, "registered");
570         return 0;
571 }
572 static void __exit sd_mod_exit(void)
573 {
574         usb_deregister(&sd_driver);
575         PDEBUG(D_PROBE, "deregistered");
576 }
577
578 module_init(sd_mod_init);
579 module_exit(sd_mod_exit);
580
581 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
582 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
583
584 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
585 MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");