pd-main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * device driver for Telegent tlg2300 based TV cards
  3. *
  4. * Author :
  5. * Kang Yong <kangyong@telegent.com>
  6. * Zhang Xiaobing <xbzhang@telegent.com>
  7. * Huang Shijie <zyziii@telegent.com> or <shijie8@gmail.com>
  8. *
  9. * (c) 2009 Telegent Systems
  10. * (c) 2010 Telegent Systems
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/module.h>
  31. #include <linux/kref.h>
  32. #include <linux/suspend.h>
  33. #include <linux/usb/quirks.h>
  34. #include <linux/ctype.h>
  35. #include <linux/string.h>
  36. #include <linux/types.h>
  37. #include <linux/firmware.h>
  38. #include "vendorcmds.h"
  39. #include "pd-common.h"
  40. #define VENDOR_ID 0x1B24
  41. #define PRODUCT_ID 0x4001
  42. static struct usb_device_id id_table[] = {
  43. { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 0) },
  44. { USB_DEVICE_AND_INTERFACE_INFO(VENDOR_ID, PRODUCT_ID, 255, 1, 1) },
  45. { },
  46. };
  47. MODULE_DEVICE_TABLE(usb, id_table);
  48. int debug_mode;
  49. module_param(debug_mode, int, 0644);
  50. MODULE_PARM_DESC(debug_mode, "0 = disable, 1 = enable, 2 = verbose");
  51. static const char *firmware_name = "tlg2300_firmware.bin";
  52. static struct usb_driver poseidon_driver;
  53. static LIST_HEAD(pd_device_list);
  54. /*
  55. * send set request to USB firmware.
  56. */
  57. s32 send_set_req(struct poseidon *pd, u8 cmdid, s32 param, s32 *cmd_status)
  58. {
  59. s32 ret;
  60. s8 data[32] = {};
  61. u16 lower_16, upper_16;
  62. if (pd->state & POSEIDON_STATE_DISCONNECT)
  63. return -ENODEV;
  64. mdelay(30);
  65. if (param == 0) {
  66. upper_16 = lower_16 = 0;
  67. } else {
  68. /* send 32 bit param as two 16 bit param,little endian */
  69. lower_16 = (unsigned short)(param & 0xffff);
  70. upper_16 = (unsigned short)((param >> 16) & 0xffff);
  71. }
  72. ret = usb_control_msg(pd->udev,
  73. usb_rcvctrlpipe(pd->udev, 0),
  74. REQ_SET_CMD | cmdid,
  75. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  76. lower_16,
  77. upper_16,
  78. &data,
  79. sizeof(*cmd_status),
  80. USB_CTRL_GET_TIMEOUT);
  81. if (!ret) {
  82. return -ENXIO;
  83. } else {
  84. /* 1st 4 bytes into cmd_status */
  85. memcpy((char *)cmd_status, &(data[0]), sizeof(*cmd_status));
  86. }
  87. return 0;
  88. }
  89. /*
  90. * send get request to Poseidon firmware.
  91. */
  92. s32 send_get_req(struct poseidon *pd, u8 cmdid, s32 param,
  93. void *buf, s32 *cmd_status, s32 datalen)
  94. {
  95. s32 ret;
  96. s8 data[128] = {};
  97. u16 lower_16, upper_16;
  98. if (pd->state & POSEIDON_STATE_DISCONNECT)
  99. return -ENODEV;
  100. mdelay(30);
  101. if (param == 0) {
  102. upper_16 = lower_16 = 0;
  103. } else {
  104. /*send 32 bit param as two 16 bit param, little endian */
  105. lower_16 = (unsigned short)(param & 0xffff);
  106. upper_16 = (unsigned short)((param >> 16) & 0xffff);
  107. }
  108. ret = usb_control_msg(pd->udev,
  109. usb_rcvctrlpipe(pd->udev, 0),
  110. REQ_GET_CMD | cmdid,
  111. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  112. lower_16,
  113. upper_16,
  114. &data,
  115. (datalen + sizeof(*cmd_status)),
  116. USB_CTRL_GET_TIMEOUT);
  117. if (ret < 0) {
  118. return -ENXIO;
  119. } else {
  120. /* 1st 4 bytes into cmd_status, remaining data into cmd_data */
  121. memcpy((char *)cmd_status, &data[0], sizeof(*cmd_status));
  122. memcpy((char *)buf, &data[sizeof(*cmd_status)], datalen);
  123. }
  124. return 0;
  125. }
  126. static int pm_notifier_block(struct notifier_block *nb,
  127. unsigned long event, void *dummy)
  128. {
  129. struct poseidon *pd = NULL;
  130. struct list_head *node, *next;
  131. switch (event) {
  132. case PM_POST_HIBERNATION:
  133. list_for_each_safe(node, next, &pd_device_list) {
  134. struct usb_device *udev;
  135. struct usb_interface *iface;
  136. int rc = 0;
  137. pd = container_of(node, struct poseidon, device_list);
  138. udev = pd->udev;
  139. iface = pd->interface;
  140. /* It will cause the system to reload the firmware */
  141. rc = usb_lock_device_for_reset(udev, iface);
  142. if (rc >= 0) {
  143. usb_reset_device(udev);
  144. usb_unlock_device(udev);
  145. }
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. log("event :%ld\n", event);
  152. return 0;
  153. }
  154. static struct notifier_block pm_notifer = {
  155. .notifier_call = pm_notifier_block,
  156. };
  157. int set_tuner_mode(struct poseidon *pd, unsigned char mode)
  158. {
  159. s32 ret, cmd_status;
  160. if (pd->state & POSEIDON_STATE_DISCONNECT)
  161. return -ENODEV;
  162. ret = send_set_req(pd, TUNE_MODE_SELECT, mode, &cmd_status);
  163. if (ret || cmd_status)
  164. return -ENXIO;
  165. return 0;
  166. }
  167. void poseidon_delete(struct kref *kref)
  168. {
  169. struct poseidon *pd = container_of(kref, struct poseidon, kref);
  170. if (!pd)
  171. return;
  172. list_del_init(&pd->device_list);
  173. pd_dvb_usb_device_cleanup(pd);
  174. /* clean_audio_data(&pd->audio_data);*/
  175. if (pd->udev) {
  176. usb_put_dev(pd->udev);
  177. pd->udev = NULL;
  178. }
  179. if (pd->interface) {
  180. usb_put_intf(pd->interface);
  181. pd->interface = NULL;
  182. }
  183. kfree(pd);
  184. log();
  185. }
  186. static int firmware_download(struct usb_device *udev)
  187. {
  188. int ret = 0, actual_length;
  189. const struct firmware *fw = NULL;
  190. void *fwbuf = NULL;
  191. size_t fwlength = 0, offset;
  192. size_t max_packet_size;
  193. ret = request_firmware(&fw, firmware_name, &udev->dev);
  194. if (ret) {
  195. log("download err : %d", ret);
  196. return ret;
  197. }
  198. fwlength = fw->size;
  199. fwbuf = kmemdup(fw->data, fwlength, GFP_KERNEL);
  200. if (!fwbuf) {
  201. ret = -ENOMEM;
  202. goto out;
  203. }
  204. max_packet_size = udev->ep_out[0x1]->desc.wMaxPacketSize;
  205. log("\t\t download size : %d", (int)max_packet_size);
  206. for (offset = 0; offset < fwlength; offset += max_packet_size) {
  207. actual_length = 0;
  208. ret = usb_bulk_msg(udev,
  209. usb_sndbulkpipe(udev, 0x01), /* ep 1 */
  210. fwbuf + offset,
  211. min(max_packet_size, fwlength - offset),
  212. &actual_length,
  213. HZ * 10);
  214. if (ret)
  215. break;
  216. }
  217. kfree(fwbuf);
  218. out:
  219. release_firmware(fw);
  220. return ret;
  221. }
  222. static inline struct poseidon *get_pd(struct usb_interface *intf)
  223. {
  224. return usb_get_intfdata(intf);
  225. }
  226. #ifdef CONFIG_PM
  227. /* one-to-one map : poseidon{} <----> usb_device{}'s port */
  228. static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
  229. {
  230. pd->portnum = udev->portnum;
  231. }
  232. static inline int get_autopm_ref(struct poseidon *pd)
  233. {
  234. return pd->video_data.users + pd->vbi_data.users + pd->audio.users
  235. + atomic_read(&pd->dvb_data.users) + pd->radio_data.users;
  236. }
  237. /* fixup something for poseidon */
  238. static inline struct poseidon *fixup(struct poseidon *pd)
  239. {
  240. int count;
  241. /* old udev and interface have gone, so put back reference . */
  242. count = get_autopm_ref(pd);
  243. log("count : %d, ref count : %d", count, get_pm_count(pd));
  244. while (count--)
  245. usb_autopm_put_interface(pd->interface);
  246. /*usb_autopm_set_interface(pd->interface); */
  247. usb_put_dev(pd->udev);
  248. usb_put_intf(pd->interface);
  249. log("event : %d\n", pd->msg.event);
  250. return pd;
  251. }
  252. static struct poseidon *find_old_poseidon(struct usb_device *udev)
  253. {
  254. struct poseidon *pd;
  255. list_for_each_entry(pd, &pd_device_list, device_list) {
  256. if (pd->portnum == udev->portnum && in_hibernation(pd))
  257. return fixup(pd);
  258. }
  259. return NULL;
  260. }
  261. /* Is the card working now ? */
  262. static inline int is_working(struct poseidon *pd)
  263. {
  264. return get_pm_count(pd) > 0;
  265. }
  266. static int poseidon_suspend(struct usb_interface *intf, pm_message_t msg)
  267. {
  268. struct poseidon *pd = get_pd(intf);
  269. if (!pd)
  270. return 0;
  271. if (!is_working(pd)) {
  272. if (get_pm_count(pd) <= 0 && !in_hibernation(pd)) {
  273. pd->msg.event = PM_EVENT_AUTO_SUSPEND;
  274. pd->pm_resume = NULL; /* a good guard */
  275. printk(KERN_DEBUG "\n\t+ TLG2300 auto suspend +\n\n");
  276. }
  277. return 0;
  278. }
  279. pd->msg = msg; /* save it here */
  280. logpm(pd);
  281. return pd->pm_suspend ? pd->pm_suspend(pd) : 0;
  282. }
  283. static int poseidon_resume(struct usb_interface *intf)
  284. {
  285. struct poseidon *pd = get_pd(intf);
  286. if (!pd)
  287. return 0;
  288. printk(KERN_DEBUG "\n\t ++ TLG2300 resume ++\n\n");
  289. if (!is_working(pd)) {
  290. if (PM_EVENT_AUTO_SUSPEND == pd->msg.event)
  291. pd->msg = PMSG_ON;
  292. return 0;
  293. }
  294. if (in_hibernation(pd)) {
  295. logpm(pd);
  296. return 0;
  297. }
  298. logpm(pd);
  299. return pd->pm_resume ? pd->pm_resume(pd) : 0;
  300. }
  301. static void hibernation_resume(struct work_struct *w)
  302. {
  303. struct poseidon *pd = container_of(w, struct poseidon, pm_work);
  304. int count;
  305. pd->msg.event = 0; /* clear it here */
  306. pd->state &= ~POSEIDON_STATE_DISCONNECT;
  307. /* set the new interface's reference */
  308. count = get_autopm_ref(pd);
  309. while (count--)
  310. usb_autopm_get_interface(pd->interface);
  311. /* resume the context */
  312. logpm(pd);
  313. if (pd->pm_resume)
  314. pd->pm_resume(pd);
  315. }
  316. #else /* CONFIG_PM is not enabled: */
  317. static inline struct poseidon *find_old_poseidon(struct usb_device *udev)
  318. {
  319. return NULL;
  320. }
  321. static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
  322. {
  323. }
  324. #endif
  325. static int check_firmware(struct usb_device *udev, int *down_firmware)
  326. {
  327. void *buf;
  328. int ret;
  329. struct cmd_firmware_vers_s *cmd_firm;
  330. buf = kzalloc(sizeof(*cmd_firm) + sizeof(u32), GFP_KERNEL);
  331. if (!buf)
  332. return -ENOMEM;
  333. ret = usb_control_msg(udev,
  334. usb_rcvctrlpipe(udev, 0),
  335. REQ_GET_CMD | GET_FW_ID,
  336. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  337. 0,
  338. 0,
  339. buf,
  340. sizeof(*cmd_firm) + sizeof(u32),
  341. USB_CTRL_GET_TIMEOUT);
  342. kfree(buf);
  343. if (ret < 0) {
  344. *down_firmware = 1;
  345. return firmware_download(udev);
  346. }
  347. return 0;
  348. }
  349. static int poseidon_probe(struct usb_interface *interface,
  350. const struct usb_device_id *id)
  351. {
  352. struct usb_device *udev = interface_to_usbdev(interface);
  353. struct poseidon *pd = NULL;
  354. int ret = 0;
  355. int new_one = 0;
  356. /* download firmware */
  357. check_firmware(udev, &ret);
  358. if (ret)
  359. return 0;
  360. /* Do I recovery from the hibernate ? */
  361. pd = find_old_poseidon(udev);
  362. if (!pd) {
  363. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  364. if (!pd)
  365. return -ENOMEM;
  366. kref_init(&pd->kref);
  367. set_map_flags(pd, udev);
  368. new_one = 1;
  369. }
  370. pd->udev = usb_get_dev(udev);
  371. pd->interface = usb_get_intf(interface);
  372. usb_set_intfdata(interface, pd);
  373. if (new_one) {
  374. struct device *dev = &interface->dev;
  375. logpm(pd);
  376. mutex_init(&pd->lock);
  377. /* register v4l2 device */
  378. snprintf(pd->v4l2_dev.name, sizeof(pd->v4l2_dev.name), "%s %s",
  379. dev->driver->name, dev_name(dev));
  380. ret = v4l2_device_register(NULL, &pd->v4l2_dev);
  381. /* register devices in directory /dev */
  382. ret = pd_video_init(pd);
  383. poseidon_audio_init(pd);
  384. poseidon_fm_init(pd);
  385. pd_dvb_usb_device_init(pd);
  386. INIT_LIST_HEAD(&pd->device_list);
  387. list_add_tail(&pd->device_list, &pd_device_list);
  388. }
  389. device_init_wakeup(&udev->dev, 1);
  390. #ifdef CONFIG_PM
  391. pm_runtime_set_autosuspend_delay(&pd->udev->dev,
  392. 1000 * PM_SUSPEND_DELAY);
  393. usb_enable_autosuspend(pd->udev);
  394. if (in_hibernation(pd)) {
  395. INIT_WORK(&pd->pm_work, hibernation_resume);
  396. schedule_work(&pd->pm_work);
  397. }
  398. #endif
  399. return 0;
  400. }
  401. static void poseidon_disconnect(struct usb_interface *interface)
  402. {
  403. struct poseidon *pd = get_pd(interface);
  404. if (!pd)
  405. return;
  406. logpm(pd);
  407. if (in_hibernation(pd))
  408. return;
  409. mutex_lock(&pd->lock);
  410. pd->state |= POSEIDON_STATE_DISCONNECT;
  411. mutex_unlock(&pd->lock);
  412. /* stop urb transferring */
  413. stop_all_video_stream(pd);
  414. dvb_stop_streaming(&pd->dvb_data);
  415. /*unregister v4l2 device */
  416. v4l2_device_unregister(&pd->v4l2_dev);
  417. pd_dvb_usb_device_exit(pd);
  418. poseidon_fm_exit(pd);
  419. poseidon_audio_free(pd);
  420. pd_video_exit(pd);
  421. usb_set_intfdata(interface, NULL);
  422. kref_put(&pd->kref, poseidon_delete);
  423. }
  424. static struct usb_driver poseidon_driver = {
  425. .name = "poseidon",
  426. .probe = poseidon_probe,
  427. .disconnect = poseidon_disconnect,
  428. .id_table = id_table,
  429. #ifdef CONFIG_PM
  430. .suspend = poseidon_suspend,
  431. .resume = poseidon_resume,
  432. #endif
  433. .supports_autosuspend = 1,
  434. };
  435. static int __init poseidon_init(void)
  436. {
  437. int ret;
  438. ret = usb_register(&poseidon_driver);
  439. if (ret)
  440. return ret;
  441. register_pm_notifier(&pm_notifer);
  442. return ret;
  443. }
  444. static void __exit poseidon_exit(void)
  445. {
  446. log();
  447. unregister_pm_notifier(&pm_notifer);
  448. usb_deregister(&poseidon_driver);
  449. }
  450. module_init(poseidon_init);
  451. module_exit(poseidon_exit);
  452. MODULE_AUTHOR("Telegent Systems");
  453. MODULE_DESCRIPTION("For tlg2300-based USB device ");
  454. MODULE_LICENSE("GPL");
  455. MODULE_VERSION("0.0.2");