pd-main.c 12 KB

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