empeg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * USB Empeg empeg-car player driver
  3. *
  4. * Copyright (C) 2000, 2001
  5. * Gary Brubaker (xavyer@ix.netcom.com)
  6. *
  7. * Copyright (C) 1999 - 2001
  8. * Greg Kroah-Hartman (greg@kroah.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, as published by
  12. * the Free Software Foundation, version 2.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this
  15. * driver
  16. *
  17. * (07/16/2001) gb
  18. * remove unused code in empeg_close() (thanks to Oliver Neukum for
  19. * pointing this out) and rewrote empeg_set_termios().
  20. *
  21. * (05/30/2001) gkh
  22. * switched from using spinlock to a semaphore, which fixes lots of
  23. * problems.
  24. *
  25. * (04/08/2001) gb
  26. * Identify version on module load.
  27. *
  28. * (01/22/2001) gb
  29. * Added write_room() and chars_in_buffer() support.
  30. *
  31. * (12/21/2000) gb
  32. * Moved termio stuff inside the port->active check.
  33. * Moved MOD_DEC_USE_COUNT to end of empeg_close().
  34. *
  35. * (12/03/2000) gb
  36. * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open().
  37. * This notifies the tty driver that the termios have changed.
  38. *
  39. * (11/13/2000) gb
  40. * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to
  41. * empeg_open() (It only needs to be set once - Doh!)
  42. *
  43. * (11/11/2000) gb
  44. * Updated to work with id_table structure.
  45. *
  46. * (11/04/2000) gb
  47. * Forked this from visor.c, and hacked it up to work with an
  48. * Empeg ltd. empeg-car player. Constructive criticism welcomed.
  49. * I would like to say, 'Thank You' to Greg Kroah-Hartman for the
  50. * use of his code, and for his guidance, advice and patience. :)
  51. * A 'Thank You' is in order for John Ripley of Empeg ltd for his
  52. * advice, and patience too.
  53. *
  54. */
  55. #include <linux/kernel.h>
  56. #include <linux/errno.h>
  57. #include <linux/init.h>
  58. #include <linux/slab.h>
  59. #include <linux/tty.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/module.h>
  63. #include <linux/spinlock.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/usb.h>
  66. #include <linux/usb/serial.h>
  67. static int debug;
  68. /*
  69. * Version Information
  70. */
  71. #define DRIVER_VERSION "v1.2"
  72. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>"
  73. #define DRIVER_DESC "USB Empeg Mark I/II Driver"
  74. #define EMPEG_VENDOR_ID 0x084f
  75. #define EMPEG_PRODUCT_ID 0x0001
  76. /* function prototypes for an empeg-car player */
  77. static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
  78. struct file *filp);
  79. static void empeg_close(struct usb_serial_port *port);
  80. static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
  81. const unsigned char *buf,
  82. int count);
  83. static int empeg_write_room(struct tty_struct *tty);
  84. static int empeg_chars_in_buffer(struct tty_struct *tty);
  85. static void empeg_throttle(struct tty_struct *tty);
  86. static void empeg_unthrottle(struct tty_struct *tty);
  87. static int empeg_startup(struct usb_serial *serial);
  88. static void empeg_shutdown(struct usb_serial *serial);
  89. static void empeg_set_termios(struct tty_struct *tty,
  90. struct usb_serial_port *port, struct ktermios *old_termios);
  91. static void empeg_write_bulk_callback(struct urb *urb);
  92. static void empeg_read_bulk_callback(struct urb *urb);
  93. static struct usb_device_id id_table [] = {
  94. { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) },
  95. { } /* Terminating entry */
  96. };
  97. MODULE_DEVICE_TABLE(usb, id_table);
  98. static struct usb_driver empeg_driver = {
  99. .name = "empeg",
  100. .probe = usb_serial_probe,
  101. .disconnect = usb_serial_disconnect,
  102. .id_table = id_table,
  103. .no_dynamic_id = 1,
  104. };
  105. static struct usb_serial_driver empeg_device = {
  106. .driver = {
  107. .owner = THIS_MODULE,
  108. .name = "empeg",
  109. },
  110. .id_table = id_table,
  111. .usb_driver = &empeg_driver,
  112. .num_ports = 1,
  113. .open = empeg_open,
  114. .close = empeg_close,
  115. .throttle = empeg_throttle,
  116. .unthrottle = empeg_unthrottle,
  117. .attach = empeg_startup,
  118. .shutdown = empeg_shutdown,
  119. .set_termios = empeg_set_termios,
  120. .write = empeg_write,
  121. .write_room = empeg_write_room,
  122. .chars_in_buffer = empeg_chars_in_buffer,
  123. .write_bulk_callback = empeg_write_bulk_callback,
  124. .read_bulk_callback = empeg_read_bulk_callback,
  125. };
  126. #define NUM_URBS 16
  127. #define URB_TRANSFER_BUFFER_SIZE 4096
  128. static struct urb *write_urb_pool[NUM_URBS];
  129. static spinlock_t write_urb_pool_lock;
  130. static int bytes_in;
  131. static int bytes_out;
  132. /******************************************************************************
  133. * Empeg specific driver functions
  134. ******************************************************************************/
  135. static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
  136. struct file *filp)
  137. {
  138. struct usb_serial *serial = port->serial;
  139. int result = 0;
  140. dbg("%s - port %d", __func__, port->number);
  141. /* Force default termio settings */
  142. empeg_set_termios(tty, port, NULL) ;
  143. bytes_in = 0;
  144. bytes_out = 0;
  145. /* Start reading from the device */
  146. usb_fill_bulk_urb(
  147. port->read_urb,
  148. serial->dev,
  149. usb_rcvbulkpipe(serial->dev,
  150. port->bulk_in_endpointAddress),
  151. port->read_urb->transfer_buffer,
  152. port->read_urb->transfer_buffer_length,
  153. empeg_read_bulk_callback,
  154. port);
  155. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  156. if (result)
  157. dev_err(&port->dev,
  158. "%s - failed submitting read urb, error %d\n",
  159. __func__, result);
  160. return result;
  161. }
  162. static void empeg_close(struct usb_serial_port *port)
  163. {
  164. dbg("%s - port %d", __func__, port->number);
  165. /* shutdown our bulk read */
  166. usb_kill_urb(port->read_urb);
  167. /* Uncomment the following line if you want to see some statistics in your syslog */
  168. /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */
  169. }
  170. static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
  171. const unsigned char *buf, int count)
  172. {
  173. struct usb_serial *serial = port->serial;
  174. struct urb *urb;
  175. const unsigned char *current_position = buf;
  176. unsigned long flags;
  177. int status;
  178. int i;
  179. int bytes_sent = 0;
  180. int transfer_size;
  181. dbg("%s - port %d", __func__, port->number);
  182. while (count > 0) {
  183. /* try to find a free urb in our list of them */
  184. urb = NULL;
  185. spin_lock_irqsave(&write_urb_pool_lock, flags);
  186. for (i = 0; i < NUM_URBS; ++i) {
  187. if (write_urb_pool[i]->status != -EINPROGRESS) {
  188. urb = write_urb_pool[i];
  189. break;
  190. }
  191. }
  192. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  193. if (urb == NULL) {
  194. dbg("%s - no more free urbs", __func__);
  195. goto exit;
  196. }
  197. if (urb->transfer_buffer == NULL) {
  198. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC);
  199. if (urb->transfer_buffer == NULL) {
  200. dev_err(&port->dev,
  201. "%s no more kernel memory...\n",
  202. __func__);
  203. goto exit;
  204. }
  205. }
  206. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  207. memcpy(urb->transfer_buffer, current_position, transfer_size);
  208. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer);
  209. /* build up our urb */
  210. usb_fill_bulk_urb(
  211. urb,
  212. serial->dev,
  213. usb_sndbulkpipe(serial->dev,
  214. port->bulk_out_endpointAddress),
  215. urb->transfer_buffer,
  216. transfer_size,
  217. empeg_write_bulk_callback,
  218. port);
  219. /* send it down the pipe */
  220. status = usb_submit_urb(urb, GFP_ATOMIC);
  221. if (status) {
  222. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status);
  223. bytes_sent = status;
  224. break;
  225. }
  226. current_position += transfer_size;
  227. bytes_sent += transfer_size;
  228. count -= transfer_size;
  229. bytes_out += transfer_size;
  230. }
  231. exit:
  232. return bytes_sent;
  233. }
  234. static int empeg_write_room(struct tty_struct *tty)
  235. {
  236. struct usb_serial_port *port = tty->driver_data;
  237. unsigned long flags;
  238. int i;
  239. int room = 0;
  240. dbg("%s - port %d", __func__, port->number);
  241. spin_lock_irqsave(&write_urb_pool_lock, flags);
  242. /* tally up the number of bytes available */
  243. for (i = 0; i < NUM_URBS; ++i) {
  244. if (write_urb_pool[i]->status != -EINPROGRESS)
  245. room += URB_TRANSFER_BUFFER_SIZE;
  246. }
  247. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  248. dbg("%s - returns %d", __func__, room);
  249. return room;
  250. }
  251. static int empeg_chars_in_buffer(struct tty_struct *tty)
  252. {
  253. struct usb_serial_port *port = tty->driver_data;
  254. unsigned long flags;
  255. int i;
  256. int chars = 0;
  257. dbg("%s - port %d", __func__, port->number);
  258. spin_lock_irqsave(&write_urb_pool_lock, flags);
  259. /* tally up the number of bytes waiting */
  260. for (i = 0; i < NUM_URBS; ++i) {
  261. if (write_urb_pool[i]->status == -EINPROGRESS)
  262. chars += URB_TRANSFER_BUFFER_SIZE;
  263. }
  264. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  265. dbg("%s - returns %d", __func__, chars);
  266. return chars;
  267. }
  268. static void empeg_write_bulk_callback(struct urb *urb)
  269. {
  270. struct usb_serial_port *port = urb->context;
  271. int status = urb->status;
  272. dbg("%s - port %d", __func__, port->number);
  273. if (status) {
  274. dbg("%s - nonzero write bulk status received: %d",
  275. __func__, status);
  276. return;
  277. }
  278. usb_serial_port_softint(port);
  279. }
  280. static void empeg_read_bulk_callback(struct urb *urb)
  281. {
  282. struct usb_serial_port *port = urb->context;
  283. struct tty_struct *tty;
  284. unsigned char *data = urb->transfer_buffer;
  285. int result;
  286. int status = urb->status;
  287. dbg("%s - port %d", __func__, port->number);
  288. if (status) {
  289. dbg("%s - nonzero read bulk status received: %d",
  290. __func__, status);
  291. return;
  292. }
  293. usb_serial_debug_data(debug, &port->dev, __func__,
  294. urb->actual_length, data);
  295. tty = tty_port_tty_get(&port->port);
  296. if (urb->actual_length) {
  297. tty_buffer_request_room(tty, urb->actual_length);
  298. tty_insert_flip_string(tty, data, urb->actual_length);
  299. tty_flip_buffer_push(tty);
  300. bytes_in += urb->actual_length;
  301. }
  302. tty_kref_put(tty);
  303. /* Continue trying to always read */
  304. usb_fill_bulk_urb(
  305. port->read_urb,
  306. port->serial->dev,
  307. usb_rcvbulkpipe(port->serial->dev,
  308. port->bulk_in_endpointAddress),
  309. port->read_urb->transfer_buffer,
  310. port->read_urb->transfer_buffer_length,
  311. empeg_read_bulk_callback,
  312. port);
  313. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  314. if (result)
  315. dev_err(&urb->dev->dev,
  316. "%s - failed resubmitting read urb, error %d\n",
  317. __func__, result);
  318. return;
  319. }
  320. static void empeg_throttle(struct tty_struct *tty)
  321. {
  322. struct usb_serial_port *port = tty->driver_data;
  323. dbg("%s - port %d", __func__, port->number);
  324. usb_kill_urb(port->read_urb);
  325. }
  326. static void empeg_unthrottle(struct tty_struct *tty)
  327. {
  328. struct usb_serial_port *port = tty->driver_data;
  329. int result;
  330. dbg("%s - port %d", __func__, port->number);
  331. port->read_urb->dev = port->serial->dev;
  332. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  333. if (result)
  334. dev_err(&port->dev,
  335. "%s - failed submitting read urb, error %d\n",
  336. __func__, result);
  337. }
  338. static int empeg_startup(struct usb_serial *serial)
  339. {
  340. int r;
  341. dbg("%s", __func__);
  342. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  343. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  344. serial->dev->actconfig->desc.bConfigurationValue);
  345. return -ENODEV;
  346. }
  347. dbg("%s - reset config", __func__);
  348. r = usb_reset_configuration(serial->dev);
  349. /* continue on with initialization */
  350. return r;
  351. }
  352. static void empeg_shutdown(struct usb_serial *serial)
  353. {
  354. dbg("%s", __func__);
  355. }
  356. static void empeg_set_termios(struct tty_struct *tty,
  357. struct usb_serial_port *port, struct ktermios *old_termios)
  358. {
  359. struct ktermios *termios = tty->termios;
  360. dbg("%s - port %d", __func__, port->number);
  361. /*
  362. * The empeg-car player wants these particular tty settings.
  363. * You could, for example, change the baud rate, however the
  364. * player only supports 115200 (currently), so there is really
  365. * no point in support for changes to the tty settings.
  366. * (at least for now)
  367. *
  368. * The default requirements for this device are:
  369. */
  370. termios->c_iflag
  371. &= ~(IGNBRK /* disable ignore break */
  372. | BRKINT /* disable break causes interrupt */
  373. | PARMRK /* disable mark parity errors */
  374. | ISTRIP /* disable clear high bit of input characters */
  375. | INLCR /* disable translate NL to CR */
  376. | IGNCR /* disable ignore CR */
  377. | ICRNL /* disable translate CR to NL */
  378. | IXON); /* disable enable XON/XOFF flow control */
  379. termios->c_oflag
  380. &= ~OPOST; /* disable postprocess output characters */
  381. termios->c_lflag
  382. &= ~(ECHO /* disable echo input characters */
  383. | ECHONL /* disable echo new line */
  384. | ICANON /* disable erase, kill, werase, and rprnt special characters */
  385. | ISIG /* disable interrupt, quit, and suspend special characters */
  386. | IEXTEN); /* disable non-POSIX special characters */
  387. termios->c_cflag
  388. &= ~(CSIZE /* no size */
  389. | PARENB /* disable parity bit */
  390. | CBAUD); /* clear current baud rate */
  391. termios->c_cflag
  392. |= CS8; /* character size 8 bits */
  393. tty_encode_baud_rate(tty, 115200, 115200);
  394. }
  395. static int __init empeg_init(void)
  396. {
  397. struct urb *urb;
  398. int i, retval;
  399. /* create our write urb pool and transfer buffers */
  400. spin_lock_init(&write_urb_pool_lock);
  401. for (i = 0; i < NUM_URBS; ++i) {
  402. urb = usb_alloc_urb(0, GFP_KERNEL);
  403. write_urb_pool[i] = urb;
  404. if (urb == NULL) {
  405. printk(KERN_ERR "empeg: No more urbs???\n");
  406. continue;
  407. }
  408. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  409. GFP_KERNEL);
  410. if (!urb->transfer_buffer) {
  411. printk(KERN_ERR "empeg: %s - out of memory for urb "
  412. "buffers.", __func__);
  413. continue;
  414. }
  415. }
  416. retval = usb_serial_register(&empeg_device);
  417. if (retval)
  418. goto failed_usb_serial_register;
  419. retval = usb_register(&empeg_driver);
  420. if (retval)
  421. goto failed_usb_register;
  422. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  423. DRIVER_DESC "\n");
  424. return 0;
  425. failed_usb_register:
  426. usb_serial_deregister(&empeg_device);
  427. failed_usb_serial_register:
  428. for (i = 0; i < NUM_URBS; ++i) {
  429. if (write_urb_pool[i]) {
  430. kfree(write_urb_pool[i]->transfer_buffer);
  431. usb_free_urb(write_urb_pool[i]);
  432. }
  433. }
  434. return retval;
  435. }
  436. static void __exit empeg_exit(void)
  437. {
  438. int i;
  439. unsigned long flags;
  440. usb_deregister(&empeg_driver);
  441. usb_serial_deregister(&empeg_device);
  442. spin_lock_irqsave(&write_urb_pool_lock, flags);
  443. for (i = 0; i < NUM_URBS; ++i) {
  444. if (write_urb_pool[i]) {
  445. /* FIXME - uncomment the following usb_kill_urb call
  446. * when the host controllers get fixed to set urb->dev
  447. * = NULL after the urb is finished. Otherwise this
  448. * call oopses. */
  449. /* usb_kill_urb(write_urb_pool[i]); */
  450. kfree(write_urb_pool[i]->transfer_buffer);
  451. usb_free_urb(write_urb_pool[i]);
  452. }
  453. }
  454. spin_unlock_irqrestore(&write_urb_pool_lock, flags);
  455. }
  456. module_init(empeg_init);
  457. module_exit(empeg_exit);
  458. MODULE_AUTHOR(DRIVER_AUTHOR);
  459. MODULE_DESCRIPTION(DRIVER_DESC);
  460. MODULE_LICENSE("GPL");
  461. module_param(debug, bool, S_IRUGO | S_IWUSR);
  462. MODULE_PARM_DESC(debug, "Debug enabled or not");