redrat3.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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/module.h>
  50. #include <linux/slab.h>
  51. #include <linux/usb.h>
  52. #include <linux/usb/input.h>
  53. #include <media/rc-core.h>
  54. /* Driver Information */
  55. #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
  56. #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
  57. #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
  58. #define DRIVER_NAME "redrat3"
  59. /* module parameters */
  60. #ifdef CONFIG_USB_DEBUG
  61. static int debug = 1;
  62. #else
  63. static int debug;
  64. #endif
  65. #define RR3_DEBUG_STANDARD 0x1
  66. #define RR3_DEBUG_FUNCTION_TRACE 0x2
  67. #define rr3_dbg(dev, fmt, ...) \
  68. do { \
  69. if (debug & RR3_DEBUG_STANDARD) \
  70. dev_info(dev, fmt, ## __VA_ARGS__); \
  71. } while (0)
  72. #define rr3_ftr(dev, fmt, ...) \
  73. do { \
  74. if (debug & RR3_DEBUG_FUNCTION_TRACE) \
  75. dev_info(dev, fmt, ## __VA_ARGS__); \
  76. } while (0)
  77. /* bulk data transfer types */
  78. #define RR3_ERROR 0x01
  79. #define RR3_MOD_SIGNAL_IN 0x20
  80. #define RR3_MOD_SIGNAL_OUT 0x21
  81. /* Get the RR firmware version */
  82. #define RR3_FW_VERSION 0xb1
  83. #define RR3_FW_VERSION_LEN 64
  84. /* Send encoded signal bulk-sent earlier*/
  85. #define RR3_TX_SEND_SIGNAL 0xb3
  86. #define RR3_SET_IR_PARAM 0xb7
  87. #define RR3_GET_IR_PARAM 0xb8
  88. /* Blink the red LED on the device */
  89. #define RR3_BLINK_LED 0xb9
  90. /* Read serial number of device */
  91. #define RR3_READ_SER_NO 0xba
  92. #define RR3_SER_NO_LEN 4
  93. /* Start capture with the RC receiver */
  94. #define RR3_RC_DET_ENABLE 0xbb
  95. /* Stop capture with the RC receiver */
  96. #define RR3_RC_DET_DISABLE 0xbc
  97. /* Return the status of RC detector capture */
  98. #define RR3_RC_DET_STATUS 0xbd
  99. /* Reset redrat */
  100. #define RR3_RESET 0xa0
  101. /* Max number of lengths in the signal. */
  102. #define RR3_IR_IO_MAX_LENGTHS 0x01
  103. /* Periods to measure mod. freq. */
  104. #define RR3_IR_IO_PERIODS_MF 0x02
  105. /* Size of memory for main signal data */
  106. #define RR3_IR_IO_SIG_MEM_SIZE 0x03
  107. /* Delta value when measuring lengths */
  108. #define RR3_IR_IO_LENGTH_FUZZ 0x04
  109. /* Timeout for end of signal detection */
  110. #define RR3_IR_IO_SIG_TIMEOUT 0x05
  111. /* Minumum value for pause recognition. */
  112. #define RR3_IR_IO_MIN_PAUSE 0x06
  113. /* Clock freq. of EZ-USB chip */
  114. #define RR3_CLK 24000000
  115. /* Clock periods per timer count */
  116. #define RR3_CLK_PER_COUNT 12
  117. /* (RR3_CLK / RR3_CLK_PER_COUNT) */
  118. #define RR3_CLK_CONV_FACTOR 2000000
  119. /* USB bulk-in IR data endpoint address */
  120. #define RR3_BULK_IN_EP_ADDR 0x82
  121. /* Size of the fixed-length portion of the signal */
  122. #define RR3_DRIVER_MAXLENS 128
  123. #define RR3_MAX_SIG_SIZE 512
  124. #define RR3_TIME_UNIT 50
  125. #define RR3_END_OF_SIGNAL 0x7f
  126. #define RR3_TX_TRAILER_LEN 2
  127. #define RR3_RX_MIN_TIMEOUT 5
  128. #define RR3_RX_MAX_TIMEOUT 2000
  129. /* The 8051's CPUCS Register address */
  130. #define RR3_CPUCS_REG_ADDR 0x7f92
  131. #define USB_RR3USB_VENDOR_ID 0x112a
  132. #define USB_RR3USB_PRODUCT_ID 0x0001
  133. #define USB_RR3IIUSB_PRODUCT_ID 0x0005
  134. struct redrat3_header {
  135. __be16 length;
  136. __be16 transfer_type;
  137. } __packed;
  138. /* sending and receiving irdata */
  139. struct redrat3_irdata {
  140. struct redrat3_header header;
  141. __be32 pause;
  142. __be16 mod_freq_count;
  143. __be16 num_periods;
  144. __u8 max_lengths;
  145. __u8 no_lengths;
  146. __be16 max_sig_size;
  147. __be16 sig_size;
  148. __u8 no_repeats;
  149. __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
  150. __u8 sigdata[RR3_MAX_SIG_SIZE];
  151. } __packed;
  152. /* firmware errors */
  153. struct redrat3_error {
  154. struct redrat3_header header;
  155. __be16 fw_error;
  156. } __packed;
  157. /* table of devices that work with this driver */
  158. static struct usb_device_id redrat3_dev_table[] = {
  159. /* Original version of the RedRat3 */
  160. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
  161. /* Second Version/release of the RedRat3 - RetRat3-II */
  162. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
  163. {} /* Terminating entry */
  164. };
  165. /* Structure to hold all of our device specific stuff */
  166. struct redrat3_dev {
  167. /* core device bits */
  168. struct rc_dev *rc;
  169. struct device *dev;
  170. /* save off the usb device pointer */
  171. struct usb_device *udev;
  172. /* the receive endpoint */
  173. struct usb_endpoint_descriptor *ep_in;
  174. /* the buffer to receive data */
  175. void *bulk_in_buf;
  176. /* urb used to read ir data */
  177. struct urb *read_urb;
  178. /* the send endpoint */
  179. struct usb_endpoint_descriptor *ep_out;
  180. /* usb dma */
  181. dma_addr_t dma_in;
  182. /* rx signal timeout timer */
  183. struct timer_list rx_timeout;
  184. u32 hw_timeout;
  185. /* is the detector enabled*/
  186. bool det_enabled;
  187. /* Is the device currently transmitting?*/
  188. bool transmitting;
  189. /* store for current packet */
  190. struct redrat3_irdata irdata;
  191. u16 bytes_read;
  192. u32 carrier;
  193. char name[64];
  194. char phys[64];
  195. };
  196. /*
  197. * redrat3_issue_async
  198. *
  199. * Issues an async read to the ir data in port..
  200. * sets the callback to be redrat3_handle_async
  201. */
  202. static void redrat3_issue_async(struct redrat3_dev *rr3)
  203. {
  204. int res;
  205. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  206. res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
  207. if (res)
  208. rr3_dbg(rr3->dev, "%s: receive request FAILED! "
  209. "(res %d, len %d)\n", __func__, res,
  210. rr3->read_urb->transfer_buffer_length);
  211. }
  212. static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
  213. {
  214. if (!rr3->transmitting && (code != 0x40))
  215. dev_info(rr3->dev, "fw error code 0x%02x: ", code);
  216. switch (code) {
  217. case 0x00:
  218. pr_cont("No Error\n");
  219. break;
  220. /* Codes 0x20 through 0x2f are IR Firmware Errors */
  221. case 0x20:
  222. pr_cont("Initial signal pulse not long enough "
  223. "to measure carrier frequency\n");
  224. break;
  225. case 0x21:
  226. pr_cont("Not enough length values allocated for signal\n");
  227. break;
  228. case 0x22:
  229. pr_cont("Not enough memory allocated for signal data\n");
  230. break;
  231. case 0x23:
  232. pr_cont("Too many signal repeats\n");
  233. break;
  234. case 0x28:
  235. pr_cont("Insufficient memory available for IR signal "
  236. "data memory allocation\n");
  237. break;
  238. case 0x29:
  239. pr_cont("Insufficient memory available "
  240. "for IrDa signal data memory allocation\n");
  241. break;
  242. /* Codes 0x30 through 0x3f are USB Firmware Errors */
  243. case 0x30:
  244. pr_cont("Insufficient memory available for bulk "
  245. "transfer structure\n");
  246. break;
  247. /*
  248. * Other error codes... These are primarily errors that can occur in
  249. * the control messages sent to the redrat
  250. */
  251. case 0x40:
  252. if (!rr3->transmitting)
  253. pr_cont("Signal capture has been terminated\n");
  254. break;
  255. case 0x41:
  256. pr_cont("Attempt to set/get and unknown signal I/O "
  257. "algorithm parameter\n");
  258. break;
  259. case 0x42:
  260. pr_cont("Signal capture already started\n");
  261. break;
  262. default:
  263. pr_cont("Unknown Error\n");
  264. break;
  265. }
  266. }
  267. static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
  268. {
  269. u32 mod_freq = 0;
  270. u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
  271. if (mod_freq_count != 0)
  272. mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
  273. (mod_freq_count * RR3_CLK_PER_COUNT);
  274. return mod_freq;
  275. }
  276. /* this function scales down the figures for the same result... */
  277. static u32 redrat3_len_to_us(u32 length)
  278. {
  279. u32 biglen = length * 1000;
  280. u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
  281. u32 result = (u32) (biglen / divisor);
  282. /* don't allow zero lengths to go back, breaks lirc */
  283. return result ? result : 1;
  284. }
  285. /*
  286. * convert us back into redrat3 lengths
  287. *
  288. * length * 1000 length * 1000000
  289. * ------------- = ---------------- = micro
  290. * rr3clk / 1000 rr3clk
  291. * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
  292. * ----- = 4 ----- = 6 -------------- = len ---------------------
  293. * 3 2 1000000 1000
  294. */
  295. static u32 redrat3_us_to_len(u32 microsec)
  296. {
  297. u32 result;
  298. u32 divisor;
  299. microsec &= IR_MAX_DURATION;
  300. divisor = (RR3_CLK_CONV_FACTOR / 1000);
  301. result = (u32)(microsec * divisor) / 1000;
  302. /* don't allow zero lengths to go back, breaks lirc */
  303. return result ? result : 1;
  304. }
  305. /* timer callback to send reset event */
  306. static void redrat3_rx_timeout(unsigned long data)
  307. {
  308. struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
  309. rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n");
  310. ir_raw_event_reset(rr3->rc);
  311. }
  312. static void redrat3_process_ir_data(struct redrat3_dev *rr3)
  313. {
  314. DEFINE_IR_RAW_EVENT(rawir);
  315. struct device *dev;
  316. unsigned i, trailer = 0;
  317. unsigned sig_size, single_len, offset, val;
  318. unsigned long delay;
  319. u32 mod_freq;
  320. if (!rr3) {
  321. pr_err("%s called with no context!\n", __func__);
  322. return;
  323. }
  324. rr3_ftr(rr3->dev, "Entered %s\n", __func__);
  325. dev = rr3->dev;
  326. /* Make sure we reset the IR kfifo after a bit of inactivity */
  327. delay = usecs_to_jiffies(rr3->hw_timeout);
  328. mod_timer(&rr3->rx_timeout, jiffies + delay);
  329. mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
  330. rr3_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. /* Save initial pulse length to fudge trailer */
  344. if (i == 0)
  345. trailer = rawir.duration;
  346. /* cap the value to IR_MAX_DURATION */
  347. rawir.duration &= IR_MAX_DURATION;
  348. rr3_dbg(dev, "storing %s with duration %d (i: %d)\n",
  349. rawir.pulse ? "pulse" : "space", rawir.duration, i);
  350. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  351. }
  352. /* add a trailing space, if need be */
  353. if (i % 2) {
  354. rawir.pulse = false;
  355. /* this duration is made up, and may not be ideal... */
  356. if (trailer < US_TO_NS(1000))
  357. rawir.duration = US_TO_NS(2800);
  358. else
  359. rawir.duration = trailer;
  360. rr3_dbg(dev, "storing trailing space with duration %d\n",
  361. rawir.duration);
  362. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  363. }
  364. rr3_dbg(dev, "calling ir_raw_event_handle\n");
  365. ir_raw_event_handle(rr3->rc);
  366. }
  367. /* Util fn to send rr3 cmds */
  368. static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
  369. {
  370. struct usb_device *udev;
  371. u8 *data;
  372. int res;
  373. data = kzalloc(sizeof(u8), GFP_KERNEL);
  374. if (!data)
  375. return -ENOMEM;
  376. udev = rr3->udev;
  377. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
  378. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  379. 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
  380. if (res < 0) {
  381. dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
  382. __func__, res, *data);
  383. res = -EIO;
  384. } else
  385. res = data[0];
  386. kfree(data);
  387. return res;
  388. }
  389. /* Enables the long range detector and starts async receive */
  390. static int redrat3_enable_detector(struct redrat3_dev *rr3)
  391. {
  392. struct device *dev = rr3->dev;
  393. u8 ret;
  394. rr3_ftr(dev, "Entering %s\n", __func__);
  395. ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
  396. if (ret != 0)
  397. dev_dbg(dev, "%s: unexpected ret of %d\n",
  398. __func__, ret);
  399. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  400. if (ret != 1) {
  401. dev_err(dev, "%s: detector status: %d, should be 1\n",
  402. __func__, ret);
  403. return -EIO;
  404. }
  405. rr3->det_enabled = true;
  406. redrat3_issue_async(rr3);
  407. return 0;
  408. }
  409. /* Disables the rr3 long range detector */
  410. static void redrat3_disable_detector(struct redrat3_dev *rr3)
  411. {
  412. struct device *dev = rr3->dev;
  413. u8 ret;
  414. rr3_ftr(dev, "Entering %s\n", __func__);
  415. ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3);
  416. if (ret != 0)
  417. dev_err(dev, "%s: failure!\n", __func__);
  418. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  419. if (ret != 0)
  420. dev_warn(dev, "%s: detector status: %d, should be 0\n",
  421. __func__, ret);
  422. rr3->det_enabled = false;
  423. }
  424. static inline void redrat3_delete(struct redrat3_dev *rr3,
  425. struct usb_device *udev)
  426. {
  427. rr3_ftr(rr3->dev, "%s cleaning up\n", __func__);
  428. usb_kill_urb(rr3->read_urb);
  429. usb_free_urb(rr3->read_urb);
  430. usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
  431. rr3->bulk_in_buf, rr3->dma_in);
  432. kfree(rr3);
  433. }
  434. static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
  435. {
  436. __be32 *tmp;
  437. u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
  438. int len, ret, pipe;
  439. len = sizeof(*tmp);
  440. tmp = kzalloc(len, GFP_KERNEL);
  441. if (!tmp) {
  442. dev_warn(rr3->dev, "Memory allocation faillure\n");
  443. return timeout;
  444. }
  445. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  446. ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
  447. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  448. RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
  449. if (ret != len)
  450. dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
  451. else {
  452. timeout = redrat3_len_to_us(be32_to_cpup(tmp));
  453. rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
  454. }
  455. kfree(tmp);
  456. return timeout;
  457. }
  458. static void redrat3_reset(struct redrat3_dev *rr3)
  459. {
  460. struct usb_device *udev = rr3->udev;
  461. struct device *dev = rr3->dev;
  462. int rc, rxpipe, txpipe;
  463. u8 *val;
  464. int len = sizeof(u8);
  465. rr3_ftr(dev, "Entering %s\n", __func__);
  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. rr3_dbg(dev, "reset returned 0x%02x\n", rc);
  478. *val = 5;
  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. rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
  483. *val = RR3_DRIVER_MAXLENS;
  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_MAX_LENGTHS, 0, val, len, HZ * 25);
  487. rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
  488. kfree(val);
  489. }
  490. static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
  491. {
  492. int rc = 0;
  493. char *buffer;
  494. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  495. buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
  496. if (!buffer) {
  497. dev_err(rr3->dev, "Memory allocation failure\n");
  498. return;
  499. }
  500. rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
  501. RR3_FW_VERSION,
  502. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  503. 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
  504. if (rc >= 0)
  505. dev_info(rr3->dev, "Firmware rev: %s", buffer);
  506. else
  507. dev_err(rr3->dev, "Problem fetching firmware ID\n");
  508. kfree(buffer);
  509. rr3_ftr(rr3->dev, "Exiting %s\n", __func__);
  510. }
  511. static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
  512. {
  513. struct redrat3_header *header = rr3->bulk_in_buf;
  514. unsigned pktlen, pkttype;
  515. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  516. /* grab the Length and type of transfer */
  517. pktlen = be16_to_cpu(header->length);
  518. pkttype = be16_to_cpu(header->transfer_type);
  519. if (pktlen > sizeof(rr3->irdata)) {
  520. dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
  521. return;
  522. }
  523. switch (pkttype) {
  524. case RR3_ERROR:
  525. if (len >= sizeof(struct redrat3_error)) {
  526. struct redrat3_error *error = rr3->bulk_in_buf;
  527. unsigned fw_error = be16_to_cpu(error->fw_error);
  528. redrat3_dump_fw_error(rr3, fw_error);
  529. }
  530. break;
  531. case RR3_MOD_SIGNAL_IN:
  532. memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
  533. rr3->bytes_read = len;
  534. rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
  535. rr3->bytes_read, pktlen);
  536. break;
  537. default:
  538. rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
  539. pkttype, len, pktlen);
  540. break;
  541. }
  542. }
  543. static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
  544. {
  545. void *irdata = &rr3->irdata;
  546. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  547. if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
  548. dev_warn(rr3->dev, "too much data for packet\n");
  549. rr3->bytes_read = 0;
  550. return;
  551. }
  552. memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
  553. rr3->bytes_read += len;
  554. rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
  555. be16_to_cpu(rr3->irdata.header.length));
  556. }
  557. /* gather IR data from incoming urb, process it when we have enough */
  558. static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
  559. {
  560. struct device *dev = rr3->dev;
  561. unsigned pkttype;
  562. int ret = 0;
  563. rr3_ftr(dev, "Entering %s\n", __func__);
  564. if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
  565. redrat3_read_packet_start(rr3, len);
  566. } else if (rr3->bytes_read != 0) {
  567. redrat3_read_packet_continue(rr3, len);
  568. } else if (rr3->bytes_read == 0) {
  569. dev_err(dev, "error: no packet data read\n");
  570. ret = -ENODATA;
  571. goto out;
  572. }
  573. if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length))
  574. /* we're still accumulating data */
  575. return 0;
  576. /* if we get here, we've got IR data to decode */
  577. pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
  578. if (pkttype == RR3_MOD_SIGNAL_IN)
  579. redrat3_process_ir_data(rr3);
  580. else
  581. rr3_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
  582. pkttype);
  583. out:
  584. rr3->bytes_read = 0;
  585. return ret;
  586. }
  587. /* callback function from USB when async USB request has completed */
  588. static void redrat3_handle_async(struct urb *urb)
  589. {
  590. struct redrat3_dev *rr3;
  591. int ret;
  592. if (!urb)
  593. return;
  594. rr3 = urb->context;
  595. if (!rr3) {
  596. pr_err("%s called with invalid context!\n", __func__);
  597. usb_unlink_urb(urb);
  598. return;
  599. }
  600. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  601. switch (urb->status) {
  602. case 0:
  603. ret = redrat3_get_ir_data(rr3, urb->actual_length);
  604. if (!ret) {
  605. /* no error, prepare to read more */
  606. redrat3_issue_async(rr3);
  607. }
  608. break;
  609. case -ECONNRESET:
  610. case -ENOENT:
  611. case -ESHUTDOWN:
  612. usb_unlink_urb(urb);
  613. return;
  614. case -EPIPE:
  615. default:
  616. dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
  617. rr3->bytes_read = 0;
  618. break;
  619. }
  620. }
  621. static u16 mod_freq_to_val(unsigned int mod_freq)
  622. {
  623. int mult = 6000000;
  624. /* Clk used in mod. freq. generation is CLK24/4. */
  625. return 65536 - (mult / mod_freq);
  626. }
  627. static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  628. {
  629. struct redrat3_dev *rr3 = rcdev->priv;
  630. struct device *dev = rr3->dev;
  631. rr3_dbg(dev, "Setting modulation frequency to %u", carrier);
  632. if (carrier == 0)
  633. return -EINVAL;
  634. rr3->carrier = carrier;
  635. return carrier;
  636. }
  637. static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
  638. unsigned count)
  639. {
  640. struct redrat3_dev *rr3 = rcdev->priv;
  641. struct device *dev = rr3->dev;
  642. struct redrat3_irdata *irdata = NULL;
  643. int ret, ret_len;
  644. int lencheck, cur_sample_len, pipe;
  645. int *sample_lens = NULL;
  646. u8 curlencheck = 0;
  647. unsigned i, sendbuf_len;
  648. rr3_ftr(dev, "Entering %s\n", __func__);
  649. if (rr3->transmitting) {
  650. dev_warn(dev, "%s: transmitter already in use\n", __func__);
  651. return -EAGAIN;
  652. }
  653. count = min_t(unsigned, count, RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN);
  654. /* rr3 will disable rc detector on transmit */
  655. rr3->det_enabled = false;
  656. rr3->transmitting = true;
  657. sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
  658. if (!sample_lens) {
  659. ret = -ENOMEM;
  660. goto out;
  661. }
  662. irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
  663. if (!irdata) {
  664. ret = -ENOMEM;
  665. goto out;
  666. }
  667. for (i = 0; i < count; i++) {
  668. cur_sample_len = redrat3_us_to_len(txbuf[i]);
  669. if (cur_sample_len > 0xffff) {
  670. dev_warn(dev, "transmit period of %uus truncated to %uus\n",
  671. txbuf[i], redrat3_len_to_us(0xffff));
  672. cur_sample_len = 0xffff;
  673. }
  674. for (lencheck = 0; lencheck < curlencheck; lencheck++) {
  675. if (sample_lens[lencheck] == cur_sample_len)
  676. break;
  677. }
  678. if (lencheck == curlencheck) {
  679. rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
  680. i, txbuf[i], curlencheck, cur_sample_len);
  681. if (curlencheck < RR3_DRIVER_MAXLENS) {
  682. /* now convert the value to a proper
  683. * rr3 value.. */
  684. sample_lens[curlencheck] = cur_sample_len;
  685. put_unaligned_be16(cur_sample_len,
  686. &irdata->lens[curlencheck]);
  687. curlencheck++;
  688. } else {
  689. count = i - 1;
  690. break;
  691. }
  692. }
  693. irdata->sigdata[i] = lencheck;
  694. }
  695. irdata->sigdata[count] = RR3_END_OF_SIGNAL;
  696. irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
  697. sendbuf_len = offsetof(struct redrat3_irdata,
  698. sigdata[count + RR3_TX_TRAILER_LEN]);
  699. /* fill in our packet header */
  700. irdata->header.length = cpu_to_be16(sendbuf_len -
  701. sizeof(struct redrat3_header));
  702. irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
  703. irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
  704. irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
  705. irdata->no_lengths = curlencheck;
  706. irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
  707. pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
  708. ret = usb_bulk_msg(rr3->udev, pipe, irdata,
  709. sendbuf_len, &ret_len, 10 * HZ);
  710. rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
  711. /* now tell the hardware to transmit what we sent it */
  712. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  713. ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
  714. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  715. 0, 0, irdata, 2, HZ * 10);
  716. if (ret < 0)
  717. dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
  718. else
  719. ret = count;
  720. out:
  721. kfree(sample_lens);
  722. kfree(irdata);
  723. rr3->transmitting = false;
  724. /* rr3 re-enables rc detector because it was enabled before */
  725. rr3->det_enabled = true;
  726. return ret;
  727. }
  728. static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
  729. {
  730. struct device *dev = rr3->dev;
  731. struct rc_dev *rc;
  732. int ret = -ENODEV;
  733. u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
  734. rc = rc_allocate_device();
  735. if (!rc) {
  736. dev_err(dev, "remote input dev allocation failed\n");
  737. goto out;
  738. }
  739. snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
  740. "Infrared Remote Transceiver (%04x:%04x)",
  741. prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
  742. le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
  743. usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
  744. rc->input_name = rr3->name;
  745. rc->input_phys = rr3->phys;
  746. usb_to_input_id(rr3->udev, &rc->input_id);
  747. rc->dev.parent = dev;
  748. rc->priv = rr3;
  749. rc->driver_type = RC_DRIVER_IR_RAW;
  750. rc->allowed_protos = RC_BIT_ALL;
  751. rc->timeout = US_TO_NS(2750);
  752. rc->tx_ir = redrat3_transmit_ir;
  753. rc->s_tx_carrier = redrat3_set_tx_carrier;
  754. rc->driver_name = DRIVER_NAME;
  755. rc->rx_resolution = US_TO_NS(2);
  756. rc->map_name = RC_MAP_HAUPPAUGE;
  757. ret = rc_register_device(rc);
  758. if (ret < 0) {
  759. dev_err(dev, "remote dev registration failed\n");
  760. goto out;
  761. }
  762. return rc;
  763. out:
  764. rc_free_device(rc);
  765. return NULL;
  766. }
  767. static int redrat3_dev_probe(struct usb_interface *intf,
  768. const struct usb_device_id *id)
  769. {
  770. struct usb_device *udev = interface_to_usbdev(intf);
  771. struct device *dev = &intf->dev;
  772. struct usb_host_interface *uhi;
  773. struct redrat3_dev *rr3;
  774. struct usb_endpoint_descriptor *ep;
  775. struct usb_endpoint_descriptor *ep_in = NULL;
  776. struct usb_endpoint_descriptor *ep_out = NULL;
  777. u8 addr, attrs;
  778. int pipe, i;
  779. int retval = -ENOMEM;
  780. rr3_ftr(dev, "%s called\n", __func__);
  781. uhi = intf->cur_altsetting;
  782. /* find our bulk-in and bulk-out endpoints */
  783. for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
  784. ep = &uhi->endpoint[i].desc;
  785. addr = ep->bEndpointAddress;
  786. attrs = ep->bmAttributes;
  787. if ((ep_in == NULL) &&
  788. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
  789. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  790. USB_ENDPOINT_XFER_BULK)) {
  791. rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
  792. ep->bEndpointAddress);
  793. /* data comes in on 0x82, 0x81 is for other data... */
  794. if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
  795. ep_in = ep;
  796. }
  797. if ((ep_out == NULL) &&
  798. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
  799. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  800. USB_ENDPOINT_XFER_BULK)) {
  801. rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
  802. ep->bEndpointAddress);
  803. ep_out = ep;
  804. }
  805. }
  806. if (!ep_in || !ep_out) {
  807. dev_err(dev, "Couldn't find both in and out endpoints\n");
  808. retval = -ENODEV;
  809. goto no_endpoints;
  810. }
  811. /* allocate memory for our device state and initialize it */
  812. rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
  813. if (rr3 == NULL) {
  814. dev_err(dev, "Memory allocation failure\n");
  815. goto no_endpoints;
  816. }
  817. rr3->dev = &intf->dev;
  818. /* set up bulk-in endpoint */
  819. rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  820. if (!rr3->read_urb) {
  821. dev_err(dev, "Read urb allocation failure\n");
  822. goto error;
  823. }
  824. rr3->ep_in = ep_in;
  825. rr3->bulk_in_buf = usb_alloc_coherent(udev,
  826. le16_to_cpu(ep_in->wMaxPacketSize), GFP_ATOMIC, &rr3->dma_in);
  827. if (!rr3->bulk_in_buf) {
  828. dev_err(dev, "Read buffer allocation failure\n");
  829. goto error;
  830. }
  831. pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
  832. usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
  833. le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
  834. rr3->ep_out = ep_out;
  835. rr3->udev = udev;
  836. redrat3_reset(rr3);
  837. redrat3_get_firmware_rev(rr3);
  838. /* might be all we need to do? */
  839. retval = redrat3_enable_detector(rr3);
  840. if (retval < 0)
  841. goto error;
  842. /* store current hardware timeout, in us, will use for kfifo resets */
  843. rr3->hw_timeout = redrat3_get_timeout(rr3);
  844. /* default.. will get overridden by any sends with a freq defined */
  845. rr3->carrier = 38000;
  846. rr3->rc = redrat3_init_rc_dev(rr3);
  847. if (!rr3->rc) {
  848. retval = -ENOMEM;
  849. goto error;
  850. }
  851. setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
  852. /* we can register the device now, as it is ready */
  853. usb_set_intfdata(intf, rr3);
  854. rr3_ftr(dev, "Exiting %s\n", __func__);
  855. return 0;
  856. error:
  857. redrat3_delete(rr3, rr3->udev);
  858. no_endpoints:
  859. dev_err(dev, "%s: retval = %x", __func__, retval);
  860. return retval;
  861. }
  862. static void redrat3_dev_disconnect(struct usb_interface *intf)
  863. {
  864. struct usb_device *udev = interface_to_usbdev(intf);
  865. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  866. rr3_ftr(&intf->dev, "Entering %s\n", __func__);
  867. if (!rr3)
  868. return;
  869. redrat3_disable_detector(rr3);
  870. usb_set_intfdata(intf, NULL);
  871. rc_unregister_device(rr3->rc);
  872. del_timer_sync(&rr3->rx_timeout);
  873. redrat3_delete(rr3, udev);
  874. rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n");
  875. }
  876. static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
  877. {
  878. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  879. rr3_ftr(rr3->dev, "suspend\n");
  880. usb_kill_urb(rr3->read_urb);
  881. return 0;
  882. }
  883. static int redrat3_dev_resume(struct usb_interface *intf)
  884. {
  885. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  886. rr3_ftr(rr3->dev, "resume\n");
  887. if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
  888. return -EIO;
  889. return 0;
  890. }
  891. static struct usb_driver redrat3_dev_driver = {
  892. .name = DRIVER_NAME,
  893. .probe = redrat3_dev_probe,
  894. .disconnect = redrat3_dev_disconnect,
  895. .suspend = redrat3_dev_suspend,
  896. .resume = redrat3_dev_resume,
  897. .reset_resume = redrat3_dev_resume,
  898. .id_table = redrat3_dev_table
  899. };
  900. module_usb_driver(redrat3_dev_driver);
  901. MODULE_DESCRIPTION(DRIVER_DESC);
  902. MODULE_AUTHOR(DRIVER_AUTHOR);
  903. MODULE_AUTHOR(DRIVER_AUTHOR2);
  904. MODULE_LICENSE("GPL");
  905. MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
  906. module_param(debug, int, S_IRUGO | S_IWUSR);
  907. MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) "
  908. "0x1 = standard debug messages, 0x2 = function tracing debug. "
  909. "Flag bits are addative (i.e., 0x3 for both debug types).");