pd-main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. enum tlg__analog_audio_standard get_audio_std(s32 mode, s32 country_code)
  170. {
  171. s32 nicam[] = {27, 32, 33, 34, 36, 44, 45, 46, 47, 48, 64,
  172. 65, 86, 351, 352, 353, 354, 358, 372, 852, 972};
  173. s32 btsc[] = {1, 52, 54, 55, 886};
  174. s32 eiaj[] = {81};
  175. s32 i;
  176. if (mode == TLG_MODE_FM_RADIO) {
  177. if (country_code == 1)
  178. return TLG_TUNE_ASTD_FM_US;
  179. else
  180. return TLG_TUNE_ASTD_FM_EUR;
  181. } else if (mode == TLG_MODE_ANALOG_TV_UNCOMP) {
  182. for (i = 0; i < sizeof(nicam) / sizeof(s32); i++) {
  183. if (country_code == nicam[i])
  184. return TLG_TUNE_ASTD_NICAM;
  185. }
  186. for (i = 0; i < sizeof(btsc) / sizeof(s32); i++) {
  187. if (country_code == btsc[i])
  188. return TLG_TUNE_ASTD_BTSC;
  189. }
  190. for (i = 0; i < sizeof(eiaj) / sizeof(s32); i++) {
  191. if (country_code == eiaj[i])
  192. return TLG_TUNE_ASTD_EIAJ;
  193. }
  194. return TLG_TUNE_ASTD_A2;
  195. } else {
  196. return TLG_TUNE_ASTD_NONE;
  197. }
  198. }
  199. void poseidon_delete(struct kref *kref)
  200. {
  201. struct poseidon *pd = container_of(kref, struct poseidon, kref);
  202. if (!pd)
  203. return;
  204. list_del_init(&pd->device_list);
  205. pd_dvb_usb_device_cleanup(pd);
  206. /* clean_audio_data(&pd->audio_data);*/
  207. if (pd->udev) {
  208. usb_put_dev(pd->udev);
  209. pd->udev = NULL;
  210. }
  211. if (pd->interface) {
  212. usb_put_intf(pd->interface);
  213. pd->interface = NULL;
  214. }
  215. kfree(pd);
  216. log();
  217. }
  218. static int firmware_download(struct usb_device *udev)
  219. {
  220. int ret = 0, actual_length;
  221. const struct firmware *fw = NULL;
  222. void *fwbuf = NULL;
  223. size_t fwlength = 0, offset;
  224. size_t max_packet_size;
  225. ret = request_firmware(&fw, firmware_name, &udev->dev);
  226. if (ret) {
  227. log("download err : %d", ret);
  228. return ret;
  229. }
  230. fwlength = fw->size;
  231. fwbuf = kzalloc(fwlength, GFP_KERNEL);
  232. if (!fwbuf) {
  233. ret = -ENOMEM;
  234. goto out;
  235. }
  236. memcpy(fwbuf, fw->data, fwlength);
  237. max_packet_size = udev->ep_out[0x1]->desc.wMaxPacketSize;
  238. log("\t\t download size : %d", (int)max_packet_size);
  239. for (offset = 0; offset < fwlength; offset += max_packet_size) {
  240. actual_length = 0;
  241. ret = usb_bulk_msg(udev,
  242. usb_sndbulkpipe(udev, 0x01), /* ep 1 */
  243. fwbuf + offset,
  244. min(max_packet_size, fwlength - offset),
  245. &actual_length,
  246. HZ * 10);
  247. if (ret)
  248. break;
  249. }
  250. kfree(fwbuf);
  251. out:
  252. release_firmware(fw);
  253. return ret;
  254. }
  255. #ifdef CONFIG_PM
  256. /* one-to-one map : poseidon{} <----> usb_device{}'s port */
  257. static inline void set_map_flags(struct poseidon *pd, struct usb_device *udev)
  258. {
  259. pd->portnum = udev->portnum;
  260. }
  261. static inline int get_autopm_ref(struct poseidon *pd)
  262. {
  263. return pd->video_data.users + pd->vbi_data.users + pd->audio.users
  264. + atomic_read(&pd->dvb_data.users) + pd->radio_data.users;
  265. }
  266. /* fixup something for poseidon */
  267. static inline struct poseidon *fixup(struct poseidon *pd)
  268. {
  269. int count;
  270. /* old udev and interface have gone, so put back reference . */
  271. count = get_autopm_ref(pd);
  272. log("count : %d, ref count : %d", count, get_pm_count(pd));
  273. while (count--)
  274. usb_autopm_put_interface(pd->interface);
  275. /*usb_autopm_set_interface(pd->interface); */
  276. usb_put_dev(pd->udev);
  277. usb_put_intf(pd->interface);
  278. log("event : %d\n", pd->msg.event);
  279. return pd;
  280. }
  281. static struct poseidon *find_old_poseidon(struct usb_device *udev)
  282. {
  283. struct poseidon *pd;
  284. list_for_each_entry(pd, &pd_device_list, device_list) {
  285. if (pd->portnum == udev->portnum && in_hibernation(pd))
  286. return fixup(pd);
  287. }
  288. return NULL;
  289. }
  290. /* Is the card working now ? */
  291. static inline int is_working(struct poseidon *pd)
  292. {
  293. return get_pm_count(pd) > 0;
  294. }
  295. static inline struct poseidon *get_pd(struct usb_interface *intf)
  296. {
  297. return usb_get_intfdata(intf);
  298. }
  299. static int poseidon_suspend(struct usb_interface *intf, pm_message_t msg)
  300. {
  301. struct poseidon *pd = get_pd(intf);
  302. if (!pd)
  303. return 0;
  304. if (!is_working(pd)) {
  305. if (get_pm_count(pd) <= 0 && !in_hibernation(pd)) {
  306. pd->msg.event = PM_EVENT_AUTO_SUSPEND;
  307. pd->pm_resume = NULL; /* a good guard */
  308. printk(KERN_DEBUG "\n\t+ TLG2300 auto suspend +\n\n");
  309. }
  310. return 0;
  311. }
  312. pd->msg = msg; /* save it here */
  313. logpm(pd);
  314. return pd->pm_suspend ? pd->pm_suspend(pd) : 0;
  315. }
  316. static int poseidon_resume(struct usb_interface *intf)
  317. {
  318. struct poseidon *pd = get_pd(intf);
  319. if (!pd)
  320. return 0;
  321. printk(KERN_DEBUG "\n\t ++ TLG2300 resume ++\n\n");
  322. if (!is_working(pd)) {
  323. if (PM_EVENT_AUTO_SUSPEND == pd->msg.event)
  324. pd->msg = PMSG_ON;
  325. return 0;
  326. }
  327. if (in_hibernation(pd)) {
  328. logpm(pd);
  329. return 0;
  330. }
  331. logpm(pd);
  332. return pd->pm_resume ? pd->pm_resume(pd) : 0;
  333. }
  334. static void hibernation_resume(struct work_struct *w)
  335. {
  336. struct poseidon *pd = container_of(w, struct poseidon, pm_work);
  337. int count;
  338. pd->msg.event = 0; /* clear it here */
  339. pd->state &= ~POSEIDON_STATE_DISCONNECT;
  340. /* set the new interface's reference */
  341. count = get_autopm_ref(pd);
  342. while (count--)
  343. usb_autopm_get_interface(pd->interface);
  344. /* resume the context */
  345. logpm(pd);
  346. if (pd->pm_resume)
  347. pd->pm_resume(pd);
  348. }
  349. #endif
  350. static bool check_firmware(struct usb_device *udev, int *down_firmware)
  351. {
  352. void *buf;
  353. int ret;
  354. struct cmd_firmware_vers_s *cmd_firm;
  355. buf = kzalloc(sizeof(*cmd_firm) + sizeof(u32), GFP_KERNEL);
  356. if (!buf)
  357. return -ENOMEM;
  358. ret = usb_control_msg(udev,
  359. usb_rcvctrlpipe(udev, 0),
  360. REQ_GET_CMD | GET_FW_ID,
  361. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  362. 0,
  363. 0,
  364. buf,
  365. sizeof(*cmd_firm) + sizeof(u32),
  366. USB_CTRL_GET_TIMEOUT);
  367. kfree(buf);
  368. if (ret < 0) {
  369. *down_firmware = 1;
  370. return firmware_download(udev);
  371. }
  372. return ret;
  373. }
  374. static int poseidon_probe(struct usb_interface *interface,
  375. const struct usb_device_id *id)
  376. {
  377. struct usb_device *udev = interface_to_usbdev(interface);
  378. struct poseidon *pd = NULL;
  379. int ret = 0;
  380. int new_one = 0;
  381. /* download firmware */
  382. check_firmware(udev, &ret);
  383. if (ret)
  384. return 0;
  385. /* Do I recovery from the hibernate ? */
  386. pd = find_old_poseidon(udev);
  387. if (!pd) {
  388. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  389. if (!pd)
  390. return -ENOMEM;
  391. kref_init(&pd->kref);
  392. set_map_flags(pd, udev);
  393. new_one = 1;
  394. }
  395. pd->udev = usb_get_dev(udev);
  396. pd->interface = usb_get_intf(interface);
  397. usb_set_intfdata(interface, pd);
  398. if (new_one) {
  399. struct device *dev = &interface->dev;
  400. logpm(pd);
  401. pd->country_code = 86;
  402. mutex_init(&pd->lock);
  403. /* register v4l2 device */
  404. snprintf(pd->v4l2_dev.name, sizeof(pd->v4l2_dev.name), "%s %s",
  405. dev->driver->name, dev_name(dev));
  406. ret = v4l2_device_register(NULL, &pd->v4l2_dev);
  407. /* register devices in directory /dev */
  408. ret = pd_video_init(pd);
  409. poseidon_audio_init(pd);
  410. poseidon_fm_init(pd);
  411. pd_dvb_usb_device_init(pd);
  412. INIT_LIST_HEAD(&pd->device_list);
  413. list_add_tail(&pd->device_list, &pd_device_list);
  414. }
  415. device_init_wakeup(&udev->dev, 1);
  416. #ifdef CONFIG_PM
  417. pd->udev->autosuspend_disabled = 0;
  418. pd->udev->autosuspend_delay = HZ * PM_SUSPEND_DELAY;
  419. if (in_hibernation(pd)) {
  420. INIT_WORK(&pd->pm_work, hibernation_resume);
  421. schedule_work(&pd->pm_work);
  422. }
  423. #endif
  424. return 0;
  425. }
  426. static void poseidon_disconnect(struct usb_interface *interface)
  427. {
  428. struct poseidon *pd = get_pd(interface);
  429. if (!pd)
  430. return;
  431. logpm(pd);
  432. if (in_hibernation(pd))
  433. return;
  434. mutex_lock(&pd->lock);
  435. pd->state |= POSEIDON_STATE_DISCONNECT;
  436. mutex_unlock(&pd->lock);
  437. /* stop urb transferring */
  438. stop_all_video_stream(pd);
  439. dvb_stop_streaming(&pd->dvb_data);
  440. /*unregister v4l2 device */
  441. v4l2_device_unregister(&pd->v4l2_dev);
  442. lock_kernel();
  443. {
  444. pd_dvb_usb_device_exit(pd);
  445. poseidon_fm_exit(pd);
  446. poseidon_audio_free(pd);
  447. pd_video_exit(pd);
  448. }
  449. unlock_kernel();
  450. usb_set_intfdata(interface, NULL);
  451. kref_put(&pd->kref, poseidon_delete);
  452. }
  453. struct usb_driver poseidon_driver = {
  454. .name = "poseidon",
  455. .probe = poseidon_probe,
  456. .disconnect = poseidon_disconnect,
  457. .id_table = id_table,
  458. #ifdef CONFIG_PM
  459. .suspend = poseidon_suspend,
  460. .resume = poseidon_resume,
  461. #endif
  462. .supports_autosuspend = 1,
  463. };
  464. static int __init poseidon_init(void)
  465. {
  466. int ret;
  467. ret = usb_register(&poseidon_driver);
  468. if (ret)
  469. return ret;
  470. register_pm_notifier(&pm_notifer);
  471. return ret;
  472. }
  473. static void __exit poseidon_exit(void)
  474. {
  475. log();
  476. unregister_pm_notifier(&pm_notifer);
  477. usb_deregister(&poseidon_driver);
  478. }
  479. module_init(poseidon_init);
  480. module_exit(poseidon_exit);
  481. MODULE_AUTHOR("Telegent Systems");
  482. MODULE_DESCRIPTION("For tlg2300-based USB device ");
  483. MODULE_LICENSE("GPL");