1 /* 2 * STK1160 driver 3 * 4 * Copyright (C) 2012 Ezequiel Garcia 5 * <elezegarcia--a.t--gmail.com> 6 * 7 * Based on Easycap driver by R.M. Thomas 8 * Copyright (C) 2010 R.M. Thomas 9 * <rmthomas--a.t--sciolus.org> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * TODO: 22 * 23 * 1. (Try to) detect if we must register ac97 mixer 24 * 2. Support stream at lower speed: lower frame rate or lower frame size. 25 * 26 */ 27 28 #include <linux/module.h> 29 #include <linux/init.h> 30 #include <linux/kernel.h> 31 #include <linux/errno.h> 32 #include <linux/slab.h> 33 34 #include <linux/usb.h> 35 #include <linux/mm.h> 36 #include <linux/vmalloc.h> 37 #include <media/saa7115.h> 38 39 #include "stk1160.h" 40 #include "stk1160-reg.h" 41 42 static unsigned int input; 43 module_param(input, int, 0644); 44 MODULE_PARM_DESC(input, "Set default input"); 45 46 MODULE_LICENSE("GPL"); 47 MODULE_AUTHOR("Ezequiel Garcia"); 48 MODULE_DESCRIPTION("STK1160 driver"); 49 50 /* Devices supported by this driver */ 51 static struct usb_device_id stk1160_id_table[] = { 52 { USB_DEVICE(0x05e1, 0x0408) }, 53 { } 54 }; 55 MODULE_DEVICE_TABLE(usb, stk1160_id_table); 56 57 /* saa7113 I2C address */ 58 static unsigned short saa7113_addrs[] = { 59 0x4a >> 1, 60 I2C_CLIENT_END 61 }; 62 63 /* 64 * Read/Write stk registers 65 */ 66 int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value) 67 { 68 int ret; 69 int pipe = usb_rcvctrlpipe(dev->udev, 0); 70 71 *value = 0; 72 ret = usb_control_msg(dev->udev, pipe, 0x00, 73 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 74 0x00, reg, value, sizeof(u8), HZ); 75 if (ret < 0) { 76 stk1160_err("read failed on reg 0x%x (%d)\n", 77 reg, ret); 78 return ret; 79 } 80 81 return 0; 82 } 83 84 int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value) 85 { 86 int ret; 87 int pipe = usb_sndctrlpipe(dev->udev, 0); 88 89 ret = usb_control_msg(dev->udev, pipe, 0x01, 90 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 91 value, reg, NULL, 0, HZ); 92 if (ret < 0) { 93 stk1160_err("write failed on reg 0x%x (%d)\n", 94 reg, ret); 95 return ret; 96 } 97 98 return 0; 99 } 100 101 void stk1160_select_input(struct stk1160 *dev) 102 { 103 int route; 104 static const u8 gctrl[] = { 105 0x98, 0x90, 0x88, 0x80, 0x98 106 }; 107 108 if (dev->ctl_input == STK1160_SVIDEO_INPUT) 109 route = SAA7115_SVIDEO3; 110 else 111 route = SAA7115_COMPOSITE0; 112 113 if (dev->ctl_input < ARRAY_SIZE(gctrl)) { 114 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing, 115 route, 0, 0); 116 stk1160_write_reg(dev, STK1160_GCTRL, gctrl[dev->ctl_input]); 117 } 118 } 119 120 /* TODO: We should break this into pieces */ 121 static void stk1160_reg_reset(struct stk1160 *dev) 122 { 123 int i; 124 125 static const struct regval ctl[] = { 126 {STK1160_GCTRL+2, 0x0078}, 127 128 {STK1160_RMCTL+1, 0x0000}, 129 {STK1160_RMCTL+3, 0x0002}, 130 131 {STK1160_PLLSO, 0x0010}, 132 {STK1160_PLLSO+1, 0x0000}, 133 {STK1160_PLLSO+2, 0x0014}, 134 {STK1160_PLLSO+3, 0x000E}, 135 136 {STK1160_PLLFD, 0x0046}, 137 138 /* Timing generator setup */ 139 {STK1160_TIGEN, 0x0012}, 140 {STK1160_TICTL, 0x002D}, 141 {STK1160_TICTL+1, 0x0001}, 142 {STK1160_TICTL+2, 0x0000}, 143 {STK1160_TICTL+3, 0x0000}, 144 {STK1160_TIGEN, 0x0080}, 145 146 {0xffff, 0xffff} 147 }; 148 149 for (i = 0; ctl[i].reg != 0xffff; i++) 150 stk1160_write_reg(dev, ctl[i].reg, ctl[i].val); 151 } 152 153 static void stk1160_release(struct v4l2_device *v4l2_dev) 154 { 155 struct stk1160 *dev = container_of(v4l2_dev, struct stk1160, v4l2_dev); 156 157 stk1160_info("releasing all resources\n"); 158 159 stk1160_i2c_unregister(dev); 160 161 v4l2_ctrl_handler_free(&dev->ctrl_handler); 162 v4l2_device_unregister(&dev->v4l2_dev); 163 kfree(dev->alt_max_pkt_size); 164 kfree(dev); 165 } 166 167 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */ 168 #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03)) 169 170 /* 171 * Scan usb interface and populate max_pkt_size array 172 * with information on each alternate setting. 173 * The array should be allocated by the caller. 174 */ 175 static int stk1160_scan_usb(struct usb_interface *intf, struct usb_device *udev, 176 unsigned int *max_pkt_size) 177 { 178 int i, e, sizedescr, size, ifnum; 179 const struct usb_endpoint_descriptor *desc; 180 181 bool has_video = false, has_audio = false; 182 const char *speed; 183 184 ifnum = intf->altsetting[0].desc.bInterfaceNumber; 185 186 /* Get endpoints */ 187 for (i = 0; i < intf->num_altsetting; i++) { 188 189 for (e = 0; e < intf->altsetting[i].desc.bNumEndpoints; e++) { 190 191 /* This isn't clear enough, at least to me */ 192 desc = &intf->altsetting[i].endpoint[e].desc; 193 sizedescr = le16_to_cpu(desc->wMaxPacketSize); 194 size = sizedescr & 0x7ff; 195 196 if (udev->speed == USB_SPEED_HIGH) 197 size = size * hb_mult(sizedescr); 198 199 if (usb_endpoint_xfer_isoc(desc) && 200 usb_endpoint_dir_in(desc)) { 201 switch (desc->bEndpointAddress) { 202 case STK1160_EP_AUDIO: 203 has_audio = true; 204 break; 205 case STK1160_EP_VIDEO: 206 has_video = true; 207 max_pkt_size[i] = size; 208 break; 209 } 210 } 211 } 212 } 213 214 /* Is this even possible? */ 215 if (!(has_audio || has_video)) { 216 dev_err(&udev->dev, "no audio or video endpoints found\n"); 217 return -ENODEV; 218 } 219 220 switch (udev->speed) { 221 case USB_SPEED_LOW: 222 speed = "1.5"; 223 break; 224 case USB_SPEED_FULL: 225 speed = "12"; 226 break; 227 case USB_SPEED_HIGH: 228 speed = "480"; 229 break; 230 default: 231 speed = "unknown"; 232 } 233 234 dev_info(&udev->dev, "New device %s %s @ %s Mbps (%04x:%04x, interface %d, class %d)\n", 235 udev->manufacturer ? udev->manufacturer : "", 236 udev->product ? udev->product : "", 237 speed, 238 le16_to_cpu(udev->descriptor.idVendor), 239 le16_to_cpu(udev->descriptor.idProduct), 240 ifnum, 241 intf->altsetting->desc.bInterfaceNumber); 242 243 /* This should never happen, since we rejected audio interfaces */ 244 if (has_audio) 245 dev_warn(&udev->dev, "audio interface %d found.\n\ 246 This is not implemented by this driver,\ 247 you should use snd-usb-audio instead\n", ifnum); 248 249 if (has_video) 250 dev_info(&udev->dev, "video interface %d found\n", 251 ifnum); 252 253 /* 254 * Make sure we have 480 Mbps of bandwidth, otherwise things like 255 * video stream wouldn't likely work, since 12 Mbps is generally 256 * not enough even for most streams. 257 */ 258 if (udev->speed != USB_SPEED_HIGH) 259 dev_warn(&udev->dev, "must be connected to a high-speed USB 2.0 port\n\ 260 You may not be able to stream video smoothly\n"); 261 262 return 0; 263 } 264 265 static int stk1160_probe(struct usb_interface *interface, 266 const struct usb_device_id *id) 267 { 268 int rc = 0; 269 270 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */ 271 struct usb_device *udev; 272 struct stk1160 *dev; 273 274 udev = interface_to_usbdev(interface); 275 276 /* 277 * Since usb audio class is supported by snd-usb-audio, 278 * we reject audio interface. 279 */ 280 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) 281 return -ENODEV; 282 283 /* Alloc an array for all possible max_pkt_size */ 284 alt_max_pkt_size = kmalloc(sizeof(alt_max_pkt_size[0]) * 285 interface->num_altsetting, GFP_KERNEL); 286 if (alt_max_pkt_size == NULL) 287 return -ENOMEM; 288 289 /* 290 * Scan usb posibilities and populate alt_max_pkt_size array. 291 * Also, check if device speed is fast enough. 292 */ 293 rc = stk1160_scan_usb(interface, udev, alt_max_pkt_size); 294 if (rc < 0) { 295 kfree(alt_max_pkt_size); 296 return rc; 297 } 298 299 dev = kzalloc(sizeof(struct stk1160), GFP_KERNEL); 300 if (dev == NULL) { 301 kfree(alt_max_pkt_size); 302 return -ENOMEM; 303 } 304 305 dev->alt_max_pkt_size = alt_max_pkt_size; 306 dev->udev = udev; 307 dev->num_alt = interface->num_altsetting; 308 dev->ctl_input = input; 309 310 /* We save struct device for debug purposes only */ 311 dev->dev = &interface->dev; 312 313 usb_set_intfdata(interface, dev); 314 315 /* initialize videobuf2 stuff */ 316 rc = stk1160_vb2_setup(dev); 317 if (rc < 0) 318 goto free_err; 319 320 /* 321 * There is no need to take any locks here in probe 322 * because we register the device node as the *last* thing. 323 */ 324 spin_lock_init(&dev->buf_lock); 325 mutex_init(&dev->v4l_lock); 326 mutex_init(&dev->vb_queue_lock); 327 328 rc = v4l2_ctrl_handler_init(&dev->ctrl_handler, 0); 329 if (rc) { 330 stk1160_err("v4l2_ctrl_handler_init failed (%d)\n", rc); 331 goto free_err; 332 } 333 334 /* 335 * We obtain a v4l2_dev but defer 336 * registration of video device node as the last thing. 337 * There is no need to set the name if we give a device struct 338 */ 339 dev->v4l2_dev.release = stk1160_release; 340 dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler; 341 rc = v4l2_device_register(dev->dev, &dev->v4l2_dev); 342 if (rc) { 343 stk1160_err("v4l2_device_register failed (%d)\n", rc); 344 goto free_ctrl; 345 } 346 347 rc = stk1160_i2c_register(dev); 348 if (rc < 0) 349 goto unreg_v4l2; 350 351 /* 352 * To the best of my knowledge stk1160 boards only have 353 * saa7113, but it doesn't hurt to support them all. 354 */ 355 dev->sd_saa7115 = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap, 356 "saa7115_auto", 0, saa7113_addrs); 357 358 stk1160_info("driver ver %s successfully loaded\n", 359 STK1160_VERSION); 360 361 /* i2c reset saa711x */ 362 v4l2_device_call_all(&dev->v4l2_dev, 0, core, reset, 0); 363 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); 364 365 /* reset stk1160 to default values */ 366 stk1160_reg_reset(dev); 367 368 /* select default input */ 369 stk1160_select_input(dev); 370 371 stk1160_ac97_register(dev); 372 373 rc = stk1160_video_register(dev); 374 if (rc < 0) 375 goto unreg_i2c; 376 377 return 0; 378 379 unreg_i2c: 380 stk1160_i2c_unregister(dev); 381 unreg_v4l2: 382 v4l2_device_unregister(&dev->v4l2_dev); 383 free_ctrl: 384 v4l2_ctrl_handler_free(&dev->ctrl_handler); 385 free_err: 386 kfree(alt_max_pkt_size); 387 kfree(dev); 388 389 return rc; 390 } 391 392 static void stk1160_disconnect(struct usb_interface *interface) 393 { 394 struct stk1160 *dev; 395 396 dev = usb_get_intfdata(interface); 397 usb_set_intfdata(interface, NULL); 398 399 /* 400 * Wait until all current v4l2 operation are finished 401 * then deallocate resources 402 */ 403 mutex_lock(&dev->vb_queue_lock); 404 mutex_lock(&dev->v4l_lock); 405 406 /* Here is the only place where isoc get released */ 407 stk1160_uninit_isoc(dev); 408 409 /* ac97 unregister needs to be done before usb_device is cleared */ 410 stk1160_ac97_unregister(dev); 411 412 stk1160_clear_queue(dev); 413 414 video_unregister_device(&dev->vdev); 415 v4l2_device_disconnect(&dev->v4l2_dev); 416 417 /* This way current users can detect device is gone */ 418 dev->udev = NULL; 419 420 mutex_unlock(&dev->v4l_lock); 421 mutex_unlock(&dev->vb_queue_lock); 422 423 /* 424 * This calls stk1160_release if it's the last reference. 425 * therwise, release is posponed until there are no users left. 426 */ 427 v4l2_device_put(&dev->v4l2_dev); 428 } 429 430 static struct usb_driver stk1160_usb_driver = { 431 .name = "stk1160", 432 .id_table = stk1160_id_table, 433 .probe = stk1160_probe, 434 .disconnect = stk1160_disconnect, 435 }; 436 437 module_usb_driver(stk1160_usb_driver); 438