cx231xx-core.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. /*
  2. cx231xx-core.c - driver for Conexant Cx23100/101/102
  3. USB video capture devices
  4. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  5. Based on em28xx driver
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "cx231xx.h"
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/tuner.h>
  26. #include "cx231xx-reg.h"
  27. /* #define ENABLE_DEBUG_ISOC_FRAMES */
  28. static unsigned int core_debug;
  29. module_param(core_debug, int, 0644);
  30. MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
  31. #define cx231xx_coredbg(fmt, arg...) do {\
  32. if (core_debug) \
  33. printk(KERN_INFO "%s %s :"fmt, \
  34. dev->name, __func__ , ##arg); } while (0)
  35. static unsigned int reg_debug;
  36. module_param(reg_debug, int, 0644);
  37. MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
  38. static int alt = CX231XX_PINOUT;
  39. module_param(alt, int, 0644);
  40. MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
  41. #define cx231xx_isocdbg(fmt, arg...) do {\
  42. if (core_debug) \
  43. printk(KERN_INFO "%s %s :"fmt, \
  44. dev->name, __func__ , ##arg); } while (0)
  45. /*****************************************************************
  46. * Device control list functions *
  47. ******************************************************************/
  48. LIST_HEAD(cx231xx_devlist);
  49. static DEFINE_MUTEX(cx231xx_devlist_mutex);
  50. /*
  51. * cx231xx_realease_resources()
  52. * unregisters the v4l2,i2c and usb devices
  53. * called when the device gets disconected or at module unload
  54. */
  55. void cx231xx_remove_from_devlist(struct cx231xx *dev)
  56. {
  57. if (dev == NULL)
  58. return;
  59. if (dev->udev == NULL)
  60. return;
  61. if (atomic_read(&dev->devlist_count) > 0) {
  62. mutex_lock(&cx231xx_devlist_mutex);
  63. list_del(&dev->devlist);
  64. atomic_dec(&dev->devlist_count);
  65. mutex_unlock(&cx231xx_devlist_mutex);
  66. }
  67. };
  68. void cx231xx_add_into_devlist(struct cx231xx *dev)
  69. {
  70. mutex_lock(&cx231xx_devlist_mutex);
  71. list_add_tail(&dev->devlist, &cx231xx_devlist);
  72. atomic_inc(&dev->devlist_count);
  73. mutex_unlock(&cx231xx_devlist_mutex);
  74. };
  75. static LIST_HEAD(cx231xx_extension_devlist);
  76. int cx231xx_register_extension(struct cx231xx_ops *ops)
  77. {
  78. struct cx231xx *dev = NULL;
  79. mutex_lock(&cx231xx_devlist_mutex);
  80. list_add_tail(&ops->next, &cx231xx_extension_devlist);
  81. list_for_each_entry(dev, &cx231xx_devlist, devlist) {
  82. ops->init(dev);
  83. dev_info(dev->dev, "%s initialized\n", ops->name);
  84. }
  85. mutex_unlock(&cx231xx_devlist_mutex);
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(cx231xx_register_extension);
  89. void cx231xx_unregister_extension(struct cx231xx_ops *ops)
  90. {
  91. struct cx231xx *dev = NULL;
  92. mutex_lock(&cx231xx_devlist_mutex);
  93. list_for_each_entry(dev, &cx231xx_devlist, devlist) {
  94. ops->fini(dev);
  95. dev_info(dev->dev, "%s removed\n", ops->name);
  96. }
  97. list_del(&ops->next);
  98. mutex_unlock(&cx231xx_devlist_mutex);
  99. }
  100. EXPORT_SYMBOL(cx231xx_unregister_extension);
  101. void cx231xx_init_extension(struct cx231xx *dev)
  102. {
  103. struct cx231xx_ops *ops = NULL;
  104. mutex_lock(&cx231xx_devlist_mutex);
  105. if (!list_empty(&cx231xx_extension_devlist)) {
  106. list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
  107. if (ops->init)
  108. ops->init(dev);
  109. }
  110. }
  111. mutex_unlock(&cx231xx_devlist_mutex);
  112. }
  113. void cx231xx_close_extension(struct cx231xx *dev)
  114. {
  115. struct cx231xx_ops *ops = NULL;
  116. mutex_lock(&cx231xx_devlist_mutex);
  117. if (!list_empty(&cx231xx_extension_devlist)) {
  118. list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
  119. if (ops->fini)
  120. ops->fini(dev);
  121. }
  122. }
  123. mutex_unlock(&cx231xx_devlist_mutex);
  124. }
  125. /****************************************************************
  126. * U S B related functions *
  127. *****************************************************************/
  128. int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
  129. struct cx231xx_i2c_xfer_data *req_data)
  130. {
  131. int status = 0;
  132. struct cx231xx *dev = i2c_bus->dev;
  133. struct VENDOR_REQUEST_IN ven_req;
  134. u8 saddr_len = 0;
  135. u8 _i2c_period = 0;
  136. u8 _i2c_nostop = 0;
  137. u8 _i2c_reserve = 0;
  138. if (dev->state & DEV_DISCONNECTED)
  139. return -ENODEV;
  140. /* Get the I2C period, nostop and reserve parameters */
  141. _i2c_period = i2c_bus->i2c_period;
  142. _i2c_nostop = i2c_bus->i2c_nostop;
  143. _i2c_reserve = i2c_bus->i2c_reserve;
  144. saddr_len = req_data->saddr_len;
  145. /* Set wValue */
  146. ven_req.wValue = (req_data->dev_addr << 9 | _i2c_period << 4 |
  147. saddr_len << 2 | _i2c_nostop << 1 | I2C_SYNC |
  148. _i2c_reserve << 6);
  149. /* set channel number */
  150. if (req_data->direction & I2C_M_RD) {
  151. /* channel number, for read,spec required channel_num +4 */
  152. ven_req.bRequest = i2c_bus->nr + 4;
  153. } else
  154. ven_req.bRequest = i2c_bus->nr; /* channel number, */
  155. /* set index value */
  156. switch (saddr_len) {
  157. case 0:
  158. ven_req.wIndex = 0; /* need check */
  159. break;
  160. case 1:
  161. ven_req.wIndex = (req_data->saddr_dat & 0xff);
  162. break;
  163. case 2:
  164. ven_req.wIndex = req_data->saddr_dat;
  165. break;
  166. }
  167. /* set wLength value */
  168. ven_req.wLength = req_data->buf_size;
  169. /* set bData value */
  170. ven_req.bData = 0;
  171. /* set the direction */
  172. if (req_data->direction) {
  173. ven_req.direction = USB_DIR_IN;
  174. memset(req_data->p_buffer, 0x00, ven_req.wLength);
  175. } else
  176. ven_req.direction = USB_DIR_OUT;
  177. /* set the buffer for read / write */
  178. ven_req.pBuff = req_data->p_buffer;
  179. /* call common vendor command request */
  180. status = cx231xx_send_vendor_cmd(dev, &ven_req);
  181. if (status < 0 && !dev->i2c_scan_running) {
  182. dev_err(dev->dev, "%s: failed with status -%d\n",
  183. __func__, status);
  184. }
  185. return status;
  186. }
  187. EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
  188. /*
  189. * Sends/Receives URB control messages, assuring to use a kalloced buffer
  190. * for all operations (dev->urb_buf), to avoid using stacked buffers, as
  191. * they aren't safe for usage with USB, due to DMA restrictions.
  192. * Also implements the debug code for control URB's.
  193. */
  194. static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe,
  195. __u8 request, __u8 requesttype, __u16 value, __u16 index,
  196. void *data, __u16 size, int timeout)
  197. {
  198. int rc, i;
  199. if (reg_debug) {
  200. printk(KERN_DEBUG "%s: (pipe 0x%08x): "
  201. "%s: %02x %02x %02x %02x %02x %02x %02x %02x ",
  202. dev->name,
  203. pipe,
  204. (requesttype & USB_DIR_IN) ? "IN" : "OUT",
  205. requesttype,
  206. request,
  207. value & 0xff, value >> 8,
  208. index & 0xff, index >> 8,
  209. size & 0xff, size >> 8);
  210. if (!(requesttype & USB_DIR_IN)) {
  211. printk(KERN_CONT ">>>");
  212. for (i = 0; i < size; i++)
  213. printk(KERN_CONT " %02x",
  214. ((unsigned char *)data)[i]);
  215. }
  216. }
  217. /* Do the real call to usb_control_msg */
  218. mutex_lock(&dev->ctrl_urb_lock);
  219. if (!(requesttype & USB_DIR_IN) && size)
  220. memcpy(dev->urb_buf, data, size);
  221. rc = usb_control_msg(dev->udev, pipe, request, requesttype, value,
  222. index, dev->urb_buf, size, timeout);
  223. if ((requesttype & USB_DIR_IN) && size)
  224. memcpy(data, dev->urb_buf, size);
  225. mutex_unlock(&dev->ctrl_urb_lock);
  226. if (reg_debug) {
  227. if (unlikely(rc < 0)) {
  228. printk(KERN_CONT "FAILED!\n");
  229. return rc;
  230. }
  231. if ((requesttype & USB_DIR_IN)) {
  232. printk(KERN_CONT "<<<");
  233. for (i = 0; i < size; i++)
  234. printk(KERN_CONT " %02x",
  235. ((unsigned char *)data)[i]);
  236. }
  237. printk(KERN_CONT "\n");
  238. }
  239. return rc;
  240. }
  241. /*
  242. * cx231xx_read_ctrl_reg()
  243. * reads data from the usb device specifying bRequest and wValue
  244. */
  245. int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
  246. char *buf, int len)
  247. {
  248. u8 val = 0;
  249. int ret;
  250. int pipe = usb_rcvctrlpipe(dev->udev, 0);
  251. if (dev->state & DEV_DISCONNECTED)
  252. return -ENODEV;
  253. if (len > URB_MAX_CTRL_SIZE)
  254. return -EINVAL;
  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. }
  271. if (val == 0xFF)
  272. return -EINVAL;
  273. ret = __usb_control_msg(dev, pipe, req,
  274. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  275. val, reg, buf, len, HZ);
  276. return ret;
  277. }
  278. int cx231xx_send_vendor_cmd(struct cx231xx *dev,
  279. struct VENDOR_REQUEST_IN *ven_req)
  280. {
  281. int ret;
  282. int pipe = 0;
  283. int unsend_size = 0;
  284. u8 *pdata;
  285. if (dev->state & DEV_DISCONNECTED)
  286. return -ENODEV;
  287. if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
  288. return -EINVAL;
  289. if (ven_req->direction)
  290. pipe = usb_rcvctrlpipe(dev->udev, 0);
  291. else
  292. pipe = usb_sndctrlpipe(dev->udev, 0);
  293. /*
  294. * If the cx23102 read more than 4 bytes with i2c bus,
  295. * need chop to 4 byte per request
  296. */
  297. if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) ||
  298. (ven_req->bRequest == 0x5) ||
  299. (ven_req->bRequest == 0x6) ||
  300. /* Internal Master 3 Bus can send
  301. * and receive only 4 bytes per time
  302. */
  303. (ven_req->bRequest == 0x2))) {
  304. unsend_size = 0;
  305. pdata = ven_req->pBuff;
  306. unsend_size = ven_req->wLength;
  307. /* the first package */
  308. ven_req->wValue = ven_req->wValue & 0xFFFB;
  309. ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2;
  310. ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
  311. ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  312. ven_req->wValue, ven_req->wIndex, pdata,
  313. 0x0004, HZ);
  314. unsend_size = unsend_size - 4;
  315. /* the middle package */
  316. ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42;
  317. while (unsend_size - 4 > 0) {
  318. pdata = pdata + 4;
  319. ret = __usb_control_msg(dev, pipe,
  320. ven_req->bRequest,
  321. ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  322. ven_req->wValue, ven_req->wIndex, pdata,
  323. 0x0004, HZ);
  324. unsend_size = unsend_size - 4;
  325. }
  326. /* the last package */
  327. ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40;
  328. pdata = pdata + 4;
  329. ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
  330. ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  331. ven_req->wValue, ven_req->wIndex, pdata,
  332. unsend_size, HZ);
  333. } else {
  334. ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
  335. ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  336. ven_req->wValue, ven_req->wIndex,
  337. ven_req->pBuff, ven_req->wLength, HZ);
  338. }
  339. return ret;
  340. }
  341. /*
  342. * cx231xx_write_ctrl_reg()
  343. * sends data to the usb device, specifying bRequest
  344. */
  345. int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
  346. int len)
  347. {
  348. u8 val = 0;
  349. int ret;
  350. int pipe = usb_sndctrlpipe(dev->udev, 0);
  351. if (dev->state & DEV_DISCONNECTED)
  352. return -ENODEV;
  353. if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
  354. return -EINVAL;
  355. switch (len) {
  356. case 1:
  357. val = ENABLE_ONE_BYTE;
  358. break;
  359. case 2:
  360. val = ENABLE_TWE_BYTE;
  361. break;
  362. case 3:
  363. val = ENABLE_THREE_BYTE;
  364. break;
  365. case 4:
  366. val = ENABLE_FOUR_BYTE;
  367. break;
  368. default:
  369. val = 0xFF; /* invalid option */
  370. }
  371. if (val == 0xFF)
  372. return -EINVAL;
  373. if (reg_debug) {
  374. int byte;
  375. cx231xx_isocdbg("(pipe 0x%08x): "
  376. "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
  377. pipe,
  378. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  379. req, 0, val, reg & 0xff,
  380. reg >> 8, len & 0xff, len >> 8);
  381. for (byte = 0; byte < len; byte++)
  382. cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
  383. cx231xx_isocdbg("\n");
  384. }
  385. ret = __usb_control_msg(dev, pipe, req,
  386. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  387. val, reg, buf, len, HZ);
  388. return ret;
  389. }
  390. /****************************************************************
  391. * USB Alternate Setting functions *
  392. *****************************************************************/
  393. int cx231xx_set_video_alternate(struct cx231xx *dev)
  394. {
  395. int errCode, prev_alt = dev->video_mode.alt;
  396. unsigned int min_pkt_size = dev->width * 2 + 4;
  397. u32 usb_interface_index = 0;
  398. /* When image size is bigger than a certain value,
  399. the frame size should be increased, otherwise, only
  400. green screen will be received.
  401. */
  402. if (dev->width * 2 * dev->height > 720 * 240 * 2)
  403. min_pkt_size *= 2;
  404. if (dev->width > 360) {
  405. /* resolutions: 720,704,640 */
  406. dev->video_mode.alt = 3;
  407. } else if (dev->width > 180) {
  408. /* resolutions: 360,352,320,240 */
  409. dev->video_mode.alt = 2;
  410. } else if (dev->width > 0) {
  411. /* resolutions: 180,176,160,128,88 */
  412. dev->video_mode.alt = 1;
  413. } else {
  414. /* Change to alt0 BULK to release USB bandwidth */
  415. dev->video_mode.alt = 0;
  416. }
  417. if (dev->USE_ISO == 0)
  418. dev->video_mode.alt = 0;
  419. cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt);
  420. /* Get the correct video interface Index */
  421. usb_interface_index =
  422. dev->current_pcb_config.hs_config_info[0].interface_info.
  423. video_index + 1;
  424. if (dev->video_mode.alt != prev_alt) {
  425. cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
  426. min_pkt_size, dev->video_mode.alt);
  427. if (dev->video_mode.alt_max_pkt_size != NULL)
  428. dev->video_mode.max_pkt_size =
  429. dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
  430. cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
  431. dev->video_mode.alt,
  432. dev->video_mode.max_pkt_size);
  433. errCode =
  434. usb_set_interface(dev->udev, usb_interface_index,
  435. dev->video_mode.alt);
  436. if (errCode < 0) {
  437. dev_err(dev->dev,
  438. "cannot change alt number to %d (error=%i)\n",
  439. dev->video_mode.alt, errCode);
  440. return errCode;
  441. }
  442. }
  443. return 0;
  444. }
  445. int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
  446. {
  447. int status = 0;
  448. u32 usb_interface_index = 0;
  449. u32 max_pkt_size = 0;
  450. switch (index) {
  451. case INDEX_TS1:
  452. usb_interface_index =
  453. dev->current_pcb_config.hs_config_info[0].interface_info.
  454. ts1_index + 1;
  455. dev->ts1_mode.alt = alt;
  456. if (dev->ts1_mode.alt_max_pkt_size != NULL)
  457. max_pkt_size = dev->ts1_mode.max_pkt_size =
  458. dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
  459. break;
  460. case INDEX_TS2:
  461. usb_interface_index =
  462. dev->current_pcb_config.hs_config_info[0].interface_info.
  463. ts2_index + 1;
  464. break;
  465. case INDEX_AUDIO:
  466. usb_interface_index =
  467. dev->current_pcb_config.hs_config_info[0].interface_info.
  468. audio_index + 1;
  469. dev->adev.alt = alt;
  470. if (dev->adev.alt_max_pkt_size != NULL)
  471. max_pkt_size = dev->adev.max_pkt_size =
  472. dev->adev.alt_max_pkt_size[dev->adev.alt];
  473. break;
  474. case INDEX_VIDEO:
  475. usb_interface_index =
  476. dev->current_pcb_config.hs_config_info[0].interface_info.
  477. video_index + 1;
  478. dev->video_mode.alt = alt;
  479. if (dev->video_mode.alt_max_pkt_size != NULL)
  480. max_pkt_size = dev->video_mode.max_pkt_size =
  481. dev->video_mode.alt_max_pkt_size[dev->video_mode.
  482. alt];
  483. break;
  484. case INDEX_VANC:
  485. if (dev->board.no_alt_vanc)
  486. return 0;
  487. usb_interface_index =
  488. dev->current_pcb_config.hs_config_info[0].interface_info.
  489. vanc_index + 1;
  490. dev->vbi_mode.alt = alt;
  491. if (dev->vbi_mode.alt_max_pkt_size != NULL)
  492. max_pkt_size = dev->vbi_mode.max_pkt_size =
  493. dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
  494. break;
  495. case INDEX_HANC:
  496. usb_interface_index =
  497. dev->current_pcb_config.hs_config_info[0].interface_info.
  498. hanc_index + 1;
  499. dev->sliced_cc_mode.alt = alt;
  500. if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
  501. max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
  502. dev->sliced_cc_mode.alt_max_pkt_size[dev->
  503. sliced_cc_mode.
  504. alt];
  505. break;
  506. default:
  507. break;
  508. }
  509. if (alt > 0 && max_pkt_size == 0) {
  510. dev_err(dev->dev,
  511. "can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
  512. usb_interface_index, alt);
  513. /*To workaround error number=-71 on EP0 for videograbber,
  514. need add following codes.*/
  515. if (dev->board.no_alt_vanc)
  516. return -1;
  517. }
  518. cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,"
  519. "Interface = %d\n", alt, max_pkt_size,
  520. usb_interface_index);
  521. if (usb_interface_index > 0) {
  522. status = usb_set_interface(dev->udev, usb_interface_index, alt);
  523. if (status < 0) {
  524. dev_err(dev->dev,
  525. "can't change interface %d alt no. to %d (err=%i)\n",
  526. usb_interface_index, alt, status);
  527. return status;
  528. }
  529. }
  530. return status;
  531. }
  532. EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
  533. int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
  534. {
  535. int rc = 0;
  536. if (!gpio)
  537. return rc;
  538. /* Send GPIO reset sequences specified at board entry */
  539. while (gpio->sleep >= 0) {
  540. rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
  541. if (rc < 0)
  542. return rc;
  543. if (gpio->sleep > 0)
  544. msleep(gpio->sleep);
  545. gpio++;
  546. }
  547. return rc;
  548. }
  549. int cx231xx_demod_reset(struct cx231xx *dev)
  550. {
  551. u8 status = 0;
  552. u8 value[4] = { 0, 0, 0, 0 };
  553. status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
  554. value, 4);
  555. cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
  556. value[0], value[1], value[2], value[3]);
  557. cx231xx_coredbg("Enter cx231xx_demod_reset()\n");
  558. value[1] = (u8) 0x3;
  559. status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  560. PWR_CTL_EN, value, 4);
  561. msleep(10);
  562. value[1] = (u8) 0x0;
  563. status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  564. PWR_CTL_EN, value, 4);
  565. msleep(10);
  566. value[1] = (u8) 0x3;
  567. status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  568. PWR_CTL_EN, value, 4);
  569. msleep(10);
  570. status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
  571. value, 4);
  572. cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
  573. value[0], value[1], value[2], value[3]);
  574. return status;
  575. }
  576. EXPORT_SYMBOL_GPL(cx231xx_demod_reset);
  577. int is_fw_load(struct cx231xx *dev)
  578. {
  579. return cx231xx_check_fw(dev);
  580. }
  581. EXPORT_SYMBOL_GPL(is_fw_load);
  582. int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
  583. {
  584. int errCode = 0;
  585. if (dev->mode == set_mode)
  586. return 0;
  587. if (set_mode == CX231XX_SUSPEND) {
  588. /* Set the chip in power saving mode */
  589. dev->mode = set_mode;
  590. }
  591. /* Resource is locked */
  592. if (dev->mode != CX231XX_SUSPEND)
  593. return -EINVAL;
  594. dev->mode = set_mode;
  595. if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ {
  596. /* set AGC mode to Digital */
  597. switch (dev->model) {
  598. case CX231XX_BOARD_CNXT_CARRAERA:
  599. case CX231XX_BOARD_CNXT_RDE_250:
  600. case CX231XX_BOARD_CNXT_SHELBY:
  601. case CX231XX_BOARD_CNXT_RDU_250:
  602. errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
  603. break;
  604. case CX231XX_BOARD_CNXT_RDE_253S:
  605. case CX231XX_BOARD_CNXT_RDU_253S:
  606. case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
  607. errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
  608. break;
  609. case CX231XX_BOARD_HAUPPAUGE_EXETER:
  610. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
  611. errCode = cx231xx_set_power_mode(dev,
  612. POLARIS_AVMODE_DIGITAL);
  613. break;
  614. default:
  615. break;
  616. }
  617. } else/* Set Analog Power mode */ {
  618. /* set AGC mode to Analog */
  619. switch (dev->model) {
  620. case CX231XX_BOARD_CNXT_CARRAERA:
  621. case CX231XX_BOARD_CNXT_RDE_250:
  622. case CX231XX_BOARD_CNXT_SHELBY:
  623. case CX231XX_BOARD_CNXT_RDU_250:
  624. errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
  625. break;
  626. case CX231XX_BOARD_CNXT_RDE_253S:
  627. case CX231XX_BOARD_CNXT_RDU_253S:
  628. case CX231XX_BOARD_HAUPPAUGE_EXETER:
  629. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
  630. case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
  631. case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
  632. case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
  633. errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
  634. break;
  635. default:
  636. break;
  637. }
  638. }
  639. if (errCode < 0) {
  640. dev_err(dev->dev, "Failed to set devmode to %s: error: %i",
  641. dev->mode == CX231XX_DIGITAL_MODE ? "digital" : "analog",
  642. errCode);
  643. return errCode;
  644. }
  645. return 0;
  646. }
  647. EXPORT_SYMBOL_GPL(cx231xx_set_mode);
  648. int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size)
  649. {
  650. int errCode = 0;
  651. int actlen = -1;
  652. int ret = -ENOMEM;
  653. u32 *buffer;
  654. buffer = kzalloc(4096, GFP_KERNEL);
  655. if (buffer == NULL)
  656. return -ENOMEM;
  657. memcpy(&buffer[0], firmware, 4096);
  658. ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5),
  659. buffer, 4096, &actlen, 2000);
  660. if (ret)
  661. dev_err(dev->dev,
  662. "bulk message failed: %d (%d/%d)", ret,
  663. size, actlen);
  664. else {
  665. errCode = actlen != size ? -1 : 0;
  666. }
  667. kfree(buffer);
  668. return errCode;
  669. }
  670. /*****************************************************************
  671. * URB Streaming functions *
  672. ******************************************************************/
  673. /*
  674. * IRQ callback, called by URB callback
  675. */
  676. static void cx231xx_isoc_irq_callback(struct urb *urb)
  677. {
  678. struct cx231xx_dmaqueue *dma_q = urb->context;
  679. struct cx231xx_video_mode *vmode =
  680. container_of(dma_q, struct cx231xx_video_mode, vidq);
  681. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  682. int i;
  683. switch (urb->status) {
  684. case 0: /* success */
  685. case -ETIMEDOUT: /* NAK */
  686. break;
  687. case -ECONNRESET: /* kill */
  688. case -ENOENT:
  689. case -ESHUTDOWN:
  690. return;
  691. default: /* error */
  692. cx231xx_isocdbg("urb completion error %d.\n", urb->status);
  693. break;
  694. }
  695. /* Copy data from URB */
  696. spin_lock(&dev->video_mode.slock);
  697. dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
  698. spin_unlock(&dev->video_mode.slock);
  699. /* Reset urb buffers */
  700. for (i = 0; i < urb->number_of_packets; i++) {
  701. urb->iso_frame_desc[i].status = 0;
  702. urb->iso_frame_desc[i].actual_length = 0;
  703. }
  704. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  705. if (urb->status) {
  706. cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
  707. urb->status);
  708. }
  709. }
  710. /*****************************************************************
  711. * URB Streaming functions *
  712. ******************************************************************/
  713. /*
  714. * IRQ callback, called by URB callback
  715. */
  716. static void cx231xx_bulk_irq_callback(struct urb *urb)
  717. {
  718. struct cx231xx_dmaqueue *dma_q = urb->context;
  719. struct cx231xx_video_mode *vmode =
  720. container_of(dma_q, struct cx231xx_video_mode, vidq);
  721. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  722. switch (urb->status) {
  723. case 0: /* success */
  724. case -ETIMEDOUT: /* NAK */
  725. break;
  726. case -ECONNRESET: /* kill */
  727. case -ENOENT:
  728. case -ESHUTDOWN:
  729. return;
  730. case -EPIPE: /* stall */
  731. cx231xx_isocdbg("urb completion error - device is stalled.\n");
  732. return;
  733. default: /* error */
  734. cx231xx_isocdbg("urb completion error %d.\n", urb->status);
  735. break;
  736. }
  737. /* Copy data from URB */
  738. spin_lock(&dev->video_mode.slock);
  739. dev->video_mode.bulk_ctl.bulk_copy(dev, urb);
  740. spin_unlock(&dev->video_mode.slock);
  741. /* Reset urb buffers */
  742. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  743. if (urb->status) {
  744. cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
  745. urb->status);
  746. }
  747. }
  748. /*
  749. * Stop and Deallocate URBs
  750. */
  751. void cx231xx_uninit_isoc(struct cx231xx *dev)
  752. {
  753. struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
  754. struct urb *urb;
  755. int i;
  756. bool broken_pipe = false;
  757. cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
  758. dev->video_mode.isoc_ctl.nfields = -1;
  759. for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
  760. urb = dev->video_mode.isoc_ctl.urb[i];
  761. if (urb) {
  762. if (!irqs_disabled())
  763. usb_kill_urb(urb);
  764. else
  765. usb_unlink_urb(urb);
  766. if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
  767. usb_free_coherent(dev->udev,
  768. urb->transfer_buffer_length,
  769. dev->video_mode.isoc_ctl.
  770. transfer_buffer[i],
  771. urb->transfer_dma);
  772. }
  773. if (urb->status == -EPIPE) {
  774. broken_pipe = true;
  775. }
  776. usb_free_urb(urb);
  777. dev->video_mode.isoc_ctl.urb[i] = NULL;
  778. }
  779. dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
  780. }
  781. if (broken_pipe) {
  782. cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
  783. usb_reset_endpoint(dev->udev, dev->video_mode.end_point_addr);
  784. }
  785. kfree(dev->video_mode.isoc_ctl.urb);
  786. kfree(dev->video_mode.isoc_ctl.transfer_buffer);
  787. kfree(dma_q->p_left_data);
  788. dev->video_mode.isoc_ctl.urb = NULL;
  789. dev->video_mode.isoc_ctl.transfer_buffer = NULL;
  790. dev->video_mode.isoc_ctl.num_bufs = 0;
  791. dma_q->p_left_data = NULL;
  792. if (dev->mode_tv == 0)
  793. cx231xx_capture_start(dev, 0, Raw_Video);
  794. else
  795. cx231xx_capture_start(dev, 0, TS1_serial_mode);
  796. }
  797. EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
  798. /*
  799. * Stop and Deallocate URBs
  800. */
  801. void cx231xx_uninit_bulk(struct cx231xx *dev)
  802. {
  803. struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
  804. struct urb *urb;
  805. int i;
  806. bool broken_pipe = false;
  807. cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");
  808. dev->video_mode.bulk_ctl.nfields = -1;
  809. for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
  810. urb = dev->video_mode.bulk_ctl.urb[i];
  811. if (urb) {
  812. if (!irqs_disabled())
  813. usb_kill_urb(urb);
  814. else
  815. usb_unlink_urb(urb);
  816. if (dev->video_mode.bulk_ctl.transfer_buffer[i]) {
  817. usb_free_coherent(dev->udev,
  818. urb->transfer_buffer_length,
  819. dev->video_mode.bulk_ctl.
  820. transfer_buffer[i],
  821. urb->transfer_dma);
  822. }
  823. if (urb->status == -EPIPE) {
  824. broken_pipe = true;
  825. }
  826. usb_free_urb(urb);
  827. dev->video_mode.bulk_ctl.urb[i] = NULL;
  828. }
  829. dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL;
  830. }
  831. if (broken_pipe) {
  832. cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
  833. usb_reset_endpoint(dev->udev, dev->video_mode.end_point_addr);
  834. }
  835. kfree(dev->video_mode.bulk_ctl.urb);
  836. kfree(dev->video_mode.bulk_ctl.transfer_buffer);
  837. kfree(dma_q->p_left_data);
  838. dev->video_mode.bulk_ctl.urb = NULL;
  839. dev->video_mode.bulk_ctl.transfer_buffer = NULL;
  840. dev->video_mode.bulk_ctl.num_bufs = 0;
  841. dma_q->p_left_data = NULL;
  842. if (dev->mode_tv == 0)
  843. cx231xx_capture_start(dev, 0, Raw_Video);
  844. else
  845. cx231xx_capture_start(dev, 0, TS1_serial_mode);
  846. }
  847. EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk);
  848. /*
  849. * Allocate URBs and start IRQ
  850. */
  851. int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
  852. int num_bufs, int max_pkt_size,
  853. int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
  854. {
  855. struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
  856. int i;
  857. int sb_size, pipe;
  858. struct urb *urb;
  859. int j, k;
  860. int rc;
  861. /* De-allocates all pending stuff */
  862. cx231xx_uninit_isoc(dev);
  863. dma_q->p_left_data = kzalloc(4096, GFP_KERNEL);
  864. if (dma_q->p_left_data == NULL)
  865. return -ENOMEM;
  866. dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
  867. dev->video_mode.isoc_ctl.num_bufs = num_bufs;
  868. dma_q->pos = 0;
  869. dma_q->is_partial_line = 0;
  870. dma_q->last_sav = 0;
  871. dma_q->current_field = -1;
  872. dma_q->field1_done = 0;
  873. dma_q->lines_per_field = dev->height / 2;
  874. dma_q->bytes_left_in_line = dev->width << 1;
  875. dma_q->lines_completed = 0;
  876. dma_q->mpeg_buffer_done = 0;
  877. dma_q->left_data_count = 0;
  878. dma_q->mpeg_buffer_completed = 0;
  879. dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD;
  880. dma_q->ps_head[0] = 0x00;
  881. dma_q->ps_head[1] = 0x00;
  882. dma_q->ps_head[2] = 0x01;
  883. dma_q->ps_head[3] = 0xBA;
  884. for (i = 0; i < 8; i++)
  885. dma_q->partial_buf[i] = 0;
  886. dev->video_mode.isoc_ctl.urb =
  887. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  888. if (!dev->video_mode.isoc_ctl.urb) {
  889. dev_err(dev->dev,
  890. "cannot alloc memory for usb buffers\n");
  891. return -ENOMEM;
  892. }
  893. dev->video_mode.isoc_ctl.transfer_buffer =
  894. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  895. if (!dev->video_mode.isoc_ctl.transfer_buffer) {
  896. dev_err(dev->dev,
  897. "cannot allocate memory for usbtransfer\n");
  898. kfree(dev->video_mode.isoc_ctl.urb);
  899. return -ENOMEM;
  900. }
  901. dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
  902. dev->video_mode.isoc_ctl.buf = NULL;
  903. sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
  904. if (dev->mode_tv == 1)
  905. dev->video_mode.end_point_addr = 0x81;
  906. else
  907. dev->video_mode.end_point_addr = 0x84;
  908. /* allocate urbs and transfer buffers */
  909. for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
  910. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  911. if (!urb) {
  912. cx231xx_uninit_isoc(dev);
  913. return -ENOMEM;
  914. }
  915. dev->video_mode.isoc_ctl.urb[i] = urb;
  916. dev->video_mode.isoc_ctl.transfer_buffer[i] =
  917. usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
  918. &urb->transfer_dma);
  919. if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
  920. dev_err(dev->dev,
  921. "unable to allocate %i bytes for transfer buffer %i%s\n",
  922. sb_size, i,
  923. in_interrupt() ? " while in int" : "");
  924. cx231xx_uninit_isoc(dev);
  925. return -ENOMEM;
  926. }
  927. memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
  928. pipe =
  929. usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
  930. usb_fill_int_urb(urb, dev->udev, pipe,
  931. dev->video_mode.isoc_ctl.transfer_buffer[i],
  932. sb_size, cx231xx_isoc_irq_callback, dma_q, 1);
  933. urb->number_of_packets = max_packets;
  934. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  935. k = 0;
  936. for (j = 0; j < max_packets; j++) {
  937. urb->iso_frame_desc[j].offset = k;
  938. urb->iso_frame_desc[j].length =
  939. dev->video_mode.isoc_ctl.max_pkt_size;
  940. k += dev->video_mode.isoc_ctl.max_pkt_size;
  941. }
  942. }
  943. init_waitqueue_head(&dma_q->wq);
  944. /* submit urbs and enables IRQ */
  945. for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
  946. rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
  947. GFP_ATOMIC);
  948. if (rc) {
  949. dev_err(dev->dev,
  950. "submit of urb %i failed (error=%i)\n", i,
  951. rc);
  952. cx231xx_uninit_isoc(dev);
  953. return rc;
  954. }
  955. }
  956. if (dev->mode_tv == 0)
  957. cx231xx_capture_start(dev, 1, Raw_Video);
  958. else
  959. cx231xx_capture_start(dev, 1, TS1_serial_mode);
  960. return 0;
  961. }
  962. EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
  963. /*
  964. * Allocate URBs and start IRQ
  965. */
  966. int cx231xx_init_bulk(struct cx231xx *dev, int max_packets,
  967. int num_bufs, int max_pkt_size,
  968. int (*bulk_copy) (struct cx231xx *dev, struct urb *urb))
  969. {
  970. struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
  971. int i;
  972. int sb_size, pipe;
  973. struct urb *urb;
  974. int rc;
  975. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  976. cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input);
  977. video_mux(dev, dev->video_input);
  978. /* De-allocates all pending stuff */
  979. cx231xx_uninit_bulk(dev);
  980. dev->video_mode.bulk_ctl.bulk_copy = bulk_copy;
  981. dev->video_mode.bulk_ctl.num_bufs = num_bufs;
  982. dma_q->pos = 0;
  983. dma_q->is_partial_line = 0;
  984. dma_q->last_sav = 0;
  985. dma_q->current_field = -1;
  986. dma_q->field1_done = 0;
  987. dma_q->lines_per_field = dev->height / 2;
  988. dma_q->bytes_left_in_line = dev->width << 1;
  989. dma_q->lines_completed = 0;
  990. dma_q->mpeg_buffer_done = 0;
  991. dma_q->left_data_count = 0;
  992. dma_q->mpeg_buffer_completed = 0;
  993. dma_q->ps_head[0] = 0x00;
  994. dma_q->ps_head[1] = 0x00;
  995. dma_q->ps_head[2] = 0x01;
  996. dma_q->ps_head[3] = 0xBA;
  997. for (i = 0; i < 8; i++)
  998. dma_q->partial_buf[i] = 0;
  999. dev->video_mode.bulk_ctl.urb =
  1000. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  1001. if (!dev->video_mode.bulk_ctl.urb) {
  1002. dev_err(dev->dev,
  1003. "cannot alloc memory for usb buffers\n");
  1004. return -ENOMEM;
  1005. }
  1006. dev->video_mode.bulk_ctl.transfer_buffer =
  1007. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  1008. if (!dev->video_mode.bulk_ctl.transfer_buffer) {
  1009. dev_err(dev->dev,
  1010. "cannot allocate memory for usbtransfer\n");
  1011. kfree(dev->video_mode.bulk_ctl.urb);
  1012. return -ENOMEM;
  1013. }
  1014. dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size;
  1015. dev->video_mode.bulk_ctl.buf = NULL;
  1016. sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size;
  1017. if (dev->mode_tv == 1)
  1018. dev->video_mode.end_point_addr = 0x81;
  1019. else
  1020. dev->video_mode.end_point_addr = 0x84;
  1021. /* allocate urbs and transfer buffers */
  1022. for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
  1023. urb = usb_alloc_urb(0, GFP_KERNEL);
  1024. if (!urb) {
  1025. cx231xx_uninit_bulk(dev);
  1026. return -ENOMEM;
  1027. }
  1028. dev->video_mode.bulk_ctl.urb[i] = urb;
  1029. urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  1030. dev->video_mode.bulk_ctl.transfer_buffer[i] =
  1031. usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
  1032. &urb->transfer_dma);
  1033. if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) {
  1034. dev_err(dev->dev,
  1035. "unable to allocate %i bytes for transfer buffer %i%s\n",
  1036. sb_size, i,
  1037. in_interrupt() ? " while in int" : "");
  1038. cx231xx_uninit_bulk(dev);
  1039. return -ENOMEM;
  1040. }
  1041. memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size);
  1042. pipe = usb_rcvbulkpipe(dev->udev,
  1043. dev->video_mode.end_point_addr);
  1044. usb_fill_bulk_urb(urb, dev->udev, pipe,
  1045. dev->video_mode.bulk_ctl.transfer_buffer[i],
  1046. sb_size, cx231xx_bulk_irq_callback, dma_q);
  1047. }
  1048. /* clear halt */
  1049. rc = usb_clear_halt(dev->udev, dev->video_mode.bulk_ctl.urb[0]->pipe);
  1050. if (rc < 0) {
  1051. dev_err(dev->dev,
  1052. "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
  1053. rc);
  1054. cx231xx_uninit_bulk(dev);
  1055. return rc;
  1056. }
  1057. init_waitqueue_head(&dma_q->wq);
  1058. /* submit urbs and enables IRQ */
  1059. for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
  1060. rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i],
  1061. GFP_ATOMIC);
  1062. if (rc) {
  1063. dev_err(dev->dev,
  1064. "submit of urb %i failed (error=%i)\n", i, rc);
  1065. cx231xx_uninit_bulk(dev);
  1066. return rc;
  1067. }
  1068. }
  1069. if (dev->mode_tv == 0)
  1070. cx231xx_capture_start(dev, 1, Raw_Video);
  1071. else
  1072. cx231xx_capture_start(dev, 1, TS1_serial_mode);
  1073. return 0;
  1074. }
  1075. EXPORT_SYMBOL_GPL(cx231xx_init_bulk);
  1076. void cx231xx_stop_TS1(struct cx231xx *dev)
  1077. {
  1078. u8 val[4] = { 0, 0, 0, 0 };
  1079. val[0] = 0x00;
  1080. val[1] = 0x03;
  1081. val[2] = 0x00;
  1082. val[3] = 0x00;
  1083. cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1084. TS_MODE_REG, val, 4);
  1085. val[0] = 0x00;
  1086. val[1] = 0x70;
  1087. val[2] = 0x04;
  1088. val[3] = 0x00;
  1089. cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1090. TS1_CFG_REG, val, 4);
  1091. }
  1092. /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
  1093. void cx231xx_start_TS1(struct cx231xx *dev)
  1094. {
  1095. u8 val[4] = { 0, 0, 0, 0 };
  1096. val[0] = 0x03;
  1097. val[1] = 0x03;
  1098. val[2] = 0x00;
  1099. val[3] = 0x00;
  1100. cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1101. TS_MODE_REG, val, 4);
  1102. val[0] = 0x04;
  1103. val[1] = 0xA3;
  1104. val[2] = 0x3B;
  1105. val[3] = 0x00;
  1106. cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1107. TS1_CFG_REG, val, 4);
  1108. }
  1109. /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
  1110. /*****************************************************************
  1111. * Device Init/UnInit functions *
  1112. ******************************************************************/
  1113. int cx231xx_dev_init(struct cx231xx *dev)
  1114. {
  1115. int errCode = 0;
  1116. /* Initialize I2C bus */
  1117. /* External Master 1 Bus */
  1118. dev->i2c_bus[0].nr = 0;
  1119. dev->i2c_bus[0].dev = dev;
  1120. dev->i2c_bus[0].i2c_period = I2C_SPEED_100K; /* 100 KHz */
  1121. dev->i2c_bus[0].i2c_nostop = 0;
  1122. dev->i2c_bus[0].i2c_reserve = 0;
  1123. /* External Master 2 Bus */
  1124. dev->i2c_bus[1].nr = 1;
  1125. dev->i2c_bus[1].dev = dev;
  1126. dev->i2c_bus[1].i2c_period = I2C_SPEED_100K; /* 100 KHz */
  1127. dev->i2c_bus[1].i2c_nostop = 0;
  1128. dev->i2c_bus[1].i2c_reserve = 0;
  1129. /* Internal Master 3 Bus */
  1130. dev->i2c_bus[2].nr = 2;
  1131. dev->i2c_bus[2].dev = dev;
  1132. dev->i2c_bus[2].i2c_period = I2C_SPEED_100K; /* 100kHz */
  1133. dev->i2c_bus[2].i2c_nostop = 0;
  1134. dev->i2c_bus[2].i2c_reserve = 0;
  1135. /* register I2C buses */
  1136. errCode = cx231xx_i2c_register(&dev->i2c_bus[0]);
  1137. if (errCode < 0)
  1138. return errCode;
  1139. errCode = cx231xx_i2c_register(&dev->i2c_bus[1]);
  1140. if (errCode < 0)
  1141. return errCode;
  1142. errCode = cx231xx_i2c_register(&dev->i2c_bus[2]);
  1143. if (errCode < 0)
  1144. return errCode;
  1145. errCode = cx231xx_i2c_mux_create(dev);
  1146. if (errCode < 0) {
  1147. dev_err(dev->dev,
  1148. "%s: Failed to create I2C mux\n", __func__);
  1149. return errCode;
  1150. }
  1151. errCode = cx231xx_i2c_mux_register(dev, 0);
  1152. if (errCode < 0)
  1153. return errCode;
  1154. errCode = cx231xx_i2c_mux_register(dev, 1);
  1155. if (errCode < 0)
  1156. return errCode;
  1157. /* scan the real bus segments in the order of physical port numbers */
  1158. cx231xx_do_i2c_scan(dev, I2C_0);
  1159. cx231xx_do_i2c_scan(dev, I2C_1_MUX_1);
  1160. cx231xx_do_i2c_scan(dev, I2C_2);
  1161. cx231xx_do_i2c_scan(dev, I2C_1_MUX_3);
  1162. /* init hardware */
  1163. /* Note : with out calling set power mode function,
  1164. afe can not be set up correctly */
  1165. if (dev->board.external_av) {
  1166. errCode = cx231xx_set_power_mode(dev,
  1167. POLARIS_AVMODE_ENXTERNAL_AV);
  1168. if (errCode < 0) {
  1169. dev_err(dev->dev,
  1170. "%s: Failed to set Power - errCode [%d]!\n",
  1171. __func__, errCode);
  1172. return errCode;
  1173. }
  1174. } else {
  1175. errCode = cx231xx_set_power_mode(dev,
  1176. POLARIS_AVMODE_ANALOGT_TV);
  1177. if (errCode < 0) {
  1178. dev_err(dev->dev,
  1179. "%s: Failed to set Power - errCode [%d]!\n",
  1180. __func__, errCode);
  1181. return errCode;
  1182. }
  1183. }
  1184. /* reset the Tuner, if it is a Xceive tuner */
  1185. if ((dev->board.tuner_type == TUNER_XC5000) ||
  1186. (dev->board.tuner_type == TUNER_XC2028))
  1187. cx231xx_gpio_set(dev, dev->board.tuner_gpio);
  1188. /* initialize Colibri block */
  1189. errCode = cx231xx_afe_init_super_block(dev, 0x23c);
  1190. if (errCode < 0) {
  1191. dev_err(dev->dev,
  1192. "%s: cx231xx_afe init super block - errCode [%d]!\n",
  1193. __func__, errCode);
  1194. return errCode;
  1195. }
  1196. errCode = cx231xx_afe_init_channels(dev);
  1197. if (errCode < 0) {
  1198. dev_err(dev->dev,
  1199. "%s: cx231xx_afe init channels - errCode [%d]!\n",
  1200. __func__, errCode);
  1201. return errCode;
  1202. }
  1203. /* Set DIF in By pass mode */
  1204. errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
  1205. if (errCode < 0) {
  1206. dev_err(dev->dev,
  1207. "%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
  1208. __func__, errCode);
  1209. return errCode;
  1210. }
  1211. /* I2S block related functions */
  1212. errCode = cx231xx_i2s_blk_initialize(dev);
  1213. if (errCode < 0) {
  1214. dev_err(dev->dev,
  1215. "%s: cx231xx_i2s block initialize - errCode [%d]!\n",
  1216. __func__, errCode);
  1217. return errCode;
  1218. }
  1219. /* init control pins */
  1220. errCode = cx231xx_init_ctrl_pin_status(dev);
  1221. if (errCode < 0) {
  1222. dev_err(dev->dev,
  1223. "%s: cx231xx_init ctrl pins - errCode [%d]!\n",
  1224. __func__, errCode);
  1225. return errCode;
  1226. }
  1227. /* set AGC mode to Analog */
  1228. switch (dev->model) {
  1229. case CX231XX_BOARD_CNXT_CARRAERA:
  1230. case CX231XX_BOARD_CNXT_RDE_250:
  1231. case CX231XX_BOARD_CNXT_SHELBY:
  1232. case CX231XX_BOARD_CNXT_RDU_250:
  1233. errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
  1234. break;
  1235. case CX231XX_BOARD_CNXT_RDE_253S:
  1236. case CX231XX_BOARD_CNXT_RDU_253S:
  1237. case CX231XX_BOARD_HAUPPAUGE_EXETER:
  1238. case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
  1239. case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
  1240. case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
  1241. case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
  1242. errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. if (errCode < 0) {
  1248. dev_err(dev->dev,
  1249. "%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
  1250. __func__, errCode);
  1251. return errCode;
  1252. }
  1253. /* set all alternate settings to zero initially */
  1254. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1255. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1256. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1257. if (dev->board.has_dvb)
  1258. cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
  1259. errCode = 0;
  1260. return errCode;
  1261. }
  1262. EXPORT_SYMBOL_GPL(cx231xx_dev_init);
  1263. void cx231xx_dev_uninit(struct cx231xx *dev)
  1264. {
  1265. /* Un Initialize I2C bus */
  1266. cx231xx_i2c_mux_unregister(dev);
  1267. cx231xx_i2c_unregister(&dev->i2c_bus[2]);
  1268. cx231xx_i2c_unregister(&dev->i2c_bus[1]);
  1269. cx231xx_i2c_unregister(&dev->i2c_bus[0]);
  1270. }
  1271. EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
  1272. /*****************************************************************
  1273. * G P I O related functions *
  1274. ******************************************************************/
  1275. int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
  1276. u8 len, u8 request, u8 direction)
  1277. {
  1278. int status = 0;
  1279. struct VENDOR_REQUEST_IN ven_req;
  1280. /* Set wValue */
  1281. ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
  1282. /* set request */
  1283. if (!request) {
  1284. if (direction)
  1285. ven_req.bRequest = VRT_GET_GPIO; /* 0x9 gpio */
  1286. else
  1287. ven_req.bRequest = VRT_SET_GPIO; /* 0x8 gpio */
  1288. } else {
  1289. if (direction)
  1290. ven_req.bRequest = VRT_GET_GPIE; /* 0xb gpie */
  1291. else
  1292. ven_req.bRequest = VRT_SET_GPIE; /* 0xa gpie */
  1293. }
  1294. /* set index value */
  1295. ven_req.wIndex = (u16) (gpio_bit & 0xffff);
  1296. /* set wLength value */
  1297. ven_req.wLength = len;
  1298. /* set bData value */
  1299. ven_req.bData = 0;
  1300. /* set the buffer for read / write */
  1301. ven_req.pBuff = gpio_val;
  1302. /* set the direction */
  1303. if (direction) {
  1304. ven_req.direction = USB_DIR_IN;
  1305. memset(ven_req.pBuff, 0x00, ven_req.wLength);
  1306. } else
  1307. ven_req.direction = USB_DIR_OUT;
  1308. /* call common vendor command request */
  1309. status = cx231xx_send_vendor_cmd(dev, &ven_req);
  1310. if (status < 0) {
  1311. dev_err(dev->dev, "%s: failed with status -%d\n",
  1312. __func__, status);
  1313. }
  1314. return status;
  1315. }
  1316. EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
  1317. /*****************************************************************
  1318. * C O N T R O L - Register R E A D / W R I T E functions *
  1319. *****************************************************************/
  1320. int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
  1321. {
  1322. u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
  1323. u32 tmp = 0;
  1324. int status = 0;
  1325. status =
  1326. cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
  1327. if (status < 0)
  1328. return status;
  1329. tmp = le32_to_cpu(*((__le32 *) value));
  1330. tmp |= mode;
  1331. value[0] = (u8) tmp;
  1332. value[1] = (u8) (tmp >> 8);
  1333. value[2] = (u8) (tmp >> 16);
  1334. value[3] = (u8) (tmp >> 24);
  1335. status =
  1336. cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
  1337. return status;
  1338. }
  1339. /*****************************************************************
  1340. * I 2 C Internal C O N T R O L functions *
  1341. *****************************************************************/
  1342. int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
  1343. u8 saddr_len, u32 *data, u8 data_len, int master)
  1344. {
  1345. int status = 0;
  1346. struct cx231xx_i2c_xfer_data req_data;
  1347. u8 value[64] = "0";
  1348. if (saddr_len == 0)
  1349. saddr = 0;
  1350. else if (saddr_len == 1)
  1351. saddr &= 0xff;
  1352. /* prepare xfer_data struct */
  1353. req_data.dev_addr = dev_addr >> 1;
  1354. req_data.direction = I2C_M_RD;
  1355. req_data.saddr_len = saddr_len;
  1356. req_data.saddr_dat = saddr;
  1357. req_data.buf_size = data_len;
  1358. req_data.p_buffer = (u8 *) value;
  1359. /* usb send command */
  1360. if (master == 0)
  1361. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
  1362. &req_data);
  1363. else if (master == 1)
  1364. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
  1365. &req_data);
  1366. else if (master == 2)
  1367. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
  1368. &req_data);
  1369. if (status >= 0) {
  1370. /* Copy the data read back to main buffer */
  1371. if (data_len == 1)
  1372. *data = value[0];
  1373. else if (data_len == 4)
  1374. *data =
  1375. value[0] | value[1] << 8 | value[2] << 16 | value[3]
  1376. << 24;
  1377. else if (data_len > 4)
  1378. *data = value[saddr];
  1379. }
  1380. return status;
  1381. }
  1382. int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
  1383. u8 saddr_len, u32 data, u8 data_len, int master)
  1384. {
  1385. int status = 0;
  1386. u8 value[4] = { 0, 0, 0, 0 };
  1387. struct cx231xx_i2c_xfer_data req_data;
  1388. value[0] = (u8) data;
  1389. value[1] = (u8) (data >> 8);
  1390. value[2] = (u8) (data >> 16);
  1391. value[3] = (u8) (data >> 24);
  1392. if (saddr_len == 0)
  1393. saddr = 0;
  1394. else if (saddr_len == 1)
  1395. saddr &= 0xff;
  1396. /* prepare xfer_data struct */
  1397. req_data.dev_addr = dev_addr >> 1;
  1398. req_data.direction = 0;
  1399. req_data.saddr_len = saddr_len;
  1400. req_data.saddr_dat = saddr;
  1401. req_data.buf_size = data_len;
  1402. req_data.p_buffer = value;
  1403. /* usb send command */
  1404. if (master == 0)
  1405. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
  1406. &req_data);
  1407. else if (master == 1)
  1408. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
  1409. &req_data);
  1410. else if (master == 2)
  1411. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
  1412. &req_data);
  1413. return status;
  1414. }
  1415. int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
  1416. u8 saddr_len, u32 *data, u8 data_len)
  1417. {
  1418. int status = 0;
  1419. struct cx231xx_i2c_xfer_data req_data;
  1420. u8 value[4] = { 0, 0, 0, 0 };
  1421. if (saddr_len == 0)
  1422. saddr = 0;
  1423. else if (saddr_len == 1)
  1424. saddr &= 0xff;
  1425. /* prepare xfer_data struct */
  1426. req_data.dev_addr = dev_addr >> 1;
  1427. req_data.direction = I2C_M_RD;
  1428. req_data.saddr_len = saddr_len;
  1429. req_data.saddr_dat = saddr;
  1430. req_data.buf_size = data_len;
  1431. req_data.p_buffer = (u8 *) value;
  1432. /* usb send command */
  1433. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
  1434. if (status >= 0) {
  1435. /* Copy the data read back to main buffer */
  1436. if (data_len == 1)
  1437. *data = value[0];
  1438. else
  1439. *data =
  1440. value[0] | value[1] << 8 | value[2] << 16 | value[3]
  1441. << 24;
  1442. }
  1443. return status;
  1444. }
  1445. int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
  1446. u8 saddr_len, u32 data, u8 data_len)
  1447. {
  1448. int status = 0;
  1449. u8 value[4] = { 0, 0, 0, 0 };
  1450. struct cx231xx_i2c_xfer_data req_data;
  1451. value[0] = (u8) data;
  1452. value[1] = (u8) (data >> 8);
  1453. value[2] = (u8) (data >> 16);
  1454. value[3] = (u8) (data >> 24);
  1455. if (saddr_len == 0)
  1456. saddr = 0;
  1457. else if (saddr_len == 1)
  1458. saddr &= 0xff;
  1459. /* prepare xfer_data struct */
  1460. req_data.dev_addr = dev_addr >> 1;
  1461. req_data.direction = 0;
  1462. req_data.saddr_len = saddr_len;
  1463. req_data.saddr_dat = saddr;
  1464. req_data.buf_size = data_len;
  1465. req_data.p_buffer = value;
  1466. /* usb send command */
  1467. status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
  1468. return status;
  1469. }
  1470. int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
  1471. u16 register_address, u8 bit_start, u8 bit_end,
  1472. u32 value)
  1473. {
  1474. int status = 0;
  1475. u32 tmp;
  1476. u32 mask = 0;
  1477. int i;
  1478. if (bit_start > (size - 1) || bit_end > (size - 1))
  1479. return -1;
  1480. if (size == 8) {
  1481. status =
  1482. cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
  1483. &tmp, 1);
  1484. } else {
  1485. status =
  1486. cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
  1487. &tmp, 4);
  1488. }
  1489. if (status < 0)
  1490. return status;
  1491. mask = 1 << bit_end;
  1492. for (i = bit_end; i > bit_start && i > 0; i--)
  1493. mask = mask + (1 << (i - 1));
  1494. value <<= bit_start;
  1495. if (size == 8) {
  1496. tmp &= ~mask;
  1497. tmp |= value;
  1498. tmp &= 0xff;
  1499. status =
  1500. cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
  1501. tmp, 1);
  1502. } else {
  1503. tmp &= ~mask;
  1504. tmp |= value;
  1505. status =
  1506. cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
  1507. tmp, 4);
  1508. }
  1509. return status;
  1510. }
  1511. int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
  1512. u16 saddr, u32 mask, u32 value)
  1513. {
  1514. u32 temp;
  1515. int status = 0;
  1516. status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
  1517. if (status < 0)
  1518. return status;
  1519. temp &= ~mask;
  1520. temp |= value;
  1521. status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
  1522. return status;
  1523. }
  1524. u32 cx231xx_set_field(u32 field_mask, u32 data)
  1525. {
  1526. u32 temp;
  1527. for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
  1528. data <<= 1;
  1529. return data;
  1530. }