technisat-usb2.c 20 KB

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