redrat3.c 28 KB

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