Antoine Jacquet | 8466028 | 2008-08-11 13:12:19 -0300 | [diff] [blame^] | 1 | /* |
| 2 | * DVB USB Linux driver for AME DTV-5100 USB2.0 DVB-T |
| 3 | * |
| 4 | * Copyright (C) 2008 Antoine Jacquet <royale@zerezo.com> |
| 5 | * http://royale.zerezo.com/dtv5100/ |
| 6 | * |
| 7 | * Inspired by gl861.c and au6610.c drivers |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include "dtv5100.h" |
| 25 | #include "qt1010.h" |
| 26 | |
| 27 | /* debug */ |
| 28 | static int dvb_usb_dtv5100_debug; |
| 29 | module_param_named(debug, dvb_usb_dtv5100_debug, int, 0644); |
| 30 | MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS); |
| 31 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 32 | |
| 33 | static int dtv5100_read_reg(struct dvb_usb_device *d, u8 addr, u8 reg, u8 *value) |
| 34 | { |
| 35 | return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), |
| 36 | DTV5100_I2C_READ, USB_TYPE_VENDOR|USB_DIR_IN, |
| 37 | 0, (addr << 8) + reg, value, 1, |
| 38 | DTV5100_USB_TIMEOUT); |
| 39 | } |
| 40 | |
| 41 | static int dtv5100_write_reg(struct dvb_usb_device *d, u8 addr, u8 reg, u8 value) |
| 42 | { |
| 43 | return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), |
| 44 | DTV5100_I2C_WRITE, USB_TYPE_VENDOR|USB_DIR_OUT, |
| 45 | value, (addr << 8) + reg, NULL, 0, |
| 46 | DTV5100_USB_TIMEOUT); |
| 47 | } |
| 48 | |
| 49 | /* I2C */ |
| 50 | static int dtv5100_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], |
| 51 | int num) |
| 52 | { |
| 53 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
| 54 | int i; |
| 55 | |
| 56 | if (num > 2) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 60 | return -EAGAIN; |
| 61 | |
| 62 | for (i = 0; i < num; i++) { |
| 63 | if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { |
| 64 | /* write/read request, 2 messages |
| 65 | * msg = { |
| 66 | * { reg }, |
| 67 | * { val }, |
| 68 | * } |
| 69 | */ |
| 70 | if (dtv5100_read_reg(d, msg[i].addr, msg[i].buf[0], msg[i+1].buf) < 0) |
| 71 | break; |
| 72 | i++; |
| 73 | } else { |
| 74 | /* write request, 1 message |
| 75 | * msg = { |
| 76 | * { reg, val }, |
| 77 | * } |
| 78 | */ |
| 79 | if (dtv5100_write_reg(d, msg[i].addr, msg[i].buf[0], msg[i].buf[1]) < 0) |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | mutex_unlock(&d->i2c_mutex); |
| 85 | return i; |
| 86 | } |
| 87 | |
| 88 | static u32 dtv5100_i2c_func(struct i2c_adapter *adapter) |
| 89 | { |
| 90 | return I2C_FUNC_I2C; |
| 91 | } |
| 92 | |
| 93 | static struct i2c_algorithm dtv5100_i2c_algo = { |
| 94 | .master_xfer = dtv5100_i2c_xfer, |
| 95 | .functionality = dtv5100_i2c_func, |
| 96 | }; |
| 97 | |
| 98 | /* Callbacks for DVB USB */ |
| 99 | static int dtv5100_frontend_attach(struct dvb_usb_adapter *adap) |
| 100 | { |
| 101 | adap->fe = dtv5100_fe_attach(); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static struct qt1010_config dtv5100_qt1010_config = { |
| 106 | .i2c_address = 0xc4 |
| 107 | }; |
| 108 | |
| 109 | static int dtv5100_tuner_attach(struct dvb_usb_adapter *adap) |
| 110 | { |
| 111 | return dvb_attach(qt1010_attach, |
| 112 | adap->fe, &adap->dev->i2c_adap, |
| 113 | &dtv5100_qt1010_config) == NULL ? -ENODEV : 0; |
| 114 | } |
| 115 | |
| 116 | /* DVB USB Driver stuff */ |
| 117 | static struct dvb_usb_device_properties dtv5100_properties; |
| 118 | |
| 119 | static int dtv5100_probe(struct usb_interface *intf, |
| 120 | const struct usb_device_id *id) |
| 121 | { |
| 122 | int i, ret; |
| 123 | struct usb_device *udev = interface_to_usbdev(intf); |
| 124 | |
| 125 | ret = dvb_usb_device_init(intf, &dtv5100_properties, |
| 126 | THIS_MODULE, NULL, adapter_nr); |
| 127 | if (ret) |
| 128 | return ret; |
| 129 | |
| 130 | /* initialize frontend & non qt1010 part? */ |
| 131 | for (i = 0; dtv5100_init[i].request; i++) |
| 132 | { |
| 133 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 134 | dtv5100_init[i].request, |
| 135 | USB_TYPE_VENDOR|USB_DIR_OUT, |
| 136 | dtv5100_init[i].value, |
| 137 | dtv5100_init[i].index, NULL, 0, |
| 138 | DTV5100_USB_TIMEOUT); |
| 139 | if (ret) |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static struct usb_device_id dtv5100_table [] = { |
| 147 | { USB_DEVICE(0x06be, 0xa232) }, |
| 148 | { } /* Terminating entry */ |
| 149 | }; |
| 150 | MODULE_DEVICE_TABLE(usb, dtv5100_table); |
| 151 | |
| 152 | static struct dvb_usb_device_properties dtv5100_properties = { |
| 153 | .caps = DVB_USB_IS_AN_I2C_ADAPTER, |
| 154 | .usb_ctrl = DEVICE_SPECIFIC, |
| 155 | |
| 156 | .size_of_priv = 0, |
| 157 | |
| 158 | .num_adapters = 1, |
| 159 | .adapter = {{ |
| 160 | .frontend_attach = dtv5100_frontend_attach, |
| 161 | .tuner_attach = dtv5100_tuner_attach, |
| 162 | |
| 163 | .stream = { |
| 164 | .type = USB_BULK, |
| 165 | .count = 8, |
| 166 | .endpoint = 0x82, |
| 167 | .u = { |
| 168 | .bulk = { |
| 169 | .buffersize = 4096, |
| 170 | } |
| 171 | } |
| 172 | }, |
| 173 | } }, |
| 174 | |
| 175 | .i2c_algo = &dtv5100_i2c_algo, |
| 176 | |
| 177 | .num_device_descs = 1, |
| 178 | .devices = { |
| 179 | { |
| 180 | .name = "AME DTV-5100 USB2.0 DVB-T", |
| 181 | .cold_ids = { NULL }, |
| 182 | .warm_ids = { &dtv5100_table[0], NULL }, |
| 183 | }, |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | static struct usb_driver dtv5100_driver = { |
| 188 | .name = "dvb_usb_dtv5100", |
| 189 | .probe = dtv5100_probe, |
| 190 | .disconnect = dvb_usb_device_exit, |
| 191 | .id_table = dtv5100_table, |
| 192 | }; |
| 193 | |
| 194 | /* module stuff */ |
| 195 | static int __init dtv5100_module_init(void) |
| 196 | { |
| 197 | int ret; |
| 198 | |
| 199 | ret = usb_register(&dtv5100_driver); |
| 200 | if (ret) |
| 201 | err("usb_register failed. Error number %d", ret); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static void __exit dtv5100_module_exit(void) |
| 207 | { |
| 208 | /* deregister this driver from the USB subsystem */ |
| 209 | usb_deregister(&dtv5100_driver); |
| 210 | } |
| 211 | |
| 212 | module_init(dtv5100_module_init); |
| 213 | module_exit(dtv5100_module_exit); |
| 214 | |
| 215 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 216 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 217 | MODULE_LICENSE("GPL"); |