redrat3.c 27 KB

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