redrat3.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * USB RedRat3 IR Transceiver rc-core driver
  3. *
  4. * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
  5. * based heavily on the work of Stephen Cox, with additional
  6. * help from RedRat Ltd.
  7. *
  8. * This driver began life based an an old version of the first-generation
  9. * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
  10. * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
  11. * Chris Dodge.
  12. *
  13. * The driver was then ported to rc-core and significantly rewritten again,
  14. * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
  15. * port effort was started by Stephen.
  16. *
  17. * TODO LIST:
  18. * - fix lirc not showing repeats properly
  19. * --
  20. *
  21. * The RedRat3 is a USB transceiver with both send & receive,
  22. * with 2 separate sensors available for receive to enable
  23. * both good long range reception for general use, and good
  24. * short range reception when required for learning a signal.
  25. *
  26. * http://www.redrat.co.uk/
  27. *
  28. * It uses its own little protocol to communicate, the required
  29. * parts of which are embedded within this driver.
  30. * --
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  45. *
  46. */
  47. #include <asm/unaligned.h>
  48. #include <linux/device.h>
  49. #include <linux/leds.h>
  50. #include <linux/module.h>
  51. #include <linux/slab.h>
  52. #include <linux/usb.h>
  53. #include <linux/usb/input.h>
  54. #include <media/rc-core.h>
  55. /* Driver Information */
  56. #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
  57. #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
  58. #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
  59. #define DRIVER_NAME "redrat3"
  60. /* bulk data transfer types */
  61. #define RR3_ERROR 0x01
  62. #define RR3_MOD_SIGNAL_IN 0x20
  63. #define RR3_MOD_SIGNAL_OUT 0x21
  64. /* Get the RR firmware version */
  65. #define RR3_FW_VERSION 0xb1
  66. #define RR3_FW_VERSION_LEN 64
  67. /* Send encoded signal bulk-sent earlier*/
  68. #define RR3_TX_SEND_SIGNAL 0xb3
  69. #define RR3_SET_IR_PARAM 0xb7
  70. #define RR3_GET_IR_PARAM 0xb8
  71. /* Blink the red LED on the device */
  72. #define RR3_BLINK_LED 0xb9
  73. /* Read serial number of device */
  74. #define RR3_READ_SER_NO 0xba
  75. #define RR3_SER_NO_LEN 4
  76. /* Start capture with the RC receiver */
  77. #define RR3_RC_DET_ENABLE 0xbb
  78. /* Stop capture with the RC receiver */
  79. #define RR3_RC_DET_DISABLE 0xbc
  80. /* Start capture with the wideband receiver */
  81. #define RR3_MODSIG_CAPTURE 0xb2
  82. /* Return the status of RC detector capture */
  83. #define RR3_RC_DET_STATUS 0xbd
  84. /* Reset redrat */
  85. #define RR3_RESET 0xa0
  86. /* Max number of lengths in the signal. */
  87. #define RR3_IR_IO_MAX_LENGTHS 0x01
  88. /* Periods to measure mod. freq. */
  89. #define RR3_IR_IO_PERIODS_MF 0x02
  90. /* Size of memory for main signal data */
  91. #define RR3_IR_IO_SIG_MEM_SIZE 0x03
  92. /* Delta value when measuring lengths */
  93. #define RR3_IR_IO_LENGTH_FUZZ 0x04
  94. /* Timeout for end of signal detection */
  95. #define RR3_IR_IO_SIG_TIMEOUT 0x05
  96. /* Minimum value for pause recognition. */
  97. #define RR3_IR_IO_MIN_PAUSE 0x06
  98. /* Clock freq. of EZ-USB chip */
  99. #define RR3_CLK 24000000
  100. /* Clock periods per timer count */
  101. #define RR3_CLK_PER_COUNT 12
  102. /* (RR3_CLK / RR3_CLK_PER_COUNT) */
  103. #define RR3_CLK_CONV_FACTOR 2000000
  104. /* USB bulk-in wideband IR data endpoint address */
  105. #define RR3_WIDE_IN_EP_ADDR 0x81
  106. /* USB bulk-in narrowband IR data endpoint address */
  107. #define RR3_NARROW_IN_EP_ADDR 0x82
  108. /* Size of the fixed-length portion of the signal */
  109. #define RR3_DRIVER_MAXLENS 255
  110. #define RR3_MAX_SIG_SIZE 512
  111. #define RR3_TIME_UNIT 50
  112. #define RR3_END_OF_SIGNAL 0x7f
  113. #define RR3_TX_TRAILER_LEN 2
  114. #define RR3_RX_MIN_TIMEOUT 5
  115. #define RR3_RX_MAX_TIMEOUT 2000
  116. /* The 8051's CPUCS Register address */
  117. #define RR3_CPUCS_REG_ADDR 0x7f92
  118. #define USB_RR3USB_VENDOR_ID 0x112a
  119. #define USB_RR3USB_PRODUCT_ID 0x0001
  120. #define USB_RR3IIUSB_PRODUCT_ID 0x0005
  121. /*
  122. * The redrat3 encodes an IR signal as set of different lengths and a set
  123. * of indices into those lengths. This sets how much two lengths must
  124. * differ before they are considered distinct, the value is specified
  125. * in microseconds.
  126. * Default 5, value 0 to 127.
  127. */
  128. static int length_fuzz = 5;
  129. module_param(length_fuzz, uint, 0644);
  130. MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)");
  131. /*
  132. * When receiving a continuous ir stream (for example when a user is
  133. * holding a button down on a remote), this specifies the minimum size
  134. * of a space when the redrat3 sends a irdata packet to the host. Specified
  135. * in miliseconds. Default value 18ms.
  136. * The value can be between 2 and 30 inclusive.
  137. */
  138. static int minimum_pause = 18;
  139. module_param(minimum_pause, uint, 0644);
  140. MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)");
  141. /*
  142. * The carrier frequency is measured during the first pulse of the IR
  143. * signal. The larger the number of periods used To measure, the more
  144. * accurate the result is likely to be, however some signals have short
  145. * initial pulses, so in some case it may be necessary to reduce this value.
  146. * Default 8, value 1 to 255.
  147. */
  148. static int periods_measure_carrier = 8;
  149. module_param(periods_measure_carrier, uint, 0644);
  150. MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)");
  151. struct redrat3_header {
  152. __be16 length;
  153. __be16 transfer_type;
  154. } __packed;
  155. /* sending and receiving irdata */
  156. struct redrat3_irdata {
  157. struct redrat3_header header;
  158. __be32 pause;
  159. __be16 mod_freq_count;
  160. __be16 num_periods;
  161. __u8 max_lengths;
  162. __u8 no_lengths;
  163. __be16 max_sig_size;
  164. __be16 sig_size;
  165. __u8 no_repeats;
  166. __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
  167. __u8 sigdata[RR3_MAX_SIG_SIZE];
  168. } __packed;
  169. /* firmware errors */
  170. struct redrat3_error {
  171. struct redrat3_header header;
  172. __be16 fw_error;
  173. } __packed;
  174. /* table of devices that work with this driver */
  175. static struct usb_device_id redrat3_dev_table[] = {
  176. /* Original version of the RedRat3 */
  177. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
  178. /* Second Version/release of the RedRat3 - RetRat3-II */
  179. {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
  180. {} /* Terminating entry */
  181. };
  182. /* Structure to hold all of our device specific stuff */
  183. struct redrat3_dev {
  184. /* core device bits */
  185. struct rc_dev *rc;
  186. struct device *dev;
  187. /* led control */
  188. struct led_classdev led;
  189. atomic_t flash;
  190. struct usb_ctrlrequest flash_control;
  191. struct urb *flash_urb;
  192. u8 flash_in_buf;
  193. /* learning */
  194. bool wideband;
  195. struct usb_ctrlrequest learn_control;
  196. struct urb *learn_urb;
  197. u8 learn_buf;
  198. /* save off the usb device pointer */
  199. struct usb_device *udev;
  200. /* the receive endpoint */
  201. struct usb_endpoint_descriptor *ep_narrow;
  202. /* the buffer to receive data */
  203. void *bulk_in_buf;
  204. /* urb used to read ir data */
  205. struct urb *narrow_urb;
  206. struct urb *wide_urb;
  207. /* the send endpoint */
  208. struct usb_endpoint_descriptor *ep_out;
  209. /* usb dma */
  210. dma_addr_t dma_in;
  211. /* Is the device currently transmitting?*/
  212. bool transmitting;
  213. /* store for current packet */
  214. struct redrat3_irdata irdata;
  215. u16 bytes_read;
  216. u32 carrier;
  217. char name[64];
  218. char phys[64];
  219. };
  220. static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
  221. {
  222. if (!rr3->transmitting && (code != 0x40))
  223. dev_info(rr3->dev, "fw error code 0x%02x: ", code);
  224. switch (code) {
  225. case 0x00:
  226. pr_cont("No Error\n");
  227. break;
  228. /* Codes 0x20 through 0x2f are IR Firmware Errors */
  229. case 0x20:
  230. pr_cont("Initial signal pulse not long enough to measure carrier frequency\n");
  231. break;
  232. case 0x21:
  233. pr_cont("Not enough length values allocated for signal\n");
  234. break;
  235. case 0x22:
  236. pr_cont("Not enough memory allocated for signal data\n");
  237. break;
  238. case 0x23:
  239. pr_cont("Too many signal repeats\n");
  240. break;
  241. case 0x28:
  242. pr_cont("Insufficient memory available for IR signal data memory allocation\n");
  243. break;
  244. case 0x29:
  245. pr_cont("Insufficient memory available 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 transfer structure\n");
  250. break;
  251. /*
  252. * Other error codes... These are primarily errors that can occur in
  253. * the control messages sent to the redrat
  254. */
  255. case 0x40:
  256. if (!rr3->transmitting)
  257. pr_cont("Signal capture has been terminated\n");
  258. break;
  259. case 0x41:
  260. pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n");
  261. break;
  262. case 0x42:
  263. pr_cont("Signal capture already started\n");
  264. break;
  265. default:
  266. pr_cont("Unknown Error\n");
  267. break;
  268. }
  269. }
  270. static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
  271. {
  272. u32 mod_freq = 0;
  273. u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
  274. if (mod_freq_count != 0)
  275. mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
  276. (mod_freq_count * RR3_CLK_PER_COUNT);
  277. return mod_freq;
  278. }
  279. /* this function scales down the figures for the same result... */
  280. static u32 redrat3_len_to_us(u32 length)
  281. {
  282. u32 biglen = length * 1000;
  283. u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
  284. u32 result = (u32) (biglen / divisor);
  285. /* don't allow zero lengths to go back, breaks lirc */
  286. return result ? result : 1;
  287. }
  288. /*
  289. * convert us back into redrat3 lengths
  290. *
  291. * length * 1000 length * 1000000
  292. * ------------- = ---------------- = micro
  293. * rr3clk / 1000 rr3clk
  294. * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
  295. * ----- = 4 ----- = 6 -------------- = len ---------------------
  296. * 3 2 1000000 1000
  297. */
  298. static u32 redrat3_us_to_len(u32 microsec)
  299. {
  300. u32 result;
  301. u32 divisor;
  302. microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
  303. divisor = (RR3_CLK_CONV_FACTOR / 1000);
  304. result = (u32)(microsec * divisor) / 1000;
  305. /* don't allow zero lengths to go back, breaks lirc */
  306. return result ? result : 1;
  307. }
  308. static void redrat3_process_ir_data(struct redrat3_dev *rr3)
  309. {
  310. DEFINE_IR_RAW_EVENT(rawir);
  311. struct device *dev;
  312. unsigned int i, sig_size, single_len, offset, val;
  313. u32 mod_freq;
  314. dev = rr3->dev;
  315. mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
  316. dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
  317. if (mod_freq && rr3->wideband) {
  318. DEFINE_IR_RAW_EVENT(ev);
  319. ev.carrier_report = 1;
  320. ev.carrier = mod_freq;
  321. ir_raw_event_store(rr3->rc, &ev);
  322. }
  323. /* process each rr3 encoded byte into an int */
  324. sig_size = be16_to_cpu(rr3->irdata.sig_size);
  325. for (i = 0; i < sig_size; i++) {
  326. offset = rr3->irdata.sigdata[i];
  327. val = get_unaligned_be16(&rr3->irdata.lens[offset]);
  328. single_len = redrat3_len_to_us(val);
  329. /* we should always get pulse/space/pulse/space samples */
  330. if (i % 2)
  331. rawir.pulse = false;
  332. else
  333. rawir.pulse = true;
  334. rawir.duration = US_TO_NS(single_len);
  335. /* cap the value to IR_MAX_DURATION */
  336. rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
  337. IR_MAX_DURATION : rawir.duration;
  338. dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
  339. rawir.pulse ? "pulse" : "space", rawir.duration, i);
  340. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  341. }
  342. /* add a trailing space */
  343. rawir.pulse = false;
  344. rawir.timeout = true;
  345. rawir.duration = rr3->rc->timeout;
  346. dev_dbg(dev, "storing trailing timeout with duration %d\n",
  347. rawir.duration);
  348. ir_raw_event_store_with_filter(rr3->rc, &rawir);
  349. dev_dbg(dev, "calling ir_raw_event_handle\n");
  350. ir_raw_event_handle(rr3->rc);
  351. }
  352. /* Util fn to send rr3 cmds */
  353. static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
  354. {
  355. struct usb_device *udev;
  356. u8 *data;
  357. int res;
  358. data = kzalloc(sizeof(u8), GFP_KERNEL);
  359. if (!data)
  360. return -ENOMEM;
  361. udev = rr3->udev;
  362. res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
  363. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  364. 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
  365. if (res < 0) {
  366. dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
  367. __func__, res, *data);
  368. res = -EIO;
  369. } else
  370. res = data[0];
  371. kfree(data);
  372. return res;
  373. }
  374. /* Enables the long range detector and starts async receive */
  375. static int redrat3_enable_detector(struct redrat3_dev *rr3)
  376. {
  377. struct device *dev = rr3->dev;
  378. u8 ret;
  379. ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
  380. if (ret != 0)
  381. dev_dbg(dev, "%s: unexpected ret of %d\n",
  382. __func__, ret);
  383. ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
  384. if (ret != 1) {
  385. dev_err(dev, "%s: detector status: %d, should be 1\n",
  386. __func__, ret);
  387. return -EIO;
  388. }
  389. ret = usb_submit_urb(rr3->narrow_urb, GFP_KERNEL);
  390. if (ret) {
  391. dev_err(rr3->dev, "narrow band urb failed: %d", ret);
  392. return ret;
  393. }
  394. ret = usb_submit_urb(rr3->wide_urb, GFP_KERNEL);
  395. if (ret)
  396. dev_err(rr3->dev, "wide band urb failed: %d", ret);
  397. return ret;
  398. }
  399. static inline void redrat3_delete(struct redrat3_dev *rr3,
  400. struct usb_device *udev)
  401. {
  402. usb_kill_urb(rr3->narrow_urb);
  403. usb_kill_urb(rr3->wide_urb);
  404. usb_kill_urb(rr3->flash_urb);
  405. usb_kill_urb(rr3->learn_urb);
  406. usb_free_urb(rr3->narrow_urb);
  407. usb_free_urb(rr3->wide_urb);
  408. usb_free_urb(rr3->flash_urb);
  409. usb_free_urb(rr3->learn_urb);
  410. usb_free_coherent(udev, le16_to_cpu(rr3->ep_narrow->wMaxPacketSize),
  411. rr3->bulk_in_buf, rr3->dma_in);
  412. kfree(rr3);
  413. }
  414. static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
  415. {
  416. __be32 *tmp;
  417. u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
  418. int len, ret, pipe;
  419. len = sizeof(*tmp);
  420. tmp = kzalloc(len, GFP_KERNEL);
  421. if (!tmp)
  422. return timeout;
  423. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  424. ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
  425. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  426. RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
  427. if (ret != len)
  428. dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
  429. else {
  430. timeout = redrat3_len_to_us(be32_to_cpup(tmp));
  431. dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
  432. }
  433. kfree(tmp);
  434. return timeout;
  435. }
  436. static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns)
  437. {
  438. struct redrat3_dev *rr3 = rc_dev->priv;
  439. struct usb_device *udev = rr3->udev;
  440. struct device *dev = rr3->dev;
  441. __be32 *timeout;
  442. int ret;
  443. timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
  444. if (!timeout)
  445. return -ENOMEM;
  446. *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000));
  447. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
  448. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  449. RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
  450. HZ * 25);
  451. dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
  452. be32_to_cpu(*timeout), ret);
  453. if (ret == sizeof(*timeout))
  454. ret = 0;
  455. else if (ret >= 0)
  456. ret = -EIO;
  457. kfree(timeout);
  458. return ret;
  459. }
  460. static void redrat3_reset(struct redrat3_dev *rr3)
  461. {
  462. struct usb_device *udev = rr3->udev;
  463. struct device *dev = rr3->dev;
  464. int rc, rxpipe, txpipe;
  465. u8 *val;
  466. size_t const len = sizeof(*val);
  467. rxpipe = usb_rcvctrlpipe(udev, 0);
  468. txpipe = usb_sndctrlpipe(udev, 0);
  469. val = kmalloc(len, GFP_KERNEL);
  470. if (!val)
  471. return;
  472. *val = 0x01;
  473. rc = usb_control_msg(udev, rxpipe, RR3_RESET,
  474. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  475. RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
  476. dev_dbg(dev, "reset returned 0x%02x\n", rc);
  477. *val = length_fuzz;
  478. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  479. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  480. RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
  481. dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
  482. *val = (65536 - (minimum_pause * 2000)) / 256;
  483. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  484. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  485. RR3_IR_IO_MIN_PAUSE, 0, val, len, HZ * 25);
  486. dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc);
  487. *val = periods_measure_carrier;
  488. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  489. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  490. RR3_IR_IO_PERIODS_MF, 0, val, len, HZ * 25);
  491. dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val,
  492. rc);
  493. *val = RR3_DRIVER_MAXLENS;
  494. rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
  495. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  496. RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
  497. dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
  498. kfree(val);
  499. }
  500. static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
  501. {
  502. int rc;
  503. char *buffer;
  504. buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL);
  505. if (!buffer)
  506. return;
  507. rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
  508. RR3_FW_VERSION,
  509. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  510. 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
  511. if (rc >= 0)
  512. dev_info(rr3->dev, "Firmware rev: %s", buffer);
  513. else
  514. dev_err(rr3->dev, "Problem fetching firmware ID\n");
  515. kfree(buffer);
  516. }
  517. static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
  518. {
  519. struct redrat3_header *header = rr3->bulk_in_buf;
  520. unsigned pktlen, pkttype;
  521. /* grab the Length and type of transfer */
  522. pktlen = be16_to_cpu(header->length);
  523. pkttype = be16_to_cpu(header->transfer_type);
  524. if (pktlen > sizeof(rr3->irdata)) {
  525. dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
  526. return;
  527. }
  528. switch (pkttype) {
  529. case RR3_ERROR:
  530. if (len >= sizeof(struct redrat3_error)) {
  531. struct redrat3_error *error = rr3->bulk_in_buf;
  532. unsigned fw_error = be16_to_cpu(error->fw_error);
  533. redrat3_dump_fw_error(rr3, fw_error);
  534. }
  535. break;
  536. case RR3_MOD_SIGNAL_IN:
  537. memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
  538. rr3->bytes_read = len;
  539. dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
  540. rr3->bytes_read, pktlen);
  541. break;
  542. default:
  543. dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
  544. pkttype, len, pktlen);
  545. break;
  546. }
  547. }
  548. static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
  549. {
  550. void *irdata = &rr3->irdata;
  551. if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
  552. dev_warn(rr3->dev, "too much data for packet\n");
  553. rr3->bytes_read = 0;
  554. return;
  555. }
  556. memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
  557. rr3->bytes_read += len;
  558. dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
  559. be16_to_cpu(rr3->irdata.header.length));
  560. }
  561. /* gather IR data from incoming urb, process it when we have enough */
  562. static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
  563. {
  564. struct device *dev = rr3->dev;
  565. unsigned pkttype;
  566. int ret = 0;
  567. if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
  568. redrat3_read_packet_start(rr3, len);
  569. } else if (rr3->bytes_read != 0) {
  570. redrat3_read_packet_continue(rr3, len);
  571. } else if (rr3->bytes_read == 0) {
  572. dev_err(dev, "error: no packet data read\n");
  573. ret = -ENODATA;
  574. goto out;
  575. }
  576. if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
  577. sizeof(struct redrat3_header))
  578. /* we're still accumulating data */
  579. return 0;
  580. /* if we get here, we've got IR data to decode */
  581. pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
  582. if (pkttype == RR3_MOD_SIGNAL_IN)
  583. redrat3_process_ir_data(rr3);
  584. else
  585. dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
  586. pkttype);
  587. out:
  588. rr3->bytes_read = 0;
  589. return ret;
  590. }
  591. /* callback function from USB when async USB request has completed */
  592. static void redrat3_handle_async(struct urb *urb)
  593. {
  594. struct redrat3_dev *rr3 = urb->context;
  595. int ret;
  596. switch (urb->status) {
  597. case 0:
  598. ret = redrat3_get_ir_data(rr3, urb->actual_length);
  599. if (!ret && rr3->wideband && !rr3->learn_urb->hcpriv) {
  600. ret = usb_submit_urb(rr3->learn_urb, GFP_ATOMIC);
  601. if (ret)
  602. dev_err(rr3->dev, "Failed to submit learning urb: %d",
  603. ret);
  604. }
  605. if (!ret) {
  606. /* no error, prepare to read more */
  607. ret = usb_submit_urb(urb, GFP_ATOMIC);
  608. if (ret)
  609. dev_err(rr3->dev, "Failed to resubmit urb: %d",
  610. ret);
  611. }
  612. break;
  613. case -ECONNRESET:
  614. case -ENOENT:
  615. case -ESHUTDOWN:
  616. usb_unlink_urb(urb);
  617. return;
  618. case -EPIPE:
  619. default:
  620. dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
  621. rr3->bytes_read = 0;
  622. break;
  623. }
  624. }
  625. static u16 mod_freq_to_val(unsigned int mod_freq)
  626. {
  627. int mult = 6000000;
  628. /* Clk used in mod. freq. generation is CLK24/4. */
  629. return 65536 - (mult / mod_freq);
  630. }
  631. static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  632. {
  633. struct redrat3_dev *rr3 = rcdev->priv;
  634. struct device *dev = rr3->dev;
  635. dev_dbg(dev, "Setting modulation frequency to %u", carrier);
  636. if (carrier == 0)
  637. return -EINVAL;
  638. rr3->carrier = carrier;
  639. return 0;
  640. }
  641. static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
  642. unsigned count)
  643. {
  644. struct redrat3_dev *rr3 = rcdev->priv;
  645. struct device *dev = rr3->dev;
  646. struct redrat3_irdata *irdata = NULL;
  647. int ret, ret_len;
  648. int lencheck, cur_sample_len, pipe;
  649. int *sample_lens = NULL;
  650. u8 curlencheck = 0;
  651. unsigned i, sendbuf_len;
  652. if (rr3->transmitting) {
  653. dev_warn(dev, "%s: transmitter already in use\n", __func__);
  654. return -EAGAIN;
  655. }
  656. if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
  657. return -EINVAL;
  658. /* rr3 will disable rc detector on transmit */
  659. rr3->transmitting = true;
  660. sample_lens = kcalloc(RR3_DRIVER_MAXLENS,
  661. sizeof(*sample_lens),
  662. GFP_KERNEL);
  663. if (!sample_lens)
  664. return -ENOMEM;
  665. irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
  666. if (!irdata) {
  667. ret = -ENOMEM;
  668. goto out;
  669. }
  670. for (i = 0; i < count; i++) {
  671. cur_sample_len = redrat3_us_to_len(txbuf[i]);
  672. if (cur_sample_len > 0xffff) {
  673. dev_warn(dev, "transmit period of %uus truncated to %uus\n",
  674. txbuf[i], redrat3_len_to_us(0xffff));
  675. cur_sample_len = 0xffff;
  676. }
  677. for (lencheck = 0; lencheck < curlencheck; lencheck++) {
  678. if (sample_lens[lencheck] == cur_sample_len)
  679. break;
  680. }
  681. if (lencheck == curlencheck) {
  682. dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
  683. i, txbuf[i], curlencheck, cur_sample_len);
  684. if (curlencheck < RR3_DRIVER_MAXLENS) {
  685. /* now convert the value to a proper
  686. * rr3 value.. */
  687. sample_lens[curlencheck] = cur_sample_len;
  688. put_unaligned_be16(cur_sample_len,
  689. &irdata->lens[curlencheck]);
  690. curlencheck++;
  691. } else {
  692. ret = -EINVAL;
  693. goto out;
  694. }
  695. }
  696. irdata->sigdata[i] = lencheck;
  697. }
  698. irdata->sigdata[count] = RR3_END_OF_SIGNAL;
  699. irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
  700. sendbuf_len = offsetof(struct redrat3_irdata,
  701. sigdata[count + RR3_TX_TRAILER_LEN]);
  702. /* fill in our packet header */
  703. irdata->header.length = cpu_to_be16(sendbuf_len -
  704. sizeof(struct redrat3_header));
  705. irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
  706. irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
  707. irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
  708. irdata->no_lengths = curlencheck;
  709. irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
  710. pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
  711. ret = usb_bulk_msg(rr3->udev, pipe, irdata,
  712. sendbuf_len, &ret_len, 10 * HZ);
  713. dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
  714. /* now tell the hardware to transmit what we sent it */
  715. pipe = usb_rcvctrlpipe(rr3->udev, 0);
  716. ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
  717. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  718. 0, 0, irdata, 2, HZ * 10);
  719. if (ret < 0)
  720. dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
  721. else
  722. ret = count;
  723. out:
  724. kfree(irdata);
  725. kfree(sample_lens);
  726. rr3->transmitting = false;
  727. /* rr3 re-enables rc detector because it was enabled before */
  728. return ret;
  729. }
  730. static void redrat3_brightness_set(struct led_classdev *led_dev, enum
  731. led_brightness brightness)
  732. {
  733. struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
  734. led);
  735. if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
  736. int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
  737. if (ret != 0) {
  738. dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
  739. __func__, ret);
  740. atomic_set(&rr3->flash, 0);
  741. }
  742. }
  743. }
  744. static int redrat3_wideband_receiver(struct rc_dev *rcdev, int enable)
  745. {
  746. struct redrat3_dev *rr3 = rcdev->priv;
  747. int ret = 0;
  748. rr3->wideband = enable != 0;
  749. if (enable) {
  750. ret = usb_submit_urb(rr3->learn_urb, GFP_KERNEL);
  751. if (ret)
  752. dev_err(rr3->dev, "Failed to submit learning urb: %d",
  753. ret);
  754. }
  755. return ret;
  756. }
  757. static void redrat3_learn_complete(struct urb *urb)
  758. {
  759. struct redrat3_dev *rr3 = urb->context;
  760. switch (urb->status) {
  761. case 0:
  762. break;
  763. case -ECONNRESET:
  764. case -ENOENT:
  765. case -ESHUTDOWN:
  766. usb_unlink_urb(urb);
  767. return;
  768. case -EPIPE:
  769. default:
  770. dev_err(rr3->dev, "Error: learn urb status = %d", urb->status);
  771. break;
  772. }
  773. }
  774. static void redrat3_led_complete(struct urb *urb)
  775. {
  776. struct redrat3_dev *rr3 = urb->context;
  777. switch (urb->status) {
  778. case 0:
  779. break;
  780. case -ECONNRESET:
  781. case -ENOENT:
  782. case -ESHUTDOWN:
  783. usb_unlink_urb(urb);
  784. return;
  785. case -EPIPE:
  786. default:
  787. dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
  788. break;
  789. }
  790. rr3->led.brightness = LED_OFF;
  791. atomic_dec(&rr3->flash);
  792. }
  793. static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
  794. {
  795. struct device *dev = rr3->dev;
  796. struct rc_dev *rc;
  797. int ret;
  798. u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
  799. rc = rc_allocate_device();
  800. if (!rc)
  801. return NULL;
  802. snprintf(rr3->name, sizeof(rr3->name),
  803. "RedRat3%s Infrared Remote Transceiver",
  804. prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "");
  805. usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
  806. rc->input_name = rr3->name;
  807. rc->input_phys = rr3->phys;
  808. usb_to_input_id(rr3->udev, &rc->input_id);
  809. rc->dev.parent = dev;
  810. rc->priv = rr3;
  811. rc->driver_type = RC_DRIVER_IR_RAW;
  812. rc->allowed_protocols = RC_BIT_ALL;
  813. rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT);
  814. rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT);
  815. rc->timeout = US_TO_NS(redrat3_get_timeout(rr3));
  816. rc->s_timeout = redrat3_set_timeout;
  817. rc->tx_ir = redrat3_transmit_ir;
  818. rc->s_tx_carrier = redrat3_set_tx_carrier;
  819. rc->s_carrier_report = redrat3_wideband_receiver;
  820. rc->driver_name = DRIVER_NAME;
  821. rc->rx_resolution = US_TO_NS(2);
  822. rc->map_name = RC_MAP_HAUPPAUGE;
  823. ret = rc_register_device(rc);
  824. if (ret < 0) {
  825. dev_err(dev, "remote dev registration failed\n");
  826. goto out;
  827. }
  828. return rc;
  829. out:
  830. rc_free_device(rc);
  831. return NULL;
  832. }
  833. static int redrat3_dev_probe(struct usb_interface *intf,
  834. const struct usb_device_id *id)
  835. {
  836. struct usb_device *udev = interface_to_usbdev(intf);
  837. struct device *dev = &intf->dev;
  838. struct usb_host_interface *uhi;
  839. struct redrat3_dev *rr3;
  840. struct usb_endpoint_descriptor *ep;
  841. struct usb_endpoint_descriptor *ep_narrow = NULL;
  842. struct usb_endpoint_descriptor *ep_wide = NULL;
  843. struct usb_endpoint_descriptor *ep_out = NULL;
  844. u8 addr, attrs;
  845. int pipe, i;
  846. int retval = -ENOMEM;
  847. uhi = intf->cur_altsetting;
  848. /* find our bulk-in and bulk-out endpoints */
  849. for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
  850. ep = &uhi->endpoint[i].desc;
  851. addr = ep->bEndpointAddress;
  852. attrs = ep->bmAttributes;
  853. if (((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
  854. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  855. USB_ENDPOINT_XFER_BULK)) {
  856. dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
  857. ep->bEndpointAddress);
  858. /* data comes in on 0x82, 0x81 is for learning */
  859. if (ep->bEndpointAddress == RR3_NARROW_IN_EP_ADDR)
  860. ep_narrow = ep;
  861. if (ep->bEndpointAddress == RR3_WIDE_IN_EP_ADDR)
  862. ep_wide = ep;
  863. }
  864. if ((ep_out == NULL) &&
  865. ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
  866. ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
  867. USB_ENDPOINT_XFER_BULK)) {
  868. dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
  869. ep->bEndpointAddress);
  870. ep_out = ep;
  871. }
  872. }
  873. if (!ep_narrow || !ep_out || !ep_wide) {
  874. dev_err(dev, "Couldn't find all endpoints\n");
  875. retval = -ENODEV;
  876. goto no_endpoints;
  877. }
  878. /* allocate memory for our device state and initialize it */
  879. rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
  880. if (!rr3)
  881. goto no_endpoints;
  882. rr3->dev = &intf->dev;
  883. rr3->ep_narrow = ep_narrow;
  884. rr3->ep_out = ep_out;
  885. rr3->udev = udev;
  886. /* set up bulk-in endpoint */
  887. rr3->narrow_urb = usb_alloc_urb(0, GFP_KERNEL);
  888. if (!rr3->narrow_urb)
  889. goto redrat_free;
  890. rr3->wide_urb = usb_alloc_urb(0, GFP_KERNEL);
  891. if (!rr3->wide_urb)
  892. goto redrat_free;
  893. rr3->bulk_in_buf = usb_alloc_coherent(udev,
  894. le16_to_cpu(ep_narrow->wMaxPacketSize),
  895. GFP_KERNEL, &rr3->dma_in);
  896. if (!rr3->bulk_in_buf)
  897. goto redrat_free;
  898. pipe = usb_rcvbulkpipe(udev, ep_narrow->bEndpointAddress);
  899. usb_fill_bulk_urb(rr3->narrow_urb, udev, pipe, rr3->bulk_in_buf,
  900. le16_to_cpu(ep_narrow->wMaxPacketSize),
  901. redrat3_handle_async, rr3);
  902. rr3->narrow_urb->transfer_dma = rr3->dma_in;
  903. rr3->narrow_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  904. pipe = usb_rcvbulkpipe(udev, ep_wide->bEndpointAddress);
  905. usb_fill_bulk_urb(rr3->wide_urb, udev, pipe, rr3->bulk_in_buf,
  906. le16_to_cpu(ep_narrow->wMaxPacketSize),
  907. redrat3_handle_async, rr3);
  908. rr3->wide_urb->transfer_dma = rr3->dma_in;
  909. rr3->wide_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  910. redrat3_reset(rr3);
  911. redrat3_get_firmware_rev(rr3);
  912. /* default.. will get overridden by any sends with a freq defined */
  913. rr3->carrier = 38000;
  914. atomic_set(&rr3->flash, 0);
  915. rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
  916. if (!rr3->flash_urb)
  917. goto redrat_free;
  918. /* learn urb */
  919. rr3->learn_urb = usb_alloc_urb(0, GFP_KERNEL);
  920. if (!rr3->learn_urb)
  921. goto redrat_free;
  922. /* setup packet is 'c0 b2 0000 0000 0001' */
  923. rr3->learn_control.bRequestType = 0xc0;
  924. rr3->learn_control.bRequest = RR3_MODSIG_CAPTURE;
  925. rr3->learn_control.wLength = cpu_to_le16(1);
  926. usb_fill_control_urb(rr3->learn_urb, udev, usb_rcvctrlpipe(udev, 0),
  927. (unsigned char *)&rr3->learn_control,
  928. &rr3->learn_buf, sizeof(rr3->learn_buf),
  929. redrat3_learn_complete, rr3);
  930. /* setup packet is 'c0 b9 0000 0000 0001' */
  931. rr3->flash_control.bRequestType = 0xc0;
  932. rr3->flash_control.bRequest = RR3_BLINK_LED;
  933. rr3->flash_control.wLength = cpu_to_le16(1);
  934. usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
  935. (unsigned char *)&rr3->flash_control,
  936. &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
  937. redrat3_led_complete, rr3);
  938. /* led control */
  939. rr3->led.name = "redrat3:red:feedback";
  940. rr3->led.default_trigger = "rc-feedback";
  941. rr3->led.brightness_set = redrat3_brightness_set;
  942. retval = led_classdev_register(&intf->dev, &rr3->led);
  943. if (retval)
  944. goto redrat_free;
  945. rr3->rc = redrat3_init_rc_dev(rr3);
  946. if (!rr3->rc) {
  947. retval = -ENOMEM;
  948. goto led_free;
  949. }
  950. /* might be all we need to do? */
  951. retval = redrat3_enable_detector(rr3);
  952. if (retval < 0)
  953. goto led_free;
  954. /* we can register the device now, as it is ready */
  955. usb_set_intfdata(intf, rr3);
  956. return 0;
  957. led_free:
  958. led_classdev_unregister(&rr3->led);
  959. redrat_free:
  960. redrat3_delete(rr3, rr3->udev);
  961. no_endpoints:
  962. return retval;
  963. }
  964. static void redrat3_dev_disconnect(struct usb_interface *intf)
  965. {
  966. struct usb_device *udev = interface_to_usbdev(intf);
  967. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  968. usb_set_intfdata(intf, NULL);
  969. rc_unregister_device(rr3->rc);
  970. led_classdev_unregister(&rr3->led);
  971. redrat3_delete(rr3, udev);
  972. }
  973. static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
  974. {
  975. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  976. led_classdev_suspend(&rr3->led);
  977. usb_kill_urb(rr3->narrow_urb);
  978. usb_kill_urb(rr3->wide_urb);
  979. usb_kill_urb(rr3->flash_urb);
  980. return 0;
  981. }
  982. static int redrat3_dev_resume(struct usb_interface *intf)
  983. {
  984. struct redrat3_dev *rr3 = usb_get_intfdata(intf);
  985. if (usb_submit_urb(rr3->narrow_urb, GFP_ATOMIC))
  986. return -EIO;
  987. if (usb_submit_urb(rr3->wide_urb, GFP_ATOMIC))
  988. return -EIO;
  989. led_classdev_resume(&rr3->led);
  990. return 0;
  991. }
  992. static struct usb_driver redrat3_dev_driver = {
  993. .name = DRIVER_NAME,
  994. .probe = redrat3_dev_probe,
  995. .disconnect = redrat3_dev_disconnect,
  996. .suspend = redrat3_dev_suspend,
  997. .resume = redrat3_dev_resume,
  998. .reset_resume = redrat3_dev_resume,
  999. .id_table = redrat3_dev_table
  1000. };
  1001. module_usb_driver(redrat3_dev_driver);
  1002. MODULE_DESCRIPTION(DRIVER_DESC);
  1003. MODULE_AUTHOR(DRIVER_AUTHOR);
  1004. MODULE_AUTHOR(DRIVER_AUTHOR2);
  1005. MODULE_LICENSE("GPL");
  1006. MODULE_DEVICE_TABLE(usb, redrat3_dev_table);