redrat3.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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 <linux/device.h>
  48. #include <linux/module.h>
  49. #include <linux/slab.h>
  50. #include <linux/usb.h>
  51. #include <linux/usb/input.h>
  52. #include <media/rc-core.h>
  53. /* Driver Information */
  54. #define DRIVER_VERSION "0.70"
  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. /* Raw Modulated signal data value offsets */
  122. #define RR3_PAUSE_OFFSET 0
  123. #define RR3_FREQ_COUNT_OFFSET 4
  124. #define RR3_NUM_PERIOD_OFFSET 6
  125. #define RR3_MAX_LENGTHS_OFFSET 8
  126. #define RR3_NUM_LENGTHS_OFFSET 9
  127. #define RR3_MAX_SIGS_OFFSET 10
  128. #define RR3_NUM_SIGS_OFFSET 12
  129. #define RR3_REPEATS_OFFSET 14
  130. /* Size of the fixed-length portion of the signal */
  131. #define RR3_HEADER_LENGTH 15
  132. #define RR3_DRIVER_MAXLENS 128
  133. #define RR3_MAX_SIG_SIZE 512
  134. #define RR3_MAX_BUF_SIZE \
  135. ((2 * RR3_HEADER_LENGTH) + RR3_DRIVER_MAXLENS + RR3_MAX_SIG_SIZE)
  136. #define RR3_TIME_UNIT 50
  137. #define RR3_END_OF_SIGNAL 0x7f
  138. #define RR3_TX_HEADER_OFFSET 4
  139. #define RR3_TX_TRAILER_LEN 2
  140. #define RR3_RX_MIN_TIMEOUT 5
  141. #define RR3_RX_MAX_TIMEOUT 2000
  142. /* The 8051's CPUCS Register address */
  143. #define RR3_CPUCS_REG_ADDR 0x7f92
  144. #define USB_RR3USB_VENDOR_ID 0x112a
  145. #define USB_RR3USB_PRODUCT_ID 0x0001
  146. #define USB_RR3IIUSB_PRODUCT_ID 0x0005
  147. /* table of devices that work with this driver */
  148. static struct usb_device_id redrat3_dev_table[] = {
  149. /* Original version of the RedRat3 */
  150. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
  151. /* Second Version/release of the RedRat3 - RetRat3-II */
  152. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
  153. {} /* Terminating entry */
  154. };
  155. /* Structure to hold all of our device specific stuff */
  156. struct redrat3_dev {
  157. /* core device bits */
  158. struct rc_dev *rc;
  159. struct device *dev;
  160. /* save off the usb device pointer */
  161. struct usb_device *udev;
  162. /* the receive endpoint */
  163. struct usb_endpoint_descriptor *ep_in;
  164. /* the buffer to receive data */
  165. unsigned char *bulk_in_buf;
  166. /* urb used to read ir data */
  167. struct urb *read_urb;
  168. /* the send endpoint */
  169. struct usb_endpoint_descriptor *ep_out;
  170. /* the buffer to send data */
  171. unsigned char *bulk_out_buf;
  172. /* the urb used to send data */
  173. struct urb *write_urb;
  174. /* usb dma */
  175. dma_addr_t dma_in;
  176. dma_addr_t dma_out;
  177. /* locks this structure */
  178. struct mutex lock;
  179. /* rx signal timeout timer */
  180. struct timer_list rx_timeout;
  181. u32 hw_timeout;
  182. /* is the detector enabled*/
  183. bool det_enabled;
  184. /* Is the device currently transmitting?*/
  185. bool transmitting;
  186. /* store for current packet */
  187. char pbuf[RR3_MAX_BUF_SIZE];
  188. u16 pktlen;
  189. u16 pkttype;
  190. u16 bytes_read;
  191. /* indicate whether we are going to reprocess
  192. * the USB callback with a bigger buffer */
  193. int buftoosmall;
  194. char *datap;
  195. u32 carrier;
  196. char name[128];
  197. char phys[64];
  198. };
  199. /* All incoming data buffers adhere to a very specific data format */
  200. struct redrat3_signal_header {
  201. u16 length; /* Length of data being transferred */
  202. u16 transfer_type; /* Type of data transferred */
  203. u32 pause; /* Pause between main and repeat signals */
  204. u16 mod_freq_count; /* Value of timer on mod. freq. measurement */
  205. u16 no_periods; /* No. of periods over which mod. freq. is measured */
  206. u8 max_lengths; /* Max no. of lengths (i.e. size of array) */
  207. u8 no_lengths; /* Actual no. of elements in lengths array */
  208. u16 max_sig_size; /* Max no. of values in signal data array */
  209. u16 sig_size; /* Acuto no. of values in signal data array */
  210. u8 no_repeats; /* No. of repeats of repeat signal section */
  211. /* Here forward is the lengths and signal data */
  212. };
  213. static void redrat3_dump_signal_header(struct redrat3_signal_header *header)
  214. {
  215. pr_info("%s:\n", __func__);
  216. pr_info(" * length: %u, transfer_type: 0x%02x\n",
  217. header->length, header->transfer_type);
  218. pr_info(" * pause: %u, freq_count: %u, no_periods: %u\n",
  219. header->pause, header->mod_freq_count, header->no_periods);
  220. pr_info(" * lengths: %u (max: %u)\n",
  221. header->no_lengths, header->max_lengths);
  222. pr_info(" * sig_size: %u (max: %u)\n",
  223. header->sig_size, header->max_sig_size);
  224. pr_info(" * repeats: %u\n", header->no_repeats);
  225. }
  226. static void redrat3_dump_signal_data(char *buffer, u16 len)
  227. {
  228. int offset, i;
  229. char *data_vals;
  230. pr_info("%s:", __func__);
  231. offset = RR3_TX_HEADER_OFFSET + RR3_HEADER_LENGTH
  232. + (RR3_DRIVER_MAXLENS * sizeof(u16));
  233. /* read RR3_DRIVER_MAXLENS from ctrl msg */
  234. data_vals = buffer + offset;
  235. for (i = 0; i < len; i++) {
  236. if (i % 10 == 0)
  237. pr_cont("\n * ");
  238. pr_cont("%02x ", *data_vals++);
  239. }
  240. pr_cont("\n");
  241. }
  242. /*
  243. * redrat3_issue_async
  244. *
  245. * Issues an async read to the ir data in port..
  246. * sets the callback to be redrat3_handle_async
  247. */
  248. static void redrat3_issue_async(struct redrat3_dev *rr3)
  249. {
  250. int res;
  251. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  252. memset(rr3->bulk_in_buf, 0, rr3->ep_in->wMaxPacketSize);
  253. res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
  254. if (res)
  255. rr3_dbg(rr3->dev, "%s: receive request FAILED! "
  256. "(res %d, len %d)\n", __func__, res,
  257. rr3->read_urb->transfer_buffer_length);
  258. }
  259. static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
  260. {
  261. if (!rr3->transmitting && (code != 0x40))
  262. dev_info(rr3->dev, "fw error code 0x%02x: ", code);
  263. switch (code) {
  264. case 0x00:
  265. pr_cont("No Error\n");
  266. break;
  267. /* Codes 0x20 through 0x2f are IR Firmware Errors */
  268. case 0x20:
  269. pr_cont("Initial signal pulse not long enough "
  270. "to measure carrier frequency\n");
  271. break;
  272. case 0x21:
  273. pr_cont("Not enough length values allocated for signal\n");
  274. break;
  275. case 0x22:
  276. pr_cont("Not enough memory allocated for signal data\n");
  277. break;
  278. case 0x23:
  279. pr_cont("Too many signal repeats\n");
  280. break;
  281. case 0x28:
  282. pr_cont("Insufficient memory available for IR signal "
  283. "data memory allocation\n");
  284. break;
  285. case 0x29:
  286. pr_cont("Insufficient memory available "
  287. "for IrDa signal data memory allocation\n");
  288. break;
  289. /* Codes 0x30 through 0x3f are USB Firmware Errors */
  290. case 0x30:
  291. pr_cont("Insufficient memory available for bulk "
  292. "transfer structure\n");
  293. break;
  294. /*
  295. * Other error codes... These are primarily errors that can occur in
  296. * the control messages sent to the redrat
  297. */
  298. case 0x40:
  299. if (!rr3->transmitting)
  300. pr_cont("Signal capture has been terminated\n");
  301. break;
  302. case 0x41:
  303. pr_cont("Attempt to set/get and unknown signal I/O "
  304. "algorithm parameter\n");
  305. break;
  306. case 0x42:
  307. pr_cont("Signal capture already started\n");
  308. break;
  309. default:
  310. pr_cont("Unknown Error\n");
  311. break;
  312. }
  313. }
  314. static u32 redrat3_val_to_mod_freq(struct redrat3_signal_header *ph)
  315. {
  316. u32 mod_freq = 0;
  317. if (ph->mod_freq_count != 0)
  318. mod_freq = (RR3_CLK * ph->no_periods) /
  319. (ph->mod_freq_count * RR3_CLK_PER_COUNT);
  320. return mod_freq;
  321. }
  322. /* this function scales down the figures for the same result... */
  323. static u32 redrat3_len_to_us(u32 length)
  324. {
  325. u32 biglen = length * 1000;
  326. u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
  327. u32 result = (u32) (biglen / divisor);
  328. /* don't allow zero lengths to go back, breaks lirc */
  329. return result ? result : 1;
  330. }
  331. /*
  332. * convert us back into redrat3 lengths
  333. *
  334. * length * 1000 length * 1000000
  335. * ------------- = ---------------- = micro
  336. * rr3clk / 1000 rr3clk
  337. * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
  338. * ----- = 4 ----- = 6 -------------- = len ---------------------
  339. * 3 2 1000000 1000
  340. */
  341. static u32 redrat3_us_to_len(u32 microsec)
  342. {
  343. u32 result;
  344. u32 divisor;
  345. microsec &= IR_MAX_DURATION;
  346. divisor = (RR3_CLK_CONV_FACTOR / 1000);
  347. result = (u32)(microsec * divisor) / 1000;
  348. /* don't allow zero lengths to go back, breaks lirc */
  349. return result ? result : 1;
  350. }
  351. /* timer callback to send reset event */
  352. static void redrat3_rx_timeout(unsigned long data)
  353. {
  354. struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
  355. rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n");
  356. ir_raw_event_reset(rr3->rc);
  357. }
  358. static void redrat3_process_ir_data(struct redrat3_dev *rr3)
  359. {
  360. DEFINE_IR_RAW_EVENT(rawir);
  361. struct redrat3_signal_header header;
  362. struct device *dev;
  363. int i, trailer = 0;
  364. unsigned long delay;
  365. u32 mod_freq, single_len;
  366. u16 *len_vals;
  367. u8 *data_vals;
  368. u32 tmp32;
  369. u16 tmp16;
  370. char *sig_data;
  371. if (!rr3) {
  372. pr_err("%s called with no context!\n", __func__);
  373. return;
  374. }
  375. rr3_ftr(rr3->dev, "Entered %s\n", __func__);
  376. dev = rr3->dev;
  377. sig_data = rr3->pbuf;
  378. header.length = rr3->pktlen;
  379. header.transfer_type = rr3->pkttype;
  380. /* Sanity check */
  381. if (!(header.length >= RR3_HEADER_LENGTH))
  382. dev_warn(dev, "read returned less than rr3 header len\n");
  383. /* Make sure we reset the IR kfifo after a bit of inactivity */
  384. delay = usecs_to_jiffies(rr3->hw_timeout);
  385. mod_timer(&rr3->rx_timeout, jiffies + delay);
  386. memcpy(&tmp32, sig_data + RR3_PAUSE_OFFSET, sizeof(tmp32));
  387. header.pause = be32_to_cpu(tmp32);
  388. memcpy(&tmp16, sig_data + RR3_FREQ_COUNT_OFFSET, sizeof(tmp16));
  389. header.mod_freq_count = be16_to_cpu(tmp16);
  390. memcpy(&tmp16, sig_data + RR3_NUM_PERIOD_OFFSET, sizeof(tmp16));
  391. header.no_periods = be16_to_cpu(tmp16);
  392. header.max_lengths = sig_data[RR3_MAX_LENGTHS_OFFSET];
  393. header.no_lengths = sig_data[RR3_NUM_LENGTHS_OFFSET];
  394. memcpy(&tmp16, sig_data + RR3_MAX_SIGS_OFFSET, sizeof(tmp16));
  395. header.max_sig_size = be16_to_cpu(tmp16);
  396. memcpy(&tmp16, sig_data + RR3_NUM_SIGS_OFFSET, sizeof(tmp16));
  397. header.sig_size = be16_to_cpu(tmp16);
  398. header.no_repeats= sig_data[RR3_REPEATS_OFFSET];
  399. if (debug) {
  400. redrat3_dump_signal_header(&header);
  401. redrat3_dump_signal_data(sig_data, header.sig_size);
  402. }
  403. mod_freq = redrat3_val_to_mod_freq(&header);
  404. rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq);
  405. /* Here we pull out the 'length' values from the signal */
  406. len_vals = (u16 *)(sig_data + RR3_HEADER_LENGTH);
  407. data_vals = sig_data + RR3_HEADER_LENGTH +
  408. (header.max_lengths * sizeof(u16));
  409. /* process each rr3 encoded byte into an int */
  410. for (i = 0; i < header.sig_size; i++) {
  411. u16 val = len_vals[data_vals[i]];
  412. single_len = redrat3_len_to_us((u32)be16_to_cpu(val));
  413. /* we should always get pulse/space/pulse/space samples */
  414. if (i % 2)
  415. rawir.pulse = false;
  416. else
  417. rawir.pulse = true;
  418. rawir.duration = US_TO_NS(single_len);
  419. /* Save initial pulse length to fudge trailer */
  420. if (i == 0)
  421. trailer = rawir.duration;
  422. /* cap the value to IR_MAX_DURATION */
  423. rawir.duration &= IR_MAX_DURATION;
  424. rr3_dbg(dev, "storing %s with duration %d (i: %d)\n",
  425. rawir.pulse ? "pulse" : "space", rawir.duration, i);
  426. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  427. }
  428. /* add a trailing space, if need be */
  429. if (i % 2) {
  430. rawir.pulse = false;
  431. /* this duration is made up, and may not be ideal... */
  432. if (trailer < US_TO_NS(1000))
  433. rawir.duration = US_TO_NS(2800);
  434. else
  435. rawir.duration = trailer;
  436. rr3_dbg(dev, "storing trailing space with duration %d\n",
  437. rawir.duration);
  438. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  439. }
  440. rr3_dbg(dev, "calling ir_raw_event_handle\n");
  441. ir_raw_event_handle(rr3->rc);
  442. return;
  443. }
  444. /* Util fn to send rr3 cmds */
  445. static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
  446. {
  447. struct usb_device *udev;
  448. u8 *data;
  449. int res;
  450. data = kzalloc(sizeof(u8), GFP_KERNEL);
  451. if (!data)
  452. return -ENOMEM;
  453. udev = rr3->udev;
  454. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
  455. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  456. 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
  457. if (res < 0) {
  458. dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
  459. __func__, res, *data);
  460. res = -EIO;
  461. } else
  462. res = (u8)data[0];
  463. kfree(data);
  464. return res;
  465. }
  466. /* Enables the long range detector and starts async receive */
  467. static int redrat3_enable_detector(struct redrat3_dev *rr3)
  468. {
  469. struct device *dev = rr3->dev;
  470. u8 ret;
  471. rr3_ftr(dev, "Entering %s\n", __func__);
  472. ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
  473. if (ret != 0)
  474. dev_dbg(dev, "%s: unexpected ret of %d\n",
  475. __func__, ret);
  476. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  477. if (ret != 1) {
  478. dev_err(dev, "%s: detector status: %d, should be 1\n",
  479. __func__, ret);
  480. return -EIO;
  481. }
  482. rr3->det_enabled = true;
  483. redrat3_issue_async(rr3);
  484. return 0;
  485. }
  486. /* Disables the rr3 long range detector */
  487. static void redrat3_disable_detector(struct redrat3_dev *rr3)
  488. {
  489. struct device *dev = rr3->dev;
  490. u8 ret;
  491. rr3_ftr(dev, "Entering %s\n", __func__);
  492. ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3);
  493. if (ret != 0)
  494. dev_err(dev, "%s: failure!\n", __func__);
  495. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  496. if (ret != 0)
  497. dev_warn(dev, "%s: detector status: %d, should be 0\n",
  498. __func__, ret);
  499. rr3->det_enabled = false;
  500. }
  501. static inline void redrat3_delete(struct redrat3_dev *rr3,
  502. struct usb_device *udev)
  503. {
  504. rr3_ftr(rr3->dev, "%s cleaning up\n", __func__);
  505. usb_kill_urb(rr3->read_urb);
  506. usb_kill_urb(rr3->write_urb);
  507. usb_free_urb(rr3->read_urb);
  508. usb_free_urb(rr3->write_urb);
  509. usb_free_coherent(udev, rr3->ep_in->wMaxPacketSize,
  510. rr3->bulk_in_buf, rr3->dma_in);
  511. usb_free_coherent(udev, rr3->ep_out->wMaxPacketSize,
  512. rr3->bulk_out_buf, rr3->dma_out);
  513. kfree(rr3);
  514. }
  515. static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
  516. {
  517. u32 *tmp;
  518. u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
  519. int len, ret, pipe;
  520. len = sizeof(*tmp);
  521. tmp = kzalloc(len, GFP_KERNEL);
  522. if (!tmp) {
  523. dev_warn(rr3->dev, "Memory allocation faillure\n");
  524. return timeout;
  525. }
  526. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  527. ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
  528. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  529. RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
  530. if (ret != len) {
  531. dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
  532. return timeout;
  533. }
  534. timeout = redrat3_len_to_us(be32_to_cpu(*tmp));
  535. rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
  536. return timeout;
  537. }
  538. static void redrat3_reset(struct redrat3_dev *rr3)
  539. {
  540. struct usb_device *udev = rr3->udev;
  541. struct device *dev = rr3->dev;
  542. int rc, rxpipe, txpipe;
  543. u8 *val;
  544. int len = sizeof(u8);
  545. rr3_ftr(dev, "Entering %s\n", __func__);
  546. rxpipe = usb_rcvctrlpipe(udev, 0);
  547. txpipe = usb_sndctrlpipe(udev, 0);
  548. val = kzalloc(len, GFP_KERNEL);
  549. if (!val) {
  550. dev_err(dev, "Memory allocation failure\n");
  551. return;
  552. }
  553. *val = 0x01;
  554. rc = usb_control_msg(udev, rxpipe, RR3_RESET,
  555. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  556. RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
  557. rr3_dbg(dev, "reset returned 0x%02x\n", rc);
  558. *val = 5;
  559. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  560. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  561. RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
  562. rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
  563. *val = RR3_DRIVER_MAXLENS;
  564. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  565. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  566. RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
  567. rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
  568. kfree(val);
  569. }
  570. static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
  571. {
  572. int rc = 0;
  573. char *buffer;
  574. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  575. buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
  576. if (!buffer) {
  577. dev_err(rr3->dev, "Memory allocation failure\n");
  578. return;
  579. }
  580. rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
  581. RR3_FW_VERSION,
  582. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  583. 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
  584. if (rc >= 0)
  585. dev_info(rr3->dev, "Firmware rev: %s", buffer);
  586. else
  587. dev_err(rr3->dev, "Problem fetching firmware ID\n");
  588. kfree(buffer);
  589. rr3_ftr(rr3->dev, "Exiting %s\n", __func__);
  590. }
  591. static void redrat3_read_packet_start(struct redrat3_dev *rr3, int len)
  592. {
  593. u16 tx_error;
  594. u16 hdrlen;
  595. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  596. /* grab the Length and type of transfer */
  597. memcpy(&(rr3->pktlen), (unsigned char *) rr3->bulk_in_buf,
  598. sizeof(rr3->pktlen));
  599. memcpy(&(rr3->pkttype), ((unsigned char *) rr3->bulk_in_buf +
  600. sizeof(rr3->pktlen)),
  601. sizeof(rr3->pkttype));
  602. /*data needs conversion to know what its real values are*/
  603. rr3->pktlen = be16_to_cpu(rr3->pktlen);
  604. rr3->pkttype = be16_to_cpu(rr3->pkttype);
  605. switch (rr3->pkttype) {
  606. case RR3_ERROR:
  607. memcpy(&tx_error, ((unsigned char *)rr3->bulk_in_buf
  608. + (sizeof(rr3->pktlen) + sizeof(rr3->pkttype))),
  609. sizeof(tx_error));
  610. tx_error = be16_to_cpu(tx_error);
  611. redrat3_dump_fw_error(rr3, tx_error);
  612. break;
  613. case RR3_MOD_SIGNAL_IN:
  614. hdrlen = sizeof(rr3->pktlen) + sizeof(rr3->pkttype);
  615. rr3->bytes_read = len;
  616. rr3->bytes_read -= hdrlen;
  617. rr3->datap = &(rr3->pbuf[0]);
  618. memcpy(rr3->datap, ((unsigned char *)rr3->bulk_in_buf + hdrlen),
  619. rr3->bytes_read);
  620. rr3->datap += rr3->bytes_read;
  621. rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
  622. rr3->bytes_read, rr3->pktlen);
  623. break;
  624. default:
  625. rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, "
  626. "len of %d, 0x%02x\n", rr3->pkttype, len, rr3->pktlen);
  627. break;
  628. }
  629. }
  630. static void redrat3_read_packet_continue(struct redrat3_dev *rr3, int len)
  631. {
  632. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  633. memcpy(rr3->datap, (unsigned char *)rr3->bulk_in_buf, len);
  634. rr3->datap += len;
  635. rr3->bytes_read += len;
  636. rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
  637. rr3->bytes_read, rr3->pktlen);
  638. }
  639. /* gather IR data from incoming urb, process it when we have enough */
  640. static int redrat3_get_ir_data(struct redrat3_dev *rr3, int len)
  641. {
  642. struct device *dev = rr3->dev;
  643. int ret = 0;
  644. rr3_ftr(dev, "Entering %s\n", __func__);
  645. if (rr3->pktlen > RR3_MAX_BUF_SIZE) {
  646. dev_err(rr3->dev, "error: packet larger than buffer\n");
  647. ret = -EINVAL;
  648. goto out;
  649. }
  650. if ((rr3->bytes_read == 0) &&
  651. (len >= (sizeof(rr3->pkttype) + sizeof(rr3->pktlen)))) {
  652. redrat3_read_packet_start(rr3, len);
  653. } else if (rr3->bytes_read != 0) {
  654. redrat3_read_packet_continue(rr3, len);
  655. } else if (rr3->bytes_read == 0) {
  656. dev_err(dev, "error: no packet data read\n");
  657. ret = -ENODATA;
  658. goto out;
  659. }
  660. if (rr3->bytes_read > rr3->pktlen) {
  661. dev_err(dev, "bytes_read (%d) greater than pktlen (%d)\n",
  662. rr3->bytes_read, rr3->pktlen);
  663. ret = -EINVAL;
  664. goto out;
  665. } else if (rr3->bytes_read < rr3->pktlen)
  666. /* we're still accumulating data */
  667. return 0;
  668. /* if we get here, we've got IR data to decode */
  669. if (rr3->pkttype == RR3_MOD_SIGNAL_IN)
  670. redrat3_process_ir_data(rr3);
  671. else
  672. rr3_dbg(dev, "discarding non-signal data packet "
  673. "(type 0x%02x)\n", rr3->pkttype);
  674. out:
  675. rr3->bytes_read = 0;
  676. rr3->pktlen = 0;
  677. rr3->pkttype = 0;
  678. return ret;
  679. }
  680. /* callback function from USB when async USB request has completed */
  681. static void redrat3_handle_async(struct urb *urb, struct pt_regs *regs)
  682. {
  683. struct redrat3_dev *rr3;
  684. int ret;
  685. if (!urb)
  686. return;
  687. rr3 = urb->context;
  688. if (!rr3) {
  689. pr_err("%s called with invalid context!\n", __func__);
  690. usb_unlink_urb(urb);
  691. return;
  692. }
  693. rr3_ftr(rr3->dev, "Entering %s\n", __func__);
  694. switch (urb->status) {
  695. case 0:
  696. ret = redrat3_get_ir_data(rr3, urb->actual_length);
  697. if (!ret) {
  698. /* no error, prepare to read more */
  699. redrat3_issue_async(rr3);
  700. }
  701. break;
  702. case -ECONNRESET:
  703. case -ENOENT:
  704. case -ESHUTDOWN:
  705. usb_unlink_urb(urb);
  706. return;
  707. case -EPIPE:
  708. default:
  709. dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
  710. rr3->bytes_read = 0;
  711. rr3->pktlen = 0;
  712. rr3->pkttype = 0;
  713. break;
  714. }
  715. }
  716. static void redrat3_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
  717. {
  718. struct redrat3_dev *rr3;
  719. int len;
  720. if (!urb)
  721. return;
  722. rr3 = urb->context;
  723. if (rr3) {
  724. len = urb->actual_length;
  725. rr3_ftr(rr3->dev, "%s: called (status=%d len=%d)\n",
  726. __func__, urb->status, len);
  727. }
  728. }
  729. static u16 mod_freq_to_val(unsigned int mod_freq)
  730. {
  731. int mult = 6000000;
  732. /* Clk used in mod. freq. generation is CLK24/4. */
  733. return (u16)(65536 - (mult / mod_freq));
  734. }
  735. static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  736. {
  737. struct redrat3_dev *rr3 = rcdev->priv;
  738. struct device *dev = rr3->dev;
  739. rr3_dbg(dev, "Setting modulation frequency to %u", carrier);
  740. rr3->carrier = carrier;
  741. return carrier;
  742. }
  743. static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
  744. unsigned count)
  745. {
  746. struct redrat3_dev *rr3 = rcdev->priv;
  747. struct device *dev = rr3->dev;
  748. struct redrat3_signal_header header;
  749. int i, j, ret, ret_len, offset;
  750. int lencheck, cur_sample_len, pipe;
  751. char *buffer = NULL, *sigdata = NULL;
  752. int *sample_lens = NULL;
  753. u32 tmpi;
  754. u16 tmps;
  755. u8 *datap;
  756. u8 curlencheck = 0;
  757. u16 *lengths_ptr;
  758. int sendbuf_len;
  759. rr3_ftr(dev, "Entering %s\n", __func__);
  760. if (rr3->transmitting) {
  761. dev_warn(dev, "%s: transmitter already in use\n", __func__);
  762. return -EAGAIN;
  763. }
  764. if (count > (RR3_DRIVER_MAXLENS * 2))
  765. return -EINVAL;
  766. /* rr3 will disable rc detector on transmit */
  767. rr3->det_enabled = false;
  768. rr3->transmitting = true;
  769. sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
  770. if (!sample_lens) {
  771. ret = -ENOMEM;
  772. goto out;
  773. }
  774. for (i = 0; i < count; i++) {
  775. for (lencheck = 0; lencheck < curlencheck; lencheck++) {
  776. cur_sample_len = redrat3_us_to_len(txbuf[i]);
  777. if (sample_lens[lencheck] == cur_sample_len)
  778. break;
  779. }
  780. if (lencheck == curlencheck) {
  781. cur_sample_len = redrat3_us_to_len(txbuf[i]);
  782. rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
  783. i, txbuf[i], curlencheck, cur_sample_len);
  784. if (curlencheck < 255) {
  785. /* now convert the value to a proper
  786. * rr3 value.. */
  787. sample_lens[curlencheck] = cur_sample_len;
  788. curlencheck++;
  789. } else {
  790. dev_err(dev, "signal too long\n");
  791. ret = -EINVAL;
  792. goto out;
  793. }
  794. }
  795. }
  796. sigdata = kzalloc((count + RR3_TX_TRAILER_LEN), GFP_KERNEL);
  797. if (!sigdata) {
  798. ret = -ENOMEM;
  799. goto out;
  800. }
  801. sigdata[count] = RR3_END_OF_SIGNAL;
  802. sigdata[count + 1] = RR3_END_OF_SIGNAL;
  803. for (i = 0; i < count; i++) {
  804. for (j = 0; j < curlencheck; j++) {
  805. if (sample_lens[j] == redrat3_us_to_len(txbuf[i]))
  806. sigdata[i] = j;
  807. }
  808. }
  809. offset = RR3_TX_HEADER_OFFSET;
  810. sendbuf_len = RR3_HEADER_LENGTH + (sizeof(u16) * RR3_DRIVER_MAXLENS)
  811. + count + RR3_TX_TRAILER_LEN + offset;
  812. buffer = kzalloc(sendbuf_len, GFP_KERNEL);
  813. if (!buffer) {
  814. ret = -ENOMEM;
  815. goto out;
  816. }
  817. /* fill in our packet header */
  818. header.length = sendbuf_len - offset;
  819. header.transfer_type = RR3_MOD_SIGNAL_OUT;
  820. header.pause = redrat3_len_to_us(100);
  821. header.mod_freq_count = mod_freq_to_val(rr3->carrier);
  822. header.no_periods = 0; /* n/a to transmit */
  823. header.max_lengths = RR3_DRIVER_MAXLENS;
  824. header.no_lengths = curlencheck;
  825. header.max_sig_size = RR3_MAX_SIG_SIZE;
  826. header.sig_size = count + RR3_TX_TRAILER_LEN;
  827. /* we currently rely on repeat handling in the IR encoding source */
  828. header.no_repeats = 0;
  829. tmps = cpu_to_be16(header.length);
  830. memcpy(buffer, &tmps, 2);
  831. tmps = cpu_to_be16(header.transfer_type);
  832. memcpy(buffer + 2, &tmps, 2);
  833. tmpi = cpu_to_be32(header.pause);
  834. memcpy(buffer + offset, &tmpi, sizeof(tmpi));
  835. tmps = cpu_to_be16(header.mod_freq_count);
  836. memcpy(buffer + offset + RR3_FREQ_COUNT_OFFSET, &tmps, 2);
  837. buffer[offset + RR3_NUM_LENGTHS_OFFSET] = header.no_lengths;
  838. tmps = cpu_to_be16(header.sig_size);
  839. memcpy(buffer + offset + RR3_NUM_SIGS_OFFSET, &tmps, 2);
  840. buffer[offset + RR3_REPEATS_OFFSET] = header.no_repeats;
  841. lengths_ptr = (u16 *)(buffer + offset + RR3_HEADER_LENGTH);
  842. for (i = 0; i < curlencheck; ++i)
  843. lengths_ptr[i] = cpu_to_be16(sample_lens[i]);
  844. datap = (u8 *)(buffer + offset + RR3_HEADER_LENGTH +
  845. (sizeof(u16) * RR3_DRIVER_MAXLENS));
  846. memcpy(datap, sigdata, (count + RR3_TX_TRAILER_LEN));
  847. if (debug) {
  848. redrat3_dump_signal_header(&header);
  849. redrat3_dump_signal_data(buffer, header.sig_size);
  850. }
  851. pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
  852. tmps = usb_bulk_msg(rr3->udev, pipe, buffer,
  853. sendbuf_len, &ret_len, 10 * HZ);
  854. rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, tmps);
  855. /* now tell the hardware to transmit what we sent it */
  856. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  857. ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
  858. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  859. 0, 0, buffer, 2, HZ * 10);
  860. if (ret < 0)
  861. dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
  862. else
  863. ret = count;
  864. out:
  865. kfree(sample_lens);
  866. kfree(buffer);
  867. kfree(sigdata);
  868. rr3->transmitting = false;
  869. /* rr3 re-enables rc detector because it was enabled before */
  870. rr3->det_enabled = true;
  871. return ret;
  872. }
  873. static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
  874. {
  875. struct device *dev = rr3->dev;
  876. struct rc_dev *rc;
  877. int ret = -ENODEV;
  878. u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
  879. rc = rc_allocate_device();
  880. if (!rc) {
  881. dev_err(dev, "remote input dev allocation failed\n");
  882. goto out;
  883. }
  884. snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
  885. "Infrared Remote Transceiver (%04x:%04x)",
  886. prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
  887. le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
  888. usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
  889. rc->input_name = rr3->name;
  890. rc->input_phys = rr3->phys;
  891. usb_to_input_id(rr3->udev, &rc->input_id);
  892. rc->dev.parent = dev;
  893. rc->priv = rr3;
  894. rc->driver_type = RC_DRIVER_IR_RAW;
  895. rc->allowed_protos = RC_TYPE_ALL;
  896. rc->timeout = US_TO_NS(2750);
  897. rc->tx_ir = redrat3_transmit_ir;
  898. rc->s_tx_carrier = redrat3_set_tx_carrier;
  899. rc->driver_name = DRIVER_NAME;
  900. rc->map_name = RC_MAP_HAUPPAUGE;
  901. ret = rc_register_device(rc);
  902. if (ret < 0) {
  903. dev_err(dev, "remote dev registration failed\n");
  904. goto out;
  905. }
  906. return rc;
  907. out:
  908. rc_free_device(rc);
  909. return NULL;
  910. }
  911. static int __devinit redrat3_dev_probe(struct usb_interface *intf,
  912. const struct usb_device_id *id)
  913. {
  914. struct usb_device *udev = interface_to_usbdev(intf);
  915. struct device *dev = &intf->dev;
  916. struct usb_host_interface *uhi;
  917. struct redrat3_dev *rr3;
  918. struct usb_endpoint_descriptor *ep;
  919. struct usb_endpoint_descriptor *ep_in = NULL;
  920. struct usb_endpoint_descriptor *ep_out = NULL;
  921. u8 addr, attrs;
  922. int pipe, i;
  923. int retval = -ENOMEM;
  924. rr3_ftr(dev, "%s called\n", __func__);
  925. uhi = intf->cur_altsetting;
  926. /* find our bulk-in and bulk-out endpoints */
  927. for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
  928. ep = &uhi->endpoint[i].desc;
  929. addr = ep->bEndpointAddress;
  930. attrs = ep->bmAttributes;
  931. if ((ep_in == NULL) &&
  932. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
  933. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  934. USB_ENDPOINT_XFER_BULK)) {
  935. rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
  936. ep->bEndpointAddress);
  937. /* data comes in on 0x82, 0x81 is for other data... */
  938. if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
  939. ep_in = ep;
  940. }
  941. if ((ep_out == NULL) &&
  942. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
  943. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  944. USB_ENDPOINT_XFER_BULK)) {
  945. rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
  946. ep->bEndpointAddress);
  947. ep_out = ep;
  948. }
  949. }
  950. if (!ep_in || !ep_out) {
  951. dev_err(dev, "Couldn't find both in and out endpoints\n");
  952. retval = -ENODEV;
  953. goto no_endpoints;
  954. }
  955. /* allocate memory for our device state and initialize it */
  956. rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
  957. if (rr3 == NULL) {
  958. dev_err(dev, "Memory allocation failure\n");
  959. goto no_endpoints;
  960. }
  961. rr3->dev = &intf->dev;
  962. /* set up bulk-in endpoint */
  963. rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
  964. if (!rr3->read_urb) {
  965. dev_err(dev, "Read urb allocation failure\n");
  966. goto error;
  967. }
  968. rr3->ep_in = ep_in;
  969. rr3->bulk_in_buf = usb_alloc_coherent(udev, ep_in->wMaxPacketSize,
  970. GFP_ATOMIC, &rr3->dma_in);
  971. if (!rr3->bulk_in_buf) {
  972. dev_err(dev, "Read buffer allocation failure\n");
  973. goto error;
  974. }
  975. pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
  976. usb_fill_bulk_urb(rr3->read_urb, udev, pipe,
  977. rr3->bulk_in_buf, ep_in->wMaxPacketSize,
  978. (usb_complete_t)redrat3_handle_async, rr3);
  979. /* set up bulk-out endpoint*/
  980. rr3->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  981. if (!rr3->write_urb) {
  982. dev_err(dev, "Write urb allocation failure\n");
  983. goto error;
  984. }
  985. rr3->ep_out = ep_out;
  986. rr3->bulk_out_buf = usb_alloc_coherent(udev, ep_out->wMaxPacketSize,
  987. GFP_ATOMIC, &rr3->dma_out);
  988. if (!rr3->bulk_out_buf) {
  989. dev_err(dev, "Write buffer allocation failure\n");
  990. goto error;
  991. }
  992. pipe = usb_sndbulkpipe(udev, ep_out->bEndpointAddress);
  993. usb_fill_bulk_urb(rr3->write_urb, udev, pipe,
  994. rr3->bulk_out_buf, ep_out->wMaxPacketSize,
  995. (usb_complete_t)redrat3_write_bulk_callback, rr3);
  996. mutex_init(&rr3->lock);
  997. rr3->udev = udev;
  998. redrat3_reset(rr3);
  999. redrat3_get_firmware_rev(rr3);
  1000. /* might be all we need to do? */
  1001. retval = redrat3_enable_detector(rr3);
  1002. if (retval < 0)
  1003. goto error;
  1004. /* store current hardware timeout, in us, will use for kfifo resets */
  1005. rr3->hw_timeout = redrat3_get_timeout(rr3);
  1006. /* default.. will get overridden by any sends with a freq defined */
  1007. rr3->carrier = 38000;
  1008. rr3->rc = redrat3_init_rc_dev(rr3);
  1009. if (!rr3->rc)
  1010. goto error;
  1011. setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
  1012. /* we can register the device now, as it is ready */
  1013. usb_set_intfdata(intf, rr3);
  1014. rr3_ftr(dev, "Exiting %s\n", __func__);
  1015. return 0;
  1016. error:
  1017. redrat3_delete(rr3, rr3->udev);
  1018. no_endpoints:
  1019. dev_err(dev, "%s: retval = %x", __func__, retval);
  1020. return retval;
  1021. }
  1022. static void __devexit redrat3_dev_disconnect(struct usb_interface *intf)
  1023. {
  1024. struct usb_device *udev = interface_to_usbdev(intf);
  1025. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  1026. rr3_ftr(&intf->dev, "Entering %s\n", __func__);
  1027. if (!rr3)
  1028. return;
  1029. redrat3_disable_detector(rr3);
  1030. usb_set_intfdata(intf, NULL);
  1031. rc_unregister_device(rr3->rc);
  1032. del_timer_sync(&rr3->rx_timeout);
  1033. redrat3_delete(rr3, udev);
  1034. rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n");
  1035. }
  1036. static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
  1037. {
  1038. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  1039. rr3_ftr(rr3->dev, "suspend\n");
  1040. usb_kill_urb(rr3->read_urb);
  1041. return 0;
  1042. }
  1043. static int redrat3_dev_resume(struct usb_interface *intf)
  1044. {
  1045. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  1046. rr3_ftr(rr3->dev, "resume\n");
  1047. if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
  1048. return -EIO;
  1049. return 0;
  1050. }
  1051. static struct usb_driver redrat3_dev_driver = {
  1052. .name = DRIVER_NAME,
  1053. .probe = redrat3_dev_probe,
  1054. .disconnect = redrat3_dev_disconnect,
  1055. .suspend = redrat3_dev_suspend,
  1056. .resume = redrat3_dev_resume,
  1057. .reset_resume = redrat3_dev_resume,
  1058. .id_table = redrat3_dev_table
  1059. };
  1060. module_usb_driver(redrat3_dev_driver);
  1061. MODULE_DESCRIPTION(DRIVER_DESC);
  1062. MODULE_AUTHOR(DRIVER_AUTHOR);
  1063. MODULE_AUTHOR(DRIVER_AUTHOR2);
  1064. MODULE_LICENSE("GPL");
  1065. MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
  1066. module_param(debug, int, S_IRUGO | S_IWUSR);
  1067. MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) "
  1068. "0x1 = standard debug messages, 0x2 = function tracing debug. "
  1069. "Flag bits are addative (i.e., 0x3 for both debug types).");