1 /* 2 * USB Driver for ALi m5602 based webcams 3 * 4 * Copyright (C) 2008 Erik Andrén 5 * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project. 6 * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br> 7 * 8 * Portions of code to USB interface and ALi driver software, 9 * Copyright (c) 2006 Willem Duinker 10 * v4l2 interface modeled after the V4L2 driver 11 * for SN9C10x PC Camera Controllers 12 * 13 * This program is free software; you can redistribute it and/or 14 * modify it under the terms of the GNU General Public License as 15 * published by the Free Software Foundation, version 2. 16 * 17 */ 18 19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20 21#include "m5602_ov9650.h" 22#include "m5602_ov7660.h" 23#include "m5602_mt9m111.h" 24#include "m5602_po1030.h" 25#include "m5602_s5k83a.h" 26#include "m5602_s5k4aa.h" 27 28/* Kernel module parameters */ 29int force_sensor; 30static bool dump_bridge; 31bool dump_sensor; 32 33static const struct usb_device_id m5602_table[] = { 34 {USB_DEVICE(0x0402, 0x5602)}, 35 {} 36}; 37 38MODULE_DEVICE_TABLE(usb, m5602_table); 39 40/* Reads a byte from the m5602 */ 41int m5602_read_bridge(struct sd *sd, const u8 address, u8 *i2c_data) 42{ 43 int err; 44 struct gspca_dev *gspca_dev = (struct gspca_dev *) sd; 45 struct usb_device *udev = sd->gspca_dev.dev; 46 __u8 *buf = sd->gspca_dev.usb_buf; 47 48 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 49 0x04, 0xc0, 0x14, 50 0x8100 + address, buf, 51 1, M5602_URB_MSG_TIMEOUT); 52 *i2c_data = buf[0]; 53 54 PDEBUG(D_CONF, "Reading bridge register 0x%x containing 0x%x", 55 address, *i2c_data); 56 57 /* usb_control_msg(...) returns the number of bytes sent upon success, 58 mask that and return zero instead*/ 59 return (err < 0) ? err : 0; 60} 61 62/* Writes a byte to the m5602 */ 63int m5602_write_bridge(struct sd *sd, const u8 address, const u8 i2c_data) 64{ 65 int err; 66 struct gspca_dev *gspca_dev = (struct gspca_dev *) sd; 67 struct usb_device *udev = sd->gspca_dev.dev; 68 __u8 *buf = sd->gspca_dev.usb_buf; 69 70 PDEBUG(D_CONF, "Writing bridge register 0x%x with 0x%x", 71 address, i2c_data); 72 73 memcpy(buf, bridge_urb_skeleton, 74 sizeof(bridge_urb_skeleton)); 75 buf[1] = address; 76 buf[3] = i2c_data; 77 78 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 79 0x04, 0x40, 0x19, 80 0x0000, buf, 81 4, M5602_URB_MSG_TIMEOUT); 82 83 /* usb_control_msg(...) returns the number of bytes sent upon success, 84 mask that and return zero instead */ 85 return (err < 0) ? err : 0; 86} 87 88static int m5602_wait_for_i2c(struct sd *sd) 89{ 90 int err; 91 u8 data; 92 93 do { 94 err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, &data); 95 } while ((data & I2C_BUSY) && !err); 96 return err; 97} 98 99int m5602_read_sensor(struct sd *sd, const u8 address, 100 u8 *i2c_data, const u8 len) 101{ 102 int err, i; 103 struct gspca_dev *gspca_dev = (struct gspca_dev *) sd; 104 105 if (!len || len > sd->sensor->i2c_regW) 106 return -EINVAL; 107 108 err = m5602_wait_for_i2c(sd); 109 if (err < 0) 110 return err; 111 112 err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR, 113 sd->sensor->i2c_slave_id); 114 if (err < 0) 115 return err; 116 117 err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address); 118 if (err < 0) 119 return err; 120 121 /* Sensors with registers that are of only 122 one byte width are differently read */ 123 124 /* FIXME: This works with the ov9650, but has issues with the po1030 */ 125 if (sd->sensor->i2c_regW == 1) { 126 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 1); 127 if (err < 0) 128 return err; 129 130 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08); 131 } else { 132 err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len); 133 } 134 135 for (i = 0; (i < len) && !err; i++) { 136 err = m5602_wait_for_i2c(sd); 137 if (err < 0) 138 return err; 139 140 err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i])); 141 142 PDEBUG(D_CONF, "Reading sensor register " 143 "0x%x containing 0x%x ", address, *i2c_data); 144 } 145 return err; 146} 147 148int m5602_write_sensor(struct sd *sd, const u8 address, 149 u8 *i2c_data, const u8 len) 150{ 151 int err, i; 152 u8 *p; 153 struct gspca_dev *gspca_dev = (struct gspca_dev *) sd; 154 struct usb_device *udev = sd->gspca_dev.dev; 155 __u8 *buf = sd->gspca_dev.usb_buf; 156 157 /* No sensor with a data width larger than 16 bits has yet been seen */ 158 if (len > sd->sensor->i2c_regW || !len) 159 return -EINVAL; 160 161 memcpy(buf, sensor_urb_skeleton, 162 sizeof(sensor_urb_skeleton)); 163 164 buf[11] = sd->sensor->i2c_slave_id; 165 buf[15] = address; 166 167 /* Special case larger sensor writes */ 168 p = buf + 16; 169 170 /* Copy a four byte write sequence for each byte to be written to */ 171 for (i = 0; i < len; i++) { 172 memcpy(p, sensor_urb_skeleton + 16, 4); 173 p[3] = i2c_data[i]; 174 p += 4; 175 PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x", 176 address, i2c_data[i]); 177 } 178 179 /* Copy the tailer */ 180 memcpy(p, sensor_urb_skeleton + 20, 4); 181 182 /* Set the total length */ 183 p[3] = 0x10 + len; 184 185 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 186 0x04, 0x40, 0x19, 187 0x0000, buf, 188 20 + len * 4, M5602_URB_MSG_TIMEOUT); 189 190 return (err < 0) ? err : 0; 191} 192 193/* Dump all the registers of the m5602 bridge, 194 unfortunately this breaks the camera until it's power cycled */ 195static void m5602_dump_bridge(struct sd *sd) 196{ 197 int i; 198 for (i = 0; i < 0x80; i++) { 199 unsigned char val = 0; 200 m5602_read_bridge(sd, i, &val); 201 pr_info("ALi m5602 address 0x%x contains 0x%x\n", i, val); 202 } 203 pr_info("Warning: The ALi m5602 webcam probably won't work until it's power cycled\n"); 204} 205 206static int m5602_probe_sensor(struct sd *sd) 207{ 208 /* Try the po1030 */ 209 sd->sensor = &po1030; 210 if (!sd->sensor->probe(sd)) 211 return 0; 212 213 /* Try the mt9m111 sensor */ 214 sd->sensor = &mt9m111; 215 if (!sd->sensor->probe(sd)) 216 return 0; 217 218 /* Try the s5k4aa */ 219 sd->sensor = &s5k4aa; 220 if (!sd->sensor->probe(sd)) 221 return 0; 222 223 /* Try the ov9650 */ 224 sd->sensor = &ov9650; 225 if (!sd->sensor->probe(sd)) 226 return 0; 227 228 /* Try the ov7660 */ 229 sd->sensor = &ov7660; 230 if (!sd->sensor->probe(sd)) 231 return 0; 232 233 /* Try the s5k83a */ 234 sd->sensor = &s5k83a; 235 if (!sd->sensor->probe(sd)) 236 return 0; 237 238 /* More sensor probe function goes here */ 239 pr_info("Failed to find a sensor\n"); 240 sd->sensor = NULL; 241 return -ENODEV; 242} 243 244static int m5602_configure(struct gspca_dev *gspca_dev, 245 const struct usb_device_id *id); 246 247static int m5602_init(struct gspca_dev *gspca_dev) 248{ 249 struct sd *sd = (struct sd *) gspca_dev; 250 int err; 251 252 PDEBUG(D_CONF, "Initializing ALi m5602 webcam"); 253 /* Run the init sequence */ 254 err = sd->sensor->init(sd); 255 256 return err; 257} 258 259static int m5602_init_controls(struct gspca_dev *gspca_dev) 260{ 261 struct sd *sd = (struct sd *) gspca_dev; 262 263 if (!sd->sensor->init_controls) 264 return 0; 265 266 return sd->sensor->init_controls(sd); 267} 268 269static int m5602_start_transfer(struct gspca_dev *gspca_dev) 270{ 271 struct sd *sd = (struct sd *) gspca_dev; 272 __u8 *buf = sd->gspca_dev.usb_buf; 273 int err; 274 275 /* Send start command to the camera */ 276 const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01}; 277 278 if (sd->sensor->start) 279 sd->sensor->start(sd); 280 281 memcpy(buf, buffer, sizeof(buffer)); 282 err = usb_control_msg(gspca_dev->dev, 283 usb_sndctrlpipe(gspca_dev->dev, 0), 284 0x04, 0x40, 0x19, 0x0000, buf, 285 sizeof(buffer), M5602_URB_MSG_TIMEOUT); 286 287 PDEBUG(D_STREAM, "Transfer started"); 288 return (err < 0) ? err : 0; 289} 290 291static void m5602_urb_complete(struct gspca_dev *gspca_dev, 292 u8 *data, int len) 293{ 294 struct sd *sd = (struct sd *) gspca_dev; 295 296 if (len < 6) { 297 PDEBUG(D_PACK, "Packet is less than 6 bytes"); 298 return; 299 } 300 301 /* Frame delimiter: ff xx xx xx ff ff */ 302 if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff && 303 data[2] != sd->frame_id) { 304 PDEBUG(D_FRAM, "Frame delimiter detected"); 305 sd->frame_id = data[2]; 306 307 /* Remove the extra fluff appended on each header */ 308 data += 6; 309 len -= 6; 310 311 /* Complete the last frame (if any) */ 312 gspca_frame_add(gspca_dev, LAST_PACKET, 313 NULL, 0); 314 sd->frame_count++; 315 316 /* Create a new frame */ 317 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len); 318 319 PDEBUG(D_FRAM, "Starting new frame %d", 320 sd->frame_count); 321 322 } else { 323 int cur_frame_len; 324 325 cur_frame_len = gspca_dev->image_len; 326 /* Remove urb header */ 327 data += 4; 328 len -= 4; 329 330 if (cur_frame_len + len <= gspca_dev->frsz) { 331 PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes", 332 sd->frame_count, len); 333 334 gspca_frame_add(gspca_dev, INTER_PACKET, 335 data, len); 336 } else { 337 /* Add the remaining data up to frame size */ 338 gspca_frame_add(gspca_dev, INTER_PACKET, data, 339 gspca_dev->frsz - cur_frame_len); 340 } 341 } 342} 343 344static void m5602_stop_transfer(struct gspca_dev *gspca_dev) 345{ 346 struct sd *sd = (struct sd *) gspca_dev; 347 348 /* Run the sensor specific end transfer sequence */ 349 if (sd->sensor->stop) 350 sd->sensor->stop(sd); 351} 352 353/* sub-driver description */ 354static const struct sd_desc sd_desc = { 355 .name = MODULE_NAME, 356 .config = m5602_configure, 357 .init = m5602_init, 358 .init_controls = m5602_init_controls, 359 .start = m5602_start_transfer, 360 .stopN = m5602_stop_transfer, 361 .pkt_scan = m5602_urb_complete 362}; 363 364/* this function is called at probe time */ 365static int m5602_configure(struct gspca_dev *gspca_dev, 366 const struct usb_device_id *id) 367{ 368 struct sd *sd = (struct sd *) gspca_dev; 369 struct cam *cam; 370 int err; 371 372 cam = &gspca_dev->cam; 373 374 if (dump_bridge) 375 m5602_dump_bridge(sd); 376 377 /* Probe sensor */ 378 err = m5602_probe_sensor(sd); 379 if (err) 380 goto fail; 381 382 return 0; 383 384fail: 385 PERR("ALi m5602 webcam failed"); 386 cam->cam_mode = NULL; 387 cam->nmodes = 0; 388 389 return err; 390} 391 392static int m5602_probe(struct usb_interface *intf, 393 const struct usb_device_id *id) 394{ 395 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), 396 THIS_MODULE); 397} 398 399static void m5602_disconnect(struct usb_interface *intf) 400{ 401 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 402 struct sd *sd = (struct sd *) gspca_dev; 403 404 if (sd->sensor->disconnect) 405 sd->sensor->disconnect(sd); 406 407 gspca_disconnect(intf); 408} 409 410static struct usb_driver sd_driver = { 411 .name = MODULE_NAME, 412 .id_table = m5602_table, 413 .probe = m5602_probe, 414#ifdef CONFIG_PM 415 .suspend = gspca_suspend, 416 .resume = gspca_resume, 417 .reset_resume = gspca_resume, 418#endif 419 .disconnect = m5602_disconnect 420}; 421 422module_usb_driver(sd_driver); 423 424MODULE_AUTHOR(DRIVER_AUTHOR); 425MODULE_DESCRIPTION(DRIVER_DESC); 426MODULE_LICENSE("GPL"); 427module_param(force_sensor, int, S_IRUGO | S_IWUSR); 428MODULE_PARM_DESC(force_sensor, 429 "forces detection of a sensor, " 430 "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, " 431 "4 = MT9M111, 5 = PO1030, 6 = OV7660"); 432 433module_param(dump_bridge, bool, S_IRUGO | S_IWUSR); 434MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup"); 435 436module_param(dump_sensor, bool, S_IRUGO | S_IWUSR); 437MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers " 438 "at startup providing a sensor is found"); 439