redrat3.c 29 KB

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