hid-logitech-dj.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * HID driver for Logitech Unifying receivers
  3. *
  4. * Copyright (c) 2011 Logitech
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/device.h>
  22. #include <linux/hid.h>
  23. #include <linux/module.h>
  24. #include <linux/usb.h>
  25. #include <linux/kfifo.h>
  26. #include <asm/unaligned.h>
  27. #include "hid-ids.h"
  28. #define DJ_MAX_PAIRED_DEVICES 6
  29. #define DJ_MAX_NUMBER_NOTIFICATIONS 8
  30. #define DJ_RECEIVER_INDEX 0
  31. #define DJ_DEVICE_INDEX_MIN 1
  32. #define DJ_DEVICE_INDEX_MAX 6
  33. #define DJREPORT_SHORT_LENGTH 15
  34. #define DJREPORT_LONG_LENGTH 32
  35. #define REPORT_ID_DJ_SHORT 0x20
  36. #define REPORT_ID_DJ_LONG 0x21
  37. #define REPORT_ID_HIDPP_SHORT 0x10
  38. #define REPORT_ID_HIDPP_LONG 0x11
  39. #define HIDPP_REPORT_SHORT_LENGTH 7
  40. #define HIDPP_REPORT_LONG_LENGTH 20
  41. #define HIDPP_RECEIVER_INDEX 0xff
  42. #define REPORT_TYPE_RFREPORT_FIRST 0x01
  43. #define REPORT_TYPE_RFREPORT_LAST 0x1F
  44. /* Command Switch to DJ mode */
  45. #define REPORT_TYPE_CMD_SWITCH 0x80
  46. #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
  47. #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
  48. #define TIMEOUT_NO_KEEPALIVE 0x00
  49. /* Command to Get the list of Paired devices */
  50. #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
  51. /* Device Paired Notification */
  52. #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
  53. #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
  54. #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
  55. #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
  56. #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
  57. #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
  58. #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
  59. /* Device Un-Paired Notification */
  60. #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
  61. /* Connection Status Notification */
  62. #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
  63. #define CONNECTION_STATUS_PARAM_STATUS 0x00
  64. #define STATUS_LINKLOSS 0x01
  65. /* Error Notification */
  66. #define REPORT_TYPE_NOTIF_ERROR 0x7F
  67. #define NOTIF_ERROR_PARAM_ETYPE 0x00
  68. #define ETYPE_KEEPALIVE_TIMEOUT 0x01
  69. /* supported DJ HID && RF report types */
  70. #define REPORT_TYPE_KEYBOARD 0x01
  71. #define REPORT_TYPE_MOUSE 0x02
  72. #define REPORT_TYPE_CONSUMER_CONTROL 0x03
  73. #define REPORT_TYPE_SYSTEM_CONTROL 0x04
  74. #define REPORT_TYPE_MEDIA_CENTER 0x08
  75. #define REPORT_TYPE_LEDS 0x0E
  76. /* RF Report types bitfield */
  77. #define STD_KEYBOARD 0x00000002
  78. #define STD_MOUSE 0x00000004
  79. #define MULTIMEDIA 0x00000008
  80. #define POWER_KEYS 0x00000010
  81. #define MEDIA_CENTER 0x00000100
  82. #define KBD_LEDS 0x00004000
  83. struct dj_report {
  84. u8 report_id;
  85. u8 device_index;
  86. u8 report_type;
  87. u8 report_params[DJREPORT_SHORT_LENGTH - 3];
  88. };
  89. struct dj_receiver_dev {
  90. struct hid_device *hdev;
  91. struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
  92. DJ_DEVICE_INDEX_MIN];
  93. struct work_struct work;
  94. struct kfifo notif_fifo;
  95. spinlock_t lock;
  96. bool querying_devices;
  97. };
  98. struct dj_device {
  99. struct hid_device *hdev;
  100. struct dj_receiver_dev *dj_receiver_dev;
  101. u32 reports_supported;
  102. u8 device_index;
  103. };
  104. /* Keyboard descriptor (1) */
  105. static const char kbd_descriptor[] = {
  106. 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
  107. 0x09, 0x06, /* USAGE (Keyboard) */
  108. 0xA1, 0x01, /* COLLECTION (Application) */
  109. 0x85, 0x01, /* REPORT_ID (1) */
  110. 0x95, 0x08, /* REPORT_COUNT (8) */
  111. 0x75, 0x01, /* REPORT_SIZE (1) */
  112. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  113. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  114. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  115. 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
  116. 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
  117. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  118. 0x95, 0x06, /* REPORT_COUNT (6) */
  119. 0x75, 0x08, /* REPORT_SIZE (8) */
  120. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  121. 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
  122. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  123. 0x19, 0x00, /* USAGE_MINIMUM (no event) */
  124. 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
  125. 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
  126. 0x85, 0x0e, /* REPORT_ID (14) */
  127. 0x05, 0x08, /* USAGE PAGE (LED page) */
  128. 0x95, 0x05, /* REPORT COUNT (5) */
  129. 0x75, 0x01, /* REPORT SIZE (1) */
  130. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  131. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  132. 0x19, 0x01, /* USAGE MINIMUM (1) */
  133. 0x29, 0x05, /* USAGE MAXIMUM (5) */
  134. 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
  135. 0x95, 0x01, /* REPORT COUNT (1) */
  136. 0x75, 0x03, /* REPORT SIZE (3) */
  137. 0x91, 0x01, /* OUTPUT (Constant) */
  138. 0xC0
  139. };
  140. /* Mouse descriptor (2) */
  141. static const char mse_descriptor[] = {
  142. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  143. 0x09, 0x02, /* USAGE (Mouse) */
  144. 0xA1, 0x01, /* COLLECTION (Application) */
  145. 0x85, 0x02, /* REPORT_ID = 2 */
  146. 0x09, 0x01, /* USAGE (pointer) */
  147. 0xA1, 0x00, /* COLLECTION (physical) */
  148. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  149. 0x19, 0x01, /* USAGE_MIN (1) */
  150. 0x29, 0x10, /* USAGE_MAX (16) */
  151. 0x15, 0x00, /* LOGICAL_MIN (0) */
  152. 0x25, 0x01, /* LOGICAL_MAX (1) */
  153. 0x95, 0x10, /* REPORT_COUNT (16) */
  154. 0x75, 0x01, /* REPORT_SIZE (1) */
  155. 0x81, 0x02, /* INPUT (data var abs) */
  156. 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
  157. 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
  158. 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
  159. 0x75, 0x0C, /* REPORT_SIZE (12) */
  160. 0x95, 0x02, /* REPORT_COUNT (2) */
  161. 0x09, 0x30, /* USAGE (X) */
  162. 0x09, 0x31, /* USAGE (Y) */
  163. 0x81, 0x06, /* INPUT */
  164. 0x15, 0x81, /* LOGICAL_MIN (-127) */
  165. 0x25, 0x7F, /* LOGICAL_MAX (127) */
  166. 0x75, 0x08, /* REPORT_SIZE (8) */
  167. 0x95, 0x01, /* REPORT_COUNT (1) */
  168. 0x09, 0x38, /* USAGE (wheel) */
  169. 0x81, 0x06, /* INPUT */
  170. 0x05, 0x0C, /* USAGE_PAGE(consumer) */
  171. 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
  172. 0x95, 0x01, /* REPORT_COUNT (1) */
  173. 0x81, 0x06, /* INPUT */
  174. 0xC0, /* END_COLLECTION */
  175. 0xC0, /* END_COLLECTION */
  176. };
  177. /* Consumer Control descriptor (3) */
  178. static const char consumer_descriptor[] = {
  179. 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
  180. 0x09, 0x01, /* USAGE (Consumer Control) */
  181. 0xA1, 0x01, /* COLLECTION (Application) */
  182. 0x85, 0x03, /* REPORT_ID = 3 */
  183. 0x75, 0x10, /* REPORT_SIZE (16) */
  184. 0x95, 0x02, /* REPORT_COUNT (2) */
  185. 0x15, 0x01, /* LOGICAL_MIN (1) */
  186. 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
  187. 0x19, 0x01, /* USAGE_MIN (1) */
  188. 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
  189. 0x81, 0x00, /* INPUT (Data Ary Abs) */
  190. 0xC0, /* END_COLLECTION */
  191. }; /* */
  192. /* System control descriptor (4) */
  193. static const char syscontrol_descriptor[] = {
  194. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  195. 0x09, 0x80, /* USAGE (System Control) */
  196. 0xA1, 0x01, /* COLLECTION (Application) */
  197. 0x85, 0x04, /* REPORT_ID = 4 */
  198. 0x75, 0x02, /* REPORT_SIZE (2) */
  199. 0x95, 0x01, /* REPORT_COUNT (1) */
  200. 0x15, 0x01, /* LOGICAL_MIN (1) */
  201. 0x25, 0x03, /* LOGICAL_MAX (3) */
  202. 0x09, 0x82, /* USAGE (System Sleep) */
  203. 0x09, 0x81, /* USAGE (System Power Down) */
  204. 0x09, 0x83, /* USAGE (System Wake Up) */
  205. 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
  206. 0x75, 0x06, /* REPORT_SIZE (6) */
  207. 0x81, 0x03, /* INPUT (Cnst Var Abs) */
  208. 0xC0, /* END_COLLECTION */
  209. };
  210. /* Media descriptor (8) */
  211. static const char media_descriptor[] = {
  212. 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
  213. 0x09, 0x88, /* Usage 0x0088 */
  214. 0xa1, 0x01, /* BeginCollection */
  215. 0x85, 0x08, /* Report ID 8 */
  216. 0x19, 0x01, /* Usage Min 0x0001 */
  217. 0x29, 0xff, /* Usage Max 0x00ff */
  218. 0x15, 0x01, /* Logical Min 1 */
  219. 0x26, 0xff, 0x00, /* Logical Max 255 */
  220. 0x75, 0x08, /* Report Size 8 */
  221. 0x95, 0x01, /* Report Count 1 */
  222. 0x81, 0x00, /* Input */
  223. 0xc0, /* EndCollection */
  224. }; /* */
  225. /* HIDPP descriptor */
  226. static const char hidpp_descriptor[] = {
  227. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  228. 0x09, 0x01, /* Usage (Vendor Usage 1) */
  229. 0xa1, 0x01, /* Collection (Application) */
  230. 0x85, 0x10, /* Report ID (16) */
  231. 0x75, 0x08, /* Report Size (8) */
  232. 0x95, 0x06, /* Report Count (6) */
  233. 0x15, 0x00, /* Logical Minimum (0) */
  234. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  235. 0x09, 0x01, /* Usage (Vendor Usage 1) */
  236. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  237. 0x09, 0x01, /* Usage (Vendor Usage 1) */
  238. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  239. 0xc0, /* End Collection */
  240. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  241. 0x09, 0x02, /* Usage (Vendor Usage 2) */
  242. 0xa1, 0x01, /* Collection (Application) */
  243. 0x85, 0x11, /* Report ID (17) */
  244. 0x75, 0x08, /* Report Size (8) */
  245. 0x95, 0x13, /* Report Count (19) */
  246. 0x15, 0x00, /* Logical Minimum (0) */
  247. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  248. 0x09, 0x02, /* Usage (Vendor Usage 2) */
  249. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  250. 0x09, 0x02, /* Usage (Vendor Usage 2) */
  251. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  252. 0xc0, /* End Collection */
  253. 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
  254. 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
  255. 0xa1, 0x01, /* Collection (Application) */
  256. 0x85, 0x20, /* Report ID (32) */
  257. 0x75, 0x08, /* Report Size (8) */
  258. 0x95, 0x0e, /* Report Count (14) */
  259. 0x15, 0x00, /* Logical Minimum (0) */
  260. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  261. 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
  262. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  263. 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
  264. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  265. 0x85, 0x21, /* Report ID (33) */
  266. 0x95, 0x1f, /* Report Count (31) */
  267. 0x15, 0x00, /* Logical Minimum (0) */
  268. 0x26, 0xff, 0x00, /* Logical Maximum (255) */
  269. 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
  270. 0x81, 0x00, /* Input (Data,Arr,Abs) */
  271. 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
  272. 0x91, 0x00, /* Output (Data,Arr,Abs) */
  273. 0xc0, /* End Collection */
  274. };
  275. /* Maximum size of all defined hid reports in bytes (including report id) */
  276. #define MAX_REPORT_SIZE 8
  277. /* Make sure all descriptors are present here */
  278. #define MAX_RDESC_SIZE \
  279. (sizeof(kbd_descriptor) + \
  280. sizeof(mse_descriptor) + \
  281. sizeof(consumer_descriptor) + \
  282. sizeof(syscontrol_descriptor) + \
  283. sizeof(media_descriptor) + \
  284. sizeof(hidpp_descriptor))
  285. /* Number of possible hid report types that can be created by this driver.
  286. *
  287. * Right now, RF report types have the same report types (or report id's)
  288. * than the hid report created from those RF reports. In the future
  289. * this doesnt have to be true.
  290. *
  291. * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
  292. * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
  293. * reports and consumer control, etc. If a new RF report is created, it doesn't
  294. * has to have the same report id as its corresponding hid report, so an
  295. * translation may have to take place for future report types.
  296. */
  297. #define NUMBER_OF_HID_REPORTS 32
  298. static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
  299. [1] = 8, /* Standard keyboard */
  300. [2] = 8, /* Standard mouse */
  301. [3] = 5, /* Consumer control */
  302. [4] = 2, /* System control */
  303. [8] = 2, /* Media Center */
  304. };
  305. #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
  306. static struct hid_ll_driver logi_dj_ll_driver;
  307. static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
  308. static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
  309. struct dj_report *dj_report)
  310. {
  311. /* Called in delayed work context */
  312. struct dj_device *dj_dev;
  313. unsigned long flags;
  314. spin_lock_irqsave(&djrcv_dev->lock, flags);
  315. dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
  316. djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
  317. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  318. if (dj_dev != NULL) {
  319. hid_destroy_device(dj_dev->hdev);
  320. kfree(dj_dev);
  321. } else {
  322. dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
  323. __func__);
  324. }
  325. }
  326. static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
  327. struct dj_report *dj_report)
  328. {
  329. /* Called in delayed work context */
  330. struct hid_device *djrcv_hdev = djrcv_dev->hdev;
  331. struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
  332. struct usb_device *usbdev = interface_to_usbdev(intf);
  333. struct hid_device *dj_hiddev;
  334. struct dj_device *dj_dev;
  335. /* Device index goes from 1 to 6, we need 3 bytes to store the
  336. * semicolon, the index, and a null terminator
  337. */
  338. unsigned char tmpstr[3];
  339. if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
  340. SPFUNCTION_DEVICE_LIST_EMPTY) {
  341. dbg_hid("%s: device list is empty\n", __func__);
  342. djrcv_dev->querying_devices = false;
  343. return;
  344. }
  345. if (djrcv_dev->paired_dj_devices[dj_report->device_index]) {
  346. /* The device is already known. No need to reallocate it. */
  347. dbg_hid("%s: device is already known\n", __func__);
  348. return;
  349. }
  350. dj_hiddev = hid_allocate_device();
  351. if (IS_ERR(dj_hiddev)) {
  352. dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
  353. __func__);
  354. return;
  355. }
  356. dj_hiddev->ll_driver = &logi_dj_ll_driver;
  357. dj_hiddev->dev.parent = &djrcv_hdev->dev;
  358. dj_hiddev->bus = BUS_USB;
  359. dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
  360. dj_hiddev->product =
  361. (dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]
  362. << 8) |
  363. dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
  364. snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
  365. "Logitech Unifying Device. Wireless PID:%04x",
  366. dj_hiddev->product);
  367. dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
  368. usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
  369. snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
  370. strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
  371. dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
  372. if (!dj_dev) {
  373. dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
  374. __func__);
  375. goto dj_device_allocate_fail;
  376. }
  377. dj_dev->reports_supported = get_unaligned_le32(
  378. dj_report->report_params + DEVICE_PAIRED_RF_REPORT_TYPE);
  379. dj_dev->hdev = dj_hiddev;
  380. dj_dev->dj_receiver_dev = djrcv_dev;
  381. dj_dev->device_index = dj_report->device_index;
  382. dj_hiddev->driver_data = dj_dev;
  383. djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
  384. if (hid_add_device(dj_hiddev)) {
  385. dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
  386. __func__);
  387. goto hid_add_device_fail;
  388. }
  389. return;
  390. hid_add_device_fail:
  391. djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
  392. kfree(dj_dev);
  393. dj_device_allocate_fail:
  394. hid_destroy_device(dj_hiddev);
  395. }
  396. static void delayedwork_callback(struct work_struct *work)
  397. {
  398. struct dj_receiver_dev *djrcv_dev =
  399. container_of(work, struct dj_receiver_dev, work);
  400. struct dj_report dj_report;
  401. unsigned long flags;
  402. int count;
  403. int retval;
  404. dbg_hid("%s\n", __func__);
  405. spin_lock_irqsave(&djrcv_dev->lock, flags);
  406. count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
  407. sizeof(struct dj_report));
  408. if (count != sizeof(struct dj_report)) {
  409. dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
  410. "notifications available\n", __func__);
  411. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  412. return;
  413. }
  414. if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
  415. if (schedule_work(&djrcv_dev->work) == 0) {
  416. dbg_hid("%s: did not schedule the work item, was "
  417. "already queued\n", __func__);
  418. }
  419. }
  420. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  421. switch (dj_report.report_type) {
  422. case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
  423. logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
  424. break;
  425. case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
  426. logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
  427. break;
  428. default:
  429. /* A normal report (i. e. not belonging to a pair/unpair notification)
  430. * arriving here, means that the report arrived but we did not have a
  431. * paired dj_device associated to the report's device_index, this
  432. * means that the original "device paired" notification corresponding
  433. * to this dj_device never arrived to this driver. The reason is that
  434. * hid-core discards all packets coming from a device while probe() is
  435. * executing. */
  436. if (!djrcv_dev->paired_dj_devices[dj_report.device_index]) {
  437. /* ok, we don't know the device, just re-ask the
  438. * receiver for the list of connected devices. */
  439. retval = logi_dj_recv_query_paired_devices(djrcv_dev);
  440. if (!retval) {
  441. /* everything went fine, so just leave */
  442. break;
  443. }
  444. dev_err(&djrcv_dev->hdev->dev,
  445. "%s:logi_dj_recv_query_paired_devices "
  446. "error:%d\n", __func__, retval);
  447. }
  448. dbg_hid("%s: unexpected report type\n", __func__);
  449. }
  450. }
  451. static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
  452. struct dj_report *dj_report)
  453. {
  454. /* We are called from atomic context (tasklet && djrcv->lock held) */
  455. kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
  456. if (schedule_work(&djrcv_dev->work) == 0) {
  457. dbg_hid("%s: did not schedule the work item, was already "
  458. "queued\n", __func__);
  459. }
  460. }
  461. static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
  462. struct dj_report *dj_report)
  463. {
  464. /* We are called from atomic context (tasklet && djrcv->lock held) */
  465. unsigned int i;
  466. u8 reportbuffer[MAX_REPORT_SIZE];
  467. struct dj_device *djdev;
  468. djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
  469. memset(reportbuffer, 0, sizeof(reportbuffer));
  470. for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
  471. if (djdev->reports_supported & (1 << i)) {
  472. reportbuffer[0] = i;
  473. if (hid_input_report(djdev->hdev,
  474. HID_INPUT_REPORT,
  475. reportbuffer,
  476. hid_reportid_size_map[i], 1)) {
  477. dbg_hid("hid_input_report error sending null "
  478. "report\n");
  479. }
  480. }
  481. }
  482. }
  483. static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
  484. struct dj_report *dj_report)
  485. {
  486. /* We are called from atomic context (tasklet && djrcv->lock held) */
  487. struct dj_device *dj_device;
  488. dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
  489. if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
  490. (hid_reportid_size_map[dj_report->report_type] == 0)) {
  491. dbg_hid("invalid report type:%x\n", dj_report->report_type);
  492. return;
  493. }
  494. if (hid_input_report(dj_device->hdev,
  495. HID_INPUT_REPORT, &dj_report->report_type,
  496. hid_reportid_size_map[dj_report->report_type], 1)) {
  497. dbg_hid("hid_input_report error\n");
  498. }
  499. }
  500. static void logi_dj_recv_forward_hidpp(struct dj_device *dj_dev, u8 *data,
  501. int size)
  502. {
  503. /* We are called from atomic context (tasklet && djrcv->lock held) */
  504. if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
  505. dbg_hid("hid_input_report error\n");
  506. }
  507. static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
  508. struct dj_report *dj_report)
  509. {
  510. struct hid_device *hdev = djrcv_dev->hdev;
  511. struct hid_report *report;
  512. struct hid_report_enum *output_report_enum;
  513. u8 *data = (u8 *)(&dj_report->device_index);
  514. unsigned int i;
  515. output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
  516. report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
  517. if (!report) {
  518. dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__);
  519. return -ENODEV;
  520. }
  521. for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
  522. report->field[0]->value[i] = data[i];
  523. hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
  524. return 0;
  525. }
  526. static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
  527. {
  528. struct dj_report *dj_report;
  529. int retval;
  530. /* no need to protect djrcv_dev->querying_devices */
  531. if (djrcv_dev->querying_devices)
  532. return 0;
  533. dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
  534. if (!dj_report)
  535. return -ENOMEM;
  536. dj_report->report_id = REPORT_ID_DJ_SHORT;
  537. dj_report->device_index = 0xFF;
  538. dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
  539. retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
  540. kfree(dj_report);
  541. return retval;
  542. }
  543. static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
  544. unsigned timeout)
  545. {
  546. struct hid_device *hdev = djrcv_dev->hdev;
  547. struct dj_report *dj_report;
  548. u8 *buf;
  549. int retval;
  550. dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
  551. if (!dj_report)
  552. return -ENOMEM;
  553. dj_report->report_id = REPORT_ID_DJ_SHORT;
  554. dj_report->device_index = 0xFF;
  555. dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
  556. dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
  557. dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
  558. retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
  559. /*
  560. * Ugly sleep to work around a USB 3.0 bug when the receiver is still
  561. * processing the "switch-to-dj" command while we send an other command.
  562. * 50 msec should gives enough time to the receiver to be ready.
  563. */
  564. msleep(50);
  565. /*
  566. * Magical bits to set up hidpp notifications when the dj devices
  567. * are connected/disconnected.
  568. *
  569. * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
  570. * than DJREPORT_SHORT_LENGTH.
  571. */
  572. buf = (u8 *)dj_report;
  573. memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
  574. buf[0] = REPORT_ID_HIDPP_SHORT;
  575. buf[1] = 0xFF;
  576. buf[2] = 0x80;
  577. buf[3] = 0x00;
  578. buf[4] = 0x00;
  579. buf[5] = 0x09;
  580. buf[6] = 0x00;
  581. hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
  582. HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
  583. HID_REQ_SET_REPORT);
  584. kfree(dj_report);
  585. return retval;
  586. }
  587. static int logi_dj_ll_open(struct hid_device *hid)
  588. {
  589. dbg_hid("%s:%s\n", __func__, hid->phys);
  590. return 0;
  591. }
  592. static void logi_dj_ll_close(struct hid_device *hid)
  593. {
  594. dbg_hid("%s:%s\n", __func__, hid->phys);
  595. }
  596. static u8 unifying_name_query[] = {0x10, 0xff, 0x83, 0xb5, 0x40, 0x00, 0x00};
  597. static u8 unifying_name_answer[] = {0x11, 0xff, 0x83, 0xb5};
  598. static int logi_dj_ll_raw_request(struct hid_device *hid,
  599. unsigned char reportnum, __u8 *buf,
  600. size_t count, unsigned char report_type,
  601. int reqtype)
  602. {
  603. struct dj_device *djdev = hid->driver_data;
  604. struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
  605. u8 *out_buf;
  606. int ret;
  607. if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
  608. (buf[0] == REPORT_ID_HIDPP_LONG)) {
  609. if (count < 2)
  610. return -EINVAL;
  611. /* special case where we should not overwrite
  612. * the device_index */
  613. if (count == 7 && !memcmp(buf, unifying_name_query,
  614. sizeof(unifying_name_query)))
  615. buf[4] |= djdev->device_index - 1;
  616. else
  617. buf[1] = djdev->device_index;
  618. return hid_hw_raw_request(djrcv_dev->hdev, reportnum, buf,
  619. count, report_type, reqtype);
  620. }
  621. if (buf[0] != REPORT_TYPE_LEDS)
  622. return -EINVAL;
  623. out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
  624. if (!out_buf)
  625. return -ENOMEM;
  626. if (count > DJREPORT_SHORT_LENGTH - 2)
  627. count = DJREPORT_SHORT_LENGTH - 2;
  628. out_buf[0] = REPORT_ID_DJ_SHORT;
  629. out_buf[1] = djdev->device_index;
  630. memcpy(out_buf + 2, buf, count);
  631. ret = hid_hw_raw_request(djrcv_dev->hdev, out_buf[0], out_buf,
  632. DJREPORT_SHORT_LENGTH, report_type, reqtype);
  633. kfree(out_buf);
  634. return ret;
  635. }
  636. static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
  637. {
  638. memcpy(rdesc + *rsize, data, size);
  639. *rsize += size;
  640. }
  641. static int logi_dj_ll_parse(struct hid_device *hid)
  642. {
  643. struct dj_device *djdev = hid->driver_data;
  644. unsigned int rsize = 0;
  645. char *rdesc;
  646. int retval;
  647. dbg_hid("%s\n", __func__);
  648. djdev->hdev->version = 0x0111;
  649. djdev->hdev->country = 0x00;
  650. rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
  651. if (!rdesc)
  652. return -ENOMEM;
  653. if (djdev->reports_supported & STD_KEYBOARD) {
  654. dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
  655. __func__, djdev->reports_supported);
  656. rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
  657. }
  658. if (djdev->reports_supported & STD_MOUSE) {
  659. dbg_hid("%s: sending a mouse descriptor, reports_supported: "
  660. "%x\n", __func__, djdev->reports_supported);
  661. rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor));
  662. }
  663. if (djdev->reports_supported & MULTIMEDIA) {
  664. dbg_hid("%s: sending a multimedia report descriptor: %x\n",
  665. __func__, djdev->reports_supported);
  666. rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
  667. }
  668. if (djdev->reports_supported & POWER_KEYS) {
  669. dbg_hid("%s: sending a power keys report descriptor: %x\n",
  670. __func__, djdev->reports_supported);
  671. rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
  672. }
  673. if (djdev->reports_supported & MEDIA_CENTER) {
  674. dbg_hid("%s: sending a media center report descriptor: %x\n",
  675. __func__, djdev->reports_supported);
  676. rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
  677. }
  678. if (djdev->reports_supported & KBD_LEDS) {
  679. dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
  680. __func__, djdev->reports_supported);
  681. }
  682. rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor));
  683. retval = hid_parse_report(hid, rdesc, rsize);
  684. kfree(rdesc);
  685. return retval;
  686. }
  687. static int logi_dj_ll_start(struct hid_device *hid)
  688. {
  689. dbg_hid("%s\n", __func__);
  690. return 0;
  691. }
  692. static void logi_dj_ll_stop(struct hid_device *hid)
  693. {
  694. dbg_hid("%s\n", __func__);
  695. }
  696. static struct hid_ll_driver logi_dj_ll_driver = {
  697. .parse = logi_dj_ll_parse,
  698. .start = logi_dj_ll_start,
  699. .stop = logi_dj_ll_stop,
  700. .open = logi_dj_ll_open,
  701. .close = logi_dj_ll_close,
  702. .raw_request = logi_dj_ll_raw_request,
  703. };
  704. static int logi_dj_dj_event(struct hid_device *hdev,
  705. struct hid_report *report, u8 *data,
  706. int size)
  707. {
  708. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  709. struct dj_report *dj_report = (struct dj_report *) data;
  710. unsigned long flags;
  711. /*
  712. * Here we receive all data coming from iface 2, there are 3 cases:
  713. *
  714. * 1) Data is intended for this driver i. e. data contains arrival,
  715. * departure, etc notifications, in which case we queue them for delayed
  716. * processing by the work queue. We return 1 to hid-core as no further
  717. * processing is required from it.
  718. *
  719. * 2) Data informs a connection change, if the change means rf link
  720. * loss, then we must send a null report to the upper layer to discard
  721. * potentially pressed keys that may be repeated forever by the input
  722. * layer. Return 1 to hid-core as no further processing is required.
  723. *
  724. * 3) Data is an actual input event from a paired DJ device in which
  725. * case we forward it to the correct hid device (via hid_input_report()
  726. * ) and return 1 so hid-core does not anything else with it.
  727. */
  728. if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
  729. (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
  730. /*
  731. * Device index is wrong, bail out.
  732. * This driver can ignore safely the receiver notifications,
  733. * so ignore those reports too.
  734. */
  735. if (dj_report->device_index != DJ_RECEIVER_INDEX)
  736. dev_err(&hdev->dev, "%s: invalid device index:%d\n",
  737. __func__, dj_report->device_index);
  738. return false;
  739. }
  740. spin_lock_irqsave(&djrcv_dev->lock, flags);
  741. if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
  742. /* received an event for an unknown device, bail out */
  743. logi_dj_recv_queue_notification(djrcv_dev, dj_report);
  744. goto out;
  745. }
  746. switch (dj_report->report_type) {
  747. case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
  748. /* pairing notifications are handled above the switch */
  749. break;
  750. case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
  751. logi_dj_recv_queue_notification(djrcv_dev, dj_report);
  752. break;
  753. case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
  754. if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
  755. STATUS_LINKLOSS) {
  756. logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
  757. }
  758. break;
  759. default:
  760. logi_dj_recv_forward_report(djrcv_dev, dj_report);
  761. }
  762. out:
  763. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  764. return true;
  765. }
  766. static int logi_dj_hidpp_event(struct hid_device *hdev,
  767. struct hid_report *report, u8 *data,
  768. int size)
  769. {
  770. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  771. struct dj_report *dj_report = (struct dj_report *) data;
  772. unsigned long flags;
  773. u8 device_index = dj_report->device_index;
  774. if (device_index == HIDPP_RECEIVER_INDEX) {
  775. /* special case were the device wants to know its unifying
  776. * name */
  777. if (size == HIDPP_REPORT_LONG_LENGTH &&
  778. !memcmp(data, unifying_name_answer,
  779. sizeof(unifying_name_answer)) &&
  780. ((data[4] & 0xF0) == 0x40))
  781. device_index = (data[4] & 0x0F) + 1;
  782. else
  783. return false;
  784. }
  785. /*
  786. * Data is from the HID++ collection, in this case, we forward the
  787. * data to the corresponding child dj device and return 0 to hid-core
  788. * so he data also goes to the hidraw device of the receiver. This
  789. * allows a user space application to implement the full HID++ routing
  790. * via the receiver.
  791. */
  792. if ((device_index < DJ_DEVICE_INDEX_MIN) ||
  793. (device_index > DJ_DEVICE_INDEX_MAX)) {
  794. /*
  795. * Device index is wrong, bail out.
  796. * This driver can ignore safely the receiver notifications,
  797. * so ignore those reports too.
  798. */
  799. dev_err(&hdev->dev, "%s: invalid device index:%d\n",
  800. __func__, dj_report->device_index);
  801. return false;
  802. }
  803. spin_lock_irqsave(&djrcv_dev->lock, flags);
  804. if (!djrcv_dev->paired_dj_devices[device_index])
  805. /* received an event for an unknown device, bail out */
  806. goto out;
  807. logi_dj_recv_forward_hidpp(djrcv_dev->paired_dj_devices[device_index],
  808. data, size);
  809. out:
  810. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  811. return false;
  812. }
  813. static int logi_dj_raw_event(struct hid_device *hdev,
  814. struct hid_report *report, u8 *data,
  815. int size)
  816. {
  817. dbg_hid("%s, size:%d\n", __func__, size);
  818. switch (data[0]) {
  819. case REPORT_ID_DJ_SHORT:
  820. if (size != DJREPORT_SHORT_LENGTH) {
  821. dev_err(&hdev->dev, "DJ report of bad size (%d)", size);
  822. return false;
  823. }
  824. return logi_dj_dj_event(hdev, report, data, size);
  825. case REPORT_ID_HIDPP_SHORT:
  826. if (size != HIDPP_REPORT_SHORT_LENGTH) {
  827. dev_err(&hdev->dev,
  828. "Short HID++ report of bad size (%d)", size);
  829. return false;
  830. }
  831. return logi_dj_hidpp_event(hdev, report, data, size);
  832. case REPORT_ID_HIDPP_LONG:
  833. if (size != HIDPP_REPORT_LONG_LENGTH) {
  834. dev_err(&hdev->dev,
  835. "Long HID++ report of bad size (%d)", size);
  836. return false;
  837. }
  838. return logi_dj_hidpp_event(hdev, report, data, size);
  839. }
  840. return false;
  841. }
  842. static int logi_dj_probe(struct hid_device *hdev,
  843. const struct hid_device_id *id)
  844. {
  845. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  846. struct dj_receiver_dev *djrcv_dev;
  847. int retval;
  848. dbg_hid("%s called for ifnum %d\n", __func__,
  849. intf->cur_altsetting->desc.bInterfaceNumber);
  850. /* Ignore interfaces 0 and 1, they will not carry any data, dont create
  851. * any hid_device for them */
  852. if (intf->cur_altsetting->desc.bInterfaceNumber !=
  853. LOGITECH_DJ_INTERFACE_NUMBER) {
  854. dbg_hid("%s: ignoring ifnum %d\n", __func__,
  855. intf->cur_altsetting->desc.bInterfaceNumber);
  856. return -ENODEV;
  857. }
  858. /* Treat interface 2 */
  859. djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
  860. if (!djrcv_dev) {
  861. dev_err(&hdev->dev,
  862. "%s:failed allocating dj_receiver_dev\n", __func__);
  863. return -ENOMEM;
  864. }
  865. djrcv_dev->hdev = hdev;
  866. INIT_WORK(&djrcv_dev->work, delayedwork_callback);
  867. spin_lock_init(&djrcv_dev->lock);
  868. if (kfifo_alloc(&djrcv_dev->notif_fifo,
  869. DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
  870. GFP_KERNEL)) {
  871. dev_err(&hdev->dev,
  872. "%s:failed allocating notif_fifo\n", __func__);
  873. kfree(djrcv_dev);
  874. return -ENOMEM;
  875. }
  876. hid_set_drvdata(hdev, djrcv_dev);
  877. /* Call to usbhid to fetch the HID descriptors of interface 2 and
  878. * subsequently call to the hid/hid-core to parse the fetched
  879. * descriptors, this will in turn create the hidraw and hiddev nodes
  880. * for interface 2 of the receiver */
  881. retval = hid_parse(hdev);
  882. if (retval) {
  883. dev_err(&hdev->dev,
  884. "%s:parse of interface 2 failed\n", __func__);
  885. goto hid_parse_fail;
  886. }
  887. if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, REPORT_ID_DJ_SHORT,
  888. 0, DJREPORT_SHORT_LENGTH - 1)) {
  889. retval = -ENODEV;
  890. goto hid_parse_fail;
  891. }
  892. /* Starts the usb device and connects to upper interfaces hiddev and
  893. * hidraw */
  894. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  895. if (retval) {
  896. dev_err(&hdev->dev,
  897. "%s:hid_hw_start returned error\n", __func__);
  898. goto hid_hw_start_fail;
  899. }
  900. retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
  901. if (retval < 0) {
  902. dev_err(&hdev->dev,
  903. "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
  904. __func__, retval);
  905. goto switch_to_dj_mode_fail;
  906. }
  907. /* This is enabling the polling urb on the IN endpoint */
  908. retval = hid_hw_open(hdev);
  909. if (retval < 0) {
  910. dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
  911. __func__, retval);
  912. goto llopen_failed;
  913. }
  914. /* Allow incoming packets to arrive: */
  915. hid_device_io_start(hdev);
  916. retval = logi_dj_recv_query_paired_devices(djrcv_dev);
  917. if (retval < 0) {
  918. dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
  919. "error:%d\n", __func__, retval);
  920. goto logi_dj_recv_query_paired_devices_failed;
  921. }
  922. return retval;
  923. logi_dj_recv_query_paired_devices_failed:
  924. hid_hw_close(hdev);
  925. llopen_failed:
  926. switch_to_dj_mode_fail:
  927. hid_hw_stop(hdev);
  928. hid_hw_start_fail:
  929. hid_parse_fail:
  930. kfifo_free(&djrcv_dev->notif_fifo);
  931. kfree(djrcv_dev);
  932. hid_set_drvdata(hdev, NULL);
  933. return retval;
  934. }
  935. #ifdef CONFIG_PM
  936. static int logi_dj_reset_resume(struct hid_device *hdev)
  937. {
  938. int retval;
  939. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  940. retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
  941. if (retval < 0) {
  942. dev_err(&hdev->dev,
  943. "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
  944. __func__, retval);
  945. }
  946. return 0;
  947. }
  948. #endif
  949. static void logi_dj_remove(struct hid_device *hdev)
  950. {
  951. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  952. struct dj_device *dj_dev;
  953. int i;
  954. dbg_hid("%s\n", __func__);
  955. cancel_work_sync(&djrcv_dev->work);
  956. hid_hw_close(hdev);
  957. hid_hw_stop(hdev);
  958. /* I suppose that at this point the only context that can access
  959. * the djrecv_data is this thread as the work item is guaranteed to
  960. * have finished and no more raw_event callbacks should arrive after
  961. * the remove callback was triggered so no locks are put around the
  962. * code below */
  963. for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
  964. dj_dev = djrcv_dev->paired_dj_devices[i];
  965. if (dj_dev != NULL) {
  966. hid_destroy_device(dj_dev->hdev);
  967. kfree(dj_dev);
  968. djrcv_dev->paired_dj_devices[i] = NULL;
  969. }
  970. }
  971. kfifo_free(&djrcv_dev->notif_fifo);
  972. kfree(djrcv_dev);
  973. hid_set_drvdata(hdev, NULL);
  974. }
  975. static const struct hid_device_id logi_dj_receivers[] = {
  976. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  977. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
  978. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  979. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
  980. {}
  981. };
  982. MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
  983. static struct hid_driver logi_djreceiver_driver = {
  984. .name = "logitech-djreceiver",
  985. .id_table = logi_dj_receivers,
  986. .probe = logi_dj_probe,
  987. .remove = logi_dj_remove,
  988. .raw_event = logi_dj_raw_event,
  989. #ifdef CONFIG_PM
  990. .reset_resume = logi_dj_reset_resume,
  991. #endif
  992. };
  993. module_hid_driver(logi_djreceiver_driver);
  994. MODULE_LICENSE("GPL");
  995. MODULE_AUTHOR("Logitech");
  996. MODULE_AUTHOR("Nestor Lopez Casado");
  997. MODULE_AUTHOR("nlopezcasad@logitech.com");