hid-logitech-dj.c 29 KB

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