technisat-usb2.c 19 KB

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