technisat-usb2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. txlen = 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. LED_OFF,
  183. LED_BLINK,
  184. LED_ON,
  185. 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 != LED_OFF)
  195. return 0;
  196. switch (state) {
  197. case LED_ON:
  198. led[1] = 0x82;
  199. break;
  200. case 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 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;
  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, LED_BLINK);
  259. else
  260. technisat_usb2_set_led(state->dev, 0, LED_ON);
  261. } else
  262. technisat_usb2_set_led(state->dev, 0, 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, LED_OFF);
  303. /* red led is turned on all the time */
  304. technisat_usb2_set_led(d, 1, 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. /* frontend attach */
  373. static int technisat_usb2_set_voltage(struct dvb_frontend *fe,
  374. fe_sec_voltage_t voltage)
  375. {
  376. int i;
  377. u8 gpio[3] = { 0 }; /* 0 = 2, 1 = 3, 2 = 4 */
  378. gpio[2] = 1; /* high - voltage ? */
  379. switch (voltage) {
  380. case SEC_VOLTAGE_13:
  381. gpio[0] = 1;
  382. break;
  383. case SEC_VOLTAGE_18:
  384. gpio[0] = 1;
  385. gpio[1] = 1;
  386. break;
  387. default:
  388. case SEC_VOLTAGE_OFF:
  389. break;
  390. }
  391. for (i = 0; i < 3; i++)
  392. if (stv090x_set_gpio(fe, i+2, 0, gpio[i], 0) != 0)
  393. return -EREMOTEIO;
  394. return 0;
  395. }
  396. static struct stv090x_config technisat_usb2_stv090x_config = {
  397. .device = STV0903,
  398. .demod_mode = STV090x_SINGLE,
  399. .clk_mode = STV090x_CLK_EXT,
  400. .xtal = 8000000,
  401. .address = 0x68,
  402. .ts1_mode = STV090x_TSMODE_DVBCI,
  403. .ts1_clk = 13400000,
  404. .ts1_tei = 1,
  405. .repeater_level = STV090x_RPTLEVEL_64,
  406. .tuner_bbgain = 6,
  407. };
  408. static struct stv6110x_config technisat_usb2_stv6110x_config = {
  409. .addr = 0x60,
  410. .refclk = 16000000,
  411. .clk_div = 2,
  412. };
  413. static int technisat_usb2_frontend_attach(struct dvb_usb_adapter *a)
  414. {
  415. struct usb_device *udev = a->dev->udev;
  416. int ret;
  417. a->fe = dvb_attach(stv090x_attach, &technisat_usb2_stv090x_config,
  418. &a->dev->i2c_adap, STV090x_DEMODULATOR_0);
  419. if (a->fe) {
  420. struct stv6110x_devctl *ctl;
  421. ctl = dvb_attach(stv6110x_attach,
  422. a->fe,
  423. &technisat_usb2_stv6110x_config,
  424. &a->dev->i2c_adap);
  425. if (ctl) {
  426. technisat_usb2_stv090x_config.tuner_init = ctl->tuner_init;
  427. technisat_usb2_stv090x_config.tuner_sleep = ctl->tuner_sleep;
  428. technisat_usb2_stv090x_config.tuner_set_mode = ctl->tuner_set_mode;
  429. technisat_usb2_stv090x_config.tuner_set_frequency = ctl->tuner_set_frequency;
  430. technisat_usb2_stv090x_config.tuner_get_frequency = ctl->tuner_get_frequency;
  431. technisat_usb2_stv090x_config.tuner_set_bandwidth = ctl->tuner_set_bandwidth;
  432. technisat_usb2_stv090x_config.tuner_get_bandwidth = ctl->tuner_get_bandwidth;
  433. technisat_usb2_stv090x_config.tuner_set_bbgain = ctl->tuner_set_bbgain;
  434. technisat_usb2_stv090x_config.tuner_get_bbgain = ctl->tuner_get_bbgain;
  435. technisat_usb2_stv090x_config.tuner_set_refclk = ctl->tuner_set_refclk;
  436. technisat_usb2_stv090x_config.tuner_get_status = ctl->tuner_get_status;
  437. /* call the init function once to initialize
  438. tuner's clock output divider and demod's
  439. master clock */
  440. if (a->fe->ops.init)
  441. a->fe->ops.init(a->fe);
  442. if (mutex_lock_interruptible(&a->dev->i2c_mutex) < 0)
  443. return -EAGAIN;
  444. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  445. SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST,
  446. USB_TYPE_VENDOR | USB_DIR_OUT,
  447. 0, 0,
  448. NULL, 0, 500);
  449. mutex_unlock(&a->dev->i2c_mutex);
  450. if (ret != 0)
  451. err("could not set IF_CLK to external");
  452. a->fe->ops.set_voltage = technisat_usb2_set_voltage;
  453. /* if everything was successful assign a nice name to the frontend */
  454. strlcpy(a->fe->ops.info.name, a->dev->desc->name,
  455. sizeof(a->fe->ops.info.name));
  456. } else {
  457. dvb_frontend_detach(a->fe);
  458. a->fe = NULL;
  459. }
  460. }
  461. technisat_usb2_set_led_timer(a->dev, 1, 1);
  462. return a->fe == NULL ? -ENODEV : 0;
  463. }
  464. /* Remote control */
  465. /* the device is giving providing raw IR-signals to the host mapping
  466. * it only to one remote control is just the default implementation
  467. */
  468. #define NOMINAL_IR_BIT_TRANSITION_TIME_US 889
  469. #define NOMINAL_IR_BIT_TIME_US (2 * NOMINAL_IR_BIT_TRANSITION_TIME_US)
  470. #define FIRMWARE_CLOCK_TICK 83333
  471. #define FIRMWARE_CLOCK_DIVISOR 256
  472. #define IR_PERCENT_TOLERANCE 15
  473. #define NOMINAL_IR_BIT_TRANSITION_TICKS ((NOMINAL_IR_BIT_TRANSITION_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
  474. #define NOMINAL_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICKS / FIRMWARE_CLOCK_DIVISOR)
  475. #define NOMINAL_IR_BIT_TIME_TICKS ((NOMINAL_IR_BIT_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
  476. #define NOMINAL_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICKS / FIRMWARE_CLOCK_DIVISOR)
  477. #define MINIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT - ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  478. #define MAXIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT + ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  479. #define MINIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT - ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  480. #define MAXIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT + ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
  481. static int technisat_usb2_get_ir(struct dvb_usb_device *d)
  482. {
  483. u8 buf[62], *b;
  484. int ret;
  485. struct ir_raw_event ev;
  486. buf[0] = GET_IR_DATA_VENDOR_REQUEST;
  487. buf[1] = 0x08;
  488. buf[2] = 0x8f;
  489. buf[3] = MINIMUM_IR_BIT_TRANSITION_TICK_COUNT;
  490. buf[4] = MAXIMUM_IR_BIT_TIME_TICK_COUNT;
  491. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  492. return -EAGAIN;
  493. ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
  494. GET_IR_DATA_VENDOR_REQUEST,
  495. USB_TYPE_VENDOR | USB_DIR_OUT,
  496. 0, 0,
  497. buf, 5, 500);
  498. if (ret < 0)
  499. goto unlock;
  500. buf[1] = 0;
  501. buf[2] = 0;
  502. ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
  503. GET_IR_DATA_VENDOR_REQUEST,
  504. USB_TYPE_VENDOR | USB_DIR_IN,
  505. 0x8080, 0,
  506. buf, sizeof(buf), 500);
  507. unlock:
  508. mutex_unlock(&d->i2c_mutex);
  509. if (ret < 0)
  510. return ret;
  511. if (ret == 1)
  512. return 0; /* no key pressed */
  513. /* decoding */
  514. b = buf+1;
  515. #if 0
  516. deb_rc("RC: %d ", ret);
  517. debug_dump(b, ret, deb_rc);
  518. #endif
  519. ev.pulse = 0;
  520. while (1) {
  521. ev.pulse = !ev.pulse;
  522. ev.duration = (*b * FIRMWARE_CLOCK_DIVISOR * FIRMWARE_CLOCK_TICK) / 1000;
  523. ir_raw_event_store(d->rc_dev, &ev);
  524. b++;
  525. if (*b == 0xff) {
  526. ev.pulse = 0;
  527. ev.duration = 888888*2;
  528. ir_raw_event_store(d->rc_dev, &ev);
  529. break;
  530. }
  531. }
  532. ir_raw_event_handle(d->rc_dev);
  533. return 1;
  534. }
  535. static int technisat_usb2_rc_query(struct dvb_usb_device *d)
  536. {
  537. int ret = technisat_usb2_get_ir(d);
  538. if (ret < 0)
  539. return ret;
  540. if (ret == 0)
  541. return 0;
  542. if (!disable_led_control)
  543. technisat_usb2_set_led(d, 1, LED_BLINK);
  544. return 0;
  545. }
  546. /* DVB-USB and USB stuff follows */
  547. static struct usb_device_id technisat_usb2_id_table[] = {
  548. { USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_DVB_S2) },
  549. { 0 } /* Terminating entry */
  550. };
  551. /* device description */
  552. static struct dvb_usb_device_properties technisat_usb2_devices = {
  553. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  554. .usb_ctrl = CYPRESS_FX2,
  555. .identify_state = technisat_usb2_identify_state,
  556. .firmware = "dvb-usb-SkyStar_USB_HD_FW_v17_63.HEX.fw",
  557. .size_of_priv = sizeof(struct technisat_usb2_state),
  558. .i2c_algo = &technisat_usb2_i2c_algo,
  559. .power_ctrl = technisat_usb2_power_ctrl,
  560. .read_mac_address = technisat_usb2_read_mac_address,
  561. .num_adapters = 1,
  562. .adapter = {
  563. {
  564. .frontend_attach = technisat_usb2_frontend_attach,
  565. .stream = {
  566. .type = USB_ISOC,
  567. .count = 8,
  568. .endpoint = 0x2,
  569. .u = {
  570. .isoc = {
  571. .framesperurb = 32,
  572. .framesize = 2048,
  573. .interval = 3,
  574. }
  575. }
  576. },
  577. .size_of_priv = 0,
  578. },
  579. },
  580. .num_device_descs = 1,
  581. .devices = {
  582. { "Technisat SkyStar USB HD (DVB-S/S2)",
  583. { &technisat_usb2_id_table[0], NULL },
  584. { NULL },
  585. },
  586. },
  587. .rc.core = {
  588. .rc_interval = 100,
  589. .rc_codes = RC_MAP_TECHNISAT_USB2,
  590. .module_name = "technisat-usb2",
  591. .rc_query = technisat_usb2_rc_query,
  592. .allowed_protos = RC_TYPE_ALL,
  593. .driver_type = RC_DRIVER_IR_RAW,
  594. }
  595. };
  596. static int technisat_usb2_probe(struct usb_interface *intf,
  597. const struct usb_device_id *id)
  598. {
  599. struct dvb_usb_device *dev;
  600. if (dvb_usb_device_init(intf, &technisat_usb2_devices, THIS_MODULE,
  601. &dev, adapter_nr) != 0)
  602. return -ENODEV;
  603. if (dev) {
  604. struct technisat_usb2_state *state = dev->priv;
  605. state->dev = dev;
  606. if (!disable_led_control) {
  607. INIT_DELAYED_WORK(&state->green_led_work,
  608. technisat_usb2_green_led_control);
  609. schedule_delayed_work(&state->green_led_work,
  610. msecs_to_jiffies(500));
  611. }
  612. }
  613. return 0;
  614. }
  615. static void technisat_usb2_disconnect(struct usb_interface *intf)
  616. {
  617. struct dvb_usb_device *dev = usb_get_intfdata(intf);
  618. /* work and stuff was only created when the device is is hot-state */
  619. if (dev != NULL) {
  620. struct technisat_usb2_state *state = dev->priv;
  621. if (state != NULL) {
  622. cancel_delayed_work_sync(&state->green_led_work);
  623. flush_scheduled_work();
  624. }
  625. }
  626. dvb_usb_device_exit(intf);
  627. }
  628. static struct usb_driver technisat_usb2_driver = {
  629. .name = "dvb_usb_technisat_usb2",
  630. .probe = technisat_usb2_probe,
  631. .disconnect = technisat_usb2_disconnect,
  632. .id_table = technisat_usb2_id_table,
  633. };
  634. /* module stuff */
  635. static int __init technisat_usb2_module_init(void)
  636. {
  637. int result = usb_register(&technisat_usb2_driver);
  638. if (result) {
  639. err("usb_register failed. Code %d", result);
  640. return result;
  641. }
  642. return 0;
  643. }
  644. static void __exit technisat_usb2_module_exit(void)
  645. {
  646. usb_deregister(&technisat_usb2_driver);
  647. }
  648. module_init(technisat_usb2_module_init);
  649. module_exit(technisat_usb2_module_exit);
  650. MODULE_AUTHOR("Patrick Boettcher <pboettcher@kernellabs.com>");
  651. MODULE_DESCRIPTION("Driver for Technisat DVB-S/S2 USB 2.0 device");
  652. MODULE_VERSION("1.0");
  653. MODULE_LICENSE("GPL");