Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1 | /* |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 2 | cx231xx-core.c - driver for Conexant Cx23100/101/102 |
| 3 | USB video capture devices |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 4 | |
| 5 | Copyright (C) 2008 <srinivasa.deevi at conexant dot com> |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 6 | Based on em28xx driver |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/usb.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <media/v4l2-common.h> |
| 29 | |
| 30 | #include "cx231xx.h" |
| 31 | #include "cx231xx-reg.h" |
| 32 | |
| 33 | /* #define ENABLE_DEBUG_ISOC_FRAMES */ |
| 34 | |
| 35 | static unsigned int core_debug; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 36 | module_param(core_debug, int, 0644); |
| 37 | MODULE_PARM_DESC(core_debug, "enable debug messages [core]"); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 38 | |
| 39 | #define cx231xx_coredbg(fmt, arg...) do {\ |
| 40 | if (core_debug) \ |
| 41 | printk(KERN_INFO "%s %s :"fmt, \ |
| 42 | dev->name, __func__ , ##arg); } while (0) |
| 43 | |
| 44 | static unsigned int reg_debug; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 45 | module_param(reg_debug, int, 0644); |
| 46 | MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]"); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 47 | |
| 48 | #define cx231xx_regdbg(fmt, arg...) do {\ |
| 49 | if (reg_debug) \ |
| 50 | printk(KERN_INFO "%s %s :"fmt, \ |
| 51 | dev->name, __func__ , ##arg); } while (0) |
| 52 | |
| 53 | static int alt = CX231XX_PINOUT; |
| 54 | module_param(alt, int, 0644); |
| 55 | MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint"); |
| 56 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 57 | #define cx231xx_isocdbg(fmt, arg...) do {\ |
| 58 | if (core_debug) \ |
| 59 | printk(KERN_INFO "%s %s :"fmt, \ |
| 60 | dev->name, __func__ , ##arg); } while (0) |
| 61 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 62 | /***************************************************************** |
| 63 | * Device control list functions * |
| 64 | ******************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 65 | |
| 66 | static LIST_HEAD(cx231xx_devlist); |
| 67 | static DEFINE_MUTEX(cx231xx_devlist_mutex); |
| 68 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 69 | /* |
| 70 | * cx231xx_realease_resources() |
| 71 | * unregisters the v4l2,i2c and usb devices |
| 72 | * called when the device gets disconected or at module unload |
| 73 | */ |
| 74 | void cx231xx_remove_from_devlist(struct cx231xx *dev) |
| 75 | { |
| 76 | mutex_lock(&cx231xx_devlist_mutex); |
| 77 | list_del(&dev->devlist); |
| 78 | mutex_unlock(&cx231xx_devlist_mutex); |
| 79 | }; |
| 80 | |
| 81 | void cx231xx_add_into_devlist(struct cx231xx *dev) |
| 82 | { |
| 83 | mutex_lock(&cx231xx_devlist_mutex); |
| 84 | list_add_tail(&dev->devlist, &cx231xx_devlist); |
| 85 | mutex_unlock(&cx231xx_devlist_mutex); |
| 86 | }; |
| 87 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 88 | static LIST_HEAD(cx231xx_extension_devlist); |
| 89 | static DEFINE_MUTEX(cx231xx_extension_devlist_lock); |
| 90 | |
| 91 | int cx231xx_register_extension(struct cx231xx_ops *ops) |
| 92 | { |
| 93 | struct cx231xx *dev = NULL; |
| 94 | |
| 95 | mutex_lock(&cx231xx_devlist_mutex); |
| 96 | mutex_lock(&cx231xx_extension_devlist_lock); |
| 97 | list_add_tail(&ops->next, &cx231xx_extension_devlist); |
| 98 | list_for_each_entry(dev, &cx231xx_devlist, devlist) { |
| 99 | if (dev) |
| 100 | ops->init(dev); |
| 101 | } |
Mauro Carvalho Chehab | 4be1ad3 | 2009-03-22 08:28:30 -0300 | [diff] [blame] | 102 | printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 103 | mutex_unlock(&cx231xx_extension_devlist_lock); |
| 104 | mutex_unlock(&cx231xx_devlist_mutex); |
| 105 | return 0; |
| 106 | } |
| 107 | EXPORT_SYMBOL(cx231xx_register_extension); |
| 108 | |
| 109 | void cx231xx_unregister_extension(struct cx231xx_ops *ops) |
| 110 | { |
| 111 | struct cx231xx *dev = NULL; |
| 112 | |
| 113 | mutex_lock(&cx231xx_devlist_mutex); |
| 114 | list_for_each_entry(dev, &cx231xx_devlist, devlist) { |
| 115 | if (dev) |
| 116 | ops->fini(dev); |
| 117 | } |
| 118 | |
| 119 | mutex_lock(&cx231xx_extension_devlist_lock); |
Mauro Carvalho Chehab | 4be1ad3 | 2009-03-22 08:28:30 -0300 | [diff] [blame] | 120 | printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 121 | list_del(&ops->next); |
| 122 | mutex_unlock(&cx231xx_extension_devlist_lock); |
| 123 | mutex_unlock(&cx231xx_devlist_mutex); |
| 124 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 125 | EXPORT_SYMBOL(cx231xx_unregister_extension); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 126 | |
| 127 | void cx231xx_init_extension(struct cx231xx *dev) |
| 128 | { |
| 129 | struct cx231xx_ops *ops = NULL; |
| 130 | |
| 131 | mutex_lock(&cx231xx_extension_devlist_lock); |
| 132 | if (!list_empty(&cx231xx_extension_devlist)) { |
| 133 | list_for_each_entry(ops, &cx231xx_extension_devlist, next) { |
| 134 | if (ops->init) |
| 135 | ops->init(dev); |
| 136 | } |
| 137 | } |
| 138 | mutex_unlock(&cx231xx_extension_devlist_lock); |
| 139 | } |
| 140 | |
| 141 | void cx231xx_close_extension(struct cx231xx *dev) |
| 142 | { |
| 143 | struct cx231xx_ops *ops = NULL; |
| 144 | |
| 145 | mutex_lock(&cx231xx_extension_devlist_lock); |
| 146 | if (!list_empty(&cx231xx_extension_devlist)) { |
| 147 | list_for_each_entry(ops, &cx231xx_extension_devlist, next) { |
| 148 | if (ops->fini) |
| 149 | ops->fini(dev); |
| 150 | } |
| 151 | } |
| 152 | mutex_unlock(&cx231xx_extension_devlist_lock); |
| 153 | } |
| 154 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 155 | /**************************************************************** |
| 156 | * U S B related functions * |
| 157 | *****************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 158 | int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 159 | struct cx231xx_i2c_xfer_data *req_data) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 160 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 161 | int status = 0; |
| 162 | struct cx231xx *dev = i2c_bus->dev; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 163 | struct VENDOR_REQUEST_IN ven_req; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 164 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 165 | u8 saddr_len = 0; |
| 166 | u8 _i2c_period = 0; |
| 167 | u8 _i2c_nostop = 0; |
| 168 | u8 _i2c_reserve = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 169 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 170 | /* Get the I2C period, nostop and reserve parameters */ |
| 171 | _i2c_period = i2c_bus->i2c_period; |
| 172 | _i2c_nostop = i2c_bus->i2c_nostop; |
| 173 | _i2c_reserve = i2c_bus->i2c_reserve; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 174 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 175 | saddr_len = req_data->saddr_len; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 176 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 177 | /* Set wValue */ |
| 178 | if (saddr_len == 1) /* need check saddr_len == 0 */ |
| 179 | ven_req.wValue = |
| 180 | req_data-> |
| 181 | dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 | |
| 182 | _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6; |
| 183 | else |
| 184 | ven_req.wValue = |
| 185 | req_data-> |
| 186 | dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 | |
| 187 | _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 188 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 189 | /* set channel number */ |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 190 | if (req_data->direction & I2C_M_RD) { |
| 191 | /* channel number, for read,spec required channel_num +4 */ |
| 192 | ven_req.bRequest = i2c_bus->nr + 4; |
| 193 | } else |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 194 | ven_req.bRequest = i2c_bus->nr; /* channel number, */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 195 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 196 | /* set index value */ |
| 197 | switch (saddr_len) { |
| 198 | case 0: |
| 199 | ven_req.wIndex = 0; /* need check */ |
| 200 | break; |
| 201 | case 1: |
| 202 | ven_req.wIndex = (req_data->saddr_dat & 0xff); |
| 203 | break; |
| 204 | case 2: |
| 205 | ven_req.wIndex = req_data->saddr_dat; |
| 206 | break; |
| 207 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 208 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 209 | /* set wLength value */ |
| 210 | ven_req.wLength = req_data->buf_size; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 211 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 212 | /* set bData value */ |
| 213 | ven_req.bData = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 214 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 215 | /* set the direction */ |
| 216 | if (req_data->direction) { |
| 217 | ven_req.direction = USB_DIR_IN; |
| 218 | memset(req_data->p_buffer, 0x00, ven_req.wLength); |
| 219 | } else |
| 220 | ven_req.direction = USB_DIR_OUT; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 221 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 222 | /* set the buffer for read / write */ |
| 223 | ven_req.pBuff = req_data->p_buffer; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 224 | |
| 225 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 226 | /* call common vendor command request */ |
| 227 | status = cx231xx_send_vendor_cmd(dev, &ven_req); |
| 228 | if (status < 0) { |
| 229 | cx231xx_info |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 230 | ("UsbInterface::sendCommand, failed with status -%d\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 231 | status); |
| 232 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 233 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 234 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 235 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 236 | EXPORT_SYMBOL_GPL(cx231xx_send_usb_command); |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 237 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 238 | /* |
| 239 | * cx231xx_read_ctrl_reg() |
| 240 | * reads data from the usb device specifying bRequest and wValue |
| 241 | */ |
| 242 | int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 243 | char *buf, int len) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 244 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 245 | u8 val = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 246 | int ret; |
| 247 | int pipe = usb_rcvctrlpipe(dev->udev, 0); |
| 248 | |
| 249 | if (dev->state & DEV_DISCONNECTED) |
| 250 | return -ENODEV; |
| 251 | |
| 252 | if (len > URB_MAX_CTRL_SIZE) |
| 253 | return -EINVAL; |
| 254 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 255 | switch (len) { |
| 256 | case 1: |
| 257 | val = ENABLE_ONE_BYTE; |
| 258 | break; |
| 259 | case 2: |
| 260 | val = ENABLE_TWE_BYTE; |
| 261 | break; |
| 262 | case 3: |
| 263 | val = ENABLE_THREE_BYTE; |
| 264 | break; |
| 265 | case 4: |
| 266 | val = ENABLE_FOUR_BYTE; |
| 267 | break; |
| 268 | default: |
| 269 | val = 0xFF; /* invalid option */ |
| 270 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 271 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 272 | if (val == 0xFF) |
| 273 | return -EINVAL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 274 | |
| 275 | if (reg_debug) { |
| 276 | cx231xx_isocdbg("(pipe 0x%08x): " |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 277 | "IN: %02x %02x %02x %02x %02x %02x %02x %02x ", |
| 278 | pipe, |
| 279 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 280 | req, 0, val, |
| 281 | reg & 0xff, reg >> 8, len & 0xff, len >> 8); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 282 | } |
| 283 | |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 284 | mutex_lock(&dev->ctrl_urb_lock); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 285 | ret = usb_control_msg(dev->udev, pipe, req, |
| 286 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 287 | val, reg, dev->urb_buf, len, HZ); |
| 288 | if (ret < 0) { |
| 289 | cx231xx_isocdbg(" failed!\n"); |
| 290 | /* mutex_unlock(&dev->ctrl_urb_lock); */ |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | if (len) |
| 295 | memcpy(buf, dev->urb_buf, len); |
| 296 | |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 297 | mutex_unlock(&dev->ctrl_urb_lock); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 298 | |
| 299 | if (reg_debug) { |
| 300 | int byte; |
| 301 | |
| 302 | cx231xx_isocdbg("<<<"); |
| 303 | for (byte = 0; byte < len; byte++) |
| 304 | cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]); |
| 305 | cx231xx_isocdbg("\n"); |
| 306 | } |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 311 | int cx231xx_send_vendor_cmd(struct cx231xx *dev, |
| 312 | struct VENDOR_REQUEST_IN *ven_req) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 313 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 314 | int ret; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 315 | int pipe = 0; |
| 316 | |
| 317 | if (dev->state & DEV_DISCONNECTED) |
| 318 | return -ENODEV; |
| 319 | |
| 320 | if ((ven_req->wLength > URB_MAX_CTRL_SIZE)) |
| 321 | return -EINVAL; |
| 322 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 323 | if (ven_req->direction) |
| 324 | pipe = usb_rcvctrlpipe(dev->udev, 0); |
| 325 | else |
| 326 | pipe = usb_sndctrlpipe(dev->udev, 0); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 327 | |
| 328 | if (reg_debug) { |
| 329 | int byte; |
| 330 | |
| 331 | cx231xx_isocdbg("(pipe 0x%08x): " |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 332 | "OUT: %02x %02x %02x %04x %04x %04x >>>", |
| 333 | pipe, |
| 334 | ven_req-> |
| 335 | direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 336 | ven_req->bRequest, 0, ven_req->wValue, |
| 337 | ven_req->wIndex, ven_req->wLength); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 338 | |
| 339 | for (byte = 0; byte < ven_req->wLength; byte++) |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 340 | cx231xx_isocdbg(" %02x", |
| 341 | (unsigned char)ven_req->pBuff[byte]); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 342 | cx231xx_isocdbg("\n"); |
| 343 | } |
| 344 | |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 345 | mutex_lock(&dev->ctrl_urb_lock); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 346 | ret = usb_control_msg(dev->udev, pipe, ven_req->bRequest, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 347 | ven_req-> |
| 348 | direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 349 | ven_req->wValue, ven_req->wIndex, ven_req->pBuff, |
| 350 | ven_req->wLength, HZ); |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 351 | mutex_unlock(&dev->ctrl_urb_lock); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * cx231xx_write_ctrl_reg() |
| 358 | * sends data to the usb device, specifying bRequest |
| 359 | */ |
| 360 | int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 361 | int len) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 362 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 363 | u8 val = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 364 | int ret; |
| 365 | int pipe = usb_sndctrlpipe(dev->udev, 0); |
| 366 | |
| 367 | if (dev->state & DEV_DISCONNECTED) |
| 368 | return -ENODEV; |
| 369 | |
| 370 | if ((len < 1) || (len > URB_MAX_CTRL_SIZE)) |
| 371 | return -EINVAL; |
| 372 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 373 | switch (len) { |
| 374 | case 1: |
| 375 | val = ENABLE_ONE_BYTE; |
| 376 | break; |
| 377 | case 2: |
| 378 | val = ENABLE_TWE_BYTE; |
| 379 | break; |
| 380 | case 3: |
| 381 | val = ENABLE_THREE_BYTE; |
| 382 | break; |
| 383 | case 4: |
| 384 | val = ENABLE_FOUR_BYTE; |
| 385 | break; |
| 386 | default: |
| 387 | val = 0xFF; /* invalid option */ |
| 388 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 389 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 390 | if (val == 0xFF) |
| 391 | return -EINVAL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 392 | |
| 393 | if (reg_debug) { |
| 394 | int byte; |
| 395 | |
| 396 | cx231xx_isocdbg("(pipe 0x%08x): " |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 397 | "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>", |
| 398 | pipe, |
| 399 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 400 | req, 0, val, reg & 0xff, |
| 401 | reg >> 8, len & 0xff, len >> 8); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 402 | |
| 403 | for (byte = 0; byte < len; byte++) |
| 404 | cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]); |
| 405 | cx231xx_isocdbg("\n"); |
| 406 | } |
| 407 | |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 408 | mutex_lock(&dev->ctrl_urb_lock); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 409 | memcpy(dev->urb_buf, buf, len); |
| 410 | ret = usb_control_msg(dev->udev, pipe, req, |
| 411 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 412 | val, reg, dev->urb_buf, len, HZ); |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 413 | mutex_unlock(&dev->ctrl_urb_lock); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 418 | /**************************************************************** |
| 419 | * USB Alternate Setting functions * |
| 420 | *****************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 421 | |
| 422 | int cx231xx_set_video_alternate(struct cx231xx *dev) |
| 423 | { |
| 424 | int errCode, prev_alt = dev->video_mode.alt; |
| 425 | unsigned int min_pkt_size = dev->width * 2 + 4; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 426 | u32 usb_interface_index = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 427 | |
| 428 | /* When image size is bigger than a certain value, |
| 429 | the frame size should be increased, otherwise, only |
| 430 | green screen will be received. |
| 431 | */ |
| 432 | if (dev->width * 2 * dev->height > 720 * 240 * 2) |
| 433 | min_pkt_size *= 2; |
| 434 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 435 | if (dev->width > 360) { |
| 436 | /* resolutions: 720,704,640 */ |
| 437 | dev->video_mode.alt = 3; |
| 438 | } else if (dev->width > 180) { |
| 439 | /* resolutions: 360,352,320,240 */ |
| 440 | dev->video_mode.alt = 2; |
| 441 | } else if (dev->width > 0) { |
| 442 | /* resolutions: 180,176,160,128,88 */ |
| 443 | dev->video_mode.alt = 1; |
| 444 | } else { |
| 445 | /* Change to alt0 BULK to release USB bandwidth */ |
| 446 | dev->video_mode.alt = 0; |
| 447 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 448 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 449 | /* Get the correct video interface Index */ |
| 450 | usb_interface_index = |
| 451 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 452 | video_index + 1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 453 | |
| 454 | if (dev->video_mode.alt != prev_alt) { |
| 455 | cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n", |
| 456 | min_pkt_size, dev->video_mode.alt); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 457 | dev->video_mode.max_pkt_size = |
| 458 | dev->video_mode.alt_max_pkt_size[dev->video_mode.alt]; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 459 | cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 460 | dev->video_mode.alt, |
| 461 | dev->video_mode.max_pkt_size); |
| 462 | cx231xx_info |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 463 | (" setting alt %d with wMaxPktSize=%u , Interface = %d\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 464 | dev->video_mode.alt, dev->video_mode.max_pkt_size, |
| 465 | usb_interface_index); |
| 466 | errCode = |
| 467 | usb_set_interface(dev->udev, usb_interface_index, |
| 468 | dev->video_mode.alt); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 469 | if (errCode < 0) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 470 | cx231xx_errdev |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 471 | ("cannot change alt number to %d (error=%i)\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 472 | dev->video_mode.alt, errCode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 473 | return errCode; |
| 474 | } |
| 475 | } |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt) |
| 480 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 481 | int status = 0; |
| 482 | u32 usb_interface_index = 0; |
| 483 | u32 max_pkt_size = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 484 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 485 | switch (index) { |
| 486 | case INDEX_TS1: |
| 487 | usb_interface_index = |
| 488 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 489 | ts1_index + 1; |
| 490 | dev->video_mode.alt = alt; |
| 491 | if (dev->ts1_mode.alt_max_pkt_size != NULL) |
| 492 | max_pkt_size = dev->ts1_mode.max_pkt_size = |
| 493 | dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt]; |
| 494 | break; |
| 495 | case INDEX_TS2: |
| 496 | usb_interface_index = |
| 497 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 498 | ts2_index + 1; |
| 499 | break; |
| 500 | case INDEX_AUDIO: |
| 501 | usb_interface_index = |
| 502 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 503 | audio_index + 1; |
| 504 | dev->adev.alt = alt; |
| 505 | if (dev->adev.alt_max_pkt_size != NULL) |
| 506 | max_pkt_size = dev->adev.max_pkt_size = |
| 507 | dev->adev.alt_max_pkt_size[dev->adev.alt]; |
| 508 | break; |
| 509 | case INDEX_VIDEO: |
| 510 | usb_interface_index = |
| 511 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 512 | video_index + 1; |
| 513 | dev->video_mode.alt = alt; |
| 514 | if (dev->video_mode.alt_max_pkt_size != NULL) |
| 515 | max_pkt_size = dev->video_mode.max_pkt_size = |
| 516 | dev->video_mode.alt_max_pkt_size[dev->video_mode. |
| 517 | alt]; |
| 518 | break; |
| 519 | case INDEX_VANC: |
| 520 | usb_interface_index = |
| 521 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 522 | vanc_index + 1; |
| 523 | dev->vbi_mode.alt = alt; |
| 524 | if (dev->vbi_mode.alt_max_pkt_size != NULL) |
| 525 | max_pkt_size = dev->vbi_mode.max_pkt_size = |
| 526 | dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt]; |
| 527 | break; |
| 528 | case INDEX_HANC: |
| 529 | usb_interface_index = |
| 530 | dev->current_pcb_config.hs_config_info[0].interface_info. |
| 531 | hanc_index + 1; |
| 532 | dev->sliced_cc_mode.alt = alt; |
| 533 | if (dev->sliced_cc_mode.alt_max_pkt_size != NULL) |
| 534 | max_pkt_size = dev->sliced_cc_mode.max_pkt_size = |
| 535 | dev->sliced_cc_mode.alt_max_pkt_size[dev-> |
| 536 | sliced_cc_mode. |
| 537 | alt]; |
| 538 | break; |
| 539 | default: |
| 540 | break; |
| 541 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 542 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 543 | if (alt > 0 && max_pkt_size == 0) { |
| 544 | cx231xx_errdev |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 545 | ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n", |
| 546 | usb_interface_index, alt); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 547 | return -1; |
| 548 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 549 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 550 | cx231xx_info |
| 551 | (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n", |
| 552 | alt, max_pkt_size, usb_interface_index); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 553 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 554 | if (usb_interface_index > 0) { |
| 555 | status = usb_set_interface(dev->udev, usb_interface_index, alt); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 556 | if (status < 0) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 557 | cx231xx_errdev |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 558 | ("can't change interface %d alt no. to %d (err=%i)\n", |
| 559 | usb_interface_index, alt, status); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 560 | return status; |
| 561 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 562 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 563 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 564 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 565 | } |
| 566 | EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting); |
| 567 | |
| 568 | int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio) |
| 569 | { |
| 570 | int rc = 0; |
| 571 | |
| 572 | if (!gpio) |
| 573 | return rc; |
| 574 | |
| 575 | /* Send GPIO reset sequences specified at board entry */ |
| 576 | while (gpio->sleep >= 0) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 577 | rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val); |
| 578 | if (rc < 0) |
| 579 | return rc; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 580 | |
| 581 | if (gpio->sleep > 0) |
| 582 | msleep(gpio->sleep); |
| 583 | |
| 584 | gpio++; |
| 585 | } |
| 586 | return rc; |
| 587 | } |
| 588 | |
| 589 | int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode) |
| 590 | { |
| 591 | if (dev->mode == set_mode) |
| 592 | return 0; |
| 593 | |
| 594 | if (set_mode == CX231XX_SUSPEND) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 595 | /* Set the chip in power saving mode */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 596 | dev->mode = set_mode; |
| 597 | } |
| 598 | |
| 599 | /* Resource is locked */ |
| 600 | if (dev->mode != CX231XX_SUSPEND) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | dev->mode = set_mode; |
| 604 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 605 | if (dev->mode == CX231XX_DIGITAL_MODE) |
| 606 | ;/* Set Digital power mode */ |
| 607 | else |
| 608 | ;/* Set Analog Power mode */ |
| 609 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 610 | return 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 611 | } |
| 612 | EXPORT_SYMBOL_GPL(cx231xx_set_mode); |
| 613 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 614 | /***************************************************************** |
| 615 | * URB Streaming functions * |
| 616 | ******************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | * IRQ callback, called by URB callback |
| 620 | */ |
| 621 | static void cx231xx_irq_callback(struct urb *urb) |
| 622 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 623 | struct cx231xx_dmaqueue *dma_q = urb->context; |
| 624 | struct cx231xx_video_mode *vmode = |
| 625 | container_of(dma_q, struct cx231xx_video_mode, vidq); |
| 626 | struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 627 | int rc, i; |
| 628 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 629 | switch (urb->status) { |
| 630 | case 0: /* success */ |
| 631 | case -ETIMEDOUT: /* NAK */ |
| 632 | break; |
| 633 | case -ECONNRESET: /* kill */ |
| 634 | case -ENOENT: |
| 635 | case -ESHUTDOWN: |
| 636 | return; |
| 637 | default: /* error */ |
| 638 | cx231xx_isocdbg("urb completition error %d.\n", urb->status); |
| 639 | break; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* Copy data from URB */ |
| 643 | spin_lock(&dev->video_mode.slock); |
| 644 | rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb); |
| 645 | spin_unlock(&dev->video_mode.slock); |
| 646 | |
| 647 | /* Reset urb buffers */ |
| 648 | for (i = 0; i < urb->number_of_packets; i++) { |
| 649 | urb->iso_frame_desc[i].status = 0; |
| 650 | urb->iso_frame_desc[i].actual_length = 0; |
| 651 | } |
| 652 | urb->status = 0; |
| 653 | |
| 654 | urb->status = usb_submit_urb(urb, GFP_ATOMIC); |
| 655 | if (urb->status) { |
| 656 | cx231xx_isocdbg("urb resubmit failed (error=%i)\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 657 | urb->status); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Stop and Deallocate URBs |
| 663 | */ |
| 664 | void cx231xx_uninit_isoc(struct cx231xx *dev) |
| 665 | { |
| 666 | struct urb *urb; |
| 667 | int i; |
| 668 | |
| 669 | cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n"); |
| 670 | |
| 671 | dev->video_mode.isoc_ctl.nfields = -1; |
| 672 | for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { |
| 673 | urb = dev->video_mode.isoc_ctl.urb[i]; |
| 674 | if (urb) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 675 | if (!irqs_disabled()) |
| 676 | usb_kill_urb(urb); |
| 677 | else |
| 678 | usb_unlink_urb(urb); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 679 | |
| 680 | if (dev->video_mode.isoc_ctl.transfer_buffer[i]) { |
| 681 | usb_buffer_free(dev->udev, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 682 | urb->transfer_buffer_length, |
| 683 | dev->video_mode.isoc_ctl. |
| 684 | transfer_buffer[i], |
| 685 | urb->transfer_dma); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 686 | } |
| 687 | usb_free_urb(urb); |
| 688 | dev->video_mode.isoc_ctl.urb[i] = NULL; |
| 689 | } |
| 690 | dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL; |
| 691 | } |
| 692 | |
| 693 | kfree(dev->video_mode.isoc_ctl.urb); |
| 694 | kfree(dev->video_mode.isoc_ctl.transfer_buffer); |
| 695 | |
| 696 | dev->video_mode.isoc_ctl.urb = NULL; |
| 697 | dev->video_mode.isoc_ctl.transfer_buffer = NULL; |
| 698 | dev->video_mode.isoc_ctl.num_bufs = 0; |
| 699 | |
| 700 | cx231xx_capture_start(dev, 0, Raw_Video); |
| 701 | } |
| 702 | EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc); |
| 703 | |
| 704 | /* |
| 705 | * Allocate URBs and start IRQ |
| 706 | */ |
| 707 | int cx231xx_init_isoc(struct cx231xx *dev, int max_packets, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 708 | int num_bufs, int max_pkt_size, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 709 | int (*isoc_copy) (struct cx231xx *dev, struct urb *urb)) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 710 | { |
| 711 | struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq; |
| 712 | int i; |
| 713 | int sb_size, pipe; |
| 714 | struct urb *urb; |
| 715 | int j, k; |
| 716 | int rc; |
| 717 | |
| 718 | cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n"); |
| 719 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 720 | dev->video_input = dev->video_input > 2 ? 2 : dev->video_input; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 721 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 722 | cx231xx_info("Setting Video mux to %d\n", dev->video_input); |
| 723 | video_mux(dev, dev->video_input); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 724 | |
| 725 | /* De-allocates all pending stuff */ |
| 726 | cx231xx_uninit_isoc(dev); |
| 727 | |
| 728 | dev->video_mode.isoc_ctl.isoc_copy = isoc_copy; |
| 729 | dev->video_mode.isoc_ctl.num_bufs = num_bufs; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 730 | dma_q->pos = 0; |
| 731 | dma_q->is_partial_line = 0; |
| 732 | dma_q->last_sav = 0; |
| 733 | dma_q->current_field = -1; |
| 734 | dma_q->field1_done = 0; |
| 735 | dma_q->lines_per_field = dev->height / 2; |
| 736 | dma_q->bytes_left_in_line = dev->width << 1; |
| 737 | dma_q->lines_completed = 0; |
| 738 | for (i = 0; i < 8; i++) |
| 739 | dma_q->partial_buf[i] = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 740 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 741 | dev->video_mode.isoc_ctl.urb = |
| 742 | kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 743 | if (!dev->video_mode.isoc_ctl.urb) { |
| 744 | cx231xx_errdev("cannot alloc memory for usb buffers\n"); |
| 745 | return -ENOMEM; |
| 746 | } |
| 747 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 748 | dev->video_mode.isoc_ctl.transfer_buffer = |
| 749 | kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 750 | if (!dev->video_mode.isoc_ctl.transfer_buffer) { |
| 751 | cx231xx_errdev("cannot allocate memory for usbtransfer\n"); |
| 752 | kfree(dev->video_mode.isoc_ctl.urb); |
| 753 | return -ENOMEM; |
| 754 | } |
| 755 | |
| 756 | dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size; |
| 757 | dev->video_mode.isoc_ctl.buf = NULL; |
| 758 | |
| 759 | sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size; |
| 760 | |
| 761 | /* allocate urbs and transfer buffers */ |
| 762 | for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { |
| 763 | urb = usb_alloc_urb(max_packets, GFP_KERNEL); |
| 764 | if (!urb) { |
| 765 | cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i); |
| 766 | cx231xx_uninit_isoc(dev); |
| 767 | return -ENOMEM; |
| 768 | } |
| 769 | dev->video_mode.isoc_ctl.urb[i] = urb; |
| 770 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 771 | dev->video_mode.isoc_ctl.transfer_buffer[i] = |
| 772 | usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL, |
| 773 | &urb->transfer_dma); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 774 | if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) { |
| 775 | cx231xx_err("unable to allocate %i bytes for transfer" |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 776 | " buffer %i%s\n", |
| 777 | sb_size, i, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 778 | in_interrupt() ? " while in int" : ""); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 779 | cx231xx_uninit_isoc(dev); |
| 780 | return -ENOMEM; |
| 781 | } |
| 782 | memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size); |
| 783 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 784 | pipe = |
| 785 | usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 786 | |
| 787 | usb_fill_int_urb(urb, dev->udev, pipe, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 788 | dev->video_mode.isoc_ctl.transfer_buffer[i], |
| 789 | sb_size, cx231xx_irq_callback, dma_q, 1); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 790 | |
| 791 | urb->number_of_packets = max_packets; |
| 792 | urb->transfer_flags = URB_ISO_ASAP; |
| 793 | |
| 794 | k = 0; |
| 795 | for (j = 0; j < max_packets; j++) { |
| 796 | urb->iso_frame_desc[j].offset = k; |
| 797 | urb->iso_frame_desc[j].length = |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 798 | dev->video_mode.isoc_ctl.max_pkt_size; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 799 | k += dev->video_mode.isoc_ctl.max_pkt_size; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | init_waitqueue_head(&dma_q->wq); |
| 804 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 805 | /* submit urbs and enables IRQ */ |
| 806 | for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 807 | rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i], |
| 808 | GFP_ATOMIC); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 809 | if (rc) { |
| 810 | cx231xx_err("submit of urb %i failed (error=%i)\n", i, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 811 | rc); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 812 | cx231xx_uninit_isoc(dev); |
| 813 | return rc; |
| 814 | } |
| 815 | } |
| 816 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 817 | cx231xx_capture_start(dev, 1, Raw_Video); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(cx231xx_init_isoc); |
| 822 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 823 | /***************************************************************** |
| 824 | * Device Init/UnInit functions * |
| 825 | ******************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 826 | int cx231xx_dev_init(struct cx231xx *dev) |
| 827 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 828 | int errCode = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 829 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 830 | /* Initialize I2C bus */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 831 | |
| 832 | /* External Master 1 Bus */ |
| 833 | dev->i2c_bus[0].nr = 0; |
| 834 | dev->i2c_bus[0].dev = dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 835 | dev->i2c_bus[0].i2c_period = I2C_SPEED_1M; /* 1MHz */ |
| 836 | dev->i2c_bus[0].i2c_nostop = 0; |
| 837 | dev->i2c_bus[0].i2c_reserve = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 838 | |
| 839 | /* External Master 2 Bus */ |
| 840 | dev->i2c_bus[1].nr = 1; |
| 841 | dev->i2c_bus[1].dev = dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 842 | dev->i2c_bus[1].i2c_period = I2C_SPEED_1M; /* 1MHz */ |
| 843 | dev->i2c_bus[1].i2c_nostop = 0; |
| 844 | dev->i2c_bus[1].i2c_reserve = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 845 | |
| 846 | /* Internal Master 3 Bus */ |
| 847 | dev->i2c_bus[2].nr = 2; |
| 848 | dev->i2c_bus[2].dev = dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 849 | dev->i2c_bus[2].i2c_period = I2C_SPEED_400K; /* 400kHz */ |
| 850 | dev->i2c_bus[2].i2c_nostop = 0; |
| 851 | dev->i2c_bus[2].i2c_reserve = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 852 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 853 | /* register I2C buses */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 854 | cx231xx_i2c_register(&dev->i2c_bus[0]); |
| 855 | cx231xx_i2c_register(&dev->i2c_bus[1]); |
| 856 | cx231xx_i2c_register(&dev->i2c_bus[2]); |
| 857 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 858 | /* init hardware */ |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 859 | /* Note : with out calling set power mode function, |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 860 | afe can not be set up correctly */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 861 | errCode = cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 862 | if (errCode < 0) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 863 | cx231xx_errdev |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 864 | ("%s: Failed to set Power - errCode [%d]!\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 865 | __func__, errCode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 866 | return errCode; |
| 867 | } |
| 868 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 869 | /* initialize Colibri block */ |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 870 | errCode = cx231xx_afe_init_super_block(dev, 0x23c); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 871 | if (errCode < 0) { |
| 872 | cx231xx_errdev |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 873 | ("%s: cx231xx_afe init super block - errCode [%d]!\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 874 | __func__, errCode); |
| 875 | return errCode; |
| 876 | } |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 877 | errCode = cx231xx_afe_init_channels(dev); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 878 | if (errCode < 0) { |
| 879 | cx231xx_errdev |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 880 | ("%s: cx231xx_afe init channels - errCode [%d]!\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 881 | __func__, errCode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 882 | return errCode; |
| 883 | } |
| 884 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 885 | /* Set DIF in By pass mode */ |
| 886 | errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND); |
| 887 | if (errCode < 0) { |
| 888 | cx231xx_errdev |
| 889 | ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n", |
| 890 | __func__, errCode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 891 | return errCode; |
| 892 | } |
| 893 | |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 894 | /* I2S block related functions */ |
| 895 | errCode = cx231xx_i2s_blk_initialize(dev); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 896 | if (errCode < 0) { |
| 897 | cx231xx_errdev |
Sri Deevi | ecc67d1 | 2009-03-21 22:00:20 -0300 | [diff] [blame] | 898 | ("%s: cx231xx_i2s block initialize - errCode [%d]!\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 899 | __func__, errCode); |
| 900 | return errCode; |
| 901 | } |
| 902 | |
| 903 | /* init control pins */ |
| 904 | errCode = cx231xx_init_ctrl_pin_status(dev); |
| 905 | if (errCode < 0) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 906 | cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 907 | __func__, errCode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 908 | return errCode; |
| 909 | } |
| 910 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 911 | /* set AGC mode to Analog */ |
| 912 | errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1); |
| 913 | if (errCode < 0) { |
| 914 | cx231xx_errdev |
| 915 | ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n", |
| 916 | __func__, errCode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 917 | return errCode; |
| 918 | } |
| 919 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 920 | /* set all alternate settings to zero initially */ |
| 921 | cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0); |
| 922 | cx231xx_set_alt_setting(dev, INDEX_VANC, 0); |
| 923 | cx231xx_set_alt_setting(dev, INDEX_HANC, 0); |
| 924 | if (dev->board.has_dvb) |
| 925 | cx231xx_set_alt_setting(dev, INDEX_TS1, 0); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 926 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 927 | /* set the I2C master port to 3 on channel 1 */ |
| 928 | errCode = cx231xx_enable_i2c_for_tuner(dev, I2C_3); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 929 | |
| 930 | return errCode; |
| 931 | } |
| 932 | EXPORT_SYMBOL_GPL(cx231xx_dev_init); |
| 933 | |
| 934 | void cx231xx_dev_uninit(struct cx231xx *dev) |
| 935 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 936 | /* Un Initialize I2C bus */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 937 | cx231xx_i2c_unregister(&dev->i2c_bus[2]); |
| 938 | cx231xx_i2c_unregister(&dev->i2c_bus[1]); |
| 939 | cx231xx_i2c_unregister(&dev->i2c_bus[0]); |
| 940 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 941 | EXPORT_SYMBOL_GPL(cx231xx_dev_uninit); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 942 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 943 | /***************************************************************** |
| 944 | * G P I O related functions * |
| 945 | ******************************************************************/ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 946 | int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val, |
| 947 | u8 len, u8 request, u8 direction) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 948 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 949 | int status = 0; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 950 | struct VENDOR_REQUEST_IN ven_req; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 951 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 952 | /* Set wValue */ |
| 953 | ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 954 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 955 | /* set request */ |
| 956 | if (!request) { |
| 957 | if (direction) |
| 958 | ven_req.bRequest = VRT_GET_GPIO; /* 0x8 gpio */ |
| 959 | else |
| 960 | ven_req.bRequest = VRT_SET_GPIO; /* 0x9 gpio */ |
| 961 | } else { |
| 962 | if (direction) |
| 963 | ven_req.bRequest = VRT_GET_GPIE; /* 0xa gpie */ |
| 964 | else |
| 965 | ven_req.bRequest = VRT_SET_GPIE; /* 0xb gpie */ |
| 966 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 967 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 968 | /* set index value */ |
| 969 | ven_req.wIndex = (u16) (gpio_bit & 0xffff); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 970 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 971 | /* set wLength value */ |
| 972 | ven_req.wLength = len; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 973 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 974 | /* set bData value */ |
| 975 | ven_req.bData = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 976 | |
| 977 | /* set the buffer for read / write */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 978 | ven_req.pBuff = gpio_val; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 979 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 980 | /* set the direction */ |
| 981 | if (direction) { |
| 982 | ven_req.direction = USB_DIR_IN; |
| 983 | memset(ven_req.pBuff, 0x00, ven_req.wLength); |
| 984 | } else |
| 985 | ven_req.direction = USB_DIR_OUT; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 986 | |
| 987 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 988 | /* call common vendor command request */ |
| 989 | status = cx231xx_send_vendor_cmd(dev, &ven_req); |
| 990 | if (status < 0) { |
| 991 | cx231xx_info |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 992 | ("UsbInterface::sendCommand, failed with status -%d\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 993 | status); |
| 994 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 995 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 996 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 997 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 998 | EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd); |
| 999 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1000 | /***************************************************************** |
| 1001 | * C O N T R O L - Register R E A D / W R I T E functions * |
| 1002 | *****************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1003 | int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode) |
| 1004 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1005 | u8 value[4] = { 0x0, 0x0, 0x0, 0x0 }; |
| 1006 | u32 tmp = 0; |
| 1007 | int status = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1008 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1009 | status = |
| 1010 | cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4); |
| 1011 | if (status < 0) |
| 1012 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1013 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1014 | tmp = *((u32 *) value); |
| 1015 | tmp |= mode; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1016 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1017 | value[0] = (u8) tmp; |
| 1018 | value[1] = (u8) (tmp >> 8); |
| 1019 | value[2] = (u8) (tmp >> 16); |
| 1020 | value[3] = (u8) (tmp >> 24); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1021 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1022 | status = |
| 1023 | cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1024 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1025 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1026 | } |
| 1027 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1028 | /***************************************************************** |
| 1029 | * I 2 C Internal C O N T R O L functions * |
| 1030 | *****************************************************************/ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1031 | int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1032 | u8 saddr_len, u32 *data, u8 data_len) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1033 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1034 | int status = 0; |
| 1035 | struct cx231xx_i2c_xfer_data req_data; |
| 1036 | u8 value[4] = { 0, 0, 0, 0 }; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1037 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1038 | if (saddr_len == 0) |
| 1039 | saddr = 0; |
| 1040 | else if (saddr_len == 0) |
| 1041 | saddr &= 0xff; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1042 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1043 | /* prepare xfer_data struct */ |
| 1044 | req_data.dev_addr = dev_addr >> 1; |
| 1045 | req_data.direction = I2C_M_RD; |
| 1046 | req_data.saddr_len = saddr_len; |
| 1047 | req_data.saddr_dat = saddr; |
| 1048 | req_data.buf_size = data_len; |
| 1049 | req_data.p_buffer = (u8 *) value; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1050 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1051 | /* usb send command */ |
| 1052 | status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1053 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1054 | if (status >= 0) { |
| 1055 | /* Copy the data read back to main buffer */ |
| 1056 | if (data_len == 1) |
| 1057 | *data = value[0]; |
| 1058 | else |
| 1059 | *data = |
| 1060 | value[0] | value[1] << 8 | value[2] << 16 | value[3] |
| 1061 | << 24; |
| 1062 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1063 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1064 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1068 | u8 saddr_len, u32 data, u8 data_len) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1069 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1070 | int status = 0; |
| 1071 | u8 value[4] = { 0, 0, 0, 0 }; |
| 1072 | struct cx231xx_i2c_xfer_data req_data; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1073 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1074 | value[0] = (u8) data; |
| 1075 | value[1] = (u8) (data >> 8); |
| 1076 | value[2] = (u8) (data >> 16); |
| 1077 | value[3] = (u8) (data >> 24); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1078 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1079 | if (saddr_len == 0) |
| 1080 | saddr = 0; |
| 1081 | else if (saddr_len == 0) |
| 1082 | saddr &= 0xff; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1083 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1084 | /* prepare xfer_data struct */ |
| 1085 | req_data.dev_addr = dev_addr >> 1; |
| 1086 | req_data.direction = 0; |
| 1087 | req_data.saddr_len = saddr_len; |
| 1088 | req_data.saddr_dat = saddr; |
| 1089 | req_data.buf_size = data_len; |
| 1090 | req_data.p_buffer = value; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1091 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1092 | /* usb send command */ |
| 1093 | status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1094 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1095 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1096 | } |
| 1097 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1098 | int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size, |
| 1099 | u16 register_address, u8 bit_start, u8 bit_end, |
| 1100 | u32 value) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1101 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1102 | int status = 0; |
| 1103 | u32 tmp; |
| 1104 | u32 mask = 0; |
| 1105 | int i; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1106 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1107 | if (bit_start > (size - 1) || bit_end > (size - 1)) |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1108 | return -1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1109 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1110 | if (size == 8) { |
| 1111 | status = |
| 1112 | cx231xx_read_i2c_data(dev, dev_addr, register_address, 2, |
| 1113 | &tmp, 1); |
| 1114 | } else { |
| 1115 | status = |
| 1116 | cx231xx_read_i2c_data(dev, dev_addr, register_address, 2, |
| 1117 | &tmp, 4); |
| 1118 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1119 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1120 | if (status < 0) |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1121 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1122 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1123 | mask = 1 << bit_end; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1124 | for (i = bit_end; i > bit_start && i > 0; i--) |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1125 | mask = mask + (1 << (i - 1)); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1126 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1127 | value <<= bit_start; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1128 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1129 | if (size == 8) { |
| 1130 | tmp &= ~mask; |
| 1131 | tmp |= value; |
| 1132 | tmp &= 0xff; |
| 1133 | status = |
| 1134 | cx231xx_write_i2c_data(dev, dev_addr, register_address, 2, |
| 1135 | tmp, 1); |
| 1136 | } else { |
| 1137 | tmp &= ~mask; |
| 1138 | tmp |= value; |
| 1139 | status = |
| 1140 | cx231xx_write_i2c_data(dev, dev_addr, register_address, 2, |
| 1141 | tmp, 4); |
| 1142 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1143 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1144 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1145 | } |
| 1146 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1147 | int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1148 | u16 saddr, u32 mask, u32 value) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1149 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1150 | u32 temp; |
| 1151 | int status = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1152 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1153 | status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1154 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1155 | if (status < 0) |
| 1156 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1157 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1158 | temp &= ~mask; |
| 1159 | temp |= value; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1160 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1161 | status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1162 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1163 | return status; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | u32 cx231xx_set_field(u32 field_mask, u32 data) |
| 1167 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1168 | u32 temp; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1169 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 1170 | for (temp = field_mask; (temp & 1) == 0; temp >>= 1) |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1171 | data <<= 1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1172 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 1173 | return data; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1174 | } |