hid-logitech-dj.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 "usbhid/usbhid.h"
  26. #include "hid-ids.h"
  27. #include "hid-logitech-dj.h"
  28. /* Keyboard descriptor (1) */
  29. static const char kbd_descriptor[] = {
  30. 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
  31. 0x09, 0x06, /* USAGE (Keyboard) */
  32. 0xA1, 0x01, /* COLLECTION (Application) */
  33. 0x85, 0x01, /* REPORT_ID (1) */
  34. 0x95, 0x08, /* REPORT_COUNT (8) */
  35. 0x75, 0x01, /* REPORT_SIZE (1) */
  36. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  37. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  38. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  39. 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
  40. 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
  41. 0x81, 0x02, /* INPUT (Data,Var,Abs) */
  42. 0x95, 0x05, /* REPORT COUNT (5) */
  43. 0x05, 0x08, /* USAGE PAGE (LED page) */
  44. 0x19, 0x01, /* USAGE MINIMUM (1) */
  45. 0x29, 0x05, /* USAGE MAXIMUM (5) */
  46. 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
  47. 0x95, 0x01, /* REPORT COUNT (1) */
  48. 0x75, 0x03, /* REPORT SIZE (3) */
  49. 0x91, 0x01, /* OUTPUT (Constant) */
  50. 0x95, 0x06, /* REPORT_COUNT (6) */
  51. 0x75, 0x08, /* REPORT_SIZE (8) */
  52. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  53. 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
  54. 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
  55. 0x19, 0x00, /* USAGE_MINIMUM (no event) */
  56. 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
  57. 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
  58. 0xC0
  59. };
  60. /* Mouse descriptor (2) */
  61. static const char mse_descriptor[] = {
  62. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  63. 0x09, 0x02, /* USAGE (Mouse) */
  64. 0xA1, 0x01, /* COLLECTION (Application) */
  65. 0x85, 0x02, /* REPORT_ID = 2 */
  66. 0x09, 0x01, /* USAGE (pointer) */
  67. 0xA1, 0x00, /* COLLECTION (physical) */
  68. 0x05, 0x09, /* USAGE_PAGE (buttons) */
  69. 0x19, 0x01, /* USAGE_MIN (1) */
  70. 0x29, 0x10, /* USAGE_MAX (16) */
  71. 0x15, 0x00, /* LOGICAL_MIN (0) */
  72. 0x25, 0x01, /* LOGICAL_MAX (1) */
  73. 0x95, 0x10, /* REPORT_COUNT (16) */
  74. 0x75, 0x01, /* REPORT_SIZE (1) */
  75. 0x81, 0x02, /* INPUT (data var abs) */
  76. 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
  77. 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
  78. 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
  79. 0x75, 0x0C, /* REPORT_SIZE (12) */
  80. 0x95, 0x02, /* REPORT_COUNT (2) */
  81. 0x09, 0x30, /* USAGE (X) */
  82. 0x09, 0x31, /* USAGE (Y) */
  83. 0x81, 0x06, /* INPUT */
  84. 0x15, 0x81, /* LOGICAL_MIN (-127) */
  85. 0x25, 0x7F, /* LOGICAL_MAX (127) */
  86. 0x75, 0x08, /* REPORT_SIZE (8) */
  87. 0x95, 0x01, /* REPORT_COUNT (1) */
  88. 0x09, 0x38, /* USAGE (wheel) */
  89. 0x81, 0x06, /* INPUT */
  90. 0x05, 0x0C, /* USAGE_PAGE(consumer) */
  91. 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
  92. 0x95, 0x01, /* REPORT_COUNT (1) */
  93. 0x81, 0x06, /* INPUT */
  94. 0xC0, /* END_COLLECTION */
  95. 0xC0, /* END_COLLECTION */
  96. };
  97. /* Consumer Control descriptor (3) */
  98. static const char consumer_descriptor[] = {
  99. 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
  100. 0x09, 0x01, /* USAGE (Consumer Control) */
  101. 0xA1, 0x01, /* COLLECTION (Application) */
  102. 0x85, 0x03, /* REPORT_ID = 3 */
  103. 0x75, 0x10, /* REPORT_SIZE (16) */
  104. 0x95, 0x02, /* REPORT_COUNT (2) */
  105. 0x15, 0x01, /* LOGICAL_MIN (1) */
  106. 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
  107. 0x19, 0x01, /* USAGE_MIN (1) */
  108. 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
  109. 0x81, 0x00, /* INPUT (Data Ary Abs) */
  110. 0xC0, /* END_COLLECTION */
  111. }; /* */
  112. /* System control descriptor (4) */
  113. static const char syscontrol_descriptor[] = {
  114. 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
  115. 0x09, 0x80, /* USAGE (System Control) */
  116. 0xA1, 0x01, /* COLLECTION (Application) */
  117. 0x85, 0x04, /* REPORT_ID = 4 */
  118. 0x75, 0x02, /* REPORT_SIZE (2) */
  119. 0x95, 0x01, /* REPORT_COUNT (1) */
  120. 0x15, 0x01, /* LOGICAL_MIN (1) */
  121. 0x25, 0x03, /* LOGICAL_MAX (3) */
  122. 0x09, 0x82, /* USAGE (System Sleep) */
  123. 0x09, 0x81, /* USAGE (System Power Down) */
  124. 0x09, 0x83, /* USAGE (System Wake Up) */
  125. 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
  126. 0x75, 0x06, /* REPORT_SIZE (6) */
  127. 0x81, 0x03, /* INPUT (Cnst Var Abs) */
  128. 0xC0, /* END_COLLECTION */
  129. };
  130. /* Media descriptor (8) */
  131. static const char media_descriptor[] = {
  132. 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
  133. 0x09, 0x88, /* Usage 0x0088 */
  134. 0xa1, 0x01, /* BeginCollection */
  135. 0x85, 0x08, /* Report ID 8 */
  136. 0x19, 0x01, /* Usage Min 0x0001 */
  137. 0x29, 0xff, /* Usage Max 0x00ff */
  138. 0x15, 0x01, /* Logical Min 1 */
  139. 0x26, 0xff, 0x00, /* Logical Max 255 */
  140. 0x75, 0x08, /* Report Size 8 */
  141. 0x95, 0x01, /* Report Count 1 */
  142. 0x81, 0x00, /* Input */
  143. 0xc0, /* EndCollection */
  144. }; /* */
  145. /* Maximum size of all defined hid reports in bytes (including report id) */
  146. #define MAX_REPORT_SIZE 8
  147. /* Number of possible hid report types that can be created by this driver.
  148. *
  149. * Right now, RF report types have the same report types (or report id's)
  150. * than the hid report created from those RF reports. In the future
  151. * this doesnt have to be true.
  152. *
  153. * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
  154. * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
  155. * reports and consumer control, etc. If a new RF report is created, it doesn't
  156. * has to have the same report id as its corresponding hid report, so an
  157. * translation may have to take place for future report types.
  158. */
  159. #define NUMBER_OF_HID_REPORTS 32
  160. static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
  161. [1] = 8, /* Standard keyboard */
  162. [2] = 8, /* Standard mouse */
  163. [3] = 5, /* Consumer control */
  164. [4] = 2, /* System control */
  165. [8] = 2, /* Media Center */
  166. };
  167. #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
  168. static struct hid_ll_driver logi_dj_ll_driver;
  169. static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
  170. size_t count,
  171. unsigned char report_type);
  172. static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
  173. struct dj_report *dj_report)
  174. {
  175. /* Called in delayed work context */
  176. struct dj_device *dj_dev;
  177. unsigned long flags;
  178. spin_lock_irqsave(&djrcv_dev->lock, flags);
  179. dj_dev = djrcv_dev->paired_dj_devices[dj_report->device_index];
  180. djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
  181. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  182. if (dj_dev != NULL) {
  183. hid_destroy_device(dj_dev->hdev);
  184. kfree(dj_dev);
  185. } else {
  186. dev_err(&djrcv_dev->hdev->dev, "%s: can't destroy a NULL device\n",
  187. __func__);
  188. }
  189. }
  190. static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
  191. struct dj_report *dj_report)
  192. {
  193. /* Called in delayed work context */
  194. struct hid_device *djrcv_hdev = djrcv_dev->hdev;
  195. struct usb_interface *intf = to_usb_interface(djrcv_hdev->dev.parent);
  196. struct usb_device *usbdev = interface_to_usbdev(intf);
  197. struct hid_device *dj_hiddev;
  198. struct dj_device *dj_dev;
  199. /* Device index goes from 1 to 6, we need 3 bytes to store the
  200. * semicolon, the index, and a null terminator
  201. */
  202. unsigned char tmpstr[3];
  203. if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
  204. SPFUNCTION_DEVICE_LIST_EMPTY) {
  205. dbg_hid("%s: device list is empty\n", __func__);
  206. return;
  207. }
  208. if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
  209. (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
  210. dev_err(&djrcv_hdev->dev, "%s: invalid device index:%d\n",
  211. __func__, dj_report->device_index);
  212. return;
  213. }
  214. dj_hiddev = hid_allocate_device();
  215. if (IS_ERR(dj_hiddev)) {
  216. dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n",
  217. __func__);
  218. return;
  219. }
  220. dj_hiddev->ll_driver = &logi_dj_ll_driver;
  221. dj_hiddev->hid_output_raw_report = logi_dj_output_hidraw_report;
  222. dj_hiddev->dev.parent = &djrcv_hdev->dev;
  223. dj_hiddev->bus = BUS_USB;
  224. dj_hiddev->vendor = le16_to_cpu(usbdev->descriptor.idVendor);
  225. dj_hiddev->product = le16_to_cpu(usbdev->descriptor.idProduct);
  226. snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
  227. "Logitech Unifying Device. Wireless PID:%02x%02x",
  228. dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB],
  229. dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]);
  230. usb_make_path(usbdev, dj_hiddev->phys, sizeof(dj_hiddev->phys));
  231. snprintf(tmpstr, sizeof(tmpstr), ":%d", dj_report->device_index);
  232. strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
  233. dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
  234. if (!dj_dev) {
  235. dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n",
  236. __func__);
  237. goto dj_device_allocate_fail;
  238. }
  239. dj_dev->reports_supported = le32_to_cpu(
  240. dj_report->report_params[DEVICE_PAIRED_RF_REPORT_TYPE]);
  241. dj_dev->hdev = dj_hiddev;
  242. dj_dev->dj_receiver_dev = djrcv_dev;
  243. dj_dev->device_index = dj_report->device_index;
  244. dj_hiddev->driver_data = dj_dev;
  245. djrcv_dev->paired_dj_devices[dj_report->device_index] = dj_dev;
  246. if (hid_add_device(dj_hiddev)) {
  247. dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n",
  248. __func__);
  249. goto hid_add_device_fail;
  250. }
  251. return;
  252. hid_add_device_fail:
  253. djrcv_dev->paired_dj_devices[dj_report->device_index] = NULL;
  254. kfree(dj_dev);
  255. dj_device_allocate_fail:
  256. hid_destroy_device(dj_hiddev);
  257. }
  258. static void delayedwork_callback(struct work_struct *work)
  259. {
  260. struct dj_receiver_dev *djrcv_dev =
  261. container_of(work, struct dj_receiver_dev, work);
  262. struct dj_report dj_report;
  263. unsigned long flags;
  264. int count;
  265. dbg_hid("%s\n", __func__);
  266. spin_lock_irqsave(&djrcv_dev->lock, flags);
  267. count = kfifo_out(&djrcv_dev->notif_fifo, &dj_report,
  268. sizeof(struct dj_report));
  269. if (count != sizeof(struct dj_report)) {
  270. dev_err(&djrcv_dev->hdev->dev, "%s: workitem triggered without "
  271. "notifications available\n", __func__);
  272. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  273. return;
  274. }
  275. if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) {
  276. if (schedule_work(&djrcv_dev->work) == 0) {
  277. dbg_hid("%s: did not schedule the work item, was "
  278. "already queued\n", __func__);
  279. }
  280. }
  281. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  282. switch (dj_report.report_type) {
  283. case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
  284. logi_dj_recv_add_djhid_device(djrcv_dev, &dj_report);
  285. break;
  286. case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
  287. logi_dj_recv_destroy_djhid_device(djrcv_dev, &dj_report);
  288. break;
  289. default:
  290. dbg_hid("%s: unexpected report type\n", __func__);
  291. }
  292. }
  293. static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
  294. struct dj_report *dj_report)
  295. {
  296. /* We are called from atomic context (tasklet && djrcv->lock held) */
  297. kfifo_in(&djrcv_dev->notif_fifo, dj_report, sizeof(struct dj_report));
  298. if (schedule_work(&djrcv_dev->work) == 0) {
  299. dbg_hid("%s: did not schedule the work item, was already "
  300. "queued\n", __func__);
  301. }
  302. }
  303. static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
  304. struct dj_report *dj_report)
  305. {
  306. /* We are called from atomic context (tasklet && djrcv->lock held) */
  307. unsigned int i;
  308. u8 reportbuffer[MAX_REPORT_SIZE];
  309. struct dj_device *djdev;
  310. djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
  311. if (!djdev) {
  312. dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
  313. " is NULL, index %d\n", dj_report->device_index);
  314. return;
  315. }
  316. memset(reportbuffer, 0, sizeof(reportbuffer));
  317. for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
  318. if (djdev->reports_supported & (1 << i)) {
  319. reportbuffer[0] = i;
  320. if (hid_input_report(djdev->hdev,
  321. HID_INPUT_REPORT,
  322. reportbuffer,
  323. hid_reportid_size_map[i], 1)) {
  324. dbg_hid("hid_input_report error sending null "
  325. "report\n");
  326. }
  327. }
  328. }
  329. }
  330. static void logi_dj_recv_forward_report(struct dj_receiver_dev *djrcv_dev,
  331. struct dj_report *dj_report)
  332. {
  333. /* We are called from atomic context (tasklet && djrcv->lock held) */
  334. struct dj_device *dj_device;
  335. dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
  336. if (dj_device == NULL) {
  337. dbg_hid("djrcv_dev->paired_dj_devices[dj_report->device_index]"
  338. " is NULL, index %d\n", dj_report->device_index);
  339. return;
  340. }
  341. if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
  342. (hid_reportid_size_map[dj_report->report_type] == 0)) {
  343. dbg_hid("invalid report type:%x\n", dj_report->report_type);
  344. return;
  345. }
  346. if (hid_input_report(dj_device->hdev,
  347. HID_INPUT_REPORT, &dj_report->report_type,
  348. hid_reportid_size_map[dj_report->report_type], 1)) {
  349. dbg_hid("hid_input_report error\n");
  350. }
  351. }
  352. static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
  353. struct dj_report *dj_report)
  354. {
  355. struct hid_device *hdev = djrcv_dev->hdev;
  356. int sent_bytes;
  357. if (!hdev->hid_output_raw_report) {
  358. dev_err(&hdev->dev, "%s:"
  359. "hid_output_raw_report is null\n", __func__);
  360. return -ENODEV;
  361. }
  362. sent_bytes = hdev->hid_output_raw_report(hdev, (u8 *) dj_report,
  363. sizeof(struct dj_report),
  364. HID_OUTPUT_REPORT);
  365. return (sent_bytes < 0) ? sent_bytes : 0;
  366. }
  367. static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
  368. {
  369. struct dj_report dj_report;
  370. memset(&dj_report, 0, sizeof(dj_report));
  371. dj_report.report_id = REPORT_ID_DJ_SHORT;
  372. dj_report.device_index = 0xFF;
  373. dj_report.report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
  374. return logi_dj_recv_send_report(djrcv_dev, &dj_report);
  375. }
  376. static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
  377. unsigned timeout)
  378. {
  379. struct dj_report dj_report;
  380. memset(&dj_report, 0, sizeof(dj_report));
  381. dj_report.report_id = REPORT_ID_DJ_SHORT;
  382. dj_report.device_index = 0xFF;
  383. dj_report.report_type = REPORT_TYPE_CMD_SWITCH;
  384. dj_report.report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
  385. dj_report.report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout;
  386. return logi_dj_recv_send_report(djrcv_dev, &dj_report);
  387. }
  388. static int logi_dj_ll_open(struct hid_device *hid)
  389. {
  390. dbg_hid("%s:%s\n", __func__, hid->phys);
  391. return 0;
  392. }
  393. static void logi_dj_ll_close(struct hid_device *hid)
  394. {
  395. dbg_hid("%s:%s\n", __func__, hid->phys);
  396. }
  397. static int logi_dj_output_hidraw_report(struct hid_device *hid, u8 * buf,
  398. size_t count,
  399. unsigned char report_type)
  400. {
  401. /* Called by hid raw to send data */
  402. dbg_hid("%s\n", __func__);
  403. return 0;
  404. }
  405. static int logi_dj_ll_parse(struct hid_device *hid)
  406. {
  407. struct dj_device *djdev = hid->driver_data;
  408. int retval;
  409. dbg_hid("%s\n", __func__);
  410. djdev->hdev->version = 0x0111;
  411. djdev->hdev->country = 0x00;
  412. if (djdev->reports_supported & STD_KEYBOARD) {
  413. dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n",
  414. __func__, djdev->reports_supported);
  415. retval = hid_parse_report(hid,
  416. (u8 *) kbd_descriptor,
  417. sizeof(kbd_descriptor));
  418. if (retval) {
  419. dbg_hid("%s: sending a kbd descriptor, hid_parse failed"
  420. " error: %d\n", __func__, retval);
  421. return retval;
  422. }
  423. }
  424. if (djdev->reports_supported & STD_MOUSE) {
  425. dbg_hid("%s: sending a mouse descriptor, reports_supported: "
  426. "%x\n", __func__, djdev->reports_supported);
  427. retval = hid_parse_report(hid,
  428. (u8 *) mse_descriptor,
  429. sizeof(mse_descriptor));
  430. if (retval) {
  431. dbg_hid("%s: sending a mouse descriptor, hid_parse "
  432. "failed error: %d\n", __func__, retval);
  433. return retval;
  434. }
  435. }
  436. if (djdev->reports_supported & MULTIMEDIA) {
  437. dbg_hid("%s: sending a multimedia report descriptor: %x\n",
  438. __func__, djdev->reports_supported);
  439. retval = hid_parse_report(hid,
  440. (u8 *) consumer_descriptor,
  441. sizeof(consumer_descriptor));
  442. if (retval) {
  443. dbg_hid("%s: sending a consumer_descriptor, hid_parse "
  444. "failed error: %d\n", __func__, retval);
  445. return retval;
  446. }
  447. }
  448. if (djdev->reports_supported & POWER_KEYS) {
  449. dbg_hid("%s: sending a power keys report descriptor: %x\n",
  450. __func__, djdev->reports_supported);
  451. retval = hid_parse_report(hid,
  452. (u8 *) syscontrol_descriptor,
  453. sizeof(syscontrol_descriptor));
  454. if (retval) {
  455. dbg_hid("%s: sending a syscontrol_descriptor, "
  456. "hid_parse failed error: %d\n",
  457. __func__, retval);
  458. return retval;
  459. }
  460. }
  461. if (djdev->reports_supported & MEDIA_CENTER) {
  462. dbg_hid("%s: sending a media center report descriptor: %x\n",
  463. __func__, djdev->reports_supported);
  464. retval = hid_parse_report(hid,
  465. (u8 *) media_descriptor,
  466. sizeof(media_descriptor));
  467. if (retval) {
  468. dbg_hid("%s: sending a media_descriptor, hid_parse "
  469. "failed error: %d\n", __func__, retval);
  470. return retval;
  471. }
  472. }
  473. if (djdev->reports_supported & KBD_LEDS) {
  474. dbg_hid("%s: need to send kbd leds report descriptor: %x\n",
  475. __func__, djdev->reports_supported);
  476. }
  477. return 0;
  478. }
  479. static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
  480. unsigned int code, int value)
  481. {
  482. /* Sent by the input layer to handle leds and Force Feedback */
  483. struct hid_device *dj_hiddev = input_get_drvdata(dev);
  484. struct dj_device *dj_dev = dj_hiddev->driver_data;
  485. struct dj_receiver_dev *djrcv_dev =
  486. dev_get_drvdata(dj_hiddev->dev.parent);
  487. struct hid_device *dj_rcv_hiddev = djrcv_dev->hdev;
  488. struct hid_report_enum *output_report_enum;
  489. struct hid_field *field;
  490. struct hid_report *report;
  491. unsigned char data[8];
  492. int offset;
  493. dbg_hid("%s: %s, type:%d | code:%d | value:%d\n",
  494. __func__, dev->phys, type, code, value);
  495. if (type != EV_LED)
  496. return -1;
  497. offset = hidinput_find_field(dj_hiddev, type, code, &field);
  498. if (offset == -1) {
  499. dev_warn(&dev->dev, "event field not found\n");
  500. return -1;
  501. }
  502. hid_set_field(field, offset, value);
  503. hid_output_report(field->report, &data[0]);
  504. output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT];
  505. report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
  506. hid_set_field(report->field[0], 0, dj_dev->device_index);
  507. hid_set_field(report->field[0], 1, REPORT_TYPE_LEDS);
  508. hid_set_field(report->field[0], 2, data[1]);
  509. usbhid_submit_report(dj_rcv_hiddev, report, USB_DIR_OUT);
  510. return 0;
  511. }
  512. static int logi_dj_ll_start(struct hid_device *hid)
  513. {
  514. dbg_hid("%s\n", __func__);
  515. return 0;
  516. }
  517. static void logi_dj_ll_stop(struct hid_device *hid)
  518. {
  519. dbg_hid("%s\n", __func__);
  520. }
  521. static struct hid_ll_driver logi_dj_ll_driver = {
  522. .parse = logi_dj_ll_parse,
  523. .start = logi_dj_ll_start,
  524. .stop = logi_dj_ll_stop,
  525. .open = logi_dj_ll_open,
  526. .close = logi_dj_ll_close,
  527. .hidinput_input_event = logi_dj_ll_input_event,
  528. };
  529. static int logi_dj_raw_event(struct hid_device *hdev,
  530. struct hid_report *report, u8 *data,
  531. int size)
  532. {
  533. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  534. struct dj_report *dj_report = (struct dj_report *) data;
  535. unsigned long flags;
  536. bool report_processed = false;
  537. dbg_hid("%s, size:%d\n", __func__, size);
  538. /* Here we receive all data coming from iface 2, there are 4 cases:
  539. *
  540. * 1) Data should continue its normal processing i.e. data does not
  541. * come from the DJ collection, in which case we do nothing and
  542. * return 0, so hid-core can continue normal processing (will forward
  543. * to associated hidraw device)
  544. *
  545. * 2) Data is from DJ collection, and is intended for this driver i. e.
  546. * data contains arrival, departure, etc notifications, in which case
  547. * we queue them for delayed processing by the work queue. We return 1
  548. * to hid-core as no further processing is required from it.
  549. *
  550. * 3) Data is from DJ collection, and informs a connection change,
  551. * if the change means rf link loss, then we must send a null report
  552. * to the upper layer to discard potentially pressed keys that may be
  553. * repeated forever by the input layer. Return 1 to hid-core as no
  554. * further processing is required.
  555. *
  556. * 4) Data is from DJ collection and is an actual input event from
  557. * a paired DJ device in which case we forward it to the correct hid
  558. * device (via hid_input_report() ) and return 1 so hid-core does not do
  559. * anything else with it.
  560. */
  561. spin_lock_irqsave(&djrcv_dev->lock, flags);
  562. if (dj_report->report_id == REPORT_ID_DJ_SHORT) {
  563. switch (dj_report->report_type) {
  564. case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
  565. case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
  566. logi_dj_recv_queue_notification(djrcv_dev, dj_report);
  567. break;
  568. case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
  569. if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
  570. STATUS_LINKLOSS) {
  571. logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
  572. }
  573. break;
  574. default:
  575. logi_dj_recv_forward_report(djrcv_dev, dj_report);
  576. }
  577. report_processed = true;
  578. }
  579. spin_unlock_irqrestore(&djrcv_dev->lock, flags);
  580. return report_processed;
  581. }
  582. static int logi_dj_probe(struct hid_device *hdev,
  583. const struct hid_device_id *id)
  584. {
  585. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  586. struct dj_receiver_dev *djrcv_dev;
  587. int retval;
  588. if (is_dj_device((struct dj_device *)hdev->driver_data))
  589. return -ENODEV;
  590. dbg_hid("%s called for ifnum %d\n", __func__,
  591. intf->cur_altsetting->desc.bInterfaceNumber);
  592. /* Ignore interfaces 0 and 1, they will not carry any data, dont create
  593. * any hid_device for them */
  594. if (intf->cur_altsetting->desc.bInterfaceNumber !=
  595. LOGITECH_DJ_INTERFACE_NUMBER) {
  596. dbg_hid("%s: ignoring ifnum %d\n", __func__,
  597. intf->cur_altsetting->desc.bInterfaceNumber);
  598. return -ENODEV;
  599. }
  600. /* Treat interface 2 */
  601. djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL);
  602. if (!djrcv_dev) {
  603. dev_err(&hdev->dev,
  604. "%s:failed allocating dj_receiver_dev\n", __func__);
  605. return -ENOMEM;
  606. }
  607. djrcv_dev->hdev = hdev;
  608. INIT_WORK(&djrcv_dev->work, delayedwork_callback);
  609. spin_lock_init(&djrcv_dev->lock);
  610. if (kfifo_alloc(&djrcv_dev->notif_fifo,
  611. DJ_MAX_NUMBER_NOTIFICATIONS * sizeof(struct dj_report),
  612. GFP_KERNEL)) {
  613. dev_err(&hdev->dev,
  614. "%s:failed allocating notif_fifo\n", __func__);
  615. kfree(djrcv_dev);
  616. return -ENOMEM;
  617. }
  618. hid_set_drvdata(hdev, djrcv_dev);
  619. /* Call to usbhid to fetch the HID descriptors of interface 2 and
  620. * subsequently call to the hid/hid-core to parse the fetched
  621. * descriptors, this will in turn create the hidraw and hiddev nodes
  622. * for interface 2 of the receiver */
  623. retval = hid_parse(hdev);
  624. if (retval) {
  625. dev_err(&hdev->dev,
  626. "%s:parse of interface 2 failed\n", __func__);
  627. goto hid_parse_fail;
  628. }
  629. /* Starts the usb device and connects to upper interfaces hiddev and
  630. * hidraw */
  631. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  632. if (retval) {
  633. dev_err(&hdev->dev,
  634. "%s:hid_hw_start returned error\n", __func__);
  635. goto hid_hw_start_fail;
  636. }
  637. retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
  638. if (retval < 0) {
  639. dev_err(&hdev->dev,
  640. "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
  641. __func__, retval);
  642. goto switch_to_dj_mode_fail;
  643. }
  644. /* This is enabling the polling urb on the IN endpoint */
  645. retval = hdev->ll_driver->open(hdev);
  646. if (retval < 0) {
  647. dev_err(&hdev->dev, "%s:hdev->ll_driver->open returned "
  648. "error:%d\n", __func__, retval);
  649. goto llopen_failed;
  650. }
  651. retval = logi_dj_recv_query_paired_devices(djrcv_dev);
  652. if (retval < 0) {
  653. dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices "
  654. "error:%d\n", __func__, retval);
  655. goto logi_dj_recv_query_paired_devices_failed;
  656. }
  657. return retval;
  658. logi_dj_recv_query_paired_devices_failed:
  659. hdev->ll_driver->close(hdev);
  660. llopen_failed:
  661. switch_to_dj_mode_fail:
  662. hid_hw_stop(hdev);
  663. hid_hw_start_fail:
  664. hid_parse_fail:
  665. kfifo_free(&djrcv_dev->notif_fifo);
  666. kfree(djrcv_dev);
  667. hid_set_drvdata(hdev, NULL);
  668. return retval;
  669. }
  670. #ifdef CONFIG_PM
  671. static int logi_dj_reset_resume(struct hid_device *hdev)
  672. {
  673. int retval;
  674. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  675. retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
  676. if (retval < 0) {
  677. dev_err(&hdev->dev,
  678. "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n",
  679. __func__, retval);
  680. }
  681. return 0;
  682. }
  683. #endif
  684. static void logi_dj_remove(struct hid_device *hdev)
  685. {
  686. struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
  687. struct dj_device *dj_dev;
  688. int i;
  689. dbg_hid("%s\n", __func__);
  690. cancel_work_sync(&djrcv_dev->work);
  691. hdev->ll_driver->close(hdev);
  692. hid_hw_stop(hdev);
  693. /* I suppose that at this point the only context that can access
  694. * the djrecv_data is this thread as the work item is guaranteed to
  695. * have finished and no more raw_event callbacks should arrive after
  696. * the remove callback was triggered so no locks are put around the
  697. * code below */
  698. for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
  699. dj_dev = djrcv_dev->paired_dj_devices[i];
  700. if (dj_dev != NULL) {
  701. hid_destroy_device(dj_dev->hdev);
  702. kfree(dj_dev);
  703. djrcv_dev->paired_dj_devices[i] = NULL;
  704. }
  705. }
  706. kfifo_free(&djrcv_dev->notif_fifo);
  707. kfree(djrcv_dev);
  708. hid_set_drvdata(hdev, NULL);
  709. }
  710. static int logi_djdevice_probe(struct hid_device *hdev,
  711. const struct hid_device_id *id)
  712. {
  713. int ret;
  714. struct dj_device *dj_dev = hdev->driver_data;
  715. if (!is_dj_device(dj_dev))
  716. return -ENODEV;
  717. ret = hid_parse(hdev);
  718. if (!ret)
  719. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  720. return ret;
  721. }
  722. static const struct hid_device_id logi_dj_receivers[] = {
  723. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  724. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
  725. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  726. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
  727. {}
  728. };
  729. MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
  730. static struct hid_driver logi_djreceiver_driver = {
  731. .name = "logitech-djreceiver",
  732. .id_table = logi_dj_receivers,
  733. .probe = logi_dj_probe,
  734. .remove = logi_dj_remove,
  735. .raw_event = logi_dj_raw_event,
  736. #ifdef CONFIG_PM
  737. .reset_resume = logi_dj_reset_resume,
  738. #endif
  739. };
  740. static const struct hid_device_id logi_dj_devices[] = {
  741. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  742. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)},
  743. {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
  744. USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)},
  745. {}
  746. };
  747. static struct hid_driver logi_djdevice_driver = {
  748. .name = "logitech-djdevice",
  749. .id_table = logi_dj_devices,
  750. .probe = logi_djdevice_probe,
  751. };
  752. static int __init logi_dj_init(void)
  753. {
  754. int retval;
  755. dbg_hid("Logitech-DJ:%s\n", __func__);
  756. retval = hid_register_driver(&logi_djreceiver_driver);
  757. if (retval)
  758. return retval;
  759. retval = hid_register_driver(&logi_djdevice_driver);
  760. if (retval)
  761. hid_unregister_driver(&logi_djreceiver_driver);
  762. return retval;
  763. }
  764. static void __exit logi_dj_exit(void)
  765. {
  766. dbg_hid("Logitech-DJ:%s\n", __func__);
  767. hid_unregister_driver(&logi_djdevice_driver);
  768. hid_unregister_driver(&logi_djreceiver_driver);
  769. }
  770. module_init(logi_dj_init);
  771. module_exit(logi_dj_exit);
  772. MODULE_LICENSE("GPL");
  773. MODULE_AUTHOR("Logitech");
  774. MODULE_AUTHOR("Nestor Lopez Casado");
  775. MODULE_AUTHOR("nlopezcasad@logitech.com");