cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

m5602_core.c (11158B)


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