hid-logitech-dj.c 28 KB

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