hid-logitech-dj.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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");