visor.c 29 KB

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