visor.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this driver
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_driver.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/spinlock.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/serial.h>
  28. #include "visor.h"
  29. /*
  30. * Version Information
  31. */
  32. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  33. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  34. /* function prototypes for a handspring visor */
  35. static int visor_open (struct usb_serial_port *port, struct file *filp);
  36. static void visor_close (struct usb_serial_port *port, struct file *filp);
  37. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  38. static int visor_write_room (struct usb_serial_port *port);
  39. static int visor_chars_in_buffer (struct usb_serial_port *port);
  40. static void visor_throttle (struct usb_serial_port *port);
  41. static void visor_unthrottle (struct usb_serial_port *port);
  42. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id);
  43. static int visor_calc_num_ports(struct usb_serial *serial);
  44. static void visor_shutdown (struct usb_serial *serial);
  45. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  46. static void visor_write_bulk_callback (struct urb *urb);
  47. static void visor_read_bulk_callback (struct urb *urb);
  48. static void visor_read_int_callback (struct urb *urb);
  49. static int clie_3_5_startup (struct usb_serial *serial);
  50. static int treo_attach (struct usb_serial *serial);
  51. static int clie_5_attach (struct usb_serial *serial);
  52. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id);
  53. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id);
  54. /* Parameters that may be passed into the module. */
  55. static int debug;
  56. static __u16 vendor;
  57. static __u16 product;
  58. static struct usb_device_id id_table [] = {
  59. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  60. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  61. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  62. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  63. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  64. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  65. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  66. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  67. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  68. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  69. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  70. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  71. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  72. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  73. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  74. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  75. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  76. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  77. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  78. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  79. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  80. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  81. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  82. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  83. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  84. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  85. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  86. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  87. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  88. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  89. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  90. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  91. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  92. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  93. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  94. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  95. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  96. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  97. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  98. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  99. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  100. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  101. { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
  102. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  103. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  104. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  105. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  106. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  107. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  108. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  109. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  110. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  111. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  112. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  113. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  114. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  115. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  116. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  117. { }, /* optional parameter entry */
  118. { } /* Terminating entry */
  119. };
  120. static struct usb_device_id clie_id_5_table [] = {
  121. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  122. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  123. { }, /* optional parameter entry */
  124. { } /* Terminating entry */
  125. };
  126. static struct usb_device_id clie_id_3_5_table [] = {
  127. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  128. { } /* Terminating entry */
  129. };
  130. static struct usb_device_id id_table_combined [] = {
  131. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  132. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  133. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  134. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  135. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  136. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  137. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  146. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  147. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  148. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  154. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  155. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  156. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  157. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  158. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  159. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  160. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  161. { }, /* optional parameter entry */
  162. { } /* Terminating entry */
  163. };
  164. MODULE_DEVICE_TABLE (usb, id_table_combined);
  165. static struct usb_driver visor_driver = {
  166. .name = "visor",
  167. .probe = usb_serial_probe,
  168. .disconnect = usb_serial_disconnect,
  169. .id_table = id_table_combined,
  170. .no_dynamic_id = 1,
  171. };
  172. /* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
  173. static struct usb_serial_driver handspring_device = {
  174. .driver = {
  175. .owner = THIS_MODULE,
  176. .name = "visor",
  177. },
  178. .description = "Handspring Visor / Palm OS",
  179. .usb_driver = &visor_driver,
  180. .id_table = id_table,
  181. .num_ports = 2,
  182. .open = visor_open,
  183. .close = visor_close,
  184. .throttle = visor_throttle,
  185. .unthrottle = visor_unthrottle,
  186. .attach = treo_attach,
  187. .probe = visor_probe,
  188. .calc_num_ports = visor_calc_num_ports,
  189. .shutdown = visor_shutdown,
  190. .ioctl = visor_ioctl,
  191. .write = visor_write,
  192. .write_room = visor_write_room,
  193. .chars_in_buffer = visor_chars_in_buffer,
  194. .write_bulk_callback = visor_write_bulk_callback,
  195. .read_bulk_callback = visor_read_bulk_callback,
  196. .read_int_callback = visor_read_int_callback,
  197. };
  198. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  199. static struct usb_serial_driver clie_5_device = {
  200. .driver = {
  201. .owner = THIS_MODULE,
  202. .name = "clie_5",
  203. },
  204. .description = "Sony Clie 5.0",
  205. .usb_driver = &visor_driver,
  206. .id_table = clie_id_5_table,
  207. .num_ports = 2,
  208. .open = visor_open,
  209. .close = visor_close,
  210. .throttle = visor_throttle,
  211. .unthrottle = visor_unthrottle,
  212. .attach = clie_5_attach,
  213. .probe = visor_probe,
  214. .calc_num_ports = visor_calc_num_ports,
  215. .shutdown = visor_shutdown,
  216. .ioctl = visor_ioctl,
  217. .write = visor_write,
  218. .write_room = visor_write_room,
  219. .chars_in_buffer = visor_chars_in_buffer,
  220. .write_bulk_callback = visor_write_bulk_callback,
  221. .read_bulk_callback = visor_read_bulk_callback,
  222. .read_int_callback = visor_read_int_callback,
  223. };
  224. /* device info for the Sony Clie OS version 3.5 */
  225. static struct usb_serial_driver clie_3_5_device = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "clie_3.5",
  229. },
  230. .description = "Sony Clie 3.5",
  231. .usb_driver = &visor_driver,
  232. .id_table = clie_id_3_5_table,
  233. .num_ports = 1,
  234. .open = visor_open,
  235. .close = visor_close,
  236. .throttle = visor_throttle,
  237. .unthrottle = visor_unthrottle,
  238. .attach = clie_3_5_startup,
  239. .ioctl = visor_ioctl,
  240. .write = visor_write,
  241. .write_room = visor_write_room,
  242. .chars_in_buffer = visor_chars_in_buffer,
  243. .write_bulk_callback = visor_write_bulk_callback,
  244. .read_bulk_callback = visor_read_bulk_callback,
  245. };
  246. struct visor_private {
  247. spinlock_t lock;
  248. int bytes_in;
  249. int bytes_out;
  250. int outstanding_urbs;
  251. unsigned char throttled;
  252. unsigned char actually_throttled;
  253. };
  254. /* number of outstanding urbs to prevent userspace DoS from happening */
  255. #define URB_UPPER_LIMIT 42
  256. static int stats;
  257. /******************************************************************************
  258. * Handspring Visor specific driver functions
  259. ******************************************************************************/
  260. static int visor_open (struct usb_serial_port *port, struct file *filp)
  261. {
  262. struct usb_serial *serial = port->serial;
  263. struct visor_private *priv = usb_get_serial_port_data(port);
  264. unsigned long flags;
  265. int result = 0;
  266. dbg("%s - port %d", __func__, port->number);
  267. if (!port->read_urb) {
  268. /* this is needed for some brain dead Sony devices */
  269. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  270. return -ENODEV;
  271. }
  272. spin_lock_irqsave(&priv->lock, flags);
  273. priv->bytes_in = 0;
  274. priv->bytes_out = 0;
  275. priv->throttled = 0;
  276. spin_unlock_irqrestore(&priv->lock, flags);
  277. /*
  278. * Force low_latency on so that our tty_push actually forces the data
  279. * through, otherwise it is scheduled, and with high data rates (like
  280. * with OHCI) data can get lost.
  281. */
  282. if (port->tty)
  283. port->tty->low_latency = 1;
  284. /* Start reading from the device */
  285. usb_fill_bulk_urb (port->read_urb, serial->dev,
  286. usb_rcvbulkpipe (serial->dev,
  287. port->bulk_in_endpointAddress),
  288. port->read_urb->transfer_buffer,
  289. port->read_urb->transfer_buffer_length,
  290. visor_read_bulk_callback, port);
  291. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  292. if (result) {
  293. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n",
  294. __func__, result);
  295. goto exit;
  296. }
  297. if (port->interrupt_in_urb) {
  298. dbg("%s - adding interrupt input for treo", __func__);
  299. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  300. if (result)
  301. dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n",
  302. __func__, result);
  303. }
  304. exit:
  305. return result;
  306. }
  307. static void visor_close (struct usb_serial_port *port, struct file * filp)
  308. {
  309. struct visor_private *priv = usb_get_serial_port_data(port);
  310. unsigned char *transfer_buffer;
  311. dbg("%s - port %d", __func__, port->number);
  312. /* shutdown our urbs */
  313. usb_kill_urb(port->read_urb);
  314. usb_kill_urb(port->interrupt_in_urb);
  315. mutex_lock(&port->serial->disc_mutex);
  316. if (!port->serial->disconnected) {
  317. /* Try to send shutdown message, unless the device is gone */
  318. transfer_buffer = kmalloc (0x12, GFP_KERNEL);
  319. if (transfer_buffer) {
  320. usb_control_msg (port->serial->dev,
  321. usb_rcvctrlpipe(port->serial->dev, 0),
  322. VISOR_CLOSE_NOTIFICATION, 0xc2,
  323. 0x0000, 0x0000,
  324. transfer_buffer, 0x12, 300);
  325. kfree (transfer_buffer);
  326. }
  327. }
  328. mutex_unlock(&port->serial->disc_mutex);
  329. if (stats)
  330. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  331. priv->bytes_in, priv->bytes_out);
  332. }
  333. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  334. {
  335. struct visor_private *priv = usb_get_serial_port_data(port);
  336. struct usb_serial *serial = port->serial;
  337. struct urb *urb;
  338. unsigned char *buffer;
  339. unsigned long flags;
  340. int status;
  341. dbg("%s - port %d", __func__, port->number);
  342. spin_lock_irqsave(&priv->lock, flags);
  343. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  344. spin_unlock_irqrestore(&priv->lock, flags);
  345. dbg("%s - write limit hit\n", __func__);
  346. return 0;
  347. }
  348. priv->outstanding_urbs++;
  349. spin_unlock_irqrestore(&priv->lock, flags);
  350. buffer = kmalloc (count, GFP_ATOMIC);
  351. if (!buffer) {
  352. dev_err(&port->dev, "out of memory\n");
  353. count = -ENOMEM;
  354. goto error_no_buffer;
  355. }
  356. urb = usb_alloc_urb(0, GFP_ATOMIC);
  357. if (!urb) {
  358. dev_err(&port->dev, "no more free urbs\n");
  359. count = -ENOMEM;
  360. goto error_no_urb;
  361. }
  362. memcpy (buffer, buf, count);
  363. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  364. usb_fill_bulk_urb (urb, serial->dev,
  365. usb_sndbulkpipe (serial->dev,
  366. port->bulk_out_endpointAddress),
  367. buffer, count,
  368. visor_write_bulk_callback, port);
  369. /* send it down the pipe */
  370. status = usb_submit_urb(urb, GFP_ATOMIC);
  371. if (status) {
  372. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  373. __func__, status);
  374. count = status;
  375. goto error;
  376. } else {
  377. spin_lock_irqsave(&priv->lock, flags);
  378. priv->bytes_out += count;
  379. spin_unlock_irqrestore(&priv->lock, flags);
  380. }
  381. /* we are done with this urb, so let the host driver
  382. * really free it when it is finished with it */
  383. usb_free_urb(urb);
  384. return count;
  385. error:
  386. usb_free_urb(urb);
  387. error_no_urb:
  388. kfree(buffer);
  389. error_no_buffer:
  390. spin_lock_irqsave(&priv->lock, flags);
  391. --priv->outstanding_urbs;
  392. spin_unlock_irqrestore(&priv->lock, flags);
  393. return count;
  394. }
  395. static int visor_write_room (struct usb_serial_port *port)
  396. {
  397. struct visor_private *priv = usb_get_serial_port_data(port);
  398. unsigned long flags;
  399. dbg("%s - port %d", __func__, port->number);
  400. /*
  401. * We really can take anything the user throws at us
  402. * but let's pick a nice big number to tell the tty
  403. * layer that we have lots of free space, unless we don't.
  404. */
  405. spin_lock_irqsave(&priv->lock, flags);
  406. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. dbg("%s - write limit hit\n", __func__);
  409. return 0;
  410. }
  411. spin_unlock_irqrestore(&priv->lock, flags);
  412. return 2048;
  413. }
  414. static int visor_chars_in_buffer (struct usb_serial_port *port)
  415. {
  416. dbg("%s - port %d", __func__, port->number);
  417. /*
  418. * We can't really account for how much data we
  419. * have sent out, but hasn't made it through to the
  420. * device, so just tell the tty layer that everything
  421. * is flushed.
  422. *
  423. * FIXME: Should walk outstanding_urbs
  424. */
  425. return 0;
  426. }
  427. static void visor_write_bulk_callback (struct urb *urb)
  428. {
  429. struct usb_serial_port *port = urb->context;
  430. struct visor_private *priv = usb_get_serial_port_data(port);
  431. int status = urb->status;
  432. unsigned long flags;
  433. /* free up the transfer buffer, as usb_free_urb() does not do this */
  434. kfree (urb->transfer_buffer);
  435. dbg("%s - port %d", __func__, port->number);
  436. if (status)
  437. dbg("%s - nonzero write bulk status received: %d",
  438. __func__, status);
  439. spin_lock_irqsave(&priv->lock, flags);
  440. --priv->outstanding_urbs;
  441. spin_unlock_irqrestore(&priv->lock, flags);
  442. usb_serial_port_softint(port);
  443. }
  444. static void visor_read_bulk_callback (struct urb *urb)
  445. {
  446. struct usb_serial_port *port = urb->context;
  447. struct visor_private *priv = usb_get_serial_port_data(port);
  448. unsigned char *data = urb->transfer_buffer;
  449. int status = urb->status;
  450. struct tty_struct *tty;
  451. int result;
  452. int available_room;
  453. dbg("%s - port %d", __func__, port->number);
  454. if (status) {
  455. dbg("%s - nonzero read bulk status received: %d",
  456. __func__, status);
  457. return;
  458. }
  459. usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
  460. tty = port->tty;
  461. if (tty && urb->actual_length) {
  462. available_room = tty_buffer_request_room(tty, urb->actual_length);
  463. if (available_room) {
  464. tty_insert_flip_string(tty, data, available_room);
  465. tty_flip_buffer_push(tty);
  466. }
  467. spin_lock(&priv->lock);
  468. priv->bytes_in += available_room;
  469. } else {
  470. spin_lock(&priv->lock);
  471. }
  472. /* Continue trying to always read if we should */
  473. if (!priv->throttled) {
  474. usb_fill_bulk_urb (port->read_urb, port->serial->dev,
  475. usb_rcvbulkpipe(port->serial->dev,
  476. port->bulk_in_endpointAddress),
  477. port->read_urb->transfer_buffer,
  478. port->read_urb->transfer_buffer_length,
  479. visor_read_bulk_callback, port);
  480. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  481. if (result)
  482. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
  483. } else {
  484. priv->actually_throttled = 1;
  485. }
  486. spin_unlock(&priv->lock);
  487. }
  488. static void visor_read_int_callback (struct urb *urb)
  489. {
  490. struct usb_serial_port *port = urb->context;
  491. int status = urb->status;
  492. int result;
  493. switch (status) {
  494. case 0:
  495. /* success */
  496. break;
  497. case -ECONNRESET:
  498. case -ENOENT:
  499. case -ESHUTDOWN:
  500. /* this urb is terminated, clean up */
  501. dbg("%s - urb shutting down with status: %d",
  502. __func__, status);
  503. return;
  504. default:
  505. dbg("%s - nonzero urb status received: %d",
  506. __func__, status);
  507. goto exit;
  508. }
  509. /*
  510. * This information is still unknown what it can be used for.
  511. * If anyone has an idea, please let the author know...
  512. *
  513. * Rumor has it this endpoint is used to notify when data
  514. * is ready to be read from the bulk ones.
  515. */
  516. usb_serial_debug_data(debug, &port->dev, __func__,
  517. urb->actual_length, urb->transfer_buffer);
  518. exit:
  519. result = usb_submit_urb (urb, GFP_ATOMIC);
  520. if (result)
  521. dev_err(&urb->dev->dev, "%s - Error %d submitting interrupt urb\n",
  522. __func__, result);
  523. }
  524. static void visor_throttle (struct usb_serial_port *port)
  525. {
  526. struct visor_private *priv = usb_get_serial_port_data(port);
  527. unsigned long flags;
  528. dbg("%s - port %d", __func__, port->number);
  529. spin_lock_irqsave(&priv->lock, flags);
  530. priv->throttled = 1;
  531. spin_unlock_irqrestore(&priv->lock, flags);
  532. }
  533. static void visor_unthrottle (struct usb_serial_port *port)
  534. {
  535. struct visor_private *priv = usb_get_serial_port_data(port);
  536. unsigned long flags;
  537. int result;
  538. dbg("%s - port %d", __func__, port->number);
  539. spin_lock_irqsave(&priv->lock, flags);
  540. priv->throttled = 0;
  541. priv->actually_throttled = 0;
  542. spin_unlock_irqrestore(&priv->lock, flags);
  543. port->read_urb->dev = port->serial->dev;
  544. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  545. if (result)
  546. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __func__, result);
  547. }
  548. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id)
  549. {
  550. struct device *dev = &serial->dev->dev;
  551. struct visor_connection_info *connection_info;
  552. unsigned char *transfer_buffer;
  553. char *string;
  554. int retval = 0;
  555. int i;
  556. int num_ports = 0;
  557. dbg("%s", __func__);
  558. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  559. if (!transfer_buffer) {
  560. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  561. sizeof(*connection_info));
  562. return -ENOMEM;
  563. }
  564. /* send a get connection info request */
  565. retval = usb_control_msg (serial->dev,
  566. usb_rcvctrlpipe(serial->dev, 0),
  567. VISOR_GET_CONNECTION_INFORMATION,
  568. 0xc2, 0x0000, 0x0000, transfer_buffer,
  569. sizeof(*connection_info), 300);
  570. if (retval < 0) {
  571. dev_err(dev, "%s - error %d getting connection information\n",
  572. __func__, retval);
  573. goto exit;
  574. }
  575. if (retval == sizeof(*connection_info)) {
  576. connection_info = (struct visor_connection_info *)transfer_buffer;
  577. num_ports = le16_to_cpu(connection_info->num_ports);
  578. for (i = 0; i < num_ports; ++i) {
  579. switch (connection_info->connections[i].port_function_id) {
  580. case VISOR_FUNCTION_GENERIC:
  581. string = "Generic";
  582. break;
  583. case VISOR_FUNCTION_DEBUGGER:
  584. string = "Debugger";
  585. break;
  586. case VISOR_FUNCTION_HOTSYNC:
  587. string = "HotSync";
  588. break;
  589. case VISOR_FUNCTION_CONSOLE:
  590. string = "Console";
  591. break;
  592. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  593. string = "Remote File System";
  594. break;
  595. default:
  596. string = "unknown";
  597. break;
  598. }
  599. dev_info(dev, "%s: port %d, is for %s use\n",
  600. serial->type->description,
  601. connection_info->connections[i].port, string);
  602. }
  603. }
  604. /*
  605. * Handle devices that report invalid stuff here.
  606. */
  607. if (num_ports == 0 || num_ports > 2) {
  608. dev_warn (dev, "%s: No valid connect info available\n",
  609. serial->type->description);
  610. num_ports = 2;
  611. }
  612. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  613. num_ports);
  614. /*
  615. * save off our num_ports info so that we can use it in the
  616. * calc_num_ports callback
  617. */
  618. usb_set_serial_data(serial, (void *)(long)num_ports);
  619. /* ask for the number of bytes available, but ignore the response as it is broken */
  620. retval = usb_control_msg (serial->dev,
  621. usb_rcvctrlpipe(serial->dev, 0),
  622. VISOR_REQUEST_BYTES_AVAILABLE,
  623. 0xc2, 0x0000, 0x0005, transfer_buffer,
  624. 0x02, 300);
  625. if (retval < 0)
  626. dev_err(dev, "%s - error %d getting bytes available request\n",
  627. __func__, retval);
  628. retval = 0;
  629. exit:
  630. kfree (transfer_buffer);
  631. return retval;
  632. }
  633. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id)
  634. {
  635. struct device *dev = &serial->dev->dev;
  636. struct palm_ext_connection_info *connection_info;
  637. unsigned char *transfer_buffer;
  638. int retval;
  639. dbg("%s", __func__);
  640. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  641. if (!transfer_buffer) {
  642. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  643. sizeof(*connection_info));
  644. return -ENOMEM;
  645. }
  646. retval = usb_control_msg (serial->dev,
  647. usb_rcvctrlpipe(serial->dev, 0),
  648. PALM_GET_EXT_CONNECTION_INFORMATION,
  649. 0xc2, 0x0000, 0x0000, transfer_buffer,
  650. sizeof (*connection_info), 300);
  651. if (retval < 0)
  652. dev_err(dev, "%s - error %d getting connection info\n",
  653. __func__, retval);
  654. else
  655. usb_serial_debug_data(debug, &serial->dev->dev, __func__,
  656. retval, transfer_buffer);
  657. kfree (transfer_buffer);
  658. return 0;
  659. }
  660. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id)
  661. {
  662. int retval = 0;
  663. int (*startup) (struct usb_serial *serial, const struct usb_device_id *id);
  664. dbg("%s", __func__);
  665. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  666. err("active config #%d != 1 ??",
  667. serial->dev->actconfig->desc.bConfigurationValue);
  668. return -ENODEV;
  669. }
  670. if (id->driver_info) {
  671. startup = (void *)id->driver_info;
  672. retval = startup(serial, id);
  673. }
  674. return retval;
  675. }
  676. static int visor_calc_num_ports (struct usb_serial *serial)
  677. {
  678. int num_ports = (int)(long)(usb_get_serial_data(serial));
  679. if (num_ports)
  680. usb_set_serial_data(serial, NULL);
  681. return num_ports;
  682. }
  683. static int generic_startup(struct usb_serial *serial)
  684. {
  685. struct usb_serial_port **ports = serial->port;
  686. struct visor_private *priv;
  687. int i;
  688. for (i = 0; i < serial->num_ports; ++i) {
  689. priv = kzalloc (sizeof(*priv), GFP_KERNEL);
  690. if (!priv) {
  691. while (i-- != 0) {
  692. priv = usb_get_serial_port_data(ports[i]);
  693. usb_set_serial_port_data(ports[i], NULL);
  694. kfree(priv);
  695. }
  696. return -ENOMEM;
  697. }
  698. spin_lock_init(&priv->lock);
  699. usb_set_serial_port_data(ports[i], priv);
  700. }
  701. return 0;
  702. }
  703. static int clie_3_5_startup (struct usb_serial *serial)
  704. {
  705. struct device *dev = &serial->dev->dev;
  706. int result;
  707. u8 data;
  708. dbg("%s", __func__);
  709. /*
  710. * Note that PEG-300 series devices expect the following two calls.
  711. */
  712. /* get the config number */
  713. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  714. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  715. 0, 0, &data, 1, 3000);
  716. if (result < 0) {
  717. dev_err(dev, "%s: get config number failed: %d\n", __func__, result);
  718. return result;
  719. }
  720. if (result != 1) {
  721. dev_err(dev, "%s: get config number bad return length: %d\n", __func__, result);
  722. return -EIO;
  723. }
  724. /* get the interface number */
  725. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  726. USB_REQ_GET_INTERFACE,
  727. USB_DIR_IN | USB_RECIP_INTERFACE,
  728. 0, 0, &data, 1, 3000);
  729. if (result < 0) {
  730. dev_err(dev, "%s: get interface number failed: %d\n", __func__, result);
  731. return result;
  732. }
  733. if (result != 1) {
  734. dev_err(dev, "%s: get interface number bad return length: %d\n", __func__, result);
  735. return -EIO;
  736. }
  737. return generic_startup(serial);
  738. }
  739. static int treo_attach (struct usb_serial *serial)
  740. {
  741. struct usb_serial_port *swap_port;
  742. /* Only do this endpoint hack for the Handspring devices with
  743. * interrupt in endpoints, which for now are the Treo devices. */
  744. if (!((le16_to_cpu(serial->dev->descriptor.idVendor) == HANDSPRING_VENDOR_ID) ||
  745. (le16_to_cpu(serial->dev->descriptor.idVendor) == KYOCERA_VENDOR_ID)) ||
  746. (serial->num_interrupt_in == 0))
  747. goto generic_startup;
  748. dbg("%s", __func__);
  749. /*
  750. * It appears that Treos and Kyoceras want to use the
  751. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  752. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  753. * Note that swapping the bulk out endpoints would break lots of
  754. * apps that want to communicate on the second port.
  755. */
  756. #define COPY_PORT(dest, src) \
  757. dest->read_urb = src->read_urb; \
  758. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress; \
  759. dest->bulk_in_buffer = src->bulk_in_buffer; \
  760. dest->interrupt_in_urb = src->interrupt_in_urb; \
  761. dest->interrupt_in_endpointAddress = src->interrupt_in_endpointAddress; \
  762. dest->interrupt_in_buffer = src->interrupt_in_buffer;
  763. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  764. if (!swap_port)
  765. return -ENOMEM;
  766. COPY_PORT(swap_port, serial->port[0]);
  767. COPY_PORT(serial->port[0], serial->port[1]);
  768. COPY_PORT(serial->port[1], swap_port);
  769. kfree(swap_port);
  770. generic_startup:
  771. return generic_startup(serial);
  772. }
  773. static int clie_5_attach (struct usb_serial *serial)
  774. {
  775. dbg("%s", __func__);
  776. /* TH55 registers 2 ports.
  777. Communication in from the UX50/TH55 uses bulk_in_endpointAddress from port 0
  778. Communication out to the UX50/TH55 uses bulk_out_endpointAddress from port 1
  779. Lets do a quick and dirty mapping
  780. */
  781. /* some sanity check */
  782. if (serial->num_ports < 2)
  783. return -1;
  784. /* port 0 now uses the modified endpoint Address */
  785. serial->port[0]->bulk_out_endpointAddress = serial->port[1]->bulk_out_endpointAddress;
  786. return generic_startup(serial);
  787. }
  788. static void visor_shutdown (struct usb_serial *serial)
  789. {
  790. struct visor_private *priv;
  791. int i;
  792. dbg("%s", __func__);
  793. for (i = 0; i < serial->num_ports; i++) {
  794. priv = usb_get_serial_port_data(serial->port[i]);
  795. if (priv) {
  796. usb_set_serial_port_data(serial->port[i], NULL);
  797. kfree(priv);
  798. }
  799. }
  800. }
  801. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  802. {
  803. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  804. return -ENOIOCTLCMD;
  805. }
  806. static int __init visor_init (void)
  807. {
  808. int i, retval;
  809. /* Only if parameters were passed to us */
  810. if ((vendor>0) && (product>0)) {
  811. struct usb_device_id usb_dev_temp[]=
  812. {{USB_DEVICE(vendor, product),
  813. .driver_info = (kernel_ulong_t)&palm_os_4_probe }};
  814. /* Find the last entry in id_table */
  815. for (i=0; ; i++) {
  816. if (id_table[i].idVendor==0) {
  817. id_table[i] = usb_dev_temp[0];
  818. break;
  819. }
  820. }
  821. /* Find the last entry in id_table_combined */
  822. for (i=0; ; i++) {
  823. if (id_table_combined[i].idVendor==0) {
  824. id_table_combined[i] = usb_dev_temp[0];
  825. break;
  826. }
  827. }
  828. info("Untested USB device specified at time of module insertion");
  829. info("Warning: This is not guaranteed to work");
  830. info("Using a newer kernel is preferred to this method");
  831. info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
  832. vendor, product);
  833. }
  834. retval = usb_serial_register(&handspring_device);
  835. if (retval)
  836. goto failed_handspring_register;
  837. retval = usb_serial_register(&clie_3_5_device);
  838. if (retval)
  839. goto failed_clie_3_5_register;
  840. retval = usb_serial_register(&clie_5_device);
  841. if (retval)
  842. goto failed_clie_5_register;
  843. retval = usb_register(&visor_driver);
  844. if (retval)
  845. goto failed_usb_register;
  846. info(DRIVER_DESC);
  847. return 0;
  848. failed_usb_register:
  849. usb_serial_deregister(&clie_5_device);
  850. failed_clie_5_register:
  851. usb_serial_deregister(&clie_3_5_device);
  852. failed_clie_3_5_register:
  853. usb_serial_deregister(&handspring_device);
  854. failed_handspring_register:
  855. return retval;
  856. }
  857. static void __exit visor_exit (void)
  858. {
  859. usb_deregister (&visor_driver);
  860. usb_serial_deregister (&handspring_device);
  861. usb_serial_deregister (&clie_3_5_device);
  862. usb_serial_deregister (&clie_5_device);
  863. }
  864. module_init(visor_init);
  865. module_exit(visor_exit);
  866. MODULE_AUTHOR( DRIVER_AUTHOR );
  867. MODULE_DESCRIPTION( DRIVER_DESC );
  868. MODULE_LICENSE("GPL");
  869. module_param(debug, bool, S_IRUGO | S_IWUSR);
  870. MODULE_PARM_DESC(debug, "Debug enabled or not");
  871. module_param(stats, bool, S_IRUGO | S_IWUSR);
  872. MODULE_PARM_DESC(stats, "Enables statistics or not");
  873. module_param(vendor, ushort, 0);
  874. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  875. module_param(product, ushort, 0);
  876. MODULE_PARM_DESC(product, "User specified product ID");