technisat-usb2.c 20 KB

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