redrat3.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /*
  2. * USB RedRat3 IR Transceiver rc-core driver
  3. *
  4. * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
  5. * based heavily on the work of Stephen Cox, with additional
  6. * help from RedRat Ltd.
  7. *
  8. * This driver began life based an an old version of the first-generation
  9. * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
  10. * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
  11. * Chris Dodge.
  12. *
  13. * The driver was then ported to rc-core and significantly rewritten again,
  14. * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
  15. * port effort was started by Stephen.
  16. *
  17. * TODO LIST:
  18. * - fix lirc not showing repeats properly
  19. * --
  20. *
  21. * The RedRat3 is a USB transceiver with both send & receive,
  22. * with 2 separate sensors available for receive to enable
  23. * both good long range reception for general use, and good
  24. * short range reception when required for learning a signal.
  25. *
  26. * http://www.redrat.co.uk/
  27. *
  28. * It uses its own little protocol to communicate, the required
  29. * parts of which are embedded within this driver.
  30. * --
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  45. *
  46. */
  47. #include <asm/unaligned.h>
  48. #include <linux/device.h>
  49. #include <linux/leds.h>
  50. #include <linux/module.h>
  51. #include <linux/slab.h>
  52. #include <linux/usb.h>
  53. #include <linux/usb/input.h>
  54. #include <media/rc-core.h>
  55. /* Driver Information */
  56. #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
  57. #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
  58. #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
  59. #define DRIVER_NAME "redrat3"
  60. /* bulk data transfer types */
  61. #define RR3_ERROR 0x01
  62. #define RR3_MOD_SIGNAL_IN 0x20
  63. #define RR3_MOD_SIGNAL_OUT 0x21
  64. /* Get the RR firmware version */
  65. #define RR3_FW_VERSION 0xb1
  66. #define RR3_FW_VERSION_LEN 64
  67. /* Send encoded signal bulk-sent earlier*/
  68. #define RR3_TX_SEND_SIGNAL 0xb3
  69. #define RR3_SET_IR_PARAM 0xb7
  70. #define RR3_GET_IR_PARAM 0xb8
  71. /* Blink the red LED on the device */
  72. #define RR3_BLINK_LED 0xb9
  73. /* Read serial number of device */
  74. #define RR3_READ_SER_NO 0xba
  75. #define RR3_SER_NO_LEN 4
  76. /* Start capture with the RC receiver */
  77. #define RR3_RC_DET_ENABLE 0xbb
  78. /* Stop capture with the RC receiver */
  79. #define RR3_RC_DET_DISABLE 0xbc
  80. /* Return the status of RC detector capture */
  81. #define RR3_RC_DET_STATUS 0xbd
  82. /* Reset redrat */
  83. #define RR3_RESET 0xa0
  84. /* Max number of lengths in the signal. */
  85. #define RR3_IR_IO_MAX_LENGTHS 0x01
  86. /* Periods to measure mod. freq. */
  87. #define RR3_IR_IO_PERIODS_MF 0x02
  88. /* Size of memory for main signal data */
  89. #define RR3_IR_IO_SIG_MEM_SIZE 0x03
  90. /* Delta value when measuring lengths */
  91. #define RR3_IR_IO_LENGTH_FUZZ 0x04
  92. /* Timeout for end of signal detection */
  93. #define RR3_IR_IO_SIG_TIMEOUT 0x05
  94. /* Minimum value for pause recognition. */
  95. #define RR3_IR_IO_MIN_PAUSE 0x06
  96. /* Clock freq. of EZ-USB chip */
  97. #define RR3_CLK 24000000
  98. /* Clock periods per timer count */
  99. #define RR3_CLK_PER_COUNT 12
  100. /* (RR3_CLK / RR3_CLK_PER_COUNT) */
  101. #define RR3_CLK_CONV_FACTOR 2000000
  102. /* USB bulk-in IR data endpoint address */
  103. #define RR3_BULK_IN_EP_ADDR 0x82
  104. /* Size of the fixed-length portion of the signal */
  105. #define RR3_DRIVER_MAXLENS 128
  106. #define RR3_MAX_SIG_SIZE 512
  107. #define RR3_TIME_UNIT 50
  108. #define RR3_END_OF_SIGNAL 0x7f
  109. #define RR3_TX_TRAILER_LEN 2
  110. #define RR3_RX_MIN_TIMEOUT 5
  111. #define RR3_RX_MAX_TIMEOUT 2000
  112. /* The 8051's CPUCS Register address */
  113. #define RR3_CPUCS_REG_ADDR 0x7f92
  114. #define USB_RR3USB_VENDOR_ID 0x112a
  115. #define USB_RR3USB_PRODUCT_ID 0x0001
  116. #define USB_RR3IIUSB_PRODUCT_ID 0x0005
  117. /*
  118. * The redrat3 encodes an IR signal as set of different lengths and a set
  119. * of indices into those lengths. This sets how much two lengths must
  120. * differ before they are considered distinct, the value is specified
  121. * in microseconds.
  122. * Default 5, value 0 to 127.
  123. */
  124. static int length_fuzz = 5;
  125. module_param(length_fuzz, uint, 0644);
  126. MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)");
  127. /*
  128. * When receiving a continuous ir stream (for example when a user is
  129. * holding a button down on a remote), this specifies the minimum size
  130. * of a space when the redrat3 sends a irdata packet to the host. Specified
  131. * in miliseconds. Default value 18ms.
  132. * The value can be between 2 and 30 inclusive.
  133. */
  134. static int minimum_pause = 18;
  135. module_param(minimum_pause, uint, 0644);
  136. MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)");
  137. /*
  138. * The carrier frequency is measured during the first pulse of the IR
  139. * signal. The larger the number of periods used To measure, the more
  140. * accurate the result is likely to be, however some signals have short
  141. * initial pulses, so in some case it may be necessary to reduce this value.
  142. * Default 8, value 1 to 255.
  143. */
  144. static int periods_measure_carrier = 8;
  145. module_param(periods_measure_carrier, uint, 0644);
  146. MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)");
  147. struct redrat3_header {
  148. __be16 length;
  149. __be16 transfer_type;
  150. } __packed;
  151. /* sending and receiving irdata */
  152. struct redrat3_irdata {
  153. struct redrat3_header header;
  154. __be32 pause;
  155. __be16 mod_freq_count;
  156. __be16 num_periods;
  157. __u8 max_lengths;
  158. __u8 no_lengths;
  159. __be16 max_sig_size;
  160. __be16 sig_size;
  161. __u8 no_repeats;
  162. __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
  163. __u8 sigdata[RR3_MAX_SIG_SIZE];
  164. } __packed;
  165. /* firmware errors */
  166. struct redrat3_error {
  167. struct redrat3_header header;
  168. __be16 fw_error;
  169. } __packed;
  170. /* table of devices that work with this driver */
  171. static struct usb_device_id redrat3_dev_table[] = {
  172. /* Original version of the RedRat3 */
  173. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
  174. /* Second Version/release of the RedRat3 - RetRat3-II */
  175. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
  176. {} /* Terminating entry */
  177. };
  178. /* Structure to hold all of our device specific stuff */
  179. struct redrat3_dev {
  180. /* core device bits */
  181. struct rc_dev *rc;
  182. struct device *dev;
  183. /* led control */
  184. struct led_classdev led;
  185. atomic_t flash;
  186. struct usb_ctrlrequest flash_control;
  187. struct urb *flash_urb;
  188. u8 flash_in_buf;
  189. /* save off the usb device pointer */
  190. struct usb_device *udev;
  191. /* the receive endpoint */
  192. struct usb_endpoint_descriptor *ep_in;
  193. /* the buffer to receive data */
  194. void *bulk_in_buf;
  195. /* urb used to read ir data */
  196. struct urb *read_urb;
  197. /* the send endpoint */
  198. struct usb_endpoint_descriptor *ep_out;
  199. /* usb dma */
  200. dma_addr_t dma_in;
  201. /* Is the device currently transmitting?*/
  202. bool transmitting;
  203. /* store for current packet */
  204. struct redrat3_irdata irdata;
  205. u16 bytes_read;
  206. u32 carrier;
  207. char name[64];
  208. char phys[64];
  209. };
  210. /*
  211. * redrat3_issue_async
  212. *
  213. * Issues an async read to the ir data in port..
  214. * sets the callback to be redrat3_handle_async
  215. */
  216. static void redrat3_issue_async(struct redrat3_dev *rr3)
  217. {
  218. int res;
  219. res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
  220. if (res)
  221. dev_dbg(rr3->dev,
  222. "%s: receive request FAILED! (res %d, len %d)\n",
  223. __func__, res, rr3->read_urb->transfer_buffer_length);
  224. }
  225. static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
  226. {
  227. if (!rr3->transmitting && (code != 0x40))
  228. dev_info(rr3->dev, "fw error code 0x%02x: ", code);
  229. switch (code) {
  230. case 0x00:
  231. pr_cont("No Error\n");
  232. break;
  233. /* Codes 0x20 through 0x2f are IR Firmware Errors */
  234. case 0x20:
  235. pr_cont("Initial signal pulse not long enough "
  236. "to measure carrier frequency\n");
  237. break;
  238. case 0x21:
  239. pr_cont("Not enough length values allocated for signal\n");
  240. break;
  241. case 0x22:
  242. pr_cont("Not enough memory allocated for signal data\n");
  243. break;
  244. case 0x23:
  245. pr_cont("Too many signal repeats\n");
  246. break;
  247. case 0x28:
  248. pr_cont("Insufficient memory available for IR signal "
  249. "data memory allocation\n");
  250. break;
  251. case 0x29:
  252. pr_cont("Insufficient memory available "
  253. "for IrDa signal data memory allocation\n");
  254. break;
  255. /* Codes 0x30 through 0x3f are USB Firmware Errors */
  256. case 0x30:
  257. pr_cont("Insufficient memory available for bulk "
  258. "transfer structure\n");
  259. break;
  260. /*
  261. * Other error codes... These are primarily errors that can occur in
  262. * the control messages sent to the redrat
  263. */
  264. case 0x40:
  265. if (!rr3->transmitting)
  266. pr_cont("Signal capture has been terminated\n");
  267. break;
  268. case 0x41:
  269. pr_cont("Attempt to set/get and unknown signal I/O "
  270. "algorithm parameter\n");
  271. break;
  272. case 0x42:
  273. pr_cont("Signal capture already started\n");
  274. break;
  275. default:
  276. pr_cont("Unknown Error\n");
  277. break;
  278. }
  279. }
  280. static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
  281. {
  282. u32 mod_freq = 0;
  283. u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
  284. if (mod_freq_count != 0)
  285. mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
  286. (mod_freq_count * RR3_CLK_PER_COUNT);
  287. return mod_freq;
  288. }
  289. /* this function scales down the figures for the same result... */
  290. static u32 redrat3_len_to_us(u32 length)
  291. {
  292. u32 biglen = length * 1000;
  293. u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
  294. u32 result = (u32) (biglen / divisor);
  295. /* don't allow zero lengths to go back, breaks lirc */
  296. return result ? result : 1;
  297. }
  298. /*
  299. * convert us back into redrat3 lengths
  300. *
  301. * length * 1000 length * 1000000
  302. * ------------- = ---------------- = micro
  303. * rr3clk / 1000 rr3clk
  304. * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
  305. * ----- = 4 ----- = 6 -------------- = len ---------------------
  306. * 3 2 1000000 1000
  307. */
  308. static u32 redrat3_us_to_len(u32 microsec)
  309. {
  310. u32 result;
  311. u32 divisor;
  312. microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
  313. divisor = (RR3_CLK_CONV_FACTOR / 1000);
  314. result = (u32)(microsec * divisor) / 1000;
  315. /* don't allow zero lengths to go back, breaks lirc */
  316. return result ? result : 1;
  317. }
  318. static void redrat3_process_ir_data(struct redrat3_dev *rr3)
  319. {
  320. DEFINE_IR_RAW_EVENT(rawir);
  321. struct device *dev;
  322. unsigned int i, sig_size, single_len, offset, val;
  323. u32 mod_freq;
  324. if (!rr3) {
  325. pr_err("%s called with no context!\n", __func__);
  326. return;
  327. }
  328. dev = rr3->dev;
  329. mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
  330. dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
  331. /* process each rr3 encoded byte into an int */
  332. sig_size = be16_to_cpu(rr3->irdata.sig_size);
  333. for (i = 0; i < sig_size; i++) {
  334. offset = rr3->irdata.sigdata[i];
  335. val = get_unaligned_be16(&rr3->irdata.lens[offset]);
  336. single_len = redrat3_len_to_us(val);
  337. /* we should always get pulse/space/pulse/space samples */
  338. if (i % 2)
  339. rawir.pulse = false;
  340. else
  341. rawir.pulse = true;
  342. rawir.duration = US_TO_NS(single_len);
  343. /* cap the value to IR_MAX_DURATION */
  344. rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
  345. IR_MAX_DURATION : rawir.duration;
  346. dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
  347. rawir.pulse ? "pulse" : "space", rawir.duration, i);
  348. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  349. }
  350. /* add a trailing space */
  351. rawir.pulse = false;
  352. rawir.timeout = true;
  353. rawir.duration = rr3->rc->timeout;
  354. dev_dbg(dev, "storing trailing timeout with duration %d\n",
  355. rawir.duration);
  356. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  357. dev_dbg(dev, "calling ir_raw_event_handle\n");
  358. ir_raw_event_handle(rr3->rc);
  359. }
  360. /* Util fn to send rr3 cmds */
  361. static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
  362. {
  363. struct usb_device *udev;
  364. u8 *data;
  365. int res;
  366. data = kzalloc(sizeof(u8), GFP_KERNEL);
  367. if (!data)
  368. return -ENOMEM;
  369. udev = rr3->udev;
  370. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
  371. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  372. 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
  373. if (res < 0) {
  374. dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
  375. __func__, res, *data);
  376. res = -EIO;
  377. } else
  378. res = data[0];
  379. kfree(data);
  380. return res;
  381. }
  382. /* Enables the long range detector and starts async receive */
  383. static int redrat3_enable_detector(struct redrat3_dev *rr3)
  384. {
  385. struct device *dev = rr3->dev;
  386. u8 ret;
  387. ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
  388. if (ret != 0)
  389. dev_dbg(dev, "%s: unexpected ret of %d\n",
  390. __func__, ret);
  391. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  392. if (ret != 1) {
  393. dev_err(dev, "%s: detector status: %d, should be 1\n",
  394. __func__, ret);
  395. return -EIO;
  396. }
  397. redrat3_issue_async(rr3);
  398. return 0;
  399. }
  400. static inline void redrat3_delete(struct redrat3_dev *rr3,
  401. struct usb_device *udev)
  402. {
  403. usb_kill_urb(rr3->read_urb);
  404. usb_kill_urb(rr3->flash_urb);
  405. usb_free_urb(rr3->read_urb);
  406. usb_free_urb(rr3->flash_urb);
  407. usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
  408. rr3->bulk_in_buf, rr3->dma_in);
  409. kfree(rr3);
  410. }
  411. static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
  412. {
  413. __be32 *tmp;
  414. u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
  415. int len, ret, pipe;
  416. len = sizeof(*tmp);
  417. tmp = kzalloc(len, GFP_KERNEL);
  418. if (!tmp) {
  419. dev_warn(rr3->dev, "Memory allocation faillure\n");
  420. return timeout;
  421. }
  422. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  423. ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
  424. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  425. RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
  426. if (ret != len)
  427. dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
  428. else {
  429. timeout = redrat3_len_to_us(be32_to_cpup(tmp));
  430. dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
  431. }
  432. kfree(tmp);
  433. return timeout;
  434. }
  435. static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns)
  436. {
  437. struct redrat3_dev *rr3 = rc_dev->priv;
  438. struct usb_device *udev = rr3->udev;
  439. struct device *dev = rr3->dev;
  440. __be32 *timeout;
  441. int ret;
  442. timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
  443. if (!timeout)
  444. return -ENOMEM;
  445. *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000));
  446. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
  447. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  448. RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
  449. HZ * 25);
  450. dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
  451. be32_to_cpu(*timeout), ret);
  452. if (ret == sizeof(*timeout))
  453. ret = 0;
  454. else if (ret >= 0)
  455. ret = -EIO;
  456. kfree(timeout);
  457. return ret;
  458. }
  459. static void redrat3_reset(struct redrat3_dev *rr3)
  460. {
  461. struct usb_device *udev = rr3->udev;
  462. struct device *dev = rr3->dev;
  463. int rc, rxpipe, txpipe;
  464. u8 *val;
  465. int len = sizeof(u8);
  466. rxpipe = usb_rcvctrlpipe(udev, 0);
  467. txpipe = usb_sndctrlpipe(udev, 0);
  468. val = kmalloc(len, GFP_KERNEL);
  469. if (!val) {
  470. dev_err(dev, "Memory allocation failure\n");
  471. return;
  472. }
  473. *val = 0x01;
  474. rc = usb_control_msg(udev, rxpipe, RR3_RESET,
  475. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  476. RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
  477. dev_dbg(dev, "reset returned 0x%02x\n", rc);
  478. *val = length_fuzz;
  479. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  480. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  481. RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
  482. dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
  483. *val = (65536 - (minimum_pause * 2000)) / 256;
  484. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  485. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  486. RR3_IR_IO_MIN_PAUSE, 0, val, len, HZ * 25);
  487. dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc);
  488. *val = periods_measure_carrier;
  489. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  490. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  491. RR3_IR_IO_PERIODS_MF, 0, val, len, HZ * 25);
  492. dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val,
  493. rc);
  494. *val = RR3_DRIVER_MAXLENS;
  495. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  496. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  497. RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
  498. dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
  499. kfree(val);
  500. }
  501. static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
  502. {
  503. int rc = 0;
  504. char *buffer;
  505. buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
  506. if (!buffer) {
  507. dev_err(rr3->dev, "Memory allocation failure\n");
  508. return;
  509. }
  510. rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
  511. RR3_FW_VERSION,
  512. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  513. 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
  514. if (rc >= 0)
  515. dev_info(rr3->dev, "Firmware rev: %s", buffer);
  516. else
  517. dev_err(rr3->dev, "Problem fetching firmware ID\n");
  518. kfree(buffer);
  519. }
  520. static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
  521. {
  522. struct redrat3_header *header = rr3->bulk_in_buf;
  523. unsigned pktlen, pkttype;
  524. /* grab the Length and type of transfer */
  525. pktlen = be16_to_cpu(header->length);
  526. pkttype = be16_to_cpu(header->transfer_type);
  527. if (pktlen > sizeof(rr3->irdata)) {
  528. dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
  529. return;
  530. }
  531. switch (pkttype) {
  532. case RR3_ERROR:
  533. if (len >= sizeof(struct redrat3_error)) {
  534. struct redrat3_error *error = rr3->bulk_in_buf;
  535. unsigned fw_error = be16_to_cpu(error->fw_error);
  536. redrat3_dump_fw_error(rr3, fw_error);
  537. }
  538. break;
  539. case RR3_MOD_SIGNAL_IN:
  540. memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
  541. rr3->bytes_read = len;
  542. dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
  543. rr3->bytes_read, pktlen);
  544. break;
  545. default:
  546. dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
  547. pkttype, len, pktlen);
  548. break;
  549. }
  550. }
  551. static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
  552. {
  553. void *irdata = &rr3->irdata;
  554. if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
  555. dev_warn(rr3->dev, "too much data for packet\n");
  556. rr3->bytes_read = 0;
  557. return;
  558. }
  559. memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
  560. rr3->bytes_read += len;
  561. dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
  562. be16_to_cpu(rr3->irdata.header.length));
  563. }
  564. /* gather IR data from incoming urb, process it when we have enough */
  565. static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
  566. {
  567. struct device *dev = rr3->dev;
  568. unsigned pkttype;
  569. int ret = 0;
  570. if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
  571. redrat3_read_packet_start(rr3, len);
  572. } else if (rr3->bytes_read != 0) {
  573. redrat3_read_packet_continue(rr3, len);
  574. } else if (rr3->bytes_read == 0) {
  575. dev_err(dev, "error: no packet data read\n");
  576. ret = -ENODATA;
  577. goto out;
  578. }
  579. if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
  580. sizeof(struct redrat3_header))
  581. /* we're still accumulating data */
  582. return 0;
  583. /* if we get here, we've got IR data to decode */
  584. pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
  585. if (pkttype == RR3_MOD_SIGNAL_IN)
  586. redrat3_process_ir_data(rr3);
  587. else
  588. dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
  589. pkttype);
  590. out:
  591. rr3->bytes_read = 0;
  592. return ret;
  593. }
  594. /* callback function from USB when async USB request has completed */
  595. static void redrat3_handle_async(struct urb *urb)
  596. {
  597. struct redrat3_dev *rr3;
  598. int ret;
  599. if (!urb)
  600. return;
  601. rr3 = urb->context;
  602. if (!rr3) {
  603. pr_err("%s called with invalid context!\n", __func__);
  604. usb_unlink_urb(urb);
  605. return;
  606. }
  607. switch (urb->status) {
  608. case 0:
  609. ret = redrat3_get_ir_data(rr3, urb->actual_length);
  610. if (!ret) {
  611. /* no error, prepare to read more */
  612. redrat3_issue_async(rr3);
  613. }
  614. break;
  615. case -ECONNRESET:
  616. case -ENOENT:
  617. case -ESHUTDOWN:
  618. usb_unlink_urb(urb);
  619. return;
  620. case -EPIPE:
  621. default:
  622. dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
  623. rr3->bytes_read = 0;
  624. break;
  625. }
  626. }
  627. static u16 mod_freq_to_val(unsigned int mod_freq)
  628. {
  629. int mult = 6000000;
  630. /* Clk used in mod. freq. generation is CLK24/4. */
  631. return 65536 - (mult / mod_freq);
  632. }
  633. static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  634. {
  635. struct redrat3_dev *rr3 = rcdev->priv;
  636. struct device *dev = rr3->dev;
  637. dev_dbg(dev, "Setting modulation frequency to %u", carrier);
  638. if (carrier == 0)
  639. return -EINVAL;
  640. rr3->carrier = carrier;
  641. return 0;
  642. }
  643. static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
  644. unsigned count)
  645. {
  646. struct redrat3_dev *rr3 = rcdev->priv;
  647. struct device *dev = rr3->dev;
  648. struct redrat3_irdata *irdata = NULL;
  649. int ret, ret_len;
  650. int lencheck, cur_sample_len, pipe;
  651. int *sample_lens = NULL;
  652. u8 curlencheck = 0;
  653. unsigned i, sendbuf_len;
  654. if (rr3->transmitting) {
  655. dev_warn(dev, "%s: transmitter already in use\n", __func__);
  656. return -EAGAIN;
  657. }
  658. if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
  659. return -EINVAL;
  660. /* rr3 will disable rc detector on transmit */
  661. rr3->transmitting = true;
  662. sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
  663. if (!sample_lens) {
  664. ret = -ENOMEM;
  665. goto out;
  666. }
  667. irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
  668. if (!irdata) {
  669. ret = -ENOMEM;
  670. goto out;
  671. }
  672. for (i = 0; i < count; i++) {
  673. cur_sample_len = redrat3_us_to_len(txbuf[i]);
  674. if (cur_sample_len > 0xffff) {
  675. dev_warn(dev, "transmit period of %uus truncated to %uus\n",
  676. txbuf[i], redrat3_len_to_us(0xffff));
  677. cur_sample_len = 0xffff;
  678. }
  679. for (lencheck = 0; lencheck < curlencheck; lencheck++) {
  680. if (sample_lens[lencheck] == cur_sample_len)
  681. break;
  682. }
  683. if (lencheck == curlencheck) {
  684. dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
  685. i, txbuf[i], curlencheck, cur_sample_len);
  686. if (curlencheck < RR3_DRIVER_MAXLENS) {
  687. /* now convert the value to a proper
  688. * rr3 value.. */
  689. sample_lens[curlencheck] = cur_sample_len;
  690. put_unaligned_be16(cur_sample_len,
  691. &irdata->lens[curlencheck]);
  692. curlencheck++;
  693. } else {
  694. ret = -EINVAL;
  695. goto out;
  696. }
  697. }
  698. irdata->sigdata[i] = lencheck;
  699. }
  700. irdata->sigdata[count] = RR3_END_OF_SIGNAL;
  701. irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
  702. sendbuf_len = offsetof(struct redrat3_irdata,
  703. sigdata[count + RR3_TX_TRAILER_LEN]);
  704. /* fill in our packet header */
  705. irdata->header.length = cpu_to_be16(sendbuf_len -
  706. sizeof(struct redrat3_header));
  707. irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
  708. irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
  709. irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
  710. irdata->no_lengths = curlencheck;
  711. irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
  712. pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
  713. ret = usb_bulk_msg(rr3->udev, pipe, irdata,
  714. sendbuf_len, &ret_len, 10 * HZ);
  715. dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
  716. /* now tell the hardware to transmit what we sent it */
  717. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  718. ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
  719. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  720. 0, 0, irdata, 2, HZ * 10);
  721. if (ret < 0)
  722. dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
  723. else
  724. ret = count;
  725. out:
  726. kfree(sample_lens);
  727. kfree(irdata);
  728. rr3->transmitting = false;
  729. /* rr3 re-enables rc detector because it was enabled before */
  730. return ret;
  731. }
  732. static void redrat3_brightness_set(struct led_classdev *led_dev, enum
  733. led_brightness brightness)
  734. {
  735. struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
  736. led);
  737. if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
  738. int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
  739. if (ret != 0) {
  740. dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
  741. __func__, ret);
  742. atomic_set(&rr3->flash, 0);
  743. }
  744. }
  745. }
  746. static void redrat3_led_complete(struct urb *urb)
  747. {
  748. struct redrat3_dev *rr3 = urb->context;
  749. switch (urb->status) {
  750. case 0:
  751. break;
  752. case -ECONNRESET:
  753. case -ENOENT:
  754. case -ESHUTDOWN:
  755. usb_unlink_urb(urb);
  756. return;
  757. case -EPIPE:
  758. default:
  759. dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
  760. break;
  761. }
  762. rr3->led.brightness = LED_OFF;
  763. atomic_dec(&rr3->flash);
  764. }
  765. static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
  766. {
  767. struct device *dev = rr3->dev;
  768. struct rc_dev *rc;
  769. int ret = -ENODEV;
  770. u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
  771. rc = rc_allocate_device();
  772. if (!rc) {
  773. dev_err(dev, "remote input dev allocation failed\n");
  774. goto out;
  775. }
  776. snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
  777. "Infrared Remote Transceiver (%04x:%04x)",
  778. prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
  779. le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
  780. usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
  781. rc->input_name = rr3->name;
  782. rc->input_phys = rr3->phys;
  783. usb_to_input_id(rr3->udev, &rc->input_id);
  784. rc->dev.parent = dev;
  785. rc->priv = rr3;
  786. rc->driver_type = RC_DRIVER_IR_RAW;
  787. rc->allowed_protocols = RC_BIT_ALL;
  788. rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT);
  789. rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT);
  790. rc->timeout = US_TO_NS(redrat3_get_timeout(rr3));
  791. rc->s_timeout = redrat3_set_timeout;
  792. rc->tx_ir = redrat3_transmit_ir;
  793. rc->s_tx_carrier = redrat3_set_tx_carrier;
  794. rc->driver_name = DRIVER_NAME;
  795. rc->rx_resolution = US_TO_NS(2);
  796. rc->map_name = RC_MAP_HAUPPAUGE;
  797. ret = rc_register_device(rc);
  798. if (ret < 0) {
  799. dev_err(dev, "remote dev registration failed\n");
  800. goto out;
  801. }
  802. return rc;
  803. out:
  804. rc_free_device(rc);
  805. return NULL;
  806. }
  807. static int redrat3_dev_probe(struct usb_interface *intf,
  808. const struct usb_device_id *id)
  809. {
  810. struct usb_device *udev = interface_to_usbdev(intf);
  811. struct device *dev = &intf->dev;
  812. struct usb_host_interface *uhi;
  813. struct redrat3_dev *rr3;
  814. struct usb_endpoint_descriptor *ep;
  815. struct usb_endpoint_descriptor *ep_in = NULL;
  816. struct usb_endpoint_descriptor *ep_out = NULL;
  817. u8 addr, attrs;
  818. int pipe, i;
  819. int retval = -ENOMEM;
  820. uhi = intf->cur_altsetting;
  821. /* find our bulk-in and bulk-out endpoints */
  822. for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
  823. ep = &uhi->endpoint[i].desc;
  824. addr = ep->bEndpointAddress;
  825. attrs = ep->bmAttributes;
  826. if ((ep_in == NULL) &&
  827. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
  828. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  829. USB_ENDPOINT_XFER_BULK)) {
  830. dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
  831. ep->bEndpointAddress);
  832. /* data comes in on 0x82, 0x81 is for other data... */
  833. if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
  834. ep_in = ep;
  835. }
  836. if ((ep_out == NULL) &&
  837. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
  838. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  839. USB_ENDPOINT_XFER_BULK)) {
  840. dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
  841. ep->bEndpointAddress);
  842. ep_out = ep;
  843. }
  844. }
  845. if (!ep_in || !ep_out) {
  846. dev_err(dev, "Couldn't find both in and out endpoints\n");
  847. retval = -ENODEV;
  848. goto no_endpoints;
  849. }
  850. /* allocate memory for our device state and initialize it */
  851. rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
  852. if (rr3 == NULL) {
  853. dev_err(dev, "Memory allocation failure\n");
  854. goto no_endpoints;
  855. }
  856. rr3->dev = &intf->dev;
  857. /* set up bulk-in endpoint */
  858. rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  859. if (!rr3->read_urb)
  860. goto error;
  861. rr3->ep_in = ep_in;
  862. rr3->bulk_in_buf = usb_alloc_coherent(udev,
  863. le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in);
  864. if (!rr3->bulk_in_buf) {
  865. dev_err(dev, "Read buffer allocation failure\n");
  866. goto error;
  867. }
  868. pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
  869. usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
  870. le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
  871. rr3->read_urb->transfer_dma = rr3->dma_in;
  872. rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  873. rr3->ep_out = ep_out;
  874. rr3->udev = udev;
  875. redrat3_reset(rr3);
  876. redrat3_get_firmware_rev(rr3);
  877. /* might be all we need to do? */
  878. retval = redrat3_enable_detector(rr3);
  879. if (retval < 0)
  880. goto error;
  881. /* default.. will get overridden by any sends with a freq defined */
  882. rr3->carrier = 38000;
  883. /* led control */
  884. rr3->led.name = "redrat3:red:feedback";
  885. rr3->led.default_trigger = "rc-feedback";
  886. rr3->led.brightness_set = redrat3_brightness_set;
  887. retval = led_classdev_register(&intf->dev, &rr3->led);
  888. if (retval)
  889. goto error;
  890. atomic_set(&rr3->flash, 0);
  891. rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
  892. if (!rr3->flash_urb) {
  893. retval = -ENOMEM;
  894. goto led_free_error;
  895. }
  896. /* setup packet is 'c0 b9 0000 0000 0001' */
  897. rr3->flash_control.bRequestType = 0xc0;
  898. rr3->flash_control.bRequest = RR3_BLINK_LED;
  899. rr3->flash_control.wLength = cpu_to_le16(1);
  900. usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
  901. (unsigned char *)&rr3->flash_control,
  902. &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
  903. redrat3_led_complete, rr3);
  904. rr3->rc = redrat3_init_rc_dev(rr3);
  905. if (!rr3->rc) {
  906. retval = -ENOMEM;
  907. goto led_free_error;
  908. }
  909. /* we can register the device now, as it is ready */
  910. usb_set_intfdata(intf, rr3);
  911. return 0;
  912. led_free_error:
  913. led_classdev_unregister(&rr3->led);
  914. error:
  915. redrat3_delete(rr3, rr3->udev);
  916. no_endpoints:
  917. dev_err(dev, "%s: retval = %x", __func__, retval);
  918. return retval;
  919. }
  920. static void redrat3_dev_disconnect(struct usb_interface *intf)
  921. {
  922. struct usb_device *udev = interface_to_usbdev(intf);
  923. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  924. if (!rr3)
  925. return;
  926. usb_set_intfdata(intf, NULL);
  927. rc_unregister_device(rr3->rc);
  928. led_classdev_unregister(&rr3->led);
  929. redrat3_delete(rr3, udev);
  930. }
  931. static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
  932. {
  933. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  934. led_classdev_suspend(&rr3->led);
  935. usb_kill_urb(rr3->read_urb);
  936. usb_kill_urb(rr3->flash_urb);
  937. return 0;
  938. }
  939. static int redrat3_dev_resume(struct usb_interface *intf)
  940. {
  941. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  942. if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
  943. return -EIO;
  944. led_classdev_resume(&rr3->led);
  945. return 0;
  946. }
  947. static struct usb_driver redrat3_dev_driver = {
  948. .name = DRIVER_NAME,
  949. .probe = redrat3_dev_probe,
  950. .disconnect = redrat3_dev_disconnect,
  951. .suspend = redrat3_dev_suspend,
  952. .resume = redrat3_dev_resume,
  953. .reset_resume = redrat3_dev_resume,
  954. .id_table = redrat3_dev_table
  955. };
  956. module_usb_driver(redrat3_dev_driver);
  957. MODULE_DESCRIPTION(DRIVER_DESC);
  958. MODULE_AUTHOR(DRIVER_AUTHOR);
  959. MODULE_AUTHOR(DRIVER_AUTHOR2);
  960. MODULE_LICENSE("GPL");
  961. MODULE_DEVICE_TABLE(usb, redrat3_dev_table);