technisat-usb2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * Linux driver for Technisat DVB-S/S2 USB 2.0 device
  3. *
  4. * Copyright (C) 2010 Patrick Boettcher,
  5. * Kernel Labs Inc. PO Box 745, St James, NY 11780
  6. *
  7. * Development was sponsored by Technisat Digital UK Limited, whose
  8. * registered office is Witan Gate House 500 - 600 Witan Gate West,
  9. * Milton Keynes, MK9 1SH
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * THIS PROGRAM IS PROVIDED "AS IS" AND BOTH THE COPYRIGHT HOLDER AND
  22. * TECHNISAT DIGITAL UK LTD DISCLAIM ALL WARRANTIES WITH REGARD TO
  23. * THIS PROGRAM INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY OR
  24. * FITNESS FOR A PARTICULAR PURPOSE. NEITHER THE COPYRIGHT HOLDER
  25. * NOR TECHNISAT DIGITAL UK LIMITED SHALL BE LIABLE FOR ANY SPECIAL,
  26. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  27. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  28. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  29. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS PROGRAM. See the
  30. * GNU General Public License for more details.
  31. */
  32. #define DVB_USB_LOG_PREFIX "technisat-usb2"
  33. #include "dvb-usb.h"
  34. #include "stv6110x.h"
  35. #include "stv090x.h"
  36. /* module parameters */
  37. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  38. static int debug;
  39. module_param(debug, int, 0644);
  40. MODULE_PARM_DESC(debug,
  41. "set debugging level (bit-mask: 1=info,2=eeprom,4=i2c,8=rc)." \
  42. DVB_USB_DEBUG_STATUS);
  43. /* disables all LED control command and
  44. * also does not start the signal polling thread */
  45. static int disable_led_control;
  46. module_param(disable_led_control, int, 0444);
  47. MODULE_PARM_DESC(disable_led_control,
  48. "disable LED control of the device "
  49. "(default: 0 - LED control is active).");
  50. /* device private data */
  51. struct technisat_usb2_state {
  52. struct dvb_usb_device *dev;
  53. struct delayed_work green_led_work;
  54. u8 power_state;
  55. u16 last_scan_code;
  56. u8 buf[64];
  57. };
  58. /* debug print helpers */
  59. #define deb_info(args...) dprintk(debug, 0x01, args)
  60. #define deb_eeprom(args...) dprintk(debug, 0x02, args)
  61. #define deb_i2c(args...) dprintk(debug, 0x04, args)
  62. #define deb_rc(args...) dprintk(debug, 0x08, args)
  63. /* vendor requests */
  64. #define SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST 0xB3
  65. #define SET_FRONT_END_RESET_VENDOR_REQUEST 0xB4
  66. #define GET_VERSION_INFO_VENDOR_REQUEST 0xB5
  67. #define SET_GREEN_LED_VENDOR_REQUEST 0xB6
  68. #define SET_RED_LED_VENDOR_REQUEST 0xB7
  69. #define GET_IR_DATA_VENDOR_REQUEST 0xB8
  70. #define SET_LED_TIMER_DIVIDER_VENDOR_REQUEST 0xB9
  71. #define SET_USB_REENUMERATION 0xBA
  72. /* i2c-access methods */
  73. #define I2C_SPEED_100KHZ_BIT 0x40
  74. #define I2C_STATUS_NAK 7
  75. #define I2C_STATUS_OK 8
  76. static int technisat_usb2_i2c_access(struct usb_device *udev,
  77. u8 device_addr, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
  78. {
  79. u8 *b;
  80. int ret, actual_length;
  81. b = kmalloc(64, GFP_KERNEL);
  82. if (!b)
  83. return -ENOMEM;
  84. deb_i2c("i2c-access: %02x, tx: ", device_addr);
  85. debug_dump(tx, txlen, deb_i2c);
  86. deb_i2c(" ");
  87. if (txlen > 62) {
  88. err("i2c TX buffer can't exceed 62 bytes (dev 0x%02x)",
  89. device_addr);
  90. txlen = 62;
  91. }
  92. if (rxlen > 62) {
  93. err("i2c RX buffer can't exceed 62 bytes (dev 0x%02x)",
  94. device_addr);
  95. rxlen = 62;
  96. }
  97. b[0] = I2C_SPEED_100KHZ_BIT;
  98. b[1] = device_addr << 1;
  99. if (rx != NULL) {
  100. b[0] |= rxlen;
  101. b[1] |= 1;
  102. }
  103. memcpy(&b[2], tx, txlen);
  104. ret = usb_bulk_msg(udev,
  105. usb_sndbulkpipe(udev, 0x01),
  106. b, 2 + txlen,
  107. NULL, 1000);
  108. if (ret < 0) {
  109. err("i2c-error: out failed %02x = %d", device_addr, ret);
  110. goto err;
  111. }
  112. ret = usb_bulk_msg(udev,
  113. usb_rcvbulkpipe(udev, 0x01),
  114. b, 64, &actual_length, 1000);
  115. if (ret < 0) {
  116. err("i2c-error: in failed %02x = %d", device_addr, ret);
  117. goto err;
  118. }
  119. if (b[0] != I2C_STATUS_OK) {
  120. err("i2c-error: %02x = %d", device_addr, b[0]);
  121. /* handle tuner-i2c-nak */
  122. if (!(b[0] == I2C_STATUS_NAK &&
  123. device_addr == 0x60
  124. /* && device_is_technisat_usb2 */))
  125. goto err;
  126. }
  127. deb_i2c("status: %d, ", b[0]);
  128. if (rx != NULL) {
  129. memcpy(rx, &b[2], rxlen);
  130. deb_i2c("rx (%d): ", rxlen);
  131. debug_dump(rx, rxlen, deb_i2c);
  132. }
  133. deb_i2c("\n");
  134. err:
  135. kfree(b);
  136. return ret;
  137. }
  138. static int technisat_usb2_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
  139. int num)
  140. {
  141. int ret = 0, i;
  142. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  143. /* Ensure nobody else hits the i2c bus while we're sending our
  144. sequence of messages, (such as the remote control thread) */
  145. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  146. return -EAGAIN;
  147. for (i = 0; i < num; i++) {
  148. if (i+1 < num && msg[i+1].flags & I2C_M_RD) {
  149. ret = technisat_usb2_i2c_access(d->udev, msg[i+1].addr,
  150. msg[i].buf, msg[i].len,
  151. msg[i+1].buf, msg[i+1].len);
  152. if (ret != 0)
  153. break;
  154. i++;
  155. } else {
  156. ret = technisat_usb2_i2c_access(d->udev, msg[i].addr,
  157. msg[i].buf, msg[i].len,
  158. NULL, 0);
  159. if (ret != 0)
  160. break;
  161. }
  162. }
  163. if (ret == 0)
  164. ret = i;
  165. mutex_unlock(&d->i2c_mutex);
  166. return ret;
  167. }
  168. static u32 technisat_usb2_i2c_func(struct i2c_adapter *adapter)
  169. {
  170. return I2C_FUNC_I2C;
  171. }
  172. static struct i2c_algorithm technisat_usb2_i2c_algo = {
  173. .master_xfer = technisat_usb2_i2c_xfer,
  174. .functionality = technisat_usb2_i2c_func,
  175. };
  176. #if 0
  177. static void technisat_usb2_frontend_reset(struct usb_device *udev)
  178. {
  179. usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  180. SET_FRONT_END_RESET_VENDOR_REQUEST,
  181. USB_TYPE_VENDOR | USB_DIR_OUT,
  182. 10, 0,
  183. NULL, 0, 500);
  184. }
  185. #endif
  186. /* LED control */
  187. enum technisat_usb2_led_state {
  188. TECH_LED_OFF,
  189. TECH_LED_BLINK,
  190. TECH_LED_ON,
  191. TECH_LED_UNDEFINED
  192. };
  193. static int technisat_usb2_set_led(struct dvb_usb_device *d, int red,
  194. enum technisat_usb2_led_state st)
  195. {
  196. struct technisat_usb2_state *state = d->priv;
  197. u8 *led = state->buf;
  198. int ret;
  199. led[0] = red ? SET_RED_LED_VENDOR_REQUEST : SET_GREEN_LED_VENDOR_REQUEST;
  200. if (disable_led_control && st != TECH_LED_OFF)
  201. return 0;
  202. switch (st) {
  203. case TECH_LED_ON:
  204. led[1] = 0x82;
  205. break;
  206. case TECH_LED_BLINK:
  207. led[1] = 0x82;
  208. if (red) {
  209. led[2] = 0x02;
  210. led[3] = 10;
  211. led[4] = 10;
  212. } else {
  213. led[2] = 0xff;
  214. led[3] = 50;
  215. led[4] = 50;
  216. }
  217. led[5] = 1;
  218. break;
  219. default:
  220. case TECH_LED_OFF:
  221. led[1] = 0x80;
  222. break;
  223. }
  224. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  225. return -EAGAIN;
  226. ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  227. red ? SET_RED_LED_VENDOR_REQUEST : SET_GREEN_LED_VENDOR_REQUEST,
  228. USB_TYPE_VENDOR | USB_DIR_OUT,
  229. 0, 0,
  230. led, 8, 500);
  231. mutex_unlock(&d->i2c_mutex);
  232. return ret;
  233. }
  234. static int technisat_usb2_set_led_timer(struct dvb_usb_device *d, u8 red, u8 green)
  235. {
  236. struct technisat_usb2_state *state = d->priv;
  237. u8 *b = state->buf;
  238. int ret;
  239. b[0] = 0;
  240. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  241. return -EAGAIN;
  242. ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  243. SET_LED_TIMER_DIVIDER_VENDOR_REQUEST,
  244. USB_TYPE_VENDOR | USB_DIR_OUT,
  245. (red << 8) | green, 0,
  246. b, 1, 500);
  247. mutex_unlock(&d->i2c_mutex);
  248. return ret;
  249. }
  250. static void technisat_usb2_green_led_control(struct work_struct *work)
  251. {
  252. struct technisat_usb2_state *state =
  253. container_of(work, struct technisat_usb2_state, green_led_work.work);
  254. struct dvb_frontend *fe = state->dev->adapter[0].fe_adap[0].fe;
  255. if (state->power_state == 0)
  256. goto schedule;
  257. if (fe != NULL) {
  258. enum fe_status status;
  259. if (fe->ops.read_status(fe, &status) != 0)
  260. goto schedule;
  261. if (status & FE_HAS_LOCK) {
  262. u32 ber;
  263. if (fe->ops.read_ber(fe, &ber) != 0)
  264. goto schedule;
  265. if (ber > 1000)
  266. technisat_usb2_set_led(state->dev, 0, TECH_LED_BLINK);
  267. else
  268. technisat_usb2_set_led(state->dev, 0, TECH_LED_ON);
  269. } else
  270. technisat_usb2_set_led(state->dev, 0, TECH_LED_OFF);
  271. }
  272. schedule:
  273. schedule_delayed_work(&state->green_led_work,
  274. msecs_to_jiffies(500));
  275. }
  276. /* method to find out whether the firmware has to be downloaded or not */
  277. static int technisat_usb2_identify_state(struct usb_device *udev,
  278. struct dvb_usb_device_properties *props,
  279. struct dvb_usb_device_description **desc, int *cold)
  280. {
  281. int ret;
  282. u8 *version;
  283. version = kmalloc(3, GFP_KERNEL);
  284. if (!version)
  285. return -ENOMEM;
  286. /* first select the interface */
  287. if (usb_set_interface(udev, 0, 1) != 0)
  288. err("could not set alternate setting to 0");
  289. else
  290. info("set alternate setting");
  291. *cold = 0; /* by default do not download a firmware - just in case something is wrong */
  292. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  293. GET_VERSION_INFO_VENDOR_REQUEST,
  294. USB_TYPE_VENDOR | USB_DIR_IN,
  295. 0, 0,
  296. version, 3, 500);
  297. if (ret < 0)
  298. *cold = 1;
  299. else {
  300. info("firmware version: %d.%d", version[1], version[2]);
  301. *cold = 0;
  302. }
  303. kfree(version);
  304. return 0;
  305. }
  306. /* power control */
  307. static int technisat_usb2_power_ctrl(struct dvb_usb_device *d, int level)
  308. {
  309. struct technisat_usb2_state *state = d->priv;
  310. state->power_state = level;
  311. if (disable_led_control)
  312. return 0;
  313. /* green led is turned off in any case - will be turned on when tuning */
  314. technisat_usb2_set_led(d, 0, TECH_LED_OFF);
  315. /* red led is turned on all the time */
  316. technisat_usb2_set_led(d, 1, TECH_LED_ON);
  317. return 0;
  318. }
  319. /* mac address reading - from the eeprom */
  320. #if 0
  321. static void technisat_usb2_eeprom_dump(struct dvb_usb_device *d)
  322. {
  323. u8 reg;
  324. u8 b[16];
  325. int i, j;
  326. /* full EEPROM dump */
  327. for (j = 0; j < 256 * 4; j += 16) {
  328. reg = j;
  329. if (technisat_usb2_i2c_access(d->udev, 0x50 + j / 256, &reg, 1, b, 16) != 0)
  330. break;
  331. deb_eeprom("EEPROM: %01x%02x: ", j / 256, reg);
  332. for (i = 0; i < 16; i++)
  333. deb_eeprom("%02x ", b[i]);
  334. deb_eeprom("\n");
  335. }
  336. }
  337. #endif
  338. static u8 technisat_usb2_calc_lrc(const u8 *b, u16 length)
  339. {
  340. u8 lrc = 0;
  341. while (--length)
  342. lrc ^= *b++;
  343. return lrc;
  344. }
  345. static int technisat_usb2_eeprom_lrc_read(struct dvb_usb_device *d,
  346. u16 offset, u8 *b, u16 length, u8 tries)
  347. {
  348. u8 bo = offset & 0xff;
  349. struct i2c_msg msg[] = {
  350. {
  351. .addr = 0x50 | ((offset >> 8) & 0x3),
  352. .buf = &bo,
  353. .len = 1
  354. }, {
  355. .addr = 0x50 | ((offset >> 8) & 0x3),
  356. .flags = I2C_M_RD,
  357. .buf = b,
  358. .len = length
  359. }
  360. };
  361. while (tries--) {
  362. int status;
  363. if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
  364. break;
  365. status =
  366. technisat_usb2_calc_lrc(b, length - 1) == b[length - 1];
  367. if (status)
  368. return 0;
  369. }
  370. return -EREMOTEIO;
  371. }
  372. #define EEPROM_MAC_START 0x3f8
  373. #define EEPROM_MAC_TOTAL 8
  374. static int technisat_usb2_read_mac_address(struct dvb_usb_device *d,
  375. u8 mac[])
  376. {
  377. u8 buf[EEPROM_MAC_TOTAL];
  378. if (technisat_usb2_eeprom_lrc_read(d, EEPROM_MAC_START,
  379. buf, EEPROM_MAC_TOTAL, 4) != 0)
  380. return -ENODEV;
  381. memcpy(mac, buf, 6);
  382. return 0;
  383. }
  384. static struct stv090x_config technisat_usb2_stv090x_config;
  385. /* frontend attach */
  386. static int technisat_usb2_set_voltage(struct dvb_frontend *fe,
  387. enum fe_sec_voltage voltage)
  388. {
  389. int i;
  390. u8 gpio[3] = { 0 }; /* 0 = 2, 1 = 3, 2 = 4 */
  391. gpio[2] = 1; /* high - voltage ? */
  392. switch (voltage) {
  393. case SEC_VOLTAGE_13:
  394. gpio[0] = 1;
  395. break;
  396. case SEC_VOLTAGE_18:
  397. gpio[0] = 1;
  398. gpio[1] = 1;
  399. break;
  400. default:
  401. case SEC_VOLTAGE_OFF:
  402. break;
  403. }
  404. for (i = 0; i < 3; i++)
  405. if (technisat_usb2_stv090x_config.set_gpio(fe, i+2, 0,
  406. gpio[i], 0) != 0)
  407. return -EREMOTEIO;
  408. return 0;
  409. }
  410. static struct stv090x_config technisat_usb2_stv090x_config = {
  411. .device = STV0903,
  412. .demod_mode = STV090x_SINGLE,
  413. .clk_mode = STV090x_CLK_EXT,
  414. .xtal = 8000000,
  415. .address = 0x68,
  416. .ts1_mode = STV090x_TSMODE_DVBCI,
  417. .ts1_clk = 13400000,
  418. .ts1_tei = 1,
  419. .repeater_level = STV090x_RPTLEVEL_64,
  420. .tuner_bbgain = 6,
  421. };
  422. static struct stv6110x_config technisat_usb2_stv6110x_config = {
  423. .addr = 0x60,
  424. .refclk = 16000000,
  425. .clk_div = 2,
  426. };
  427. static int technisat_usb2_frontend_attach(struct dvb_usb_adapter *a)
  428. {
  429. struct usb_device *udev = a->dev->udev;
  430. int ret;
  431. a->fe_adap[0].fe = dvb_attach(stv090x_attach, &technisat_usb2_stv090x_config,
  432. &a->dev->i2c_adap, STV090x_DEMODULATOR_0);
  433. if (a->fe_adap[0].fe) {
  434. const struct stv6110x_devctl *ctl;
  435. ctl = dvb_attach(stv6110x_attach,
  436. a->fe_adap[0].fe,
  437. &technisat_usb2_stv6110x_config,
  438. &a->dev->i2c_adap);
  439. if (ctl) {
  440. technisat_usb2_stv090x_config.tuner_init = ctl->tuner_init;
  441. technisat_usb2_stv090x_config.tuner_sleep = ctl->tuner_sleep;
  442. technisat_usb2_stv090x_config.tuner_set_mode = ctl->tuner_set_mode;
  443. technisat_usb2_stv090x_config.tuner_set_frequency = ctl->tuner_set_frequency;
  444. technisat_usb2_stv090x_config.tuner_get_frequency = ctl->tuner_get_frequency;
  445. technisat_usb2_stv090x_config.tuner_set_bandwidth = ctl->tuner_set_bandwidth;
  446. technisat_usb2_stv090x_config.tuner_get_bandwidth = ctl->tuner_get_bandwidth;
  447. technisat_usb2_stv090x_config.tuner_set_bbgain = ctl->tuner_set_bbgain;
  448. technisat_usb2_stv090x_config.tuner_get_bbgain = ctl->tuner_get_bbgain;
  449. technisat_usb2_stv090x_config.tuner_set_refclk = ctl->tuner_set_refclk;
  450. technisat_usb2_stv090x_config.tuner_get_status = ctl->tuner_get_status;
  451. /* call the init function once to initialize
  452. tuner's clock output divider and demod's
  453. master clock */
  454. if (a->fe_adap[0].fe->ops.init)
  455. a->fe_adap[0].fe->ops.init(a->fe_adap[0].fe);
  456. if (mutex_lock_interruptible(&a->dev->i2c_mutex) < 0)
  457. return -EAGAIN;
  458. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  459. SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST,
  460. USB_TYPE_VENDOR | USB_DIR_OUT,
  461. 0, 0,
  462. NULL, 0, 500);
  463. mutex_unlock(&a->dev->i2c_mutex);
  464. if (ret != 0)
  465. err("could not set IF_CLK to external");
  466. a->fe_adap[0].fe->ops.set_voltage = technisat_usb2_set_voltage;
  467. /* if everything was successful assign a nice name to the frontend */
  468. strlcpy(a->fe_adap[0].fe->ops.info.name, a->dev->desc->name,
  469. sizeof(a->fe_adap[0].fe->ops.info.name));
  470. } else {
  471. dvb_frontend_detach(a->fe_adap[0].fe);
  472. a->fe_adap[0].fe = NULL;
  473. }
  474. }
  475. technisat_usb2_set_led_timer(a->dev, 1, 1);
  476. return a->fe_adap[0].fe == NULL ? -ENODEV : 0;
  477. }
  478. /* Remote control */
  479. /* the device is giving providing raw IR-signals to the host mapping
  480. * it only to one remote control is just the default implementation
  481. */
  482. #define NOMINAL_IR_BIT_TRANSITION_TIME_US 889
  483. #define NOMINAL_IR_BIT_TIME_US (2 * NOMINAL_IR_BIT_TRANSITION_TIME_US)
  484. #define FIRMWARE_CLOCK_TICK 83333
  485. #define FIRMWARE_CLOCK_DIVISOR 256
  486. #define IR_PERCENT_TOLERANCE 15
  487. #define NOMINAL_IR_BIT_TRANSITION_TICKS ((NOMINAL_IR_BIT_TRANSITION_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
  488. #define NOMINAL_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICKS / FIRMWARE_CLOCK_DIVISOR)
  489. #define NOMINAL_IR_BIT_TIME_TICKS ((NOMINAL_IR_BIT_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
  490. #define NOMINAL_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICKS / FIRMWARE_CLOCK_DIVISOR)
  491. #define MINIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT - ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  492. #define MAXIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT + ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  493. #define MINIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT - ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  494. #define MAXIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT + ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  495. static int technisat_usb2_get_ir(struct dvb_usb_device *d)
  496. {
  497. struct technisat_usb2_state *state = d->priv;
  498. u8 *buf = state->buf;
  499. u8 *b;
  500. int ret;
  501. struct ir_raw_event ev;
  502. buf[0] = GET_IR_DATA_VENDOR_REQUEST;
  503. buf[1] = 0x08;
  504. buf[2] = 0x8f;
  505. buf[3] = MINIMUM_IR_BIT_TRANSITION_TICK_COUNT;
  506. buf[4] = MAXIMUM_IR_BIT_TIME_TICK_COUNT;
  507. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  508. return -EAGAIN;
  509. ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  510. GET_IR_DATA_VENDOR_REQUEST,
  511. USB_TYPE_VENDOR | USB_DIR_OUT,
  512. 0, 0,
  513. buf, 5, 500);
  514. if (ret < 0)
  515. goto unlock;
  516. buf[1] = 0;
  517. buf[2] = 0;
  518. ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
  519. GET_IR_DATA_VENDOR_REQUEST,
  520. USB_TYPE_VENDOR | USB_DIR_IN,
  521. 0x8080, 0,
  522. buf, 62, 500);
  523. unlock:
  524. mutex_unlock(&d->i2c_mutex);
  525. if (ret < 0)
  526. return ret;
  527. if (ret == 1)
  528. return 0; /* no key pressed */
  529. /* decoding */
  530. b = buf+1;
  531. #if 0
  532. deb_rc("RC: %d ", ret);
  533. debug_dump(b, ret, deb_rc);
  534. #endif
  535. ev.pulse = 0;
  536. while (1) {
  537. ev.pulse = !ev.pulse;
  538. ev.duration = (*b * FIRMWARE_CLOCK_DIVISOR * FIRMWARE_CLOCK_TICK) / 1000;
  539. ir_raw_event_store(d->rc_dev, &ev);
  540. b++;
  541. if (*b == 0xff) {
  542. ev.pulse = 0;
  543. ev.duration = 888888*2;
  544. ir_raw_event_store(d->rc_dev, &ev);
  545. break;
  546. }
  547. }
  548. ir_raw_event_handle(d->rc_dev);
  549. return 1;
  550. }
  551. static int technisat_usb2_rc_query(struct dvb_usb_device *d)
  552. {
  553. int ret = technisat_usb2_get_ir(d);
  554. if (ret < 0)
  555. return ret;
  556. if (ret == 0)
  557. return 0;
  558. if (!disable_led_control)
  559. technisat_usb2_set_led(d, 1, TECH_LED_BLINK);
  560. return 0;
  561. }
  562. /* DVB-USB and USB stuff follows */
  563. static struct usb_device_id technisat_usb2_id_table[] = {
  564. { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_DVB_S2) },
  565. { 0 } /* Terminating entry */
  566. };
  567. MODULE_DEVICE_TABLE(usb, technisat_usb2_id_table);
  568. /* device description */
  569. static struct dvb_usb_device_properties technisat_usb2_devices = {
  570. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  571. .usb_ctrl = CYPRESS_FX2,
  572. .identify_state = technisat_usb2_identify_state,
  573. .firmware = "/*(DEBLOBBED)*/",
  574. .size_of_priv = sizeof(struct technisat_usb2_state),
  575. .i2c_algo = &technisat_usb2_i2c_algo,
  576. .power_ctrl = technisat_usb2_power_ctrl,
  577. .read_mac_address = technisat_usb2_read_mac_address,
  578. .num_adapters = 1,
  579. .adapter = {
  580. {
  581. .num_frontends = 1,
  582. .fe = {{
  583. .frontend_attach = technisat_usb2_frontend_attach,
  584. .stream = {
  585. .type = USB_ISOC,
  586. .count = 4,
  587. .endpoint = 0x2,
  588. .u = {
  589. .isoc = {
  590. .framesperurb = 32,
  591. .framesize = 2048,
  592. .interval = 1,
  593. }
  594. }
  595. },
  596. }},
  597. .size_of_priv = 0,
  598. },
  599. },
  600. .num_device_descs = 1,
  601. .devices = {
  602. { "Technisat SkyStar USB HD (DVB-S/S2)",
  603. { &technisat_usb2_id_table[0], NULL },
  604. { NULL },
  605. },
  606. },
  607. .rc.core = {
  608. .rc_interval = 100,
  609. .rc_codes = RC_MAP_TECHNISAT_USB2,
  610. .module_name = "technisat-usb2",
  611. .rc_query = technisat_usb2_rc_query,
  612. .allowed_protos = RC_BIT_ALL,
  613. .driver_type = RC_DRIVER_IR_RAW,
  614. }
  615. };
  616. static int technisat_usb2_probe(struct usb_interface *intf,
  617. const struct usb_device_id *id)
  618. {
  619. struct dvb_usb_device *dev;
  620. if (dvb_usb_device_init(intf, &technisat_usb2_devices, THIS_MODULE,
  621. &dev, adapter_nr) != 0)
  622. return -ENODEV;
  623. if (dev) {
  624. struct technisat_usb2_state *state = dev->priv;
  625. state->dev = dev;
  626. if (!disable_led_control) {
  627. INIT_DELAYED_WORK(&state->green_led_work,
  628. technisat_usb2_green_led_control);
  629. schedule_delayed_work(&state->green_led_work,
  630. msecs_to_jiffies(500));
  631. }
  632. }
  633. return 0;
  634. }
  635. static void technisat_usb2_disconnect(struct usb_interface *intf)
  636. {
  637. struct dvb_usb_device *dev = usb_get_intfdata(intf);
  638. /* work and stuff was only created when the device is is hot-state */
  639. if (dev != NULL) {
  640. struct technisat_usb2_state *state = dev->priv;
  641. if (state != NULL)
  642. cancel_delayed_work_sync(&state->green_led_work);
  643. }
  644. dvb_usb_device_exit(intf);
  645. }
  646. static struct usb_driver technisat_usb2_driver = {
  647. .name = "dvb_usb_technisat_usb2",
  648. .probe = technisat_usb2_probe,
  649. .disconnect = technisat_usb2_disconnect,
  650. .id_table = technisat_usb2_id_table,
  651. };
  652. module_usb_driver(technisat_usb2_driver);
  653. MODULE_AUTHOR("Patrick Boettcher <pboettcher@kernellabs.com>");
  654. MODULE_DESCRIPTION("Driver for Technisat DVB-S/S2 USB 2.0 device");
  655. MODULE_VERSION("1.0");
  656. MODULE_LICENSE("GPL");