redrat3.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
  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 = (rawir.duration > IR_MAX_DURATION) ?
  333. IR_MAX_DURATION : rawir.duration;
  334. dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
  335. rawir.pulse ? "pulse" : "space", rawir.duration, i);
  336. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  337. }
  338. /* add a trailing space, if need be */
  339. if (i % 2) {
  340. rawir.pulse = false;
  341. /* this duration is made up, and may not be ideal... */
  342. if (trailer < US_TO_NS(1000))
  343. rawir.duration = US_TO_NS(2800);
  344. else
  345. rawir.duration = trailer;
  346. dev_dbg(dev, "storing trailing space with duration %d\n",
  347. rawir.duration);
  348. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  349. }
  350. dev_dbg(dev, "calling ir_raw_event_handle\n");
  351. ir_raw_event_handle(rr3->rc);
  352. }
  353. /* Util fn to send rr3 cmds */
  354. static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
  355. {
  356. struct usb_device *udev;
  357. u8 *data;
  358. int res;
  359. data = kzalloc(sizeof(u8), GFP_KERNEL);
  360. if (!data)
  361. return -ENOMEM;
  362. udev = rr3->udev;
  363. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
  364. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  365. 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
  366. if (res < 0) {
  367. dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
  368. __func__, res, *data);
  369. res = -EIO;
  370. } else
  371. res = data[0];
  372. kfree(data);
  373. return res;
  374. }
  375. /* Enables the long range detector and starts async receive */
  376. static int redrat3_enable_detector(struct redrat3_dev *rr3)
  377. {
  378. struct device *dev = rr3->dev;
  379. u8 ret;
  380. ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
  381. if (ret != 0)
  382. dev_dbg(dev, "%s: unexpected ret of %d\n",
  383. __func__, ret);
  384. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  385. if (ret != 1) {
  386. dev_err(dev, "%s: detector status: %d, should be 1\n",
  387. __func__, ret);
  388. return -EIO;
  389. }
  390. redrat3_issue_async(rr3);
  391. return 0;
  392. }
  393. static inline void redrat3_delete(struct redrat3_dev *rr3,
  394. struct usb_device *udev)
  395. {
  396. usb_kill_urb(rr3->read_urb);
  397. usb_kill_urb(rr3->flash_urb);
  398. usb_free_urb(rr3->read_urb);
  399. usb_free_urb(rr3->flash_urb);
  400. usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
  401. rr3->bulk_in_buf, rr3->dma_in);
  402. kfree(rr3);
  403. }
  404. static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
  405. {
  406. __be32 *tmp;
  407. u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
  408. int len, ret, pipe;
  409. len = sizeof(*tmp);
  410. tmp = kzalloc(len, GFP_KERNEL);
  411. if (!tmp) {
  412. dev_warn(rr3->dev, "Memory allocation faillure\n");
  413. return timeout;
  414. }
  415. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  416. ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
  417. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  418. RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
  419. if (ret != len)
  420. dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
  421. else {
  422. timeout = redrat3_len_to_us(be32_to_cpup(tmp));
  423. dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
  424. }
  425. kfree(tmp);
  426. return timeout;
  427. }
  428. static void redrat3_reset(struct redrat3_dev *rr3)
  429. {
  430. struct usb_device *udev = rr3->udev;
  431. struct device *dev = rr3->dev;
  432. int rc, rxpipe, txpipe;
  433. u8 *val;
  434. int len = sizeof(u8);
  435. rxpipe = usb_rcvctrlpipe(udev, 0);
  436. txpipe = usb_sndctrlpipe(udev, 0);
  437. val = kmalloc(len, GFP_KERNEL);
  438. if (!val) {
  439. dev_err(dev, "Memory allocation failure\n");
  440. return;
  441. }
  442. *val = 0x01;
  443. rc = usb_control_msg(udev, rxpipe, RR3_RESET,
  444. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  445. RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
  446. dev_dbg(dev, "reset returned 0x%02x\n", rc);
  447. *val = 5;
  448. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  449. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  450. RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
  451. dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
  452. *val = RR3_DRIVER_MAXLENS;
  453. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  454. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  455. RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
  456. dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
  457. kfree(val);
  458. }
  459. static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
  460. {
  461. int rc = 0;
  462. char *buffer;
  463. buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
  464. if (!buffer) {
  465. dev_err(rr3->dev, "Memory allocation failure\n");
  466. return;
  467. }
  468. rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
  469. RR3_FW_VERSION,
  470. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  471. 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
  472. if (rc >= 0)
  473. dev_info(rr3->dev, "Firmware rev: %s", buffer);
  474. else
  475. dev_err(rr3->dev, "Problem fetching firmware ID\n");
  476. kfree(buffer);
  477. }
  478. static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
  479. {
  480. struct redrat3_header *header = rr3->bulk_in_buf;
  481. unsigned pktlen, pkttype;
  482. /* grab the Length and type of transfer */
  483. pktlen = be16_to_cpu(header->length);
  484. pkttype = be16_to_cpu(header->transfer_type);
  485. if (pktlen > sizeof(rr3->irdata)) {
  486. dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
  487. return;
  488. }
  489. switch (pkttype) {
  490. case RR3_ERROR:
  491. if (len >= sizeof(struct redrat3_error)) {
  492. struct redrat3_error *error = rr3->bulk_in_buf;
  493. unsigned fw_error = be16_to_cpu(error->fw_error);
  494. redrat3_dump_fw_error(rr3, fw_error);
  495. }
  496. break;
  497. case RR3_MOD_SIGNAL_IN:
  498. memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
  499. rr3->bytes_read = len;
  500. dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
  501. rr3->bytes_read, pktlen);
  502. break;
  503. default:
  504. dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
  505. pkttype, len, pktlen);
  506. break;
  507. }
  508. }
  509. static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
  510. {
  511. void *irdata = &rr3->irdata;
  512. if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
  513. dev_warn(rr3->dev, "too much data for packet\n");
  514. rr3->bytes_read = 0;
  515. return;
  516. }
  517. memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
  518. rr3->bytes_read += len;
  519. dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
  520. be16_to_cpu(rr3->irdata.header.length));
  521. }
  522. /* gather IR data from incoming urb, process it when we have enough */
  523. static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
  524. {
  525. struct device *dev = rr3->dev;
  526. unsigned pkttype;
  527. int ret = 0;
  528. if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
  529. redrat3_read_packet_start(rr3, len);
  530. } else if (rr3->bytes_read != 0) {
  531. redrat3_read_packet_continue(rr3, len);
  532. } else if (rr3->bytes_read == 0) {
  533. dev_err(dev, "error: no packet data read\n");
  534. ret = -ENODATA;
  535. goto out;
  536. }
  537. if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
  538. sizeof(struct redrat3_header))
  539. /* we're still accumulating data */
  540. return 0;
  541. /* if we get here, we've got IR data to decode */
  542. pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
  543. if (pkttype == RR3_MOD_SIGNAL_IN)
  544. redrat3_process_ir_data(rr3);
  545. else
  546. dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
  547. pkttype);
  548. out:
  549. rr3->bytes_read = 0;
  550. return ret;
  551. }
  552. /* callback function from USB when async USB request has completed */
  553. static void redrat3_handle_async(struct urb *urb)
  554. {
  555. struct redrat3_dev *rr3;
  556. int ret;
  557. if (!urb)
  558. return;
  559. rr3 = urb->context;
  560. if (!rr3) {
  561. pr_err("%s called with invalid context!\n", __func__);
  562. usb_unlink_urb(urb);
  563. return;
  564. }
  565. switch (urb->status) {
  566. case 0:
  567. ret = redrat3_get_ir_data(rr3, urb->actual_length);
  568. if (!ret) {
  569. /* no error, prepare to read more */
  570. redrat3_issue_async(rr3);
  571. }
  572. break;
  573. case -ECONNRESET:
  574. case -ENOENT:
  575. case -ESHUTDOWN:
  576. usb_unlink_urb(urb);
  577. return;
  578. case -EPIPE:
  579. default:
  580. dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
  581. rr3->bytes_read = 0;
  582. break;
  583. }
  584. }
  585. static u16 mod_freq_to_val(unsigned int mod_freq)
  586. {
  587. int mult = 6000000;
  588. /* Clk used in mod. freq. generation is CLK24/4. */
  589. return 65536 - (mult / mod_freq);
  590. }
  591. static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  592. {
  593. struct redrat3_dev *rr3 = rcdev->priv;
  594. struct device *dev = rr3->dev;
  595. dev_dbg(dev, "Setting modulation frequency to %u", carrier);
  596. if (carrier == 0)
  597. return -EINVAL;
  598. rr3->carrier = carrier;
  599. return carrier;
  600. }
  601. static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
  602. unsigned count)
  603. {
  604. struct redrat3_dev *rr3 = rcdev->priv;
  605. struct device *dev = rr3->dev;
  606. struct redrat3_irdata *irdata = NULL;
  607. int ret, ret_len;
  608. int lencheck, cur_sample_len, pipe;
  609. int *sample_lens = NULL;
  610. u8 curlencheck = 0;
  611. unsigned i, sendbuf_len;
  612. if (rr3->transmitting) {
  613. dev_warn(dev, "%s: transmitter already in use\n", __func__);
  614. return -EAGAIN;
  615. }
  616. if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
  617. return -EINVAL;
  618. /* rr3 will disable rc detector on transmit */
  619. rr3->transmitting = true;
  620. sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
  621. if (!sample_lens) {
  622. ret = -ENOMEM;
  623. goto out;
  624. }
  625. irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
  626. if (!irdata) {
  627. ret = -ENOMEM;
  628. goto out;
  629. }
  630. for (i = 0; i < count; i++) {
  631. cur_sample_len = redrat3_us_to_len(txbuf[i]);
  632. if (cur_sample_len > 0xffff) {
  633. dev_warn(dev, "transmit period of %uus truncated to %uus\n",
  634. txbuf[i], redrat3_len_to_us(0xffff));
  635. cur_sample_len = 0xffff;
  636. }
  637. for (lencheck = 0; lencheck < curlencheck; lencheck++) {
  638. if (sample_lens[lencheck] == cur_sample_len)
  639. break;
  640. }
  641. if (lencheck == curlencheck) {
  642. dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
  643. i, txbuf[i], curlencheck, cur_sample_len);
  644. if (curlencheck < RR3_DRIVER_MAXLENS) {
  645. /* now convert the value to a proper
  646. * rr3 value.. */
  647. sample_lens[curlencheck] = cur_sample_len;
  648. put_unaligned_be16(cur_sample_len,
  649. &irdata->lens[curlencheck]);
  650. curlencheck++;
  651. } else {
  652. ret = -EINVAL;
  653. goto out;
  654. }
  655. }
  656. irdata->sigdata[i] = lencheck;
  657. }
  658. irdata->sigdata[count] = RR3_END_OF_SIGNAL;
  659. irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
  660. sendbuf_len = offsetof(struct redrat3_irdata,
  661. sigdata[count + RR3_TX_TRAILER_LEN]);
  662. /* fill in our packet header */
  663. irdata->header.length = cpu_to_be16(sendbuf_len -
  664. sizeof(struct redrat3_header));
  665. irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
  666. irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
  667. irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
  668. irdata->no_lengths = curlencheck;
  669. irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
  670. pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
  671. ret = usb_bulk_msg(rr3->udev, pipe, irdata,
  672. sendbuf_len, &ret_len, 10 * HZ);
  673. dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
  674. /* now tell the hardware to transmit what we sent it */
  675. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  676. ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
  677. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  678. 0, 0, irdata, 2, HZ * 10);
  679. if (ret < 0)
  680. dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
  681. else
  682. ret = count;
  683. out:
  684. kfree(sample_lens);
  685. kfree(irdata);
  686. rr3->transmitting = false;
  687. /* rr3 re-enables rc detector because it was enabled before */
  688. return ret;
  689. }
  690. static void redrat3_brightness_set(struct led_classdev *led_dev, enum
  691. led_brightness brightness)
  692. {
  693. struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
  694. led);
  695. if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
  696. int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
  697. if (ret != 0) {
  698. dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
  699. __func__, ret);
  700. atomic_set(&rr3->flash, 0);
  701. }
  702. }
  703. }
  704. static void redrat3_led_complete(struct urb *urb)
  705. {
  706. struct redrat3_dev *rr3 = urb->context;
  707. switch (urb->status) {
  708. case 0:
  709. break;
  710. case -ECONNRESET:
  711. case -ENOENT:
  712. case -ESHUTDOWN:
  713. usb_unlink_urb(urb);
  714. return;
  715. case -EPIPE:
  716. default:
  717. dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
  718. break;
  719. }
  720. rr3->led.brightness = LED_OFF;
  721. atomic_dec(&rr3->flash);
  722. }
  723. static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
  724. {
  725. struct device *dev = rr3->dev;
  726. struct rc_dev *rc;
  727. int ret = -ENODEV;
  728. u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
  729. rc = rc_allocate_device();
  730. if (!rc) {
  731. dev_err(dev, "remote input dev allocation failed\n");
  732. goto out;
  733. }
  734. snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
  735. "Infrared Remote Transceiver (%04x:%04x)",
  736. prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
  737. le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
  738. usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
  739. rc->input_name = rr3->name;
  740. rc->input_phys = rr3->phys;
  741. usb_to_input_id(rr3->udev, &rc->input_id);
  742. rc->dev.parent = dev;
  743. rc->priv = rr3;
  744. rc->driver_type = RC_DRIVER_IR_RAW;
  745. rc->allowed_protocols = RC_BIT_ALL;
  746. rc->timeout = US_TO_NS(2750);
  747. rc->tx_ir = redrat3_transmit_ir;
  748. rc->s_tx_carrier = redrat3_set_tx_carrier;
  749. rc->driver_name = DRIVER_NAME;
  750. rc->rx_resolution = US_TO_NS(2);
  751. rc->map_name = RC_MAP_HAUPPAUGE;
  752. ret = rc_register_device(rc);
  753. if (ret < 0) {
  754. dev_err(dev, "remote dev registration failed\n");
  755. goto out;
  756. }
  757. return rc;
  758. out:
  759. rc_free_device(rc);
  760. return NULL;
  761. }
  762. static int redrat3_dev_probe(struct usb_interface *intf,
  763. const struct usb_device_id *id)
  764. {
  765. struct usb_device *udev = interface_to_usbdev(intf);
  766. struct device *dev = &intf->dev;
  767. struct usb_host_interface *uhi;
  768. struct redrat3_dev *rr3;
  769. struct usb_endpoint_descriptor *ep;
  770. struct usb_endpoint_descriptor *ep_in = NULL;
  771. struct usb_endpoint_descriptor *ep_out = NULL;
  772. u8 addr, attrs;
  773. int pipe, i;
  774. int retval = -ENOMEM;
  775. uhi = intf->cur_altsetting;
  776. /* find our bulk-in and bulk-out endpoints */
  777. for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
  778. ep = &uhi->endpoint[i].desc;
  779. addr = ep->bEndpointAddress;
  780. attrs = ep->bmAttributes;
  781. if ((ep_in == NULL) &&
  782. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
  783. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  784. USB_ENDPOINT_XFER_BULK)) {
  785. dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
  786. ep->bEndpointAddress);
  787. /* data comes in on 0x82, 0x81 is for other data... */
  788. if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
  789. ep_in = ep;
  790. }
  791. if ((ep_out == NULL) &&
  792. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
  793. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  794. USB_ENDPOINT_XFER_BULK)) {
  795. dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
  796. ep->bEndpointAddress);
  797. ep_out = ep;
  798. }
  799. }
  800. if (!ep_in || !ep_out) {
  801. dev_err(dev, "Couldn't find both in and out endpoints\n");
  802. retval = -ENODEV;
  803. goto no_endpoints;
  804. }
  805. /* allocate memory for our device state and initialize it */
  806. rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
  807. if (rr3 == NULL) {
  808. dev_err(dev, "Memory allocation failure\n");
  809. goto no_endpoints;
  810. }
  811. rr3->dev = &intf->dev;
  812. /* set up bulk-in endpoint */
  813. rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  814. if (!rr3->read_urb) {
  815. dev_err(dev, "Read urb allocation failure\n");
  816. goto error;
  817. }
  818. rr3->ep_in = ep_in;
  819. rr3->bulk_in_buf = usb_alloc_coherent(udev,
  820. le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in);
  821. if (!rr3->bulk_in_buf) {
  822. dev_err(dev, "Read buffer allocation failure\n");
  823. goto error;
  824. }
  825. pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
  826. usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
  827. le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
  828. rr3->read_urb->transfer_dma = rr3->dma_in;
  829. rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  830. rr3->ep_out = ep_out;
  831. rr3->udev = udev;
  832. redrat3_reset(rr3);
  833. redrat3_get_firmware_rev(rr3);
  834. /* might be all we need to do? */
  835. retval = redrat3_enable_detector(rr3);
  836. if (retval < 0)
  837. goto error;
  838. /* store current hardware timeout, in us, will use for kfifo resets */
  839. rr3->hw_timeout = redrat3_get_timeout(rr3);
  840. /* default.. will get overridden by any sends with a freq defined */
  841. rr3->carrier = 38000;
  842. /* led control */
  843. rr3->led.name = "redrat3:red:feedback";
  844. rr3->led.default_trigger = "rc-feedback";
  845. rr3->led.brightness_set = redrat3_brightness_set;
  846. retval = led_classdev_register(&intf->dev, &rr3->led);
  847. if (retval)
  848. goto error;
  849. atomic_set(&rr3->flash, 0);
  850. rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
  851. if (!rr3->flash_urb) {
  852. retval = -ENOMEM;
  853. goto led_free_error;
  854. }
  855. /* setup packet is 'c0 b9 0000 0000 0001' */
  856. rr3->flash_control.bRequestType = 0xc0;
  857. rr3->flash_control.bRequest = RR3_BLINK_LED;
  858. rr3->flash_control.wLength = cpu_to_le16(1);
  859. usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
  860. (unsigned char *)&rr3->flash_control,
  861. &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
  862. redrat3_led_complete, rr3);
  863. rr3->rc = redrat3_init_rc_dev(rr3);
  864. if (!rr3->rc) {
  865. retval = -ENOMEM;
  866. goto led_free_error;
  867. }
  868. setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
  869. /* we can register the device now, as it is ready */
  870. usb_set_intfdata(intf, rr3);
  871. return 0;
  872. led_free_error:
  873. led_classdev_unregister(&rr3->led);
  874. error:
  875. redrat3_delete(rr3, rr3->udev);
  876. no_endpoints:
  877. dev_err(dev, "%s: retval = %x", __func__, retval);
  878. return retval;
  879. }
  880. static void redrat3_dev_disconnect(struct usb_interface *intf)
  881. {
  882. struct usb_device *udev = interface_to_usbdev(intf);
  883. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  884. if (!rr3)
  885. return;
  886. usb_set_intfdata(intf, NULL);
  887. rc_unregister_device(rr3->rc);
  888. led_classdev_unregister(&rr3->led);
  889. del_timer_sync(&rr3->rx_timeout);
  890. redrat3_delete(rr3, udev);
  891. }
  892. static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
  893. {
  894. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  895. led_classdev_suspend(&rr3->led);
  896. usb_kill_urb(rr3->read_urb);
  897. usb_kill_urb(rr3->flash_urb);
  898. return 0;
  899. }
  900. static int redrat3_dev_resume(struct usb_interface *intf)
  901. {
  902. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  903. if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
  904. return -EIO;
  905. led_classdev_resume(&rr3->led);
  906. return 0;
  907. }
  908. static struct usb_driver redrat3_dev_driver = {
  909. .name = DRIVER_NAME,
  910. .probe = redrat3_dev_probe,
  911. .disconnect = redrat3_dev_disconnect,
  912. .suspend = redrat3_dev_suspend,
  913. .resume = redrat3_dev_resume,
  914. .reset_resume = redrat3_dev_resume,
  915. .id_table = redrat3_dev_table
  916. };
  917. module_usb_driver(redrat3_dev_driver);
  918. MODULE_DESCRIPTION(DRIVER_DESC);
  919. MODULE_AUTHOR(DRIVER_AUTHOR);
  920. MODULE_AUTHOR(DRIVER_AUTHOR2);
  921. MODULE_LICENSE("GPL");
  922. MODULE_DEVICE_TABLE(usb, redrat3_dev_table);