1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DVB USB Linux driver for AME DTV-5100 USB2.0 DVB-T 4 * 5 * Copyright (C) 2008 Antoine Jacquet <royale@zerezo.com> 6 * http://royale.zerezo.com/dtv5100/ 7 * 8 * Inspired by gl861.c and au6610.c drivers 9 */ 10 11 #include "dtv5100.h" 12 #include "zl10353.h" 13 #include "qt1010.h" 14 15 /* debug */ 16 static int dvb_usb_dtv5100_debug; 17 module_param_named(debug, dvb_usb_dtv5100_debug, int, 0644); 18 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS); 19 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 20 21 struct dtv5100_state { 22 unsigned char data[80]; 23 }; 24 25 static int dtv5100_i2c_msg(struct dvb_usb_device *d, u8 addr, 26 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) 27 { 28 struct dtv5100_state *st = d->priv; 29 unsigned int pipe; 30 u8 request; 31 u8 type; 32 u16 value; 33 u16 index; 34 35 switch (wlen) { 36 case 1: 37 /* write { reg }, read { value } */ 38 pipe = usb_rcvctrlpipe(d->udev, 0); 39 request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_READ : 40 DTV5100_TUNER_READ); 41 type = USB_TYPE_VENDOR | USB_DIR_IN; 42 value = 0; 43 break; 44 case 2: 45 /* write { reg, value } */ 46 pipe = usb_sndctrlpipe(d->udev, 0); 47 request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_WRITE : 48 DTV5100_TUNER_WRITE); 49 type = USB_TYPE_VENDOR | USB_DIR_OUT; 50 value = wbuf[1]; 51 break; 52 default: 53 warn("wlen = %x, aborting.", wlen); 54 return -EINVAL; 55 } 56 index = (addr << 8) + wbuf[0]; 57 58 if (rlen > sizeof(st->data)) { 59 warn("rlen = %x is too big!\n", rlen); 60 return -EINVAL; 61 } 62 63 memcpy(st->data, rbuf, rlen); 64 msleep(1); /* avoid I2C errors */ 65 return usb_control_msg(d->udev, pipe, request, 66 type, value, index, st->data, rlen, 67 DTV5100_USB_TIMEOUT); 68 } 69 70 /* I2C */ 71 static int dtv5100_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 72 int num) 73 { 74 struct dvb_usb_device *d = i2c_get_adapdata(adap); 75 int i; 76 77 if (num > 2) 78 return -EINVAL; 79 80 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 81 return -EAGAIN; 82 83 for (i = 0; i < num; i++) { 84 /* write/read request */ 85 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { 86 if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf, 87 msg[i].len, msg[i+1].buf, 88 msg[i+1].len) < 0) 89 break; 90 i++; 91 } else if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf, 92 msg[i].len, NULL, 0) < 0) 93 break; 94 } 95 96 mutex_unlock(&d->i2c_mutex); 97 return i; 98 } 99 100 static u32 dtv5100_i2c_func(struct i2c_adapter *adapter) 101 { 102 return I2C_FUNC_I2C; 103 } 104 105 static const struct i2c_algorithm dtv5100_i2c_algo = { 106 .master_xfer = dtv5100_i2c_xfer, 107 .functionality = dtv5100_i2c_func, 108 }; 109 110 /* Callbacks for DVB USB */ 111 static struct zl10353_config dtv5100_zl10353_config = { 112 .demod_address = DTV5100_DEMOD_ADDR, 113 .no_tuner = 1, 114 .parallel_ts = 1, 115 }; 116 117 static int dtv5100_frontend_attach(struct dvb_usb_adapter *adap) 118 { 119 adap->fe_adap[0].fe = dvb_attach(zl10353_attach, &dtv5100_zl10353_config, 120 &adap->dev->i2c_adap); 121 if (adap->fe_adap[0].fe == NULL) 122 return -EIO; 123 124 /* disable i2c gate, or it won't work... is this safe? */ 125 adap->fe_adap[0].fe->ops.i2c_gate_ctrl = NULL; 126 127 return 0; 128 } 129 130 static struct qt1010_config dtv5100_qt1010_config = { 131 .i2c_address = DTV5100_TUNER_ADDR 132 }; 133 134 static int dtv5100_tuner_attach(struct dvb_usb_adapter *adap) 135 { 136 return dvb_attach(qt1010_attach, 137 adap->fe_adap[0].fe, &adap->dev->i2c_adap, 138 &dtv5100_qt1010_config) == NULL ? -ENODEV : 0; 139 } 140 141 /* DVB USB Driver stuff */ 142 static struct dvb_usb_device_properties dtv5100_properties; 143 144 static int dtv5100_probe(struct usb_interface *intf, 145 const struct usb_device_id *id) 146 { 147 int i, ret; 148 struct usb_device *udev = interface_to_usbdev(intf); 149 150 /* initialize non qt1010/zl10353 part? */ 151 for (i = 0; dtv5100_init[i].request; i++) { 152 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 153 dtv5100_init[i].request, 154 USB_TYPE_VENDOR | USB_DIR_OUT, 155 dtv5100_init[i].value, 156 dtv5100_init[i].index, NULL, 0, 157 DTV5100_USB_TIMEOUT); 158 if (ret) 159 return ret; 160 } 161 162 ret = dvb_usb_device_init(intf, &dtv5100_properties, 163 THIS_MODULE, NULL, adapter_nr); 164 if (ret) 165 return ret; 166 167 return 0; 168 } 169 170 enum { 171 AME_DTV5100, 172 }; 173 174 static const struct usb_device_id dtv5100_table[] = { 175 DVB_USB_DEV(AME, AME_DTV5100), 176 { } 177 }; 178 179 MODULE_DEVICE_TABLE(usb, dtv5100_table); 180 181 static struct dvb_usb_device_properties dtv5100_properties = { 182 .caps = DVB_USB_IS_AN_I2C_ADAPTER, 183 .usb_ctrl = DEVICE_SPECIFIC, 184 185 .size_of_priv = sizeof(struct dtv5100_state), 186 187 .num_adapters = 1, 188 .adapter = {{ 189 .num_frontends = 1, 190 .fe = {{ 191 .frontend_attach = dtv5100_frontend_attach, 192 .tuner_attach = dtv5100_tuner_attach, 193 194 .stream = { 195 .type = USB_BULK, 196 .count = 8, 197 .endpoint = 0x82, 198 .u = { 199 .bulk = { 200 .buffersize = 4096, 201 } 202 } 203 }, 204 }}, 205 } }, 206 207 .i2c_algo = &dtv5100_i2c_algo, 208 209 .num_device_descs = 1, 210 .devices = { 211 { 212 .name = "AME DTV-5100 USB2.0 DVB-T", 213 .cold_ids = { NULL }, 214 .warm_ids = { &dtv5100_table[AME_DTV5100], NULL }, 215 }, 216 } 217 }; 218 219 static struct usb_driver dtv5100_driver = { 220 .name = "dvb_usb_dtv5100", 221 .probe = dtv5100_probe, 222 .disconnect = dvb_usb_device_exit, 223 .id_table = dtv5100_table, 224 }; 225 226 module_usb_driver(dtv5100_driver); 227 228 MODULE_AUTHOR(DRIVER_AUTHOR); 229 MODULE_DESCRIPTION(DRIVER_DESC); 230 MODULE_LICENSE("GPL"); 231