pd-main.c 12 KB

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