hid-logitech-dj.c 28 KB

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